blob: 0553c761bd306c33942be6eddca30ce01899996a [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
Thomas Wouters477c8d52006-05-27 19:21:47 +000014#ifdef __APPLE__
15 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000016 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000017 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
18 * at the end of this file for more information.
19 */
20# pragma weak lchown
21# pragma weak statvfs
22# pragma weak fstatvfs
23
24#endif /* __APPLE__ */
25
Thomas Wouters68bc4f92006-03-01 01:05:10 +000026#define PY_SSIZE_T_CLEAN
27
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000028#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000029
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000030#if defined(__VMS)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020031# error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#endif /* defined(__VMS) */
34
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000035#ifdef __cplusplus
36extern "C" {
37#endif
38
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000039PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000040"This module provides access to operating system functionality that is\n\
41standardized by the C Standard and the POSIX standard (a thinly\n\
42disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000043corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000045
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000046#if defined(PYOS_OS2)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020047#error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#define INCL_DOS
49#define INCL_DOSERRORS
50#define INCL_DOSPROCESS
51#define INCL_NOPMAPI
52#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000053#if defined(PYCC_GCC)
54#include <ctype.h>
55#include <io.h>
56#include <stdio.h>
57#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000059#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000060#endif
61
Ross Lagerwall4d076da2011-03-18 06:56:53 +020062#ifdef HAVE_SYS_UIO_H
63#include <sys/uio.h>
64#endif
65
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_TYPES_H */
69
70#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000071#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000073
Guido van Rossum36bc6801995-06-14 22:54:23 +000074#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000075#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000076#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000077
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000079#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000081
Guido van Rossumb6775db1994-08-01 11:34:53 +000082#ifdef HAVE_FCNTL_H
83#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000084#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000085
Guido van Rossuma6535fd2001-10-18 19:44:10 +000086#ifdef HAVE_GRP_H
87#include <grp.h>
88#endif
89
Barry Warsaw5676bd12003-01-07 20:57:09 +000090#ifdef HAVE_SYSEXITS_H
91#include <sysexits.h>
92#endif /* HAVE_SYSEXITS_H */
93
Anthony Baxter8a560de2004-10-13 15:30:56 +000094#ifdef HAVE_SYS_LOADAVG_H
95#include <sys/loadavg.h>
96#endif
97
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000098#ifdef HAVE_LANGINFO_H
99#include <langinfo.h>
100#endif
101
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000102#ifdef HAVE_SYS_SENDFILE_H
103#include <sys/sendfile.h>
104#endif
105
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500106#ifdef HAVE_SCHED_H
107#include <sched.h>
108#endif
109
Benjamin Peterson9428d532011-09-14 11:45:52 -0400110#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__)
111#define USE_XATTRS
112#endif
113
114#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400115#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400116#endif
117
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000118#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
119#ifdef HAVE_SYS_SOCKET_H
120#include <sys/socket.h>
121#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000122#endif
123
Victor Stinner8b905bd2011-10-25 13:34:04 +0200124#ifdef HAVE_DLFCN_H
125#include <dlfcn.h>
126#endif
127
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100128#if defined(MS_WINDOWS)
129# define TERMSIZE_USE_CONIO
130#elif defined(HAVE_SYS_IOCTL_H)
131# include <sys/ioctl.h>
132# if defined(HAVE_TERMIOS_H)
133# include <termios.h>
134# endif
135# if defined(TIOCGWINSZ)
136# define TERMSIZE_USE_IOCTL
137# endif
138#endif /* MS_WINDOWS */
139
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000140/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000141/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000142#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000143#include <process.h>
144#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000145#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000146#define HAVE_GETCWD 1
147#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000148#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000149#if defined(__OS2__)
150#define HAVE_EXECV 1
151#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000152#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#include <process.h>
154#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000155#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000156#define HAVE_EXECV 1
157#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000158#define HAVE_OPENDIR 1
159#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000160#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000161#define HAVE_WAIT 1
162#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000163#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000164#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000165#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000166#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000167#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000168#define HAVE_EXECV 1
169#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000170#define HAVE_SYSTEM 1
171#define HAVE_CWAIT 1
172#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000173#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000174#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000175#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
176/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000177#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178/* Unix functions that the configure script doesn't check for */
179#define HAVE_EXECV 1
180#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000181#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000182#define HAVE_FORK1 1
183#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000184#define HAVE_GETCWD 1
185#define HAVE_GETEGID 1
186#define HAVE_GETEUID 1
187#define HAVE_GETGID 1
188#define HAVE_GETPPID 1
189#define HAVE_GETUID 1
190#define HAVE_KILL 1
191#define HAVE_OPENDIR 1
192#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000193#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000194#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000195#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000196#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000197#endif /* _MSC_VER */
198#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000199#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000200#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000201
Victor Stinnera2f7c002012-02-08 03:36:25 +0100202
203
204
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000205#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000206
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000207#if defined(__sgi)&&_COMPILER_VERSION>=700
208/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
209 (default) */
210extern char *ctermid_r(char *);
211#endif
212
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000213#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000214#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000215extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000216#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000217#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000218extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000219#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000220extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000221#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000222#endif
223#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000224extern int chdir(char *);
225extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000226#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000227extern int chdir(const char *);
228extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000229#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000230#ifdef __BORLANDC__
231extern int chmod(const char *, int);
232#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000233extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000234#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000235/*#ifdef HAVE_FCHMOD
236extern int fchmod(int, mode_t);
237#endif*/
238/*#ifdef HAVE_LCHMOD
239extern int lchmod(const char *, mode_t);
240#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000241extern int chown(const char *, uid_t, gid_t);
242extern char *getcwd(char *, int);
243extern char *strerror(int);
244extern int link(const char *, const char *);
245extern int rename(const char *, const char *);
246extern int stat(const char *, struct stat *);
247extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000249extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000250#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000252extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000253#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000255
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000256#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#ifdef HAVE_UTIME_H
259#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000260#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000262#ifdef HAVE_SYS_UTIME_H
263#include <sys/utime.h>
264#define HAVE_UTIME_H /* pretend we do for the rest of this file */
265#endif /* HAVE_SYS_UTIME_H */
266
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267#ifdef HAVE_SYS_TIMES_H
268#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000269#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270
271#ifdef HAVE_SYS_PARAM_H
272#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000273#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274
275#ifdef HAVE_SYS_UTSNAME_H
276#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000277#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000279#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000280#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000281#define NAMLEN(dirent) strlen((dirent)->d_name)
282#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000283#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000284#include <direct.h>
285#define NAMLEN(dirent) strlen((dirent)->d_name)
286#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000288#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000289#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000290#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000291#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000292#endif
293#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000294#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000295#endif
296#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000297#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000298#endif
299#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000300
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000301#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000302#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000303#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000304#endif
305#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000306#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000307#endif
308#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000309#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000310#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000311#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000312#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000313#endif
314#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000315#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000316#endif
317#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000318#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000319#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000320#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000321#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000322#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000323#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000324#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000325#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
326#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000327static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000328#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000329#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000330
Guido van Rossumd48f2521997-12-05 22:19:34 +0000331#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000332#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000333#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334
Tim Petersbc2e10e2002-03-03 23:17:02 +0000335#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000336#if defined(PATH_MAX) && PATH_MAX > 1024
337#define MAXPATHLEN PATH_MAX
338#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000339#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000340#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000341#endif /* MAXPATHLEN */
342
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000343#ifdef UNION_WAIT
344/* Emulate some macros on systems that have a union instead of macros */
345
346#ifndef WIFEXITED
347#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
348#endif
349
350#ifndef WEXITSTATUS
351#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
352#endif
353
354#ifndef WTERMSIG
355#define WTERMSIG(u_wait) ((u_wait).w_termsig)
356#endif
357
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000358#define WAIT_TYPE union wait
359#define WAIT_STATUS_INT(s) (s.w_status)
360
361#else /* !UNION_WAIT */
362#define WAIT_TYPE int
363#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000364#endif /* UNION_WAIT */
365
Greg Wardb48bc172000-03-01 21:51:56 +0000366/* Don't use the "_r" form if we don't need it (also, won't have a
367 prototype for it, at least on Solaris -- maybe others as well?). */
368#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
369#define USE_CTERMID_R
370#endif
371
Fred Drake699f3522000-06-29 21:12:41 +0000372/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000373#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000374#undef FSTAT
375#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000376#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000377# define STAT win32_stat
378# define FSTAT win32_fstat
379# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000380#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000381# define STAT stat
382# define FSTAT fstat
383# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000384#endif
385
Tim Peters11b23062003-04-23 02:39:17 +0000386#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000387#include <sys/mkdev.h>
388#else
389#if defined(MAJOR_IN_SYSMACROS)
390#include <sys/sysmacros.h>
391#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000392#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
393#include <sys/mkdev.h>
394#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000395#endif
Fred Drake699f3522000-06-29 21:12:41 +0000396
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200397/* A helper used by a number of POSIX-only functions */
398#ifndef MS_WINDOWS
Georg Brandla391b112011-02-25 15:23:18 +0000399static int
400_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000401{
402#if !defined(HAVE_LARGEFILE_SUPPORT)
403 *((off_t*)addr) = PyLong_AsLong(arg);
404#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000405 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000406#endif
407 if (PyErr_Occurred())
408 return 0;
409 return 1;
410}
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200411#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000412
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000413#if defined _MSC_VER && _MSC_VER >= 1400
414/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
415 * valid and throw an assertion if it isn't.
416 * Normally, an invalid fd is likely to be a C program error and therefore
417 * an assertion can be useful, but it does contradict the POSIX standard
418 * which for write(2) states:
419 * "Otherwise, -1 shall be returned and errno set to indicate the error."
420 * "[EBADF] The fildes argument is not a valid file descriptor open for
421 * writing."
422 * Furthermore, python allows the user to enter any old integer
423 * as a fd and should merely raise a python exception on error.
424 * The Microsoft CRT doesn't provide an official way to check for the
425 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000426 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000427 * internal structures involved.
428 * The structures below must be updated for each version of visual studio
429 * according to the file internal.h in the CRT source, until MS comes
430 * up with a less hacky way to do this.
431 * (all of this is to avoid globally modifying the CRT behaviour using
432 * _set_invalid_parameter_handler() and _CrtSetReportMode())
433 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000434/* The actual size of the structure is determined at runtime.
435 * Only the first items must be present.
436 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000437typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000438 intptr_t osfhnd;
439 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000440} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000441
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000442extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000443#define IOINFO_L2E 5
444#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
445#define IOINFO_ARRAYS 64
446#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
447#define FOPEN 0x01
448#define _NO_CONSOLE_FILENO (intptr_t)-2
449
450/* This function emulates what the windows CRT does to validate file handles */
451int
452_PyVerify_fd(int fd)
453{
Victor Stinner8c62be82010-05-06 00:08:46 +0000454 const int i1 = fd >> IOINFO_L2E;
455 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000456
Antoine Pitrou22e41552010-08-15 18:07:50 +0000457 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000458
Victor Stinner8c62be82010-05-06 00:08:46 +0000459 /* Determine the actual size of the ioinfo structure,
460 * as used by the CRT loaded in memory
461 */
462 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
463 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
464 }
465 if (sizeof_ioinfo == 0) {
466 /* This should not happen... */
467 goto fail;
468 }
469
470 /* See that it isn't a special CLEAR fileno */
471 if (fd != _NO_CONSOLE_FILENO) {
472 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
473 * we check pointer validity and other info
474 */
475 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
476 /* finally, check that the file is open */
477 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
478 if (info->osfile & FOPEN) {
479 return 1;
480 }
481 }
482 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000483 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000484 errno = EBADF;
485 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000486}
487
488/* the special case of checking dup2. The target fd must be in a sensible range */
489static int
490_PyVerify_fd_dup2(int fd1, int fd2)
491{
Victor Stinner8c62be82010-05-06 00:08:46 +0000492 if (!_PyVerify_fd(fd1))
493 return 0;
494 if (fd2 == _NO_CONSOLE_FILENO)
495 return 0;
496 if ((unsigned)fd2 < _NHANDLE_)
497 return 1;
498 else
499 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000500}
501#else
502/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
503#define _PyVerify_fd_dup2(A, B) (1)
504#endif
505
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000506#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000507/* The following structure was copied from
508 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
509 include doesn't seem to be present in the Windows SDK (at least as included
510 with Visual Studio Express). */
511typedef struct _REPARSE_DATA_BUFFER {
512 ULONG ReparseTag;
513 USHORT ReparseDataLength;
514 USHORT Reserved;
515 union {
516 struct {
517 USHORT SubstituteNameOffset;
518 USHORT SubstituteNameLength;
519 USHORT PrintNameOffset;
520 USHORT PrintNameLength;
521 ULONG Flags;
522 WCHAR PathBuffer[1];
523 } SymbolicLinkReparseBuffer;
524
525 struct {
526 USHORT SubstituteNameOffset;
527 USHORT SubstituteNameLength;
528 USHORT PrintNameOffset;
529 USHORT PrintNameLength;
530 WCHAR PathBuffer[1];
531 } MountPointReparseBuffer;
532
533 struct {
534 UCHAR DataBuffer[1];
535 } GenericReparseBuffer;
536 };
537} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
538
539#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
540 GenericReparseBuffer)
541#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
542
543static int
Brian Curtind25aef52011-06-13 15:16:04 -0500544win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000545{
546 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
547 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
548 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000549
550 if (0 == DeviceIoControl(
551 reparse_point_handle,
552 FSCTL_GET_REPARSE_POINT,
553 NULL, 0, /* in buffer */
554 target_buffer, sizeof(target_buffer),
555 &n_bytes_returned,
556 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500557 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000558
559 if (reparse_tag)
560 *reparse_tag = rdb->ReparseTag;
561
Brian Curtind25aef52011-06-13 15:16:04 -0500562 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000563}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100564
565static int
566win32_warn_bytes_api()
567{
568 return PyErr_WarnEx(PyExc_DeprecationWarning,
569 "The Windows bytes API has been deprecated, "
570 "use Unicode filenames instead",
571 1);
572}
573
574static PyObject*
575win32_decode_filename(PyObject *obj)
576{
577 PyObject *unicode;
578 if (PyUnicode_Check(obj)) {
579 if (PyUnicode_READY(obj))
580 return NULL;
581 Py_INCREF(obj);
582 return obj;
583 }
584 if (!PyUnicode_FSDecoder(obj, &unicode))
585 return NULL;
586 if (win32_warn_bytes_api()) {
587 Py_DECREF(unicode);
588 return NULL;
589 }
590 return unicode;
591}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000592#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000593
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000595#ifdef WITH_NEXT_FRAMEWORK
596/* On Darwin/MacOSX a shared library or framework has no access to
597** environ directly, we must obtain it with _NSGetEnviron().
598*/
599#include <crt_externs.h>
600static char **environ;
601#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000602extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000603#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604
Barry Warsaw53699e91996-12-10 23:23:01 +0000605static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000606convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607{
Victor Stinner8c62be82010-05-06 00:08:46 +0000608 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000609#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000610 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000611#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000612 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000613#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000614#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000615 APIRET rc;
616 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
617#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000618
Victor Stinner8c62be82010-05-06 00:08:46 +0000619 d = PyDict_New();
620 if (d == NULL)
621 return NULL;
622#ifdef WITH_NEXT_FRAMEWORK
623 if (environ == NULL)
624 environ = *_NSGetEnviron();
625#endif
626#ifdef MS_WINDOWS
627 /* _wenviron must be initialized in this way if the program is started
628 through main() instead of wmain(). */
629 _wgetenv(L"");
630 if (_wenviron == NULL)
631 return d;
632 /* This part ignores errors */
633 for (e = _wenviron; *e != NULL; e++) {
634 PyObject *k;
635 PyObject *v;
636 wchar_t *p = wcschr(*e, L'=');
637 if (p == NULL)
638 continue;
639 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
640 if (k == NULL) {
641 PyErr_Clear();
642 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000643 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000644 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
645 if (v == NULL) {
646 PyErr_Clear();
647 Py_DECREF(k);
648 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000649 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000650 if (PyDict_GetItem(d, k) == NULL) {
651 if (PyDict_SetItem(d, k, v) != 0)
652 PyErr_Clear();
653 }
654 Py_DECREF(k);
655 Py_DECREF(v);
656 }
657#else
658 if (environ == NULL)
659 return d;
660 /* This part ignores errors */
661 for (e = environ; *e != NULL; e++) {
662 PyObject *k;
663 PyObject *v;
664 char *p = strchr(*e, '=');
665 if (p == NULL)
666 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000667 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000668 if (k == NULL) {
669 PyErr_Clear();
670 continue;
671 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000672 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000673 if (v == NULL) {
674 PyErr_Clear();
675 Py_DECREF(k);
676 continue;
677 }
678 if (PyDict_GetItem(d, k) == NULL) {
679 if (PyDict_SetItem(d, k, v) != 0)
680 PyErr_Clear();
681 }
682 Py_DECREF(k);
683 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000684 }
685#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000686#if defined(PYOS_OS2)
687 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
688 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
689 PyObject *v = PyBytes_FromString(buffer);
690 PyDict_SetItemString(d, "BEGINLIBPATH", v);
691 Py_DECREF(v);
692 }
693 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
694 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
695 PyObject *v = PyBytes_FromString(buffer);
696 PyDict_SetItemString(d, "ENDLIBPATH", v);
697 Py_DECREF(v);
698 }
699#endif
700 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000701}
702
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703/* Set a POSIX-specific error from errno, and return NULL */
704
Barry Warsawd58d7641998-07-23 16:14:40 +0000705static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000706posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000707{
Victor Stinner8c62be82010-05-06 00:08:46 +0000708 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000709}
Barry Warsawd58d7641998-07-23 16:14:40 +0000710static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000711posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000712{
Victor Stinner8c62be82010-05-06 00:08:46 +0000713 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000714}
715
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000716
Mark Hammondef8b6542001-05-13 08:04:26 +0000717static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000718posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000719{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000720 PyObject *name_str, *rc;
721 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
722 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000723 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000724 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
725 name_str);
726 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000727 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000728}
729
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000730#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000731static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000732win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000733{
Victor Stinner8c62be82010-05-06 00:08:46 +0000734 /* XXX We should pass the function name along in the future.
735 (winreg.c also wants to pass the function name.)
736 This would however require an additional param to the
737 Windows error object, which is non-trivial.
738 */
739 errno = GetLastError();
740 if (filename)
741 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
742 else
743 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000744}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000745
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000746static PyObject *
Victor Stinnereb5657a2011-09-30 01:44:27 +0200747win32_error_unicode(char* function, wchar_t* filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000748{
Victor Stinner8c62be82010-05-06 00:08:46 +0000749 /* XXX - see win32_error for comments on 'function' */
750 errno = GetLastError();
751 if (filename)
752 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
753 else
754 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000755}
756
Victor Stinnereb5657a2011-09-30 01:44:27 +0200757static PyObject *
758win32_error_object(char* function, PyObject* filename)
759{
760 /* XXX - see win32_error for comments on 'function' */
761 errno = GetLastError();
762 if (filename)
763 return PyErr_SetExcFromWindowsErrWithFilenameObject(
764 PyExc_WindowsError,
765 errno,
766 filename);
767 else
768 return PyErr_SetFromWindowsErr(errno);
769}
770
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000771#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000772
Guido van Rossumd48f2521997-12-05 22:19:34 +0000773#if defined(PYOS_OS2)
774/**********************************************************************
775 * Helper Function to Trim and Format OS/2 Messages
776 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000777static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000778os2_formatmsg(char *msgbuf, int msglen, char *reason)
779{
780 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
781
782 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
783 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
784
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000785 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000786 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
787 }
788
789 /* Add Optional Reason Text */
790 if (reason) {
791 strcat(msgbuf, " : ");
792 strcat(msgbuf, reason);
793 }
794}
795
796/**********************************************************************
797 * Decode an OS/2 Operating System Error Code
798 *
799 * A convenience function to lookup an OS/2 error code and return a
800 * text message we can use to raise a Python exception.
801 *
802 * Notes:
803 * The messages for errors returned from the OS/2 kernel reside in
804 * the file OSO001.MSG in the \OS2 directory hierarchy.
805 *
806 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000807static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000808os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
809{
810 APIRET rc;
811 ULONG msglen;
812
813 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
814 Py_BEGIN_ALLOW_THREADS
815 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
816 errorcode, "oso001.msg", &msglen);
817 Py_END_ALLOW_THREADS
818
819 if (rc == NO_ERROR)
820 os2_formatmsg(msgbuf, msglen, reason);
821 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000822 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000823 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000824
825 return msgbuf;
826}
827
828/* Set an OS/2-specific error and return NULL. OS/2 kernel
829 errors are not in a global variable e.g. 'errno' nor are
830 they congruent with posix error numbers. */
831
Victor Stinner8c62be82010-05-06 00:08:46 +0000832static PyObject *
833os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000834{
835 char text[1024];
836 PyObject *v;
837
838 os2_strerror(text, sizeof(text), code, "");
839
840 v = Py_BuildValue("(is)", code, text);
841 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000842 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000843 Py_DECREF(v);
844 }
845 return NULL; /* Signal to Python that an Exception is Pending */
846}
847
848#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000849
850/* POSIX generic methods */
851
Barry Warsaw53699e91996-12-10 23:23:01 +0000852static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000853posix_fildes(PyObject *fdobj, int (*func)(int))
854{
Victor Stinner8c62be82010-05-06 00:08:46 +0000855 int fd;
856 int res;
857 fd = PyObject_AsFileDescriptor(fdobj);
858 if (fd < 0)
859 return NULL;
860 if (!_PyVerify_fd(fd))
861 return posix_error();
862 Py_BEGIN_ALLOW_THREADS
863 res = (*func)(fd);
864 Py_END_ALLOW_THREADS
865 if (res < 0)
866 return posix_error();
867 Py_INCREF(Py_None);
868 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000869}
Guido van Rossum21142a01999-01-08 21:05:37 +0000870
871static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000872posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000873{
Victor Stinner8c62be82010-05-06 00:08:46 +0000874 PyObject *opath1 = NULL;
875 char *path1;
876 int res;
877 if (!PyArg_ParseTuple(args, format,
878 PyUnicode_FSConverter, &opath1))
879 return NULL;
880 path1 = PyBytes_AsString(opath1);
881 Py_BEGIN_ALLOW_THREADS
882 res = (*func)(path1);
883 Py_END_ALLOW_THREADS
884 if (res < 0)
885 return posix_error_with_allocated_filename(opath1);
886 Py_DECREF(opath1);
887 Py_INCREF(Py_None);
888 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000889}
890
Barry Warsaw53699e91996-12-10 23:23:01 +0000891static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000892posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000893 char *format,
894 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000895{
Victor Stinner8c62be82010-05-06 00:08:46 +0000896 PyObject *opath1 = NULL, *opath2 = NULL;
897 char *path1, *path2;
898 int res;
899 if (!PyArg_ParseTuple(args, format,
900 PyUnicode_FSConverter, &opath1,
901 PyUnicode_FSConverter, &opath2)) {
902 return NULL;
903 }
904 path1 = PyBytes_AsString(opath1);
905 path2 = PyBytes_AsString(opath2);
906 Py_BEGIN_ALLOW_THREADS
907 res = (*func)(path1, path2);
908 Py_END_ALLOW_THREADS
909 Py_DECREF(opath1);
910 Py_DECREF(opath2);
911 if (res != 0)
912 /* XXX how to report both path1 and path2??? */
913 return posix_error();
914 Py_INCREF(Py_None);
915 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000916}
917
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000918#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000919static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000920win32_1str(PyObject* args, char* func,
921 char* format, BOOL (__stdcall *funcA)(LPCSTR),
922 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000923{
Victor Stinner8c62be82010-05-06 00:08:46 +0000924 PyObject *uni;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100925 const char *ansi;
Victor Stinner8c62be82010-05-06 00:08:46 +0000926 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000927
Victor Stinnereb5657a2011-09-30 01:44:27 +0200928 if (PyArg_ParseTuple(args, wformat, &uni))
929 {
930 wchar_t *wstr = PyUnicode_AsUnicode(uni);
931 if (wstr == NULL)
932 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +0000933 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +0200934 result = funcW(wstr);
Victor Stinner8c62be82010-05-06 00:08:46 +0000935 Py_END_ALLOW_THREADS
936 if (!result)
Victor Stinnereb5657a2011-09-30 01:44:27 +0200937 return win32_error_object(func, uni);
Victor Stinner8c62be82010-05-06 00:08:46 +0000938 Py_INCREF(Py_None);
939 return Py_None;
940 }
Victor Stinnereb5657a2011-09-30 01:44:27 +0200941 PyErr_Clear();
942
Victor Stinner8c62be82010-05-06 00:08:46 +0000943 if (!PyArg_ParseTuple(args, format, &ansi))
944 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100945 if (win32_warn_bytes_api())
946 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +0000947 Py_BEGIN_ALLOW_THREADS
948 result = funcA(ansi);
949 Py_END_ALLOW_THREADS
950 if (!result)
951 return win32_error(func, ansi);
952 Py_INCREF(Py_None);
953 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000954
955}
956
957/* This is a reimplementation of the C library's chdir function,
958 but one that produces Win32 errors instead of DOS error codes.
959 chdir is essentially a wrapper around SetCurrentDirectory; however,
960 it also needs to set "magic" environment variables indicating
961 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000962static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000963win32_chdir(LPCSTR path)
964{
Victor Stinner8c62be82010-05-06 00:08:46 +0000965 char new_path[MAX_PATH+1];
966 int result;
967 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968
Victor Stinner8c62be82010-05-06 00:08:46 +0000969 if(!SetCurrentDirectoryA(path))
970 return FALSE;
971 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
972 if (!result)
973 return FALSE;
974 /* In the ANSI API, there should not be any paths longer
975 than MAX_PATH. */
976 assert(result <= MAX_PATH+1);
977 if (strncmp(new_path, "\\\\", 2) == 0 ||
978 strncmp(new_path, "//", 2) == 0)
979 /* UNC path, nothing to do. */
980 return TRUE;
981 env[1] = new_path[0];
982 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000983}
984
985/* The Unicode version differs from the ANSI version
986 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000987static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000988win32_wchdir(LPCWSTR path)
989{
Victor Stinner8c62be82010-05-06 00:08:46 +0000990 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
991 int result;
992 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000993
Victor Stinner8c62be82010-05-06 00:08:46 +0000994 if(!SetCurrentDirectoryW(path))
995 return FALSE;
996 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
997 if (!result)
998 return FALSE;
999 if (result > MAX_PATH+1) {
1000 new_path = malloc(result * sizeof(wchar_t));
1001 if (!new_path) {
1002 SetLastError(ERROR_OUTOFMEMORY);
1003 return FALSE;
1004 }
1005 result = GetCurrentDirectoryW(result, new_path);
1006 if (!result) {
1007 free(new_path);
1008 return FALSE;
1009 }
1010 }
1011 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1012 wcsncmp(new_path, L"//", 2) == 0)
1013 /* UNC path, nothing to do. */
1014 return TRUE;
1015 env[1] = new_path[0];
1016 result = SetEnvironmentVariableW(env, new_path);
1017 if (new_path != _new_path)
1018 free(new_path);
1019 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001020}
1021#endif
1022
Martin v. Löwis14694662006-02-03 12:54:16 +00001023#ifdef MS_WINDOWS
1024/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1025 - time stamps are restricted to second resolution
1026 - file modification times suffer from forth-and-back conversions between
1027 UTC and local time
1028 Therefore, we implement our own stat, based on the Win32 API directly.
1029*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001030#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001031
1032struct win32_stat{
1033 int st_dev;
1034 __int64 st_ino;
1035 unsigned short st_mode;
1036 int st_nlink;
1037 int st_uid;
1038 int st_gid;
1039 int st_rdev;
1040 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001041 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001042 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001043 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001044 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001045 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001046 int st_ctime_nsec;
1047};
1048
1049static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1050
1051static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001052FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001053{
Victor Stinner8c62be82010-05-06 00:08:46 +00001054 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1055 /* Cannot simply cast and dereference in_ptr,
1056 since it might not be aligned properly */
1057 __int64 in;
1058 memcpy(&in, in_ptr, sizeof(in));
1059 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001060 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001061}
1062
Thomas Wouters477c8d52006-05-27 19:21:47 +00001063static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001064time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001065{
Victor Stinner8c62be82010-05-06 00:08:46 +00001066 /* XXX endianness */
1067 __int64 out;
1068 out = time_in + secs_between_epochs;
1069 out = out * 10000000 + nsec_in / 100;
1070 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001071}
1072
Martin v. Löwis14694662006-02-03 12:54:16 +00001073/* Below, we *know* that ugo+r is 0444 */
1074#if _S_IREAD != 0400
1075#error Unsupported C library
1076#endif
1077static int
1078attributes_to_mode(DWORD attr)
1079{
Victor Stinner8c62be82010-05-06 00:08:46 +00001080 int m = 0;
1081 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1082 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1083 else
1084 m |= _S_IFREG;
1085 if (attr & FILE_ATTRIBUTE_READONLY)
1086 m |= 0444;
1087 else
1088 m |= 0666;
1089 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001090}
1091
1092static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001093attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001094{
Victor Stinner8c62be82010-05-06 00:08:46 +00001095 memset(result, 0, sizeof(*result));
1096 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1097 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1098 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1099 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1100 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001101 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001102 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001103 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1104 /* first clear the S_IFMT bits */
1105 result->st_mode ^= (result->st_mode & 0170000);
1106 /* now set the bits that make this a symlink */
1107 result->st_mode |= 0120000;
1108 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001109
Victor Stinner8c62be82010-05-06 00:08:46 +00001110 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001111}
1112
Guido van Rossumd8faa362007-04-27 19:54:29 +00001113static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001114attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001115{
Victor Stinner8c62be82010-05-06 00:08:46 +00001116 HANDLE hFindFile;
1117 WIN32_FIND_DATAA FileData;
1118 hFindFile = FindFirstFileA(pszFile, &FileData);
1119 if (hFindFile == INVALID_HANDLE_VALUE)
1120 return FALSE;
1121 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001122 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001123 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001124 info->dwFileAttributes = FileData.dwFileAttributes;
1125 info->ftCreationTime = FileData.ftCreationTime;
1126 info->ftLastAccessTime = FileData.ftLastAccessTime;
1127 info->ftLastWriteTime = FileData.ftLastWriteTime;
1128 info->nFileSizeHigh = FileData.nFileSizeHigh;
1129 info->nFileSizeLow = FileData.nFileSizeLow;
1130/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001131 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1132 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001133 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001134}
1135
1136static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001137attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001138{
Victor Stinner8c62be82010-05-06 00:08:46 +00001139 HANDLE hFindFile;
1140 WIN32_FIND_DATAW FileData;
1141 hFindFile = FindFirstFileW(pszFile, &FileData);
1142 if (hFindFile == INVALID_HANDLE_VALUE)
1143 return FALSE;
1144 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001145 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001146 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001147 info->dwFileAttributes = FileData.dwFileAttributes;
1148 info->ftCreationTime = FileData.ftCreationTime;
1149 info->ftLastAccessTime = FileData.ftLastAccessTime;
1150 info->ftLastWriteTime = FileData.ftLastWriteTime;
1151 info->nFileSizeHigh = FileData.nFileSizeHigh;
1152 info->nFileSizeLow = FileData.nFileSizeLow;
1153/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001154 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1155 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001156 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001157}
1158
Brian Curtind25aef52011-06-13 15:16:04 -05001159/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1160static int has_GetFinalPathNameByHandle = 0;
Brian Curtind25aef52011-06-13 15:16:04 -05001161static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1162 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001163static int
Brian Curtind25aef52011-06-13 15:16:04 -05001164check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001165{
Brian Curtind25aef52011-06-13 15:16:04 -05001166 HINSTANCE hKernel32;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001167 DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1168 DWORD);
1169
Brian Curtind25aef52011-06-13 15:16:04 -05001170 /* only recheck */
1171 if (!has_GetFinalPathNameByHandle)
1172 {
Martin v. Löwis50590f12012-01-14 17:54:09 +01001173 hKernel32 = GetModuleHandleW(L"KERNEL32");
Brian Curtind25aef52011-06-13 15:16:04 -05001174 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1175 "GetFinalPathNameByHandleA");
1176 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1177 "GetFinalPathNameByHandleW");
1178 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1179 Py_GetFinalPathNameByHandleW;
1180 }
1181 return has_GetFinalPathNameByHandle;
1182}
1183
1184static BOOL
1185get_target_path(HANDLE hdl, wchar_t **target_path)
1186{
1187 int buf_size, result_length;
1188 wchar_t *buf;
1189
1190 /* We have a good handle to the target, use it to determine
1191 the target path name (then we'll call lstat on it). */
1192 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1193 VOLUME_NAME_DOS);
1194 if(!buf_size)
1195 return FALSE;
1196
1197 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001198 if (!buf) {
1199 SetLastError(ERROR_OUTOFMEMORY);
1200 return FALSE;
1201 }
1202
Brian Curtind25aef52011-06-13 15:16:04 -05001203 result_length = Py_GetFinalPathNameByHandleW(hdl,
1204 buf, buf_size, VOLUME_NAME_DOS);
1205
1206 if(!result_length) {
1207 free(buf);
1208 return FALSE;
1209 }
1210
1211 if(!CloseHandle(hdl)) {
1212 free(buf);
1213 return FALSE;
1214 }
1215
1216 buf[result_length] = 0;
1217
1218 *target_path = buf;
1219 return TRUE;
1220}
1221
1222static int
1223win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1224 BOOL traverse);
1225static int
1226win32_xstat_impl(const char *path, struct win32_stat *result,
1227 BOOL traverse)
1228{
Victor Stinner26de69d2011-06-17 15:15:38 +02001229 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001230 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001231 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001232 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001233 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001234 const char *dot;
1235
Brian Curtind25aef52011-06-13 15:16:04 -05001236 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001237 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1238 traverse reparse point. */
1239 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001240 }
1241
Brian Curtinf5e76d02010-11-24 13:14:05 +00001242 hFile = CreateFileA(
1243 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001244 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001245 0, /* share mode */
1246 NULL, /* security attributes */
1247 OPEN_EXISTING,
1248 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001249 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1250 Because of this, calls like GetFinalPathNameByHandle will return
1251 the symlink path agin and not the actual final path. */
1252 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1253 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001254 NULL);
1255
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001256 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001257 /* Either the target doesn't exist, or we don't have access to
1258 get a handle to it. If the former, we need to return an error.
1259 If the latter, we can use attributes_from_dir. */
1260 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001261 return -1;
1262 /* Could not get attributes on open file. Fall back to
1263 reading the directory. */
1264 if (!attributes_from_dir(path, &info, &reparse_tag))
1265 /* Very strange. This should not fail now */
1266 return -1;
1267 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1268 if (traverse) {
1269 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001270 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001271 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001272 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001273 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001274 } else {
1275 if (!GetFileInformationByHandle(hFile, &info)) {
1276 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001277 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001278 }
1279 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001280 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1281 return -1;
1282
1283 /* Close the outer open file handle now that we're about to
1284 reopen it with different flags. */
1285 if (!CloseHandle(hFile))
1286 return -1;
1287
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001288 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001289 /* In order to call GetFinalPathNameByHandle we need to open
1290 the file without the reparse handling flag set. */
1291 hFile2 = CreateFileA(
1292 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1293 NULL, OPEN_EXISTING,
1294 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1295 NULL);
1296 if (hFile2 == INVALID_HANDLE_VALUE)
1297 return -1;
1298
1299 if (!get_target_path(hFile2, &target_path))
1300 return -1;
1301
1302 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001303 free(target_path);
1304 return code;
1305 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001306 } else
1307 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001308 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001309 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001310
1311 /* Set S_IEXEC if it is an .exe, .bat, ... */
1312 dot = strrchr(path, '.');
1313 if (dot) {
1314 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1315 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1316 result->st_mode |= 0111;
1317 }
1318 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001319}
1320
1321static int
Brian Curtind25aef52011-06-13 15:16:04 -05001322win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1323 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001324{
1325 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001326 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001327 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001328 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001329 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001330 const wchar_t *dot;
1331
Brian Curtind25aef52011-06-13 15:16:04 -05001332 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001333 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1334 traverse reparse point. */
1335 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001336 }
1337
Brian Curtinf5e76d02010-11-24 13:14:05 +00001338 hFile = CreateFileW(
1339 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001340 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001341 0, /* share mode */
1342 NULL, /* security attributes */
1343 OPEN_EXISTING,
1344 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001345 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1346 Because of this, calls like GetFinalPathNameByHandle will return
1347 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001348 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001349 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001350 NULL);
1351
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001352 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001353 /* Either the target doesn't exist, or we don't have access to
1354 get a handle to it. If the former, we need to return an error.
1355 If the latter, we can use attributes_from_dir. */
1356 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001357 return -1;
1358 /* Could not get attributes on open file. Fall back to
1359 reading the directory. */
1360 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1361 /* Very strange. This should not fail now */
1362 return -1;
1363 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1364 if (traverse) {
1365 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001366 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001367 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001368 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001369 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001370 } else {
1371 if (!GetFileInformationByHandle(hFile, &info)) {
1372 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001373 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001374 }
1375 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001376 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1377 return -1;
1378
1379 /* Close the outer open file handle now that we're about to
1380 reopen it with different flags. */
1381 if (!CloseHandle(hFile))
1382 return -1;
1383
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001384 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001385 /* In order to call GetFinalPathNameByHandle we need to open
1386 the file without the reparse handling flag set. */
1387 hFile2 = CreateFileW(
1388 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1389 NULL, OPEN_EXISTING,
1390 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1391 NULL);
1392 if (hFile2 == INVALID_HANDLE_VALUE)
1393 return -1;
1394
1395 if (!get_target_path(hFile2, &target_path))
1396 return -1;
1397
1398 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001399 free(target_path);
1400 return code;
1401 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001402 } else
1403 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001404 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001405 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001406
1407 /* Set S_IEXEC if it is an .exe, .bat, ... */
1408 dot = wcsrchr(path, '.');
1409 if (dot) {
1410 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1411 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1412 result->st_mode |= 0111;
1413 }
1414 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001415}
1416
1417static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001418win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001419{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001420 /* Protocol violation: we explicitly clear errno, instead of
1421 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001422 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001423 errno = 0;
1424 return code;
1425}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001426
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001427static int
1428win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1429{
1430 /* Protocol violation: we explicitly clear errno, instead of
1431 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001432 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001433 errno = 0;
1434 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001435}
Brian Curtind25aef52011-06-13 15:16:04 -05001436/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001437
1438 In Posix, stat automatically traverses symlinks and returns the stat
1439 structure for the target. In Windows, the equivalent GetFileAttributes by
1440 default does not traverse symlinks and instead returns attributes for
1441 the symlink.
1442
1443 Therefore, win32_lstat will get the attributes traditionally, and
1444 win32_stat will first explicitly resolve the symlink target and then will
1445 call win32_lstat on that result.
1446
Ezio Melotti4969f702011-03-15 05:59:46 +02001447 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001448
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001449static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001450win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001451{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001452 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001453}
1454
Victor Stinner8c62be82010-05-06 00:08:46 +00001455static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001456win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001457{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001458 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001459}
1460
1461static int
1462win32_stat(const char* path, struct win32_stat *result)
1463{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001464 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001465}
1466
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001467static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001468win32_stat_w(const wchar_t* path, struct win32_stat *result)
1469{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001470 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001471}
1472
1473static int
1474win32_fstat(int file_number, struct win32_stat *result)
1475{
Victor Stinner8c62be82010-05-06 00:08:46 +00001476 BY_HANDLE_FILE_INFORMATION info;
1477 HANDLE h;
1478 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001479
Victor Stinner8c62be82010-05-06 00:08:46 +00001480 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001481
Victor Stinner8c62be82010-05-06 00:08:46 +00001482 /* Protocol violation: we explicitly clear errno, instead of
1483 setting it to a POSIX error. Callers should use GetLastError. */
1484 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001485
Victor Stinner8c62be82010-05-06 00:08:46 +00001486 if (h == INVALID_HANDLE_VALUE) {
1487 /* This is really a C library error (invalid file handle).
1488 We set the Win32 error to the closes one matching. */
1489 SetLastError(ERROR_INVALID_HANDLE);
1490 return -1;
1491 }
1492 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001493
Victor Stinner8c62be82010-05-06 00:08:46 +00001494 type = GetFileType(h);
1495 if (type == FILE_TYPE_UNKNOWN) {
1496 DWORD error = GetLastError();
1497 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001498 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001499 }
1500 /* else: valid but unknown file */
1501 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001502
Victor Stinner8c62be82010-05-06 00:08:46 +00001503 if (type != FILE_TYPE_DISK) {
1504 if (type == FILE_TYPE_CHAR)
1505 result->st_mode = _S_IFCHR;
1506 else if (type == FILE_TYPE_PIPE)
1507 result->st_mode = _S_IFIFO;
1508 return 0;
1509 }
1510
1511 if (!GetFileInformationByHandle(h, &info)) {
1512 return -1;
1513 }
1514
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001515 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001516 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001517 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1518 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001519}
1520
1521#endif /* MS_WINDOWS */
1522
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001523PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001524"stat_result: Result from stat or lstat.\n\n\
1525This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001526 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001527or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1528\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001529Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1530or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001531\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001532See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001533
1534static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001535 {"st_mode", "protection bits"},
1536 {"st_ino", "inode"},
1537 {"st_dev", "device"},
1538 {"st_nlink", "number of hard links"},
1539 {"st_uid", "user ID of owner"},
1540 {"st_gid", "group ID of owner"},
1541 {"st_size", "total size, in bytes"},
1542 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1543 {NULL, "integer time of last access"},
1544 {NULL, "integer time of last modification"},
1545 {NULL, "integer time of last change"},
1546 {"st_atime", "time of last access"},
1547 {"st_mtime", "time of last modification"},
1548 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001549#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001550 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001551#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001552#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001553 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001554#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001555#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001556 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001557#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001558#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001559 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001560#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001561#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001562 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001563#endif
1564#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001565 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001566#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001567 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001568};
1569
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001570#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001571#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001572#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001573#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001574#endif
1575
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001576#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001577#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1578#else
1579#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1580#endif
1581
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001582#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001583#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1584#else
1585#define ST_RDEV_IDX ST_BLOCKS_IDX
1586#endif
1587
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001588#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1589#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1590#else
1591#define ST_FLAGS_IDX ST_RDEV_IDX
1592#endif
1593
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001594#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001595#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001596#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001597#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001598#endif
1599
1600#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1601#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1602#else
1603#define ST_BIRTHTIME_IDX ST_GEN_IDX
1604#endif
1605
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001606static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001607 "stat_result", /* name */
1608 stat_result__doc__, /* doc */
1609 stat_result_fields,
1610 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001611};
1612
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001613PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001614"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1615This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001616 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001617or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001618\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001619See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001620
1621static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 {"f_bsize", },
1623 {"f_frsize", },
1624 {"f_blocks", },
1625 {"f_bfree", },
1626 {"f_bavail", },
1627 {"f_files", },
1628 {"f_ffree", },
1629 {"f_favail", },
1630 {"f_flag", },
1631 {"f_namemax",},
1632 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001633};
1634
1635static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001636 "statvfs_result", /* name */
1637 statvfs_result__doc__, /* doc */
1638 statvfs_result_fields,
1639 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001640};
1641
Ross Lagerwall7807c352011-03-17 20:20:30 +02001642#if defined(HAVE_WAITID) && !defined(__APPLE__)
1643PyDoc_STRVAR(waitid_result__doc__,
1644"waitid_result: Result from waitid.\n\n\
1645This object may be accessed either as a tuple of\n\
1646 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1647or via the attributes si_pid, si_uid, and so on.\n\
1648\n\
1649See os.waitid for more information.");
1650
1651static PyStructSequence_Field waitid_result_fields[] = {
1652 {"si_pid", },
1653 {"si_uid", },
1654 {"si_signo", },
1655 {"si_status", },
1656 {"si_code", },
1657 {0}
1658};
1659
1660static PyStructSequence_Desc waitid_result_desc = {
1661 "waitid_result", /* name */
1662 waitid_result__doc__, /* doc */
1663 waitid_result_fields,
1664 5
1665};
1666static PyTypeObject WaitidResultType;
1667#endif
1668
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001669static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001670static PyTypeObject StatResultType;
1671static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001672#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001673static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001674#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001675static newfunc structseq_new;
1676
1677static PyObject *
1678statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1679{
Victor Stinner8c62be82010-05-06 00:08:46 +00001680 PyStructSequence *result;
1681 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001682
Victor Stinner8c62be82010-05-06 00:08:46 +00001683 result = (PyStructSequence*)structseq_new(type, args, kwds);
1684 if (!result)
1685 return NULL;
1686 /* If we have been initialized from a tuple,
1687 st_?time might be set to None. Initialize it
1688 from the int slots. */
1689 for (i = 7; i <= 9; i++) {
1690 if (result->ob_item[i+3] == Py_None) {
1691 Py_DECREF(Py_None);
1692 Py_INCREF(result->ob_item[i]);
1693 result->ob_item[i+3] = result->ob_item[i];
1694 }
1695 }
1696 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001697}
1698
1699
1700
1701/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001702static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001703
1704PyDoc_STRVAR(stat_float_times__doc__,
1705"stat_float_times([newval]) -> oldval\n\n\
1706Determine whether os.[lf]stat represents time stamps as float objects.\n\
1707If newval is True, future calls to stat() return floats, if it is False,\n\
1708future calls return ints. \n\
1709If newval is omitted, return the current setting.\n");
1710
1711static PyObject*
1712stat_float_times(PyObject* self, PyObject *args)
1713{
Victor Stinner8c62be82010-05-06 00:08:46 +00001714 int newval = -1;
1715 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1716 return NULL;
1717 if (newval == -1)
1718 /* Return old value */
1719 return PyBool_FromLong(_stat_float_times);
1720 _stat_float_times = newval;
1721 Py_INCREF(Py_None);
1722 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001723}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001724
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001725static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01001726fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001727{
Victor Stinner8c62be82010-05-06 00:08:46 +00001728 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001729#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001730 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001731#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001732 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001733#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001734 if (!ival)
1735 return;
Victor Stinner4195b5c2012-02-08 23:03:19 +01001736 if (_stat_float_times) {
1737 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1738 } else {
Victor Stinner8c62be82010-05-06 00:08:46 +00001739 fval = ival;
1740 Py_INCREF(fval);
1741 }
1742 PyStructSequence_SET_ITEM(v, index, ival);
1743 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001744}
1745
Tim Peters5aa91602002-01-30 05:46:57 +00001746/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001747 (used by posix_stat() and posix_fstat()) */
1748static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01001749_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001750{
Victor Stinner8c62be82010-05-06 00:08:46 +00001751 unsigned long ansec, mnsec, cnsec;
1752 PyObject *v = PyStructSequence_New(&StatResultType);
1753 if (v == NULL)
1754 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001755
Victor Stinner8c62be82010-05-06 00:08:46 +00001756 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001757#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001758 PyStructSequence_SET_ITEM(v, 1,
1759 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001760#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001761 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001762#endif
1763#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001764 PyStructSequence_SET_ITEM(v, 2,
1765 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001766#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001767 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001768#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001769 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1770 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1771 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001772#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001773 PyStructSequence_SET_ITEM(v, 6,
1774 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001775#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001776 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001777#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001778
Martin v. Löwis14694662006-02-03 12:54:16 +00001779#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001780 ansec = st->st_atim.tv_nsec;
1781 mnsec = st->st_mtim.tv_nsec;
1782 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001783#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001784 ansec = st->st_atimespec.tv_nsec;
1785 mnsec = st->st_mtimespec.tv_nsec;
1786 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001787#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001788 ansec = st->st_atime_nsec;
1789 mnsec = st->st_mtime_nsec;
1790 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001791#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001793#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01001794 fill_time(v, 7, st->st_atime, ansec);
1795 fill_time(v, 8, st->st_mtime, mnsec);
1796 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001797
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001798#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001799 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1800 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001801#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001802#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001803 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1804 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001805#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001806#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001807 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1808 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001809#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001810#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001811 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1812 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001813#endif
1814#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001815 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01001816 PyObject *val;
1817 unsigned long bsec,bnsec;
1818 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001819#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01001820 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001821#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01001822 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001823#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01001824 if (_stat_float_times) {
1825 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1826 } else {
1827 val = PyLong_FromLong((long)bsec);
1828 }
1829 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1830 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001832#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001833#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001834 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1835 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001836#endif
Fred Drake699f3522000-06-29 21:12:41 +00001837
Victor Stinner8c62be82010-05-06 00:08:46 +00001838 if (PyErr_Occurred()) {
1839 Py_DECREF(v);
1840 return NULL;
1841 }
Fred Drake699f3522000-06-29 21:12:41 +00001842
Victor Stinner8c62be82010-05-06 00:08:46 +00001843 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001844}
1845
Barry Warsaw53699e91996-12-10 23:23:01 +00001846static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01001847posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001848 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001849#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001850 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001851#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001852 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001853#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001854 char *wformat,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001855 int (*wstatfunc)(const wchar_t *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001856{
Victor Stinner8c62be82010-05-06 00:08:46 +00001857 STRUCT_STAT st;
1858 PyObject *opath;
1859 char *path;
1860 int res;
1861 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001862
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001863#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02001864 PyObject *po;
Victor Stinner4195b5c2012-02-08 23:03:19 +01001865 if (PyArg_ParseTuple(args, wformat, &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02001866 wchar_t *wpath = PyUnicode_AsUnicode(po);
1867 if (wpath == NULL)
1868 return NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00001869
Victor Stinner8c62be82010-05-06 00:08:46 +00001870 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00001871 res = wstatfunc(wpath, &st);
1872 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001873
Victor Stinner8c62be82010-05-06 00:08:46 +00001874 if (res != 0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001875 return win32_error_object("stat", po);
Victor Stinner4195b5c2012-02-08 23:03:19 +01001876 return _pystat_fromstructstat(&st);
Victor Stinner8c62be82010-05-06 00:08:46 +00001877 }
1878 /* Drop the argument parsing error as narrow strings
1879 are also valid. */
1880 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001881#endif
1882
Victor Stinner4195b5c2012-02-08 23:03:19 +01001883 if (!PyArg_ParseTuple(args, format,
1884 PyUnicode_FSConverter, &opath))
Victor Stinner8c62be82010-05-06 00:08:46 +00001885 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001886#ifdef MS_WINDOWS
1887 if (win32_warn_bytes_api()) {
1888 Py_DECREF(opath);
1889 return NULL;
1890 }
1891#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001892 path = PyBytes_AsString(opath);
1893 Py_BEGIN_ALLOW_THREADS
1894 res = (*statfunc)(path, &st);
1895 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001896
Victor Stinner8c62be82010-05-06 00:08:46 +00001897 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001898#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001899 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001900#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001901 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001902#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001903 }
1904 else
Victor Stinner4195b5c2012-02-08 23:03:19 +01001905 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001906
Victor Stinner8c62be82010-05-06 00:08:46 +00001907 Py_DECREF(opath);
1908 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001909}
1910
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001911/* POSIX methods */
1912
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001913PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001914"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001915Use the real uid/gid to test for access to a path. Note that most\n\
1916operations will use the effective uid/gid, therefore this routine can\n\
1917be used in a suid/sgid environment to test if the invoking user has the\n\
1918specified access to the path. The mode argument can be F_OK to test\n\
1919existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001920
1921static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001922posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001923{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001924 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00001925 int mode;
1926
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001927#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001928 DWORD attr;
Victor Stinnereb5657a2011-09-30 01:44:27 +02001929 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00001930 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02001931 wchar_t* wpath = PyUnicode_AsUnicode(po);
1932 if (wpath == NULL)
1933 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001934 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02001935 attr = GetFileAttributesW(wpath);
Victor Stinner8c62be82010-05-06 00:08:46 +00001936 Py_END_ALLOW_THREADS
1937 goto finish;
1938 }
1939 /* Drop the argument parsing error as narrow strings
1940 are also valid. */
1941 PyErr_Clear();
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001942 if (!PyArg_ParseTuple(args, "yi:access", &path, &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00001943 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001944 if (win32_warn_bytes_api())
1945 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001946 Py_BEGIN_ALLOW_THREADS
1947 attr = GetFileAttributesA(path);
1948 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001949finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001950 if (attr == 0xFFFFFFFF)
1951 /* File does not exist, or cannot read attributes */
1952 return PyBool_FromLong(0);
1953 /* Access is possible if either write access wasn't requested, or
1954 the file isn't read-only, or if it's a directory, as there are
1955 no read-only directories on Windows. */
1956 return PyBool_FromLong(!(mode & 2)
1957 || !(attr & FILE_ATTRIBUTE_READONLY)
1958 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001959#else
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001960 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00001961 int res;
1962 if (!PyArg_ParseTuple(args, "O&i:access",
1963 PyUnicode_FSConverter, &opath, &mode))
1964 return NULL;
1965 path = PyBytes_AsString(opath);
1966 Py_BEGIN_ALLOW_THREADS
1967 res = access(path, mode);
1968 Py_END_ALLOW_THREADS
1969 Py_DECREF(opath);
1970 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001971#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001972}
1973
Guido van Rossumd371ff11999-01-25 16:12:23 +00001974#ifndef F_OK
1975#define F_OK 0
1976#endif
1977#ifndef R_OK
1978#define R_OK 4
1979#endif
1980#ifndef W_OK
1981#define W_OK 2
1982#endif
1983#ifndef X_OK
1984#define X_OK 1
1985#endif
1986
1987#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001988PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001989"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001990Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001991
1992static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001993posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001994{
Victor Stinner8c62be82010-05-06 00:08:46 +00001995 int id;
1996 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001997
Victor Stinner8c62be82010-05-06 00:08:46 +00001998 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1999 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002000
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002001#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002002 /* file descriptor 0 only, the default input device (stdin) */
2003 if (id == 0) {
2004 ret = ttyname();
2005 }
2006 else {
2007 ret = NULL;
2008 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002009#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002010 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002011#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002012 if (ret == NULL)
2013 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002014 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002015}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002016#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002017
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002018#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002019PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002020"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002021Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002022
2023static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002024posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002025{
Victor Stinner8c62be82010-05-06 00:08:46 +00002026 char *ret;
2027 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002028
Greg Wardb48bc172000-03-01 21:51:56 +00002029#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002030 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002031#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002032 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002033#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002034 if (ret == NULL)
2035 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002036 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002037}
2038#endif
2039
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002040PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002041"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002042Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002043
Barry Warsaw53699e91996-12-10 23:23:01 +00002044static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002045posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002046{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002047#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002048 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002049#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002050 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00002051#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002052 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002053#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002054 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002055#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002056}
2057
Fred Drake4d1e64b2002-04-15 19:40:07 +00002058#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002060"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00002061Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002062opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00002063
2064static PyObject *
2065posix_fchdir(PyObject *self, PyObject *fdobj)
2066{
Victor Stinner8c62be82010-05-06 00:08:46 +00002067 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002068}
2069#endif /* HAVE_FCHDIR */
2070
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002072PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002073"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002074Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002075
Barry Warsaw53699e91996-12-10 23:23:01 +00002076static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002077posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002078{
Victor Stinner8c62be82010-05-06 00:08:46 +00002079 PyObject *opath = NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002080 const char *path = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002081 int i;
2082 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002083#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002084 DWORD attr;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002085 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00002086 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002087 wchar_t *wpath = PyUnicode_AsUnicode(po);
2088 if (wpath == NULL)
2089 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002090 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02002091 attr = GetFileAttributesW(wpath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002092 if (attr != 0xFFFFFFFF) {
2093 if (i & _S_IWRITE)
2094 attr &= ~FILE_ATTRIBUTE_READONLY;
2095 else
2096 attr |= FILE_ATTRIBUTE_READONLY;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002097 res = SetFileAttributesW(wpath, attr);
Victor Stinner8c62be82010-05-06 00:08:46 +00002098 }
2099 else
2100 res = 0;
2101 Py_END_ALLOW_THREADS
2102 if (!res)
Victor Stinnereb5657a2011-09-30 01:44:27 +02002103 return win32_error_object("chmod", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00002104 Py_INCREF(Py_None);
2105 return Py_None;
2106 }
2107 /* Drop the argument parsing error as narrow strings
2108 are also valid. */
2109 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002110
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002111 if (!PyArg_ParseTuple(args, "yi:chmod", &path, &i))
Victor Stinner8c62be82010-05-06 00:08:46 +00002112 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002113 if (win32_warn_bytes_api())
2114 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002115 Py_BEGIN_ALLOW_THREADS
2116 attr = GetFileAttributesA(path);
2117 if (attr != 0xFFFFFFFF) {
2118 if (i & _S_IWRITE)
2119 attr &= ~FILE_ATTRIBUTE_READONLY;
2120 else
2121 attr |= FILE_ATTRIBUTE_READONLY;
2122 res = SetFileAttributesA(path, attr);
2123 }
2124 else
2125 res = 0;
2126 Py_END_ALLOW_THREADS
2127 if (!res) {
2128 win32_error("chmod", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00002129 return NULL;
2130 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002131 Py_INCREF(Py_None);
2132 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002133#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002134 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2135 &opath, &i))
2136 return NULL;
2137 path = PyBytes_AsString(opath);
2138 Py_BEGIN_ALLOW_THREADS
2139 res = chmod(path, i);
2140 Py_END_ALLOW_THREADS
2141 if (res < 0)
2142 return posix_error_with_allocated_filename(opath);
2143 Py_DECREF(opath);
2144 Py_INCREF(Py_None);
2145 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002146#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002147}
2148
Christian Heimes4e30a842007-11-30 22:12:06 +00002149#ifdef HAVE_FCHMOD
2150PyDoc_STRVAR(posix_fchmod__doc__,
2151"fchmod(fd, mode)\n\n\
2152Change the access permissions of the file given by file\n\
2153descriptor fd.");
2154
2155static PyObject *
2156posix_fchmod(PyObject *self, PyObject *args)
2157{
Victor Stinner8c62be82010-05-06 00:08:46 +00002158 int fd, mode, res;
2159 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2160 return NULL;
2161 Py_BEGIN_ALLOW_THREADS
2162 res = fchmod(fd, mode);
2163 Py_END_ALLOW_THREADS
2164 if (res < 0)
2165 return posix_error();
2166 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002167}
2168#endif /* HAVE_FCHMOD */
2169
2170#ifdef HAVE_LCHMOD
2171PyDoc_STRVAR(posix_lchmod__doc__,
2172"lchmod(path, mode)\n\n\
2173Change the access permissions of a file. If path is a symlink, this\n\
2174affects the link itself rather than the target.");
2175
2176static PyObject *
2177posix_lchmod(PyObject *self, PyObject *args)
2178{
Victor Stinner8c62be82010-05-06 00:08:46 +00002179 PyObject *opath;
2180 char *path;
2181 int i;
2182 int res;
2183 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2184 &opath, &i))
2185 return NULL;
2186 path = PyBytes_AsString(opath);
2187 Py_BEGIN_ALLOW_THREADS
2188 res = lchmod(path, i);
2189 Py_END_ALLOW_THREADS
2190 if (res < 0)
2191 return posix_error_with_allocated_filename(opath);
2192 Py_DECREF(opath);
2193 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002194}
2195#endif /* HAVE_LCHMOD */
2196
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002197
Thomas Wouterscf297e42007-02-23 15:07:44 +00002198#ifdef HAVE_CHFLAGS
2199PyDoc_STRVAR(posix_chflags__doc__,
2200"chflags(path, flags)\n\n\
2201Set file flags.");
2202
2203static PyObject *
2204posix_chflags(PyObject *self, PyObject *args)
2205{
Victor Stinner8c62be82010-05-06 00:08:46 +00002206 PyObject *opath;
2207 char *path;
2208 unsigned long flags;
2209 int res;
2210 if (!PyArg_ParseTuple(args, "O&k:chflags",
2211 PyUnicode_FSConverter, &opath, &flags))
2212 return NULL;
2213 path = PyBytes_AsString(opath);
2214 Py_BEGIN_ALLOW_THREADS
2215 res = chflags(path, flags);
2216 Py_END_ALLOW_THREADS
2217 if (res < 0)
2218 return posix_error_with_allocated_filename(opath);
2219 Py_DECREF(opath);
2220 Py_INCREF(Py_None);
2221 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002222}
2223#endif /* HAVE_CHFLAGS */
2224
2225#ifdef HAVE_LCHFLAGS
2226PyDoc_STRVAR(posix_lchflags__doc__,
2227"lchflags(path, flags)\n\n\
2228Set file flags.\n\
2229This function will not follow symbolic links.");
2230
2231static PyObject *
2232posix_lchflags(PyObject *self, PyObject *args)
2233{
Victor Stinner8c62be82010-05-06 00:08:46 +00002234 PyObject *opath;
2235 char *path;
2236 unsigned long flags;
2237 int res;
2238 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2239 PyUnicode_FSConverter, &opath, &flags))
2240 return NULL;
2241 path = PyBytes_AsString(opath);
2242 Py_BEGIN_ALLOW_THREADS
2243 res = lchflags(path, flags);
2244 Py_END_ALLOW_THREADS
2245 if (res < 0)
2246 return posix_error_with_allocated_filename(opath);
2247 Py_DECREF(opath);
2248 Py_INCREF(Py_None);
2249 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002250}
2251#endif /* HAVE_LCHFLAGS */
2252
Martin v. Löwis244edc82001-10-04 22:44:26 +00002253#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002254PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002255"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002256Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002257
2258static PyObject *
2259posix_chroot(PyObject *self, PyObject *args)
2260{
Victor Stinner8c62be82010-05-06 00:08:46 +00002261 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002262}
2263#endif
2264
Guido van Rossum21142a01999-01-08 21:05:37 +00002265#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002266PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002267"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002268force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002269
2270static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002271posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002272{
Stefan Krah0e803b32010-11-26 16:16:47 +00002273 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002274}
2275#endif /* HAVE_FSYNC */
2276
Ross Lagerwall7807c352011-03-17 20:20:30 +02002277#ifdef HAVE_SYNC
2278PyDoc_STRVAR(posix_sync__doc__,
2279"sync()\n\n\
2280Force write of everything to disk.");
2281
2282static PyObject *
2283posix_sync(PyObject *self, PyObject *noargs)
2284{
2285 Py_BEGIN_ALLOW_THREADS
2286 sync();
2287 Py_END_ALLOW_THREADS
2288 Py_RETURN_NONE;
2289}
2290#endif
2291
Guido van Rossum21142a01999-01-08 21:05:37 +00002292#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002293
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002294#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002295extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2296#endif
2297
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002298PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002299"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002300force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002301 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002302
2303static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002304posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002305{
Stefan Krah0e803b32010-11-26 16:16:47 +00002306 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002307}
2308#endif /* HAVE_FDATASYNC */
2309
2310
Fredrik Lundh10723342000-07-10 16:38:09 +00002311#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002312PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002313"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002314Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002315
Barry Warsaw53699e91996-12-10 23:23:01 +00002316static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002317posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002318{
Victor Stinner8c62be82010-05-06 00:08:46 +00002319 PyObject *opath;
2320 char *path;
2321 long uid, gid;
2322 int res;
2323 if (!PyArg_ParseTuple(args, "O&ll:chown",
2324 PyUnicode_FSConverter, &opath,
2325 &uid, &gid))
2326 return NULL;
2327 path = PyBytes_AsString(opath);
2328 Py_BEGIN_ALLOW_THREADS
2329 res = chown(path, (uid_t) uid, (gid_t) gid);
2330 Py_END_ALLOW_THREADS
2331 if (res < 0)
2332 return posix_error_with_allocated_filename(opath);
2333 Py_DECREF(opath);
2334 Py_INCREF(Py_None);
2335 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002336}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002337#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002338
Christian Heimes4e30a842007-11-30 22:12:06 +00002339#ifdef HAVE_FCHOWN
2340PyDoc_STRVAR(posix_fchown__doc__,
2341"fchown(fd, uid, gid)\n\n\
2342Change the owner and group id of the file given by file descriptor\n\
2343fd to the numeric uid and gid.");
2344
2345static PyObject *
2346posix_fchown(PyObject *self, PyObject *args)
2347{
Victor Stinner8c62be82010-05-06 00:08:46 +00002348 int fd;
2349 long uid, gid;
2350 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002351 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002352 return NULL;
2353 Py_BEGIN_ALLOW_THREADS
2354 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2355 Py_END_ALLOW_THREADS
2356 if (res < 0)
2357 return posix_error();
2358 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002359}
2360#endif /* HAVE_FCHOWN */
2361
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002362#ifdef HAVE_LCHOWN
2363PyDoc_STRVAR(posix_lchown__doc__,
2364"lchown(path, uid, gid)\n\n\
2365Change the owner and group id of path to the numeric uid and gid.\n\
2366This function will not follow symbolic links.");
2367
2368static PyObject *
2369posix_lchown(PyObject *self, PyObject *args)
2370{
Victor Stinner8c62be82010-05-06 00:08:46 +00002371 PyObject *opath;
2372 char *path;
2373 long uid, gid;
2374 int res;
2375 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2376 PyUnicode_FSConverter, &opath,
2377 &uid, &gid))
2378 return NULL;
2379 path = PyBytes_AsString(opath);
2380 Py_BEGIN_ALLOW_THREADS
2381 res = lchown(path, (uid_t) uid, (gid_t) gid);
2382 Py_END_ALLOW_THREADS
2383 if (res < 0)
2384 return posix_error_with_allocated_filename(opath);
2385 Py_DECREF(opath);
2386 Py_INCREF(Py_None);
2387 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002388}
2389#endif /* HAVE_LCHOWN */
2390
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002391
Guido van Rossum36bc6801995-06-14 22:54:23 +00002392#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002393static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002394posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002395{
Victor Stinner8c62be82010-05-06 00:08:46 +00002396 char buf[1026];
2397 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002398
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002399#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002400 if (!use_bytes) {
2401 wchar_t wbuf[1026];
2402 wchar_t *wbuf2 = wbuf;
2403 PyObject *resobj;
2404 DWORD len;
2405 Py_BEGIN_ALLOW_THREADS
2406 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2407 /* If the buffer is large enough, len does not include the
2408 terminating \0. If the buffer is too small, len includes
2409 the space needed for the terminator. */
2410 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2411 wbuf2 = malloc(len * sizeof(wchar_t));
2412 if (wbuf2)
2413 len = GetCurrentDirectoryW(len, wbuf2);
2414 }
2415 Py_END_ALLOW_THREADS
2416 if (!wbuf2) {
2417 PyErr_NoMemory();
2418 return NULL;
2419 }
2420 if (!len) {
2421 if (wbuf2 != wbuf) free(wbuf2);
2422 return win32_error("getcwdu", NULL);
2423 }
2424 resobj = PyUnicode_FromWideChar(wbuf2, len);
2425 if (wbuf2 != wbuf) free(wbuf2);
2426 return resobj;
2427 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01002428
2429 if (win32_warn_bytes_api())
2430 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002431#endif
2432
Victor Stinner8c62be82010-05-06 00:08:46 +00002433 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002434#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002435 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002436#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002437 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002438#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002439 Py_END_ALLOW_THREADS
2440 if (res == NULL)
2441 return posix_error();
2442 if (use_bytes)
2443 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002444 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002445}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002446
2447PyDoc_STRVAR(posix_getcwd__doc__,
2448"getcwd() -> path\n\n\
2449Return a unicode string representing the current working directory.");
2450
2451static PyObject *
2452posix_getcwd_unicode(PyObject *self)
2453{
2454 return posix_getcwd(0);
2455}
2456
2457PyDoc_STRVAR(posix_getcwdb__doc__,
2458"getcwdb() -> path\n\n\
2459Return a bytes string representing the current working directory.");
2460
2461static PyObject *
2462posix_getcwd_bytes(PyObject *self)
2463{
2464 return posix_getcwd(1);
2465}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002466#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002467
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002468
Guido van Rossumb6775db1994-08-01 11:34:53 +00002469#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002470PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002471"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002472Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002473
Barry Warsaw53699e91996-12-10 23:23:01 +00002474static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002475posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002476{
Victor Stinner8c62be82010-05-06 00:08:46 +00002477 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002478}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002479#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002480
Brian Curtin1b9df392010-11-24 20:24:31 +00002481#ifdef MS_WINDOWS
2482PyDoc_STRVAR(win32_link__doc__,
2483"link(src, dst)\n\n\
2484Create a hard link to a file.");
2485
2486static PyObject *
2487win32_link(PyObject *self, PyObject *args)
2488{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002489 PyObject *src, *dst;
2490 BOOL ok;
Brian Curtin1b9df392010-11-24 20:24:31 +00002491
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002492 if (PyArg_ParseTuple(args, "UU:link", &src, &dst))
Victor Stinnereb5657a2011-09-30 01:44:27 +02002493 {
2494 wchar_t *wsrc, *wdst;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002495
2496 wsrc = PyUnicode_AsUnicode(src);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002497 if (wsrc == NULL)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002498 goto error;
2499 wdst = PyUnicode_AsUnicode(dst);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002500 if (wdst == NULL)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002501 goto error;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002502
Brian Curtinfc889c42010-11-28 23:59:46 +00002503 Py_BEGIN_ALLOW_THREADS
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002504 ok = CreateHardLinkW(wdst, wsrc, NULL);
Brian Curtinfc889c42010-11-28 23:59:46 +00002505 Py_END_ALLOW_THREADS
2506
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002507 if (!ok)
Brian Curtinfc889c42010-11-28 23:59:46 +00002508 return win32_error("link", NULL);
Brian Curtinfc889c42010-11-28 23:59:46 +00002509 Py_RETURN_NONE;
2510 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002511 else {
2512 PyErr_Clear();
2513 if (!PyArg_ParseTuple(args, "O&O&:link",
2514 PyUnicode_FSConverter, &src,
2515 PyUnicode_FSConverter, &dst))
2516 return NULL;
Brian Curtinfc889c42010-11-28 23:59:46 +00002517
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002518 if (win32_warn_bytes_api())
2519 goto error;
Brian Curtinfc889c42010-11-28 23:59:46 +00002520
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002521 Py_BEGIN_ALLOW_THREADS
2522 ok = CreateHardLinkA(PyBytes_AS_STRING(dst),
2523 PyBytes_AS_STRING(src),
2524 NULL);
2525 Py_END_ALLOW_THREADS
2526
2527 Py_XDECREF(src);
2528 Py_XDECREF(dst);
2529
2530 if (!ok)
2531 return win32_error("link", NULL);
2532 Py_RETURN_NONE;
2533
2534 error:
2535 Py_XDECREF(src);
2536 Py_XDECREF(dst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002537 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002538 }
Brian Curtin1b9df392010-11-24 20:24:31 +00002539}
2540#endif /* MS_WINDOWS */
2541
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002543PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002544"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002545Return a list containing the names of the entries in the directory.\n\
2546\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002547 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002548\n\
2549The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002550entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002551
Barry Warsaw53699e91996-12-10 23:23:01 +00002552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002553posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002554{
Victor Stinner8c62be82010-05-06 00:08:46 +00002555 /* XXX Should redo this putting the (now four) versions of opendir
2556 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002557#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002558
Victor Stinner8c62be82010-05-06 00:08:46 +00002559 PyObject *d, *v;
2560 HANDLE hFindFile;
2561 BOOL result;
2562 WIN32_FIND_DATA FileData;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002563 const char *path;
2564 Py_ssize_t pathlen;
Victor Stinner8c62be82010-05-06 00:08:46 +00002565 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2566 char *bufptr = namebuf;
2567 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002568
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002569 PyObject *po = NULL;
2570 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002571 WIN32_FIND_DATAW wFileData;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002572 wchar_t *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002573
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002574 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002575 po_wchars = L".";
2576 len = 1;
2577 } else {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02002578 po_wchars = PyUnicode_AsUnicodeAndSize(po, &len);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002579 if (po_wchars == NULL)
2580 return NULL;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002581 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002582 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002583 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2584 if (!wnamebuf) {
2585 PyErr_NoMemory();
2586 return NULL;
2587 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002588 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002589 if (len > 0) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002590 wchar_t wch = wnamebuf[len-1];
Victor Stinner8c62be82010-05-06 00:08:46 +00002591 if (wch != L'/' && wch != L'\\' && wch != L':')
2592 wnamebuf[len++] = L'\\';
2593 wcscpy(wnamebuf + len, L"*.*");
2594 }
2595 if ((d = PyList_New(0)) == NULL) {
2596 free(wnamebuf);
2597 return NULL;
2598 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002599 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002600 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002601 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002602 if (hFindFile == INVALID_HANDLE_VALUE) {
2603 int error = GetLastError();
2604 if (error == ERROR_FILE_NOT_FOUND) {
2605 free(wnamebuf);
2606 return d;
2607 }
2608 Py_DECREF(d);
2609 win32_error_unicode("FindFirstFileW", wnamebuf);
2610 free(wnamebuf);
2611 return NULL;
2612 }
2613 do {
2614 /* Skip over . and .. */
2615 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2616 wcscmp(wFileData.cFileName, L"..") != 0) {
Victor Stinner9d3b93b2011-11-22 02:27:30 +01002617 v = PyUnicode_FromWideChar(wFileData.cFileName, wcslen(wFileData.cFileName));
Victor Stinner8c62be82010-05-06 00:08:46 +00002618 if (v == NULL) {
2619 Py_DECREF(d);
2620 d = NULL;
2621 break;
2622 }
2623 if (PyList_Append(d, v) != 0) {
2624 Py_DECREF(v);
2625 Py_DECREF(d);
2626 d = NULL;
2627 break;
2628 }
2629 Py_DECREF(v);
2630 }
2631 Py_BEGIN_ALLOW_THREADS
2632 result = FindNextFileW(hFindFile, &wFileData);
2633 Py_END_ALLOW_THREADS
2634 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2635 it got to the end of the directory. */
2636 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2637 Py_DECREF(d);
2638 win32_error_unicode("FindNextFileW", wnamebuf);
2639 FindClose(hFindFile);
2640 free(wnamebuf);
2641 return NULL;
2642 }
2643 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002644
Victor Stinner8c62be82010-05-06 00:08:46 +00002645 if (FindClose(hFindFile) == FALSE) {
2646 Py_DECREF(d);
2647 win32_error_unicode("FindClose", wnamebuf);
2648 free(wnamebuf);
2649 return NULL;
2650 }
2651 free(wnamebuf);
2652 return d;
2653 }
2654 /* Drop the argument parsing error as narrow strings
2655 are also valid. */
2656 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002657
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002658 if (!PyArg_ParseTuple(args, "y#:listdir", &path, &pathlen))
Victor Stinner8c62be82010-05-06 00:08:46 +00002659 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002660 if (win32_warn_bytes_api())
2661 return NULL;
2662 if (pathlen+1 > MAX_PATH) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002663 PyErr_SetString(PyExc_ValueError, "path too long");
Victor Stinner8c62be82010-05-06 00:08:46 +00002664 return NULL;
2665 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002666 strcpy(namebuf, path);
2667 len = pathlen;
Victor Stinner8c62be82010-05-06 00:08:46 +00002668 if (len > 0) {
2669 char ch = namebuf[len-1];
2670 if (ch != SEP && ch != ALTSEP && ch != ':')
2671 namebuf[len++] = '/';
2672 strcpy(namebuf + len, "*.*");
2673 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002674
Victor Stinner8c62be82010-05-06 00:08:46 +00002675 if ((d = PyList_New(0)) == NULL)
2676 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002677
Antoine Pitroub73caab2010-08-09 23:39:31 +00002678 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002679 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002680 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002681 if (hFindFile == INVALID_HANDLE_VALUE) {
2682 int error = GetLastError();
2683 if (error == ERROR_FILE_NOT_FOUND)
2684 return d;
2685 Py_DECREF(d);
2686 return win32_error("FindFirstFile", namebuf);
2687 }
2688 do {
2689 /* Skip over . and .. */
2690 if (strcmp(FileData.cFileName, ".") != 0 &&
2691 strcmp(FileData.cFileName, "..") != 0) {
2692 v = PyBytes_FromString(FileData.cFileName);
2693 if (v == NULL) {
2694 Py_DECREF(d);
2695 d = NULL;
2696 break;
2697 }
2698 if (PyList_Append(d, v) != 0) {
2699 Py_DECREF(v);
2700 Py_DECREF(d);
2701 d = NULL;
2702 break;
2703 }
2704 Py_DECREF(v);
2705 }
2706 Py_BEGIN_ALLOW_THREADS
2707 result = FindNextFile(hFindFile, &FileData);
2708 Py_END_ALLOW_THREADS
2709 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2710 it got to the end of the directory. */
2711 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2712 Py_DECREF(d);
2713 win32_error("FindNextFile", namebuf);
2714 FindClose(hFindFile);
2715 return NULL;
2716 }
2717 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002718
Victor Stinner8c62be82010-05-06 00:08:46 +00002719 if (FindClose(hFindFile) == FALSE) {
2720 Py_DECREF(d);
2721 return win32_error("FindClose", namebuf);
2722 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002723
Victor Stinner8c62be82010-05-06 00:08:46 +00002724 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002725
Tim Peters0bb44a42000-09-15 07:44:49 +00002726#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002727
2728#ifndef MAX_PATH
2729#define MAX_PATH CCHMAXPATH
2730#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002731 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002732 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002733 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002734 PyObject *d, *v;
2735 char namebuf[MAX_PATH+5];
2736 HDIR hdir = 1;
2737 ULONG srchcnt = 1;
2738 FILEFINDBUF3 ep;
2739 APIRET rc;
2740
Victor Stinner8c62be82010-05-06 00:08:46 +00002741 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002742 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002743 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002744 name = PyBytes_AsString(oname);
2745 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002746 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002747 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002748 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002749 return NULL;
2750 }
2751 strcpy(namebuf, name);
2752 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002753 if (*pt == ALTSEP)
2754 *pt = SEP;
2755 if (namebuf[len-1] != SEP)
2756 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002757 strcpy(namebuf + len, "*.*");
2758
Neal Norwitz6c913782007-10-14 03:23:09 +00002759 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002760 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002761 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002762 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002763
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002764 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2765 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002766 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002767 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2768 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2769 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002770
2771 if (rc != NO_ERROR) {
2772 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002773 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002774 }
2775
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002776 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002777 do {
2778 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002779 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002780 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002781
2782 strcpy(namebuf, ep.achName);
2783
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002784 /* Leave Case of Name Alone -- In Native Form */
2785 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002786
Christian Heimes72b710a2008-05-26 13:28:38 +00002787 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002788 if (v == NULL) {
2789 Py_DECREF(d);
2790 d = NULL;
2791 break;
2792 }
2793 if (PyList_Append(d, v) != 0) {
2794 Py_DECREF(v);
2795 Py_DECREF(d);
2796 d = NULL;
2797 break;
2798 }
2799 Py_DECREF(v);
2800 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2801 }
2802
Victor Stinnerdcb24032010-04-22 12:08:36 +00002803 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002804 return d;
2805#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002806 PyObject *oname;
2807 char *name;
2808 PyObject *d, *v;
2809 DIR *dirp;
2810 struct dirent *ep;
2811 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002812
Victor Stinner8c62be82010-05-06 00:08:46 +00002813 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002814 /* v is never read, so it does not need to be initialized yet. */
2815 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002816 arg_is_unicode = 0;
2817 PyErr_Clear();
2818 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002819 oname = NULL;
2820 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002821 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002822 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002823 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002824 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002825 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002826 Py_BEGIN_ALLOW_THREADS
2827 dirp = opendir(name);
2828 Py_END_ALLOW_THREADS
2829 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002830 return posix_error_with_allocated_filename(oname);
2831 }
2832 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002833 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002834 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002835 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002836 Py_DECREF(oname);
2837 return NULL;
2838 }
2839 for (;;) {
2840 errno = 0;
2841 Py_BEGIN_ALLOW_THREADS
2842 ep = readdir(dirp);
2843 Py_END_ALLOW_THREADS
2844 if (ep == NULL) {
2845 if (errno == 0) {
2846 break;
2847 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002848 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002849 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002850 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002851 Py_DECREF(d);
2852 return posix_error_with_allocated_filename(oname);
2853 }
2854 }
2855 if (ep->d_name[0] == '.' &&
2856 (NAMLEN(ep) == 1 ||
2857 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2858 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002859 if (arg_is_unicode)
2860 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2861 else
2862 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002863 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002864 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002865 break;
2866 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002867 if (PyList_Append(d, v) != 0) {
2868 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002869 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002870 break;
2871 }
2872 Py_DECREF(v);
2873 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002874 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002875 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002876 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002877 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002878
Victor Stinner8c62be82010-05-06 00:08:46 +00002879 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002880
Tim Peters0bb44a42000-09-15 07:44:49 +00002881#endif /* which OS */
2882} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002883
Antoine Pitrou8250e232011-02-25 23:41:16 +00002884#ifdef HAVE_FDOPENDIR
Charles-François Natali77940902012-02-06 19:54:48 +01002885PyDoc_STRVAR(posix_flistdir__doc__,
2886"flistdir(fd) -> list_of_strings\n\n\
Charles-François Natali76961fa2012-01-10 20:25:09 +01002887Like listdir(), but uses a file descriptor instead.");
Antoine Pitrou8250e232011-02-25 23:41:16 +00002888
2889static PyObject *
Charles-François Natali77940902012-02-06 19:54:48 +01002890posix_flistdir(PyObject *self, PyObject *args)
Antoine Pitrou8250e232011-02-25 23:41:16 +00002891{
2892 PyObject *d, *v;
2893 DIR *dirp;
2894 struct dirent *ep;
2895 int fd;
2896
2897 errno = 0;
Charles-François Natali77940902012-02-06 19:54:48 +01002898 if (!PyArg_ParseTuple(args, "i:flistdir", &fd))
Antoine Pitrou8250e232011-02-25 23:41:16 +00002899 return NULL;
Charles-François Natali76961fa2012-01-10 20:25:09 +01002900 /* closedir() closes the FD, so we duplicate it */
2901 fd = dup(fd);
2902 if (fd < 0)
2903 return posix_error();
Antoine Pitrou8250e232011-02-25 23:41:16 +00002904 Py_BEGIN_ALLOW_THREADS
2905 dirp = fdopendir(fd);
2906 Py_END_ALLOW_THREADS
2907 if (dirp == NULL) {
2908 close(fd);
2909 return posix_error();
2910 }
2911 if ((d = PyList_New(0)) == NULL) {
2912 Py_BEGIN_ALLOW_THREADS
2913 closedir(dirp);
2914 Py_END_ALLOW_THREADS
2915 return NULL;
2916 }
2917 for (;;) {
2918 errno = 0;
2919 Py_BEGIN_ALLOW_THREADS
2920 ep = readdir(dirp);
2921 Py_END_ALLOW_THREADS
2922 if (ep == NULL) {
2923 if (errno == 0) {
2924 break;
2925 } else {
2926 Py_BEGIN_ALLOW_THREADS
Charles-François Natalif2840a82012-01-08 20:30:47 +01002927 rewinddir(dirp);
Antoine Pitrou8250e232011-02-25 23:41:16 +00002928 closedir(dirp);
2929 Py_END_ALLOW_THREADS
2930 Py_DECREF(d);
2931 return posix_error();
2932 }
2933 }
2934 if (ep->d_name[0] == '.' &&
2935 (NAMLEN(ep) == 1 ||
2936 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2937 continue;
2938 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2939 if (v == NULL) {
2940 Py_CLEAR(d);
2941 break;
2942 }
2943 if (PyList_Append(d, v) != 0) {
2944 Py_DECREF(v);
2945 Py_CLEAR(d);
2946 break;
2947 }
2948 Py_DECREF(v);
2949 }
2950 Py_BEGIN_ALLOW_THREADS
Charles-François Natalif2840a82012-01-08 20:30:47 +01002951 rewinddir(dirp);
Antoine Pitrou8250e232011-02-25 23:41:16 +00002952 closedir(dirp);
2953 Py_END_ALLOW_THREADS
2954
2955 return d;
2956}
2957#endif
2958
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002959#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002960/* A helper function for abspath on win32 */
2961static PyObject *
2962posix__getfullpathname(PyObject *self, PyObject *args)
2963{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002964 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002965 char outbuf[MAX_PATH*2];
2966 char *temp;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002967 PyObject *po;
2968
2969 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po))
2970 {
2971 wchar_t *wpath;
2972 wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2973 wchar_t *wtemp;
Victor Stinner8c62be82010-05-06 00:08:46 +00002974 DWORD result;
2975 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002976
2977 wpath = PyUnicode_AsUnicode(po);
2978 if (wpath == NULL)
2979 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002980 result = GetFullPathNameW(wpath,
Victor Stinner63941882011-09-29 00:42:28 +02002981 Py_ARRAY_LENGTH(woutbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00002982 woutbuf, &wtemp);
Victor Stinner63941882011-09-29 00:42:28 +02002983 if (result > Py_ARRAY_LENGTH(woutbuf)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002984 woutbufp = malloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00002985 if (!woutbufp)
2986 return PyErr_NoMemory();
2987 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2988 }
2989 if (result)
Victor Stinner9d3b93b2011-11-22 02:27:30 +01002990 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
Victor Stinner8c62be82010-05-06 00:08:46 +00002991 else
Victor Stinnereb5657a2011-09-30 01:44:27 +02002992 v = win32_error_object("GetFullPathNameW", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00002993 if (woutbufp != woutbuf)
2994 free(woutbufp);
2995 return v;
2996 }
2997 /* Drop the argument parsing error as narrow strings
2998 are also valid. */
2999 PyErr_Clear();
Victor Stinnereb5657a2011-09-30 01:44:27 +02003000
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003001 if (!PyArg_ParseTuple (args, "y:_getfullpathname",
3002 &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00003003 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003004 if (win32_warn_bytes_api())
3005 return NULL;
Victor Stinner63941882011-09-29 00:42:28 +02003006 if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00003007 outbuf, &temp)) {
3008 win32_error("GetFullPathName", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00003009 return NULL;
3010 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003011 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
3012 return PyUnicode_Decode(outbuf, strlen(outbuf),
3013 Py_FileSystemDefaultEncoding, NULL);
3014 }
3015 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00003016} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00003017
Brian Curtind25aef52011-06-13 15:16:04 -05003018
Brian Curtinf5e76d02010-11-24 13:14:05 +00003019
Brian Curtind40e6f72010-07-08 21:39:08 +00003020/* A helper function for samepath on windows */
3021static PyObject *
3022posix__getfinalpathname(PyObject *self, PyObject *args)
3023{
3024 HANDLE hFile;
3025 int buf_size;
3026 wchar_t *target_path;
3027 int result_length;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003028 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00003029 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003030
Victor Stinnereb5657a2011-09-30 01:44:27 +02003031 if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po))
Brian Curtind40e6f72010-07-08 21:39:08 +00003032 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003033 path = PyUnicode_AsUnicode(po);
3034 if (path == NULL)
3035 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00003036
3037 if(!check_GetFinalPathNameByHandle()) {
3038 /* If the OS doesn't have GetFinalPathNameByHandle, return a
3039 NotImplementedError. */
3040 return PyErr_Format(PyExc_NotImplementedError,
3041 "GetFinalPathNameByHandle not available on this platform");
3042 }
3043
3044 hFile = CreateFileW(
3045 path,
3046 0, /* desired access */
3047 0, /* share mode */
3048 NULL, /* security attributes */
3049 OPEN_EXISTING,
3050 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3051 FILE_FLAG_BACKUP_SEMANTICS,
3052 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003053
Victor Stinnereb5657a2011-09-30 01:44:27 +02003054 if(hFile == INVALID_HANDLE_VALUE)
3055 return win32_error_object("CreateFileW", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003056
3057 /* We have a good handle to the target, use it to determine the
3058 target path name. */
3059 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
3060
3061 if(!buf_size)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003062 return win32_error_object("GetFinalPathNameByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003063
3064 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
3065 if(!target_path)
3066 return PyErr_NoMemory();
3067
3068 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
3069 buf_size, VOLUME_NAME_DOS);
3070 if(!result_length)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003071 return win32_error_object("GetFinalPathNamyByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003072
3073 if(!CloseHandle(hFile))
Victor Stinnereb5657a2011-09-30 01:44:27 +02003074 return win32_error_object("CloseHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003075
3076 target_path[result_length] = 0;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003077 result = PyUnicode_FromWideChar(target_path, result_length);
Brian Curtind40e6f72010-07-08 21:39:08 +00003078 free(target_path);
3079 return result;
3080
3081} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00003082
3083static PyObject *
3084posix__getfileinformation(PyObject *self, PyObject *args)
3085{
3086 HANDLE hFile;
3087 BY_HANDLE_FILE_INFORMATION info;
3088 int fd;
3089
3090 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3091 return NULL;
3092
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003093 if (!_PyVerify_fd(fd))
3094 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003095
3096 hFile = (HANDLE)_get_osfhandle(fd);
3097 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003098 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003099
3100 if (!GetFileInformationByHandle(hFile, &info))
3101 return win32_error("_getfileinformation", NULL);
3102
3103 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3104 info.nFileIndexHigh,
3105 info.nFileIndexLow);
3106}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003107
Brian Curtin95d028f2011-06-09 09:10:38 -05003108PyDoc_STRVAR(posix__isdir__doc__,
3109"Return true if the pathname refers to an existing directory.");
3110
Brian Curtin9c669cc2011-06-08 18:17:18 -05003111static PyObject *
3112posix__isdir(PyObject *self, PyObject *args)
3113{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003114 const char *path;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003115 PyObject *po;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003116 DWORD attributes;
3117
3118 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003119 wchar_t *wpath = PyUnicode_AsUnicode(po);
3120 if (wpath == NULL)
3121 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003122
3123 attributes = GetFileAttributesW(wpath);
3124 if (attributes == INVALID_FILE_ATTRIBUTES)
3125 Py_RETURN_FALSE;
3126 goto check;
3127 }
3128 /* Drop the argument parsing error as narrow strings
3129 are also valid. */
3130 PyErr_Clear();
3131
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003132 if (!PyArg_ParseTuple(args, "y:_isdir", &path))
Brian Curtin9c669cc2011-06-08 18:17:18 -05003133 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003134 if (win32_warn_bytes_api())
3135 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003136 attributes = GetFileAttributesA(path);
3137 if (attributes == INVALID_FILE_ATTRIBUTES)
3138 Py_RETURN_FALSE;
3139
3140check:
3141 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3142 Py_RETURN_TRUE;
3143 else
3144 Py_RETURN_FALSE;
3145}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003146#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003147
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003148PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003149"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003150Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003151
Barry Warsaw53699e91996-12-10 23:23:01 +00003152static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003153posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003154{
Victor Stinner8c62be82010-05-06 00:08:46 +00003155 int res;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003156 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00003157 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003158
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003159#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02003160 PyObject *po;
3161 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode))
3162 {
3163 wchar_t *wpath = PyUnicode_AsUnicode(po);
3164 if (wpath == NULL)
3165 return NULL;
3166
Victor Stinner8c62be82010-05-06 00:08:46 +00003167 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02003168 res = CreateDirectoryW(wpath, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003169 Py_END_ALLOW_THREADS
3170 if (!res)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003171 return win32_error_object("mkdir", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00003172 Py_INCREF(Py_None);
3173 return Py_None;
3174 }
3175 /* Drop the argument parsing error as narrow strings
3176 are also valid. */
3177 PyErr_Clear();
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003178 if (!PyArg_ParseTuple(args, "y|i:mkdir", &path, &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00003179 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003180 if (win32_warn_bytes_api())
3181 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003182 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003183 res = CreateDirectoryA(path, NULL);
3184 Py_END_ALLOW_THREADS
3185 if (!res) {
3186 win32_error("mkdir", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00003187 return NULL;
3188 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003189 Py_INCREF(Py_None);
3190 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003191#else
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003192 PyObject *opath;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003193
Victor Stinner8c62be82010-05-06 00:08:46 +00003194 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3195 PyUnicode_FSConverter, &opath, &mode))
3196 return NULL;
3197 path = PyBytes_AsString(opath);
3198 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003199#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003200 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003201#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003202 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003203#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003204 Py_END_ALLOW_THREADS
3205 if (res < 0)
3206 return posix_error_with_allocated_filename(opath);
3207 Py_DECREF(opath);
3208 Py_INCREF(Py_None);
3209 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003210#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003211}
3212
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003213
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003214/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3215#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003216#include <sys/resource.h>
3217#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003218
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003219
3220#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003221PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003222"nice(inc) -> new_priority\n\n\
3223Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003224
Barry Warsaw53699e91996-12-10 23:23:01 +00003225static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003226posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003227{
Victor Stinner8c62be82010-05-06 00:08:46 +00003228 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003229
Victor Stinner8c62be82010-05-06 00:08:46 +00003230 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3231 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003232
Victor Stinner8c62be82010-05-06 00:08:46 +00003233 /* There are two flavours of 'nice': one that returns the new
3234 priority (as required by almost all standards out there) and the
3235 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3236 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003237
Victor Stinner8c62be82010-05-06 00:08:46 +00003238 If we are of the nice family that returns the new priority, we
3239 need to clear errno before the call, and check if errno is filled
3240 before calling posix_error() on a returnvalue of -1, because the
3241 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003242
Victor Stinner8c62be82010-05-06 00:08:46 +00003243 errno = 0;
3244 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003245#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003246 if (value == 0)
3247 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003248#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003249 if (value == -1 && errno != 0)
3250 /* either nice() or getpriority() returned an error */
3251 return posix_error();
3252 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003253}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003254#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003255
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003256
3257#ifdef HAVE_GETPRIORITY
3258PyDoc_STRVAR(posix_getpriority__doc__,
3259"getpriority(which, who) -> current_priority\n\n\
3260Get program scheduling priority.");
3261
3262static PyObject *
3263posix_getpriority(PyObject *self, PyObject *args)
3264{
3265 int which, who, retval;
3266
3267 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3268 return NULL;
3269 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003270 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003271 if (errno != 0)
3272 return posix_error();
3273 return PyLong_FromLong((long)retval);
3274}
3275#endif /* HAVE_GETPRIORITY */
3276
3277
3278#ifdef HAVE_SETPRIORITY
3279PyDoc_STRVAR(posix_setpriority__doc__,
3280"setpriority(which, who, prio) -> None\n\n\
3281Set program scheduling priority.");
3282
3283static PyObject *
3284posix_setpriority(PyObject *self, PyObject *args)
3285{
3286 int which, who, prio, retval;
3287
3288 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3289 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003290 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003291 if (retval == -1)
3292 return posix_error();
3293 Py_RETURN_NONE;
3294}
3295#endif /* HAVE_SETPRIORITY */
3296
3297
Barry Warsaw53699e91996-12-10 23:23:01 +00003298static PyObject *
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003299internal_rename(PyObject *self, PyObject *args, int is_replace)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003300{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003301#ifdef MS_WINDOWS
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003302 PyObject *src, *dst;
Victor Stinner8c62be82010-05-06 00:08:46 +00003303 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003304 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
3305 if (PyArg_ParseTuple(args,
3306 is_replace ? "UU:replace" : "UU:rename",
3307 &src, &dst))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003308 {
3309 wchar_t *wsrc, *wdst;
3310
3311 wsrc = PyUnicode_AsUnicode(src);
3312 if (wsrc == NULL)
3313 return NULL;
3314 wdst = PyUnicode_AsUnicode(dst);
3315 if (wdst == NULL)
3316 return NULL;
3317 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003318 result = MoveFileExW(wsrc, wdst, flags);
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003319 Py_END_ALLOW_THREADS
3320 if (!result)
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003321 return win32_error(is_replace ? "replace" : "rename", NULL);
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003322 Py_INCREF(Py_None);
3323 return Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00003324 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003325 else {
3326 PyErr_Clear();
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003327 if (!PyArg_ParseTuple(args,
3328 is_replace ? "O&O&:replace" : "O&O&:rename",
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003329 PyUnicode_FSConverter, &src,
3330 PyUnicode_FSConverter, &dst))
3331 return NULL;
3332
3333 if (win32_warn_bytes_api())
3334 goto error;
3335
3336 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003337 result = MoveFileExA(PyBytes_AS_STRING(src),
3338 PyBytes_AS_STRING(dst), flags);
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003339 Py_END_ALLOW_THREADS
3340
3341 Py_XDECREF(src);
3342 Py_XDECREF(dst);
3343
3344 if (!result)
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003345 return win32_error(is_replace ? "replace" : "rename", NULL);
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003346 Py_INCREF(Py_None);
3347 return Py_None;
3348
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003349error:
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003350 Py_XDECREF(src);
3351 Py_XDECREF(dst);
Victor Stinner8c62be82010-05-06 00:08:46 +00003352 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003353 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003354#else
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003355 return posix_2str(args,
3356 is_replace ? "O&O&:replace" : "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003357#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003358}
3359
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003360PyDoc_STRVAR(posix_rename__doc__,
3361"rename(old, new)\n\n\
3362Rename a file or directory.");
3363
3364static PyObject *
3365posix_rename(PyObject *self, PyObject *args)
3366{
3367 return internal_rename(self, args, 0);
3368}
3369
3370PyDoc_STRVAR(posix_replace__doc__,
3371"replace(old, new)\n\n\
3372Rename a file or directory, overwriting the destination.");
3373
3374static PyObject *
3375posix_replace(PyObject *self, PyObject *args)
3376{
3377 return internal_rename(self, args, 1);
3378}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003379
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003380PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003381"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003382Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003383
Barry Warsaw53699e91996-12-10 23:23:01 +00003384static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003385posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003386{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003387#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003388 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003389#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003390 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003391#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003392}
3393
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003394
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003395PyDoc_STRVAR(posix_stat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01003396"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003397Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003398
Barry Warsaw53699e91996-12-10 23:23:01 +00003399static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01003400posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003401{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003402#ifdef MS_WINDOWS
Victor Stinner4195b5c2012-02-08 23:03:19 +01003403 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003404#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01003405 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003406#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003407}
3408
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003409
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003410#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003411PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003412"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003413Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003414
Barry Warsaw53699e91996-12-10 23:23:01 +00003415static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003416posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003417{
Victor Stinner8c62be82010-05-06 00:08:46 +00003418 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003419#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003420 wchar_t *command;
3421 if (!PyArg_ParseTuple(args, "u:system", &command))
3422 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003423
Victor Stinner8c62be82010-05-06 00:08:46 +00003424 Py_BEGIN_ALLOW_THREADS
3425 sts = _wsystem(command);
3426 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003427#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003428 PyObject *command_obj;
3429 char *command;
3430 if (!PyArg_ParseTuple(args, "O&:system",
3431 PyUnicode_FSConverter, &command_obj))
3432 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003433
Victor Stinner8c62be82010-05-06 00:08:46 +00003434 command = PyBytes_AsString(command_obj);
3435 Py_BEGIN_ALLOW_THREADS
3436 sts = system(command);
3437 Py_END_ALLOW_THREADS
3438 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003439#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003440 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003441}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003442#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003443
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003444
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003445PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003446"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003447Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003448
Barry Warsaw53699e91996-12-10 23:23:01 +00003449static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003450posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003451{
Victor Stinner8c62be82010-05-06 00:08:46 +00003452 int i;
3453 if (!PyArg_ParseTuple(args, "i:umask", &i))
3454 return NULL;
3455 i = (int)umask(i);
3456 if (i < 0)
3457 return posix_error();
3458 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003459}
3460
Brian Curtind40e6f72010-07-08 21:39:08 +00003461#ifdef MS_WINDOWS
3462
3463/* override the default DeleteFileW behavior so that directory
3464symlinks can be removed with this function, the same as with
3465Unix symlinks */
3466BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3467{
3468 WIN32_FILE_ATTRIBUTE_DATA info;
3469 WIN32_FIND_DATAW find_data;
3470 HANDLE find_data_handle;
3471 int is_directory = 0;
3472 int is_link = 0;
3473
3474 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3475 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003476
Brian Curtind40e6f72010-07-08 21:39:08 +00003477 /* Get WIN32_FIND_DATA structure for the path to determine if
3478 it is a symlink */
3479 if(is_directory &&
3480 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3481 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3482
3483 if(find_data_handle != INVALID_HANDLE_VALUE) {
3484 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3485 FindClose(find_data_handle);
3486 }
3487 }
3488 }
3489
3490 if (is_directory && is_link)
3491 return RemoveDirectoryW(lpFileName);
3492
3493 return DeleteFileW(lpFileName);
3494}
3495#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003496
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003497PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003498"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003499Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003500
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003501PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003502"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003503Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003504
Barry Warsaw53699e91996-12-10 23:23:01 +00003505static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003506posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003507{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003508#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003509 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3510 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003511#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003512 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003513#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003514}
3515
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003516
Guido van Rossumb6775db1994-08-01 11:34:53 +00003517#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003518PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003519"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003520Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003521
Barry Warsaw53699e91996-12-10 23:23:01 +00003522static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003523posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003524{
Victor Stinner8c62be82010-05-06 00:08:46 +00003525 struct utsname u;
3526 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003527
Victor Stinner8c62be82010-05-06 00:08:46 +00003528 Py_BEGIN_ALLOW_THREADS
3529 res = uname(&u);
3530 Py_END_ALLOW_THREADS
3531 if (res < 0)
3532 return posix_error();
3533 return Py_BuildValue("(sssss)",
3534 u.sysname,
3535 u.nodename,
3536 u.release,
3537 u.version,
3538 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003539}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003540#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003541
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003542
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003543static int
Victor Stinnera2f7c002012-02-08 03:36:25 +01003544extract_time(PyObject *t, time_t* sec, long* nsec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003545{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003546 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003547 if (PyFloat_Check(t)) {
Victor Stinnera2f7c002012-02-08 03:36:25 +01003548 double d = PyFloat_AsDouble(t);
3549 double mod;
3550 *sec = (time_t)d;
3551 mod = fmod(d, 1.0);
3552 mod *= 1e9;
3553 *nsec = (long)mod;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003554 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003555 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003556#if SIZEOF_TIME_T > SIZEOF_LONG
3557 intval = PyLong_AsUnsignedLongLongMask(t);
3558#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003559 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003560#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003561 if (intval == -1 && PyErr_Occurred())
3562 return -1;
3563 *sec = intval;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003564 *nsec = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003565 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003566}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003567
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003568PyDoc_STRVAR(posix_utime__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003569"utime(path[, (atime, mtime)])\n\
3570Set the access and modified time of the file to the given values.\n\
3571If no second argument is used, set the access and modified times to\n\
3572the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003573
Barry Warsaw53699e91996-12-10 23:23:01 +00003574static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003575posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003576{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003577#ifdef MS_WINDOWS
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003578 PyObject *arg = Py_None;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003579 PyObject *obwpath;
Victor Stinner8c62be82010-05-06 00:08:46 +00003580 wchar_t *wpath = NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003581 const char *apath;
Victor Stinner8c62be82010-05-06 00:08:46 +00003582 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003583 time_t atimesec, mtimesec;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003584 long ansec, mnsec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003585 FILETIME atime, mtime;
3586 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003587
Brian Curtin52fbea12011-11-06 13:41:17 -06003588 if (PyArg_ParseTuple(args, "U|O:utime", &obwpath, &arg)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003589 wpath = PyUnicode_AsUnicode(obwpath);
3590 if (wpath == NULL)
3591 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003592 Py_BEGIN_ALLOW_THREADS
3593 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3594 NULL, OPEN_EXISTING,
3595 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3596 Py_END_ALLOW_THREADS
3597 if (hFile == INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003598 return win32_error_object("utime", obwpath);
3599 }
3600 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00003601 /* Drop the argument parsing error as narrow strings
3602 are also valid. */
3603 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003604
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003605 if (!PyArg_ParseTuple(args, "y|O:utime", &apath, &arg))
3606 return NULL;
3607 if (win32_warn_bytes_api())
Victor Stinner8c62be82010-05-06 00:08:46 +00003608 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003609
Victor Stinner8c62be82010-05-06 00:08:46 +00003610 Py_BEGIN_ALLOW_THREADS
3611 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3612 NULL, OPEN_EXISTING,
3613 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3614 Py_END_ALLOW_THREADS
3615 if (hFile == INVALID_HANDLE_VALUE) {
3616 win32_error("utime", apath);
Victor Stinner8c62be82010-05-06 00:08:46 +00003617 return NULL;
3618 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003619 }
3620
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003621 if (arg == Py_None) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003622 SYSTEMTIME now;
3623 GetSystemTime(&now);
3624 if (!SystemTimeToFileTime(&now, &mtime) ||
3625 !SystemTimeToFileTime(&now, &atime)) {
3626 win32_error("utime", NULL);
3627 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003628 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003629 }
3630 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3631 PyErr_SetString(PyExc_TypeError,
3632 "utime() arg 2 must be a tuple (atime, mtime)");
3633 goto done;
3634 }
3635 else {
3636 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Victor Stinnera2f7c002012-02-08 03:36:25 +01003637 &atimesec, &ansec) == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00003638 goto done;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003639 time_t_to_FILE_TIME(atimesec, ansec, &atime);
Victor Stinner8c62be82010-05-06 00:08:46 +00003640 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Victor Stinnera2f7c002012-02-08 03:36:25 +01003641 &mtimesec, &mnsec) == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00003642 goto done;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003643 time_t_to_FILE_TIME(mtimesec, mnsec, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00003644 }
3645 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3646 /* Avoid putting the file name into the error here,
3647 as that may confuse the user into believing that
3648 something is wrong with the file, when it also
3649 could be the time stamp that gives a problem. */
3650 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003651 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003652 }
3653 Py_INCREF(Py_None);
3654 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003655done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003656 CloseHandle(hFile);
3657 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003658#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003659
Victor Stinner8c62be82010-05-06 00:08:46 +00003660 PyObject *opath;
3661 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003662 time_t atime, mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003663 long ansec, mnsec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003664 int res;
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003665 PyObject* arg = Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003666
Brian Curtin52fbea12011-11-06 13:41:17 -06003667 if (!PyArg_ParseTuple(args, "O&|O:utime",
Victor Stinner8c62be82010-05-06 00:08:46 +00003668 PyUnicode_FSConverter, &opath, &arg))
3669 return NULL;
3670 path = PyBytes_AsString(opath);
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003671 if (arg == Py_None) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003672 /* optional time values not given */
3673 Py_BEGIN_ALLOW_THREADS
3674 res = utime(path, NULL);
3675 Py_END_ALLOW_THREADS
3676 }
3677 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3678 PyErr_SetString(PyExc_TypeError,
3679 "utime() arg 2 must be a tuple (atime, mtime)");
3680 Py_DECREF(opath);
3681 return NULL;
3682 }
3683 else {
3684 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Victor Stinnera2f7c002012-02-08 03:36:25 +01003685 &atime, &ansec) == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003686 Py_DECREF(opath);
3687 return NULL;
3688 }
3689 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Victor Stinnera2f7c002012-02-08 03:36:25 +01003690 &mtime, &mnsec) == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003691 Py_DECREF(opath);
3692 return NULL;
3693 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003694
3695 Py_BEGIN_ALLOW_THREADS
3696 {
3697#ifdef HAVE_UTIMENSAT
3698 struct timespec buf[2];
3699 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003700 buf[0].tv_nsec = ansec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003701 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003702 buf[1].tv_nsec = mnsec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003703 res = utimensat(AT_FDCWD, path, buf, 0);
3704#elif defined(HAVE_UTIMES)
3705 struct timeval buf[2];
3706 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003707 buf[0].tv_usec = ansec / 1000;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003708 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003709 buf[1].tv_usec = mnsec / 1000;
Victor Stinner8c62be82010-05-06 00:08:46 +00003710 res = utimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003711#elif defined(HAVE_UTIME_H)
3712 /* XXX should define struct utimbuf instead, above */
3713 struct utimbuf buf;
3714 buf.actime = atime;
3715 buf.modtime = mtime;
3716 res = utime(path, &buf);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003717#else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003718 time_t buf[2];
3719 buf[0] = atime;
3720 buf[1] = mtime;
3721 res = utime(path, buf);
3722#endif
3723 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003724 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003725 }
3726 if (res < 0) {
3727 return posix_error_with_allocated_filename(opath);
3728 }
3729 Py_DECREF(opath);
3730 Py_INCREF(Py_None);
3731 return Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003732#undef UTIME_EXTRACT
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003733#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003734}
3735
Ross Lagerwall7807c352011-03-17 20:20:30 +02003736#ifdef HAVE_FUTIMES
3737PyDoc_STRVAR(posix_futimes__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003738"futimes(fd[, (atime, mtime)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003739Set the access and modified time of the file specified by the file\n\
Brian Curtinc1b65d12011-11-07 14:18:54 -06003740descriptor fd to the given values. If no second argument is used, set the\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003741access and modified times to the current time.");
3742
3743static PyObject *
3744posix_futimes(PyObject *self, PyObject *args)
3745{
3746 int res, fd;
Brian Curtinc1b65d12011-11-07 14:18:54 -06003747 PyObject* arg = Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003748 time_t atime, mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003749 long ansec, mnsec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003750
Brian Curtinc1b65d12011-11-07 14:18:54 -06003751 if (!PyArg_ParseTuple(args, "i|O:futimes", &fd, &arg))
Ross Lagerwall7807c352011-03-17 20:20:30 +02003752 return NULL;
3753
3754 if (arg == Py_None) {
3755 /* optional time values not given */
3756 Py_BEGIN_ALLOW_THREADS
3757 res = futimes(fd, NULL);
3758 Py_END_ALLOW_THREADS
3759 }
3760 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3761 PyErr_SetString(PyExc_TypeError,
3762 "futimes() arg 2 must be a tuple (atime, mtime)");
3763 return NULL;
3764 }
3765 else {
3766 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Victor Stinnera2f7c002012-02-08 03:36:25 +01003767 &atime, &ansec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003768 return NULL;
3769 }
3770 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Victor Stinnera2f7c002012-02-08 03:36:25 +01003771 &mtime, &mnsec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003772 return NULL;
3773 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003774 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003775 {
3776#ifdef HAVE_FUTIMENS
3777 struct timespec buf[2];
3778 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003779 buf[0].tv_nsec = ansec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003780 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003781 buf[1].tv_nsec = mnsec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003782 res = futimens(fd, buf);
3783#else
3784 struct timeval buf[2];
3785 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003786 buf[0].tv_usec = ansec / 1000;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003787 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003788 buf[1].tv_usec = mnsec / 1000;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003789 res = futimes(fd, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003790#endif
3791 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003792 Py_END_ALLOW_THREADS
3793 }
3794 if (res < 0)
3795 return posix_error();
3796 Py_RETURN_NONE;
3797}
3798#endif
3799
3800#ifdef HAVE_LUTIMES
3801PyDoc_STRVAR(posix_lutimes__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003802"lutimes(path[, (atime, mtime)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003803Like utime(), but if path is a symbolic link, it is not dereferenced.");
3804
3805static PyObject *
3806posix_lutimes(PyObject *self, PyObject *args)
3807{
Brian Curtinc1b65d12011-11-07 14:18:54 -06003808 PyObject *opath;
3809 PyObject *arg = Py_None;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003810 const char *path;
3811 int res;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003812 time_t atime, mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003813 long ansec, mnsec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003814
Brian Curtinc1b65d12011-11-07 14:18:54 -06003815 if (!PyArg_ParseTuple(args, "O&|O:lutimes",
Ross Lagerwall7807c352011-03-17 20:20:30 +02003816 PyUnicode_FSConverter, &opath, &arg))
3817 return NULL;
3818 path = PyBytes_AsString(opath);
3819 if (arg == Py_None) {
3820 /* optional time values not given */
3821 Py_BEGIN_ALLOW_THREADS
3822 res = lutimes(path, NULL);
3823 Py_END_ALLOW_THREADS
3824 }
3825 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3826 PyErr_SetString(PyExc_TypeError,
3827 "lutimes() arg 2 must be a tuple (atime, mtime)");
3828 Py_DECREF(opath);
3829 return NULL;
3830 }
3831 else {
3832 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Victor Stinnera2f7c002012-02-08 03:36:25 +01003833 &atime, &ansec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003834 Py_DECREF(opath);
3835 return NULL;
3836 }
3837 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Victor Stinnera2f7c002012-02-08 03:36:25 +01003838 &mtime, &mnsec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003839 Py_DECREF(opath);
3840 return NULL;
3841 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003842 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003843 {
3844#ifdef HAVE_UTIMENSAT
3845 struct timespec buf[2];
3846 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003847 buf[0].tv_nsec = ansec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003848 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003849 buf[1].tv_nsec = mnsec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003850 res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW);
3851#else
3852 struct timeval buf[2];
3853 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003854 buf[0].tv_usec = ansec / 1000;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003855 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003856 buf[1].tv_usec = mnsec / 1000;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003857 res = lutimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003858#endif
3859 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003860 Py_END_ALLOW_THREADS
3861 }
3862 Py_DECREF(opath);
3863 if (res < 0)
3864 return posix_error();
3865 Py_RETURN_NONE;
3866}
3867#endif
3868
3869#ifdef HAVE_FUTIMENS
3870PyDoc_STRVAR(posix_futimens__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003871"futimens(fd[, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003872Updates the timestamps of a file specified by the file descriptor fd, with\n\
3873nanosecond precision.\n\
Brian Curtinc1b65d12011-11-07 14:18:54 -06003874If no second argument is given, set atime and mtime to the current time.\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003875If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3876current time.\n\
3877If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3878
3879static PyObject *
3880posix_futimens(PyObject *self, PyObject *args)
3881{
3882 int res, fd;
Brian Curtinc1b65d12011-11-07 14:18:54 -06003883 PyObject *atime = Py_None;
3884 PyObject *mtime = Py_None;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003885 struct timespec buf[2];
3886
Brian Curtinc1b65d12011-11-07 14:18:54 -06003887 if (!PyArg_ParseTuple(args, "i|OO:futimens",
Ross Lagerwall7807c352011-03-17 20:20:30 +02003888 &fd, &atime, &mtime))
3889 return NULL;
3890 if (atime == Py_None && mtime == Py_None) {
3891 /* optional time values not given */
3892 Py_BEGIN_ALLOW_THREADS
3893 res = futimens(fd, NULL);
3894 Py_END_ALLOW_THREADS
3895 }
3896 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3897 PyErr_SetString(PyExc_TypeError,
3898 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3899 return NULL;
3900 }
3901 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3902 PyErr_SetString(PyExc_TypeError,
3903 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3904 return NULL;
3905 }
3906 else {
3907 if (!PyArg_ParseTuple(atime, "ll:futimens",
3908 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3909 return NULL;
3910 }
3911 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3912 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3913 return NULL;
3914 }
3915 Py_BEGIN_ALLOW_THREADS
3916 res = futimens(fd, buf);
3917 Py_END_ALLOW_THREADS
3918 }
3919 if (res < 0)
3920 return posix_error();
3921 Py_RETURN_NONE;
3922}
3923#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003924
Guido van Rossum3b066191991-06-04 19:40:25 +00003925/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003926
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003927PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003928"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003929Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003930
Barry Warsaw53699e91996-12-10 23:23:01 +00003931static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003932posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003933{
Victor Stinner8c62be82010-05-06 00:08:46 +00003934 int sts;
3935 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3936 return NULL;
3937 _exit(sts);
3938 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003939}
3940
Martin v. Löwis114619e2002-10-07 06:44:21 +00003941#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3942static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003943free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003944{
Victor Stinner8c62be82010-05-06 00:08:46 +00003945 Py_ssize_t i;
3946 for (i = 0; i < count; i++)
3947 PyMem_Free(array[i]);
3948 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003949}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003950
Antoine Pitrou69f71142009-05-24 21:25:49 +00003951static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003952int fsconvert_strdup(PyObject *o, char**out)
3953{
Victor Stinner8c62be82010-05-06 00:08:46 +00003954 PyObject *bytes;
3955 Py_ssize_t size;
3956 if (!PyUnicode_FSConverter(o, &bytes))
3957 return 0;
3958 size = PyBytes_GET_SIZE(bytes);
3959 *out = PyMem_Malloc(size+1);
3960 if (!*out)
3961 return 0;
3962 memcpy(*out, PyBytes_AsString(bytes), size+1);
3963 Py_DECREF(bytes);
3964 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003965}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003966#endif
3967
Ross Lagerwall7807c352011-03-17 20:20:30 +02003968#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003969static char**
3970parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3971{
Victor Stinner8c62be82010-05-06 00:08:46 +00003972 char **envlist;
3973 Py_ssize_t i, pos, envc;
3974 PyObject *keys=NULL, *vals=NULL;
3975 PyObject *key, *val, *key2, *val2;
3976 char *p, *k, *v;
3977 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003978
Victor Stinner8c62be82010-05-06 00:08:46 +00003979 i = PyMapping_Size(env);
3980 if (i < 0)
3981 return NULL;
3982 envlist = PyMem_NEW(char *, i + 1);
3983 if (envlist == NULL) {
3984 PyErr_NoMemory();
3985 return NULL;
3986 }
3987 envc = 0;
3988 keys = PyMapping_Keys(env);
3989 vals = PyMapping_Values(env);
3990 if (!keys || !vals)
3991 goto error;
3992 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3993 PyErr_Format(PyExc_TypeError,
3994 "env.keys() or env.values() is not a list");
3995 goto error;
3996 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003997
Victor Stinner8c62be82010-05-06 00:08:46 +00003998 for (pos = 0; pos < i; pos++) {
3999 key = PyList_GetItem(keys, pos);
4000 val = PyList_GetItem(vals, pos);
4001 if (!key || !val)
4002 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004003
Victor Stinner8c62be82010-05-06 00:08:46 +00004004 if (PyUnicode_FSConverter(key, &key2) == 0)
4005 goto error;
4006 if (PyUnicode_FSConverter(val, &val2) == 0) {
4007 Py_DECREF(key2);
4008 goto error;
4009 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004010
4011#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00004012 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
4013 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00004014#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004015 k = PyBytes_AsString(key2);
4016 v = PyBytes_AsString(val2);
4017 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004018
Victor Stinner8c62be82010-05-06 00:08:46 +00004019 p = PyMem_NEW(char, len);
4020 if (p == NULL) {
4021 PyErr_NoMemory();
4022 Py_DECREF(key2);
4023 Py_DECREF(val2);
4024 goto error;
4025 }
4026 PyOS_snprintf(p, len, "%s=%s", k, v);
4027 envlist[envc++] = p;
4028 Py_DECREF(key2);
4029 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004030#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00004031 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004032#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004033 }
4034 Py_DECREF(vals);
4035 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004036
Victor Stinner8c62be82010-05-06 00:08:46 +00004037 envlist[envc] = 0;
4038 *envc_ptr = envc;
4039 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004040
4041error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004042 Py_XDECREF(keys);
4043 Py_XDECREF(vals);
4044 while (--envc >= 0)
4045 PyMem_DEL(envlist[envc]);
4046 PyMem_DEL(envlist);
4047 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004048}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004049
Ross Lagerwall7807c352011-03-17 20:20:30 +02004050static char**
4051parse_arglist(PyObject* argv, Py_ssize_t *argc)
4052{
4053 int i;
4054 char **argvlist = PyMem_NEW(char *, *argc+1);
4055 if (argvlist == NULL) {
4056 PyErr_NoMemory();
4057 return NULL;
4058 }
4059 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004060 PyObject* item = PySequence_ITEM(argv, i);
4061 if (item == NULL)
4062 goto fail;
4063 if (!fsconvert_strdup(item, &argvlist[i])) {
4064 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004065 goto fail;
4066 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004067 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004068 }
4069 argvlist[*argc] = NULL;
4070 return argvlist;
4071fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004072 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004073 free_string_array(argvlist, *argc);
4074 return NULL;
4075}
4076#endif
4077
4078#ifdef HAVE_EXECV
4079PyDoc_STRVAR(posix_execv__doc__,
4080"execv(path, args)\n\n\
4081Execute an executable path with arguments, replacing current process.\n\
4082\n\
4083 path: path of executable file\n\
4084 args: tuple or list of strings");
4085
4086static PyObject *
4087posix_execv(PyObject *self, PyObject *args)
4088{
4089 PyObject *opath;
4090 char *path;
4091 PyObject *argv;
4092 char **argvlist;
4093 Py_ssize_t argc;
4094
4095 /* execv has two arguments: (path, argv), where
4096 argv is a list or tuple of strings. */
4097
4098 if (!PyArg_ParseTuple(args, "O&O:execv",
4099 PyUnicode_FSConverter,
4100 &opath, &argv))
4101 return NULL;
4102 path = PyBytes_AsString(opath);
4103 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4104 PyErr_SetString(PyExc_TypeError,
4105 "execv() arg 2 must be a tuple or list");
4106 Py_DECREF(opath);
4107 return NULL;
4108 }
4109 argc = PySequence_Size(argv);
4110 if (argc < 1) {
4111 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
4112 Py_DECREF(opath);
4113 return NULL;
4114 }
4115
4116 argvlist = parse_arglist(argv, &argc);
4117 if (argvlist == NULL) {
4118 Py_DECREF(opath);
4119 return NULL;
4120 }
4121
4122 execv(path, argvlist);
4123
4124 /* If we get here it's definitely an error */
4125
4126 free_string_array(argvlist, argc);
4127 Py_DECREF(opath);
4128 return posix_error();
4129}
4130
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004131PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004132"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004133Execute a path with arguments and environment, replacing current process.\n\
4134\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004135 path: path of executable file\n\
4136 args: tuple or list of arguments\n\
4137 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004138
Barry Warsaw53699e91996-12-10 23:23:01 +00004139static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004140posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004141{
Victor Stinner8c62be82010-05-06 00:08:46 +00004142 PyObject *opath;
4143 char *path;
4144 PyObject *argv, *env;
4145 char **argvlist;
4146 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004147 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004148
Victor Stinner8c62be82010-05-06 00:08:46 +00004149 /* execve has three arguments: (path, argv, env), where
4150 argv is a list or tuple of strings and env is a dictionary
4151 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004152
Victor Stinner8c62be82010-05-06 00:08:46 +00004153 if (!PyArg_ParseTuple(args, "O&OO:execve",
4154 PyUnicode_FSConverter,
4155 &opath, &argv, &env))
4156 return NULL;
4157 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004158 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004159 PyErr_SetString(PyExc_TypeError,
4160 "execve() arg 2 must be a tuple or list");
4161 goto fail_0;
4162 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004163 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004164 if (!PyMapping_Check(env)) {
4165 PyErr_SetString(PyExc_TypeError,
4166 "execve() arg 3 must be a mapping object");
4167 goto fail_0;
4168 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004169
Ross Lagerwall7807c352011-03-17 20:20:30 +02004170 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004171 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004172 goto fail_0;
4173 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004174
Victor Stinner8c62be82010-05-06 00:08:46 +00004175 envlist = parse_envlist(env, &envc);
4176 if (envlist == NULL)
4177 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004178
Victor Stinner8c62be82010-05-06 00:08:46 +00004179 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004180
Victor Stinner8c62be82010-05-06 00:08:46 +00004181 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004182
Victor Stinner8c62be82010-05-06 00:08:46 +00004183 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004184
Victor Stinner8c62be82010-05-06 00:08:46 +00004185 while (--envc >= 0)
4186 PyMem_DEL(envlist[envc]);
4187 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004188 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004189 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004190 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004191 Py_DECREF(opath);
4192 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004193}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004194#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004195
Ross Lagerwall7807c352011-03-17 20:20:30 +02004196#ifdef HAVE_FEXECVE
4197PyDoc_STRVAR(posix_fexecve__doc__,
4198"fexecve(fd, args, env)\n\n\
4199Execute the program specified by a file descriptor with arguments and\n\
4200environment, replacing the current process.\n\
4201\n\
4202 fd: file descriptor of executable\n\
4203 args: tuple or list of arguments\n\
4204 env: dictionary of strings mapping to strings");
4205
4206static PyObject *
4207posix_fexecve(PyObject *self, PyObject *args)
4208{
4209 int fd;
4210 PyObject *argv, *env;
4211 char **argvlist;
4212 char **envlist;
4213 Py_ssize_t argc, envc;
4214
4215 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4216 &fd, &argv, &env))
4217 return NULL;
4218 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4219 PyErr_SetString(PyExc_TypeError,
4220 "fexecve() arg 2 must be a tuple or list");
4221 return NULL;
4222 }
4223 argc = PySequence_Size(argv);
4224 if (!PyMapping_Check(env)) {
4225 PyErr_SetString(PyExc_TypeError,
4226 "fexecve() arg 3 must be a mapping object");
4227 return NULL;
4228 }
4229
4230 argvlist = parse_arglist(argv, &argc);
4231 if (argvlist == NULL)
4232 return NULL;
4233
4234 envlist = parse_envlist(env, &envc);
4235 if (envlist == NULL)
4236 goto fail;
4237
4238 fexecve(fd, argvlist, envlist);
4239
4240 /* If we get here it's definitely an error */
4241
4242 (void) posix_error();
4243
4244 while (--envc >= 0)
4245 PyMem_DEL(envlist[envc]);
4246 PyMem_DEL(envlist);
4247 fail:
4248 free_string_array(argvlist, argc);
4249 return NULL;
4250}
4251#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004252
Guido van Rossuma1065681999-01-25 23:20:23 +00004253#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004254PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004255"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004256Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004257\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004258 mode: mode of process creation\n\
4259 path: path of executable file\n\
4260 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004261
4262static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004263posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004264{
Victor Stinner8c62be82010-05-06 00:08:46 +00004265 PyObject *opath;
4266 char *path;
4267 PyObject *argv;
4268 char **argvlist;
4269 int mode, i;
4270 Py_ssize_t argc;
4271 Py_intptr_t spawnval;
4272 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004273
Victor Stinner8c62be82010-05-06 00:08:46 +00004274 /* spawnv has three arguments: (mode, path, argv), where
4275 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004276
Victor Stinner8c62be82010-05-06 00:08:46 +00004277 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4278 PyUnicode_FSConverter,
4279 &opath, &argv))
4280 return NULL;
4281 path = PyBytes_AsString(opath);
4282 if (PyList_Check(argv)) {
4283 argc = PyList_Size(argv);
4284 getitem = PyList_GetItem;
4285 }
4286 else if (PyTuple_Check(argv)) {
4287 argc = PyTuple_Size(argv);
4288 getitem = PyTuple_GetItem;
4289 }
4290 else {
4291 PyErr_SetString(PyExc_TypeError,
4292 "spawnv() arg 2 must be a tuple or list");
4293 Py_DECREF(opath);
4294 return NULL;
4295 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004296
Victor Stinner8c62be82010-05-06 00:08:46 +00004297 argvlist = PyMem_NEW(char *, argc+1);
4298 if (argvlist == NULL) {
4299 Py_DECREF(opath);
4300 return PyErr_NoMemory();
4301 }
4302 for (i = 0; i < argc; i++) {
4303 if (!fsconvert_strdup((*getitem)(argv, i),
4304 &argvlist[i])) {
4305 free_string_array(argvlist, i);
4306 PyErr_SetString(
4307 PyExc_TypeError,
4308 "spawnv() arg 2 must contain only strings");
4309 Py_DECREF(opath);
4310 return NULL;
4311 }
4312 }
4313 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004314
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004315#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004316 Py_BEGIN_ALLOW_THREADS
4317 spawnval = spawnv(mode, path, argvlist);
4318 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004319#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004320 if (mode == _OLD_P_OVERLAY)
4321 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004322
Victor Stinner8c62be82010-05-06 00:08:46 +00004323 Py_BEGIN_ALLOW_THREADS
4324 spawnval = _spawnv(mode, path, argvlist);
4325 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004326#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004327
Victor Stinner8c62be82010-05-06 00:08:46 +00004328 free_string_array(argvlist, argc);
4329 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004330
Victor Stinner8c62be82010-05-06 00:08:46 +00004331 if (spawnval == -1)
4332 return posix_error();
4333 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004334#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004335 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004336#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004337 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004338#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004339}
4340
4341
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004342PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004343"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004344Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004345\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004346 mode: mode of process creation\n\
4347 path: path of executable file\n\
4348 args: tuple or list of arguments\n\
4349 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004350
4351static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004352posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004353{
Victor Stinner8c62be82010-05-06 00:08:46 +00004354 PyObject *opath;
4355 char *path;
4356 PyObject *argv, *env;
4357 char **argvlist;
4358 char **envlist;
4359 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004360 int mode;
4361 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004362 Py_intptr_t spawnval;
4363 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4364 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004365
Victor Stinner8c62be82010-05-06 00:08:46 +00004366 /* spawnve has four arguments: (mode, path, argv, env), where
4367 argv is a list or tuple of strings and env is a dictionary
4368 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004369
Victor Stinner8c62be82010-05-06 00:08:46 +00004370 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4371 PyUnicode_FSConverter,
4372 &opath, &argv, &env))
4373 return NULL;
4374 path = PyBytes_AsString(opath);
4375 if (PyList_Check(argv)) {
4376 argc = PyList_Size(argv);
4377 getitem = PyList_GetItem;
4378 }
4379 else if (PyTuple_Check(argv)) {
4380 argc = PyTuple_Size(argv);
4381 getitem = PyTuple_GetItem;
4382 }
4383 else {
4384 PyErr_SetString(PyExc_TypeError,
4385 "spawnve() arg 2 must be a tuple or list");
4386 goto fail_0;
4387 }
4388 if (!PyMapping_Check(env)) {
4389 PyErr_SetString(PyExc_TypeError,
4390 "spawnve() arg 3 must be a mapping object");
4391 goto fail_0;
4392 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004393
Victor Stinner8c62be82010-05-06 00:08:46 +00004394 argvlist = PyMem_NEW(char *, argc+1);
4395 if (argvlist == NULL) {
4396 PyErr_NoMemory();
4397 goto fail_0;
4398 }
4399 for (i = 0; i < argc; i++) {
4400 if (!fsconvert_strdup((*getitem)(argv, i),
4401 &argvlist[i]))
4402 {
4403 lastarg = i;
4404 goto fail_1;
4405 }
4406 }
4407 lastarg = argc;
4408 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004409
Victor Stinner8c62be82010-05-06 00:08:46 +00004410 envlist = parse_envlist(env, &envc);
4411 if (envlist == NULL)
4412 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004413
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004414#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004415 Py_BEGIN_ALLOW_THREADS
4416 spawnval = spawnve(mode, path, argvlist, envlist);
4417 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004418#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004419 if (mode == _OLD_P_OVERLAY)
4420 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004421
Victor Stinner8c62be82010-05-06 00:08:46 +00004422 Py_BEGIN_ALLOW_THREADS
4423 spawnval = _spawnve(mode, path, argvlist, envlist);
4424 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004425#endif
Tim Peters25059d32001-12-07 20:35:43 +00004426
Victor Stinner8c62be82010-05-06 00:08:46 +00004427 if (spawnval == -1)
4428 (void) posix_error();
4429 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004430#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004431 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004432#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004433 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004434#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004435
Victor Stinner8c62be82010-05-06 00:08:46 +00004436 while (--envc >= 0)
4437 PyMem_DEL(envlist[envc]);
4438 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004439 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004440 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004441 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004442 Py_DECREF(opath);
4443 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004444}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004445
4446/* OS/2 supports spawnvp & spawnvpe natively */
4447#if defined(PYOS_OS2)
4448PyDoc_STRVAR(posix_spawnvp__doc__,
4449"spawnvp(mode, file, args)\n\n\
4450Execute the program 'file' in a new process, using the environment\n\
4451search path to find the file.\n\
4452\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004453 mode: mode of process creation\n\
4454 file: executable file name\n\
4455 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004456
4457static PyObject *
4458posix_spawnvp(PyObject *self, PyObject *args)
4459{
Victor Stinner8c62be82010-05-06 00:08:46 +00004460 PyObject *opath;
4461 char *path;
4462 PyObject *argv;
4463 char **argvlist;
4464 int mode, i, argc;
4465 Py_intptr_t spawnval;
4466 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004467
Victor Stinner8c62be82010-05-06 00:08:46 +00004468 /* spawnvp has three arguments: (mode, path, argv), where
4469 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004470
Victor Stinner8c62be82010-05-06 00:08:46 +00004471 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4472 PyUnicode_FSConverter,
4473 &opath, &argv))
4474 return NULL;
4475 path = PyBytes_AsString(opath);
4476 if (PyList_Check(argv)) {
4477 argc = PyList_Size(argv);
4478 getitem = PyList_GetItem;
4479 }
4480 else if (PyTuple_Check(argv)) {
4481 argc = PyTuple_Size(argv);
4482 getitem = PyTuple_GetItem;
4483 }
4484 else {
4485 PyErr_SetString(PyExc_TypeError,
4486 "spawnvp() arg 2 must be a tuple or list");
4487 Py_DECREF(opath);
4488 return NULL;
4489 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004490
Victor Stinner8c62be82010-05-06 00:08:46 +00004491 argvlist = PyMem_NEW(char *, argc+1);
4492 if (argvlist == NULL) {
4493 Py_DECREF(opath);
4494 return PyErr_NoMemory();
4495 }
4496 for (i = 0; i < argc; i++) {
4497 if (!fsconvert_strdup((*getitem)(argv, i),
4498 &argvlist[i])) {
4499 free_string_array(argvlist, i);
4500 PyErr_SetString(
4501 PyExc_TypeError,
4502 "spawnvp() arg 2 must contain only strings");
4503 Py_DECREF(opath);
4504 return NULL;
4505 }
4506 }
4507 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004508
Victor Stinner8c62be82010-05-06 00:08:46 +00004509 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004510#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004511 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004512#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004513 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004514#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004515 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004516
Victor Stinner8c62be82010-05-06 00:08:46 +00004517 free_string_array(argvlist, argc);
4518 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004519
Victor Stinner8c62be82010-05-06 00:08:46 +00004520 if (spawnval == -1)
4521 return posix_error();
4522 else
4523 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004524}
4525
4526
4527PyDoc_STRVAR(posix_spawnvpe__doc__,
4528"spawnvpe(mode, file, args, env)\n\n\
4529Execute the program 'file' in a new process, using the environment\n\
4530search path to find the file.\n\
4531\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004532 mode: mode of process creation\n\
4533 file: executable file name\n\
4534 args: tuple or list of arguments\n\
4535 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004536
4537static PyObject *
4538posix_spawnvpe(PyObject *self, PyObject *args)
4539{
Ross Lagerwalldcfde5a2011-11-04 07:09:14 +02004540 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00004541 char *path;
4542 PyObject *argv, *env;
4543 char **argvlist;
4544 char **envlist;
4545 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004546 int mode;
4547 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004548 Py_intptr_t spawnval;
4549 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4550 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004551
Victor Stinner8c62be82010-05-06 00:08:46 +00004552 /* spawnvpe has four arguments: (mode, path, argv, env), where
4553 argv is a list or tuple of strings and env is a dictionary
4554 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004555
Victor Stinner8c62be82010-05-06 00:08:46 +00004556 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4557 PyUnicode_FSConverter,
4558 &opath, &argv, &env))
4559 return NULL;
4560 path = PyBytes_AsString(opath);
4561 if (PyList_Check(argv)) {
4562 argc = PyList_Size(argv);
4563 getitem = PyList_GetItem;
4564 }
4565 else if (PyTuple_Check(argv)) {
4566 argc = PyTuple_Size(argv);
4567 getitem = PyTuple_GetItem;
4568 }
4569 else {
4570 PyErr_SetString(PyExc_TypeError,
4571 "spawnvpe() arg 2 must be a tuple or list");
4572 goto fail_0;
4573 }
4574 if (!PyMapping_Check(env)) {
4575 PyErr_SetString(PyExc_TypeError,
4576 "spawnvpe() arg 3 must be a mapping object");
4577 goto fail_0;
4578 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004579
Victor Stinner8c62be82010-05-06 00:08:46 +00004580 argvlist = PyMem_NEW(char *, argc+1);
4581 if (argvlist == NULL) {
4582 PyErr_NoMemory();
4583 goto fail_0;
4584 }
4585 for (i = 0; i < argc; i++) {
4586 if (!fsconvert_strdup((*getitem)(argv, i),
4587 &argvlist[i]))
4588 {
4589 lastarg = i;
4590 goto fail_1;
4591 }
4592 }
4593 lastarg = argc;
4594 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004595
Victor Stinner8c62be82010-05-06 00:08:46 +00004596 envlist = parse_envlist(env, &envc);
4597 if (envlist == NULL)
4598 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004599
Victor Stinner8c62be82010-05-06 00:08:46 +00004600 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004601#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004602 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004603#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004604 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004605#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004606 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004607
Victor Stinner8c62be82010-05-06 00:08:46 +00004608 if (spawnval == -1)
4609 (void) posix_error();
4610 else
4611 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004612
Victor Stinner8c62be82010-05-06 00:08:46 +00004613 while (--envc >= 0)
4614 PyMem_DEL(envlist[envc]);
4615 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004616 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004617 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004618 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004619 Py_DECREF(opath);
4620 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004621}
4622#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004623#endif /* HAVE_SPAWNV */
4624
4625
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004626#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004627PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004628"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004629Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4630\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004631Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004632
4633static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004634posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004635{
Victor Stinner8c62be82010-05-06 00:08:46 +00004636 pid_t pid;
4637 int result = 0;
4638 _PyImport_AcquireLock();
4639 pid = fork1();
4640 if (pid == 0) {
4641 /* child: this clobbers and resets the import lock. */
4642 PyOS_AfterFork();
4643 } else {
4644 /* parent: release the import lock. */
4645 result = _PyImport_ReleaseLock();
4646 }
4647 if (pid == -1)
4648 return posix_error();
4649 if (result < 0) {
4650 /* Don't clobber the OSError if the fork failed. */
4651 PyErr_SetString(PyExc_RuntimeError,
4652 "not holding the import lock");
4653 return NULL;
4654 }
4655 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004656}
4657#endif
4658
4659
Guido van Rossumad0ee831995-03-01 10:34:45 +00004660#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004661PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004662"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004663Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004664Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004665
Barry Warsaw53699e91996-12-10 23:23:01 +00004666static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004667posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004668{
Victor Stinner8c62be82010-05-06 00:08:46 +00004669 pid_t pid;
4670 int result = 0;
4671 _PyImport_AcquireLock();
4672 pid = fork();
4673 if (pid == 0) {
4674 /* child: this clobbers and resets the import lock. */
4675 PyOS_AfterFork();
4676 } else {
4677 /* parent: release the import lock. */
4678 result = _PyImport_ReleaseLock();
4679 }
4680 if (pid == -1)
4681 return posix_error();
4682 if (result < 0) {
4683 /* Don't clobber the OSError if the fork failed. */
4684 PyErr_SetString(PyExc_RuntimeError,
4685 "not holding the import lock");
4686 return NULL;
4687 }
4688 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004689}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004690#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004691
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004692#ifdef HAVE_SCHED_H
4693
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004694#ifdef HAVE_SCHED_GET_PRIORITY_MAX
4695
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004696PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
4697"sched_get_priority_max(policy)\n\n\
4698Get the maximum scheduling priority for *policy*.");
4699
4700static PyObject *
4701posix_sched_get_priority_max(PyObject *self, PyObject *args)
4702{
4703 int policy, max;
4704
4705 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
4706 return NULL;
4707 max = sched_get_priority_max(policy);
4708 if (max < 0)
4709 return posix_error();
4710 return PyLong_FromLong(max);
4711}
4712
4713PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
4714"sched_get_priority_min(policy)\n\n\
4715Get the minimum scheduling priority for *policy*.");
4716
4717static PyObject *
4718posix_sched_get_priority_min(PyObject *self, PyObject *args)
4719{
4720 int policy, min;
4721
4722 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
4723 return NULL;
4724 min = sched_get_priority_min(policy);
4725 if (min < 0)
4726 return posix_error();
4727 return PyLong_FromLong(min);
4728}
4729
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004730#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
4731
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004732#ifdef HAVE_SCHED_SETSCHEDULER
4733
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004734PyDoc_STRVAR(posix_sched_getscheduler__doc__,
4735"sched_getscheduler(pid)\n\n\
4736Get the scheduling policy for the process with a PID of *pid*.\n\
4737Passing a PID of 0 returns the scheduling policy for the calling process.");
4738
4739static PyObject *
4740posix_sched_getscheduler(PyObject *self, PyObject *args)
4741{
4742 pid_t pid;
4743 int policy;
4744
4745 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
4746 return NULL;
4747 policy = sched_getscheduler(pid);
4748 if (policy < 0)
4749 return posix_error();
4750 return PyLong_FromLong(policy);
4751}
4752
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004753#endif
4754
4755#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
4756
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004757static PyObject *
4758sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4759{
4760 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05004761 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004762
4763 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
4764 return NULL;
4765 res = PyStructSequence_New(type);
4766 if (!res)
4767 return NULL;
4768 Py_INCREF(priority);
4769 PyStructSequence_SET_ITEM(res, 0, priority);
4770 return res;
4771}
4772
4773PyDoc_STRVAR(sched_param__doc__,
4774"sched_param(sched_priority): A scheduling parameter.\n\n\
4775Current has only one field: sched_priority");
4776
4777static PyStructSequence_Field sched_param_fields[] = {
4778 {"sched_priority", "the scheduling priority"},
4779 {0}
4780};
4781
4782static PyStructSequence_Desc sched_param_desc = {
4783 "sched_param", /* name */
4784 sched_param__doc__, /* doc */
4785 sched_param_fields,
4786 1
4787};
4788
4789static int
4790convert_sched_param(PyObject *param, struct sched_param *res)
4791{
4792 long priority;
4793
4794 if (Py_TYPE(param) != &SchedParamType) {
4795 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
4796 return 0;
4797 }
4798 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
4799 if (priority == -1 && PyErr_Occurred())
4800 return 0;
4801 if (priority > INT_MAX || priority < INT_MIN) {
4802 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
4803 return 0;
4804 }
4805 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
4806 return 1;
4807}
4808
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004809#endif
4810
4811#ifdef HAVE_SCHED_SETSCHEDULER
4812
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004813PyDoc_STRVAR(posix_sched_setscheduler__doc__,
4814"sched_setscheduler(pid, policy, param)\n\n\
4815Set the scheduling policy, *policy*, for *pid*.\n\
4816If *pid* is 0, the calling process is changed.\n\
4817*param* is an instance of sched_param.");
4818
4819static PyObject *
4820posix_sched_setscheduler(PyObject *self, PyObject *args)
4821{
4822 pid_t pid;
4823 int policy;
4824 struct sched_param param;
4825
4826 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
4827 &pid, &policy, &convert_sched_param, &param))
4828 return NULL;
Jesus Cea9c822272011-09-10 01:40:52 +02004829
4830 /*
Jesus Cea54b01492011-09-10 01:53:19 +02004831 ** sched_setscheduler() returns 0 in Linux, but the previous
4832 ** scheduling policy under Solaris/Illumos, and others.
4833 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02004834 */
4835 if (sched_setscheduler(pid, policy, &param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004836 return posix_error();
4837 Py_RETURN_NONE;
4838}
4839
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004840#endif
4841
4842#ifdef HAVE_SCHED_SETPARAM
4843
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004844PyDoc_STRVAR(posix_sched_getparam__doc__,
4845"sched_getparam(pid) -> sched_param\n\n\
4846Returns scheduling parameters for the process with *pid* as an instance of the\n\
4847sched_param class. A PID of 0 means the calling process.");
4848
4849static PyObject *
4850posix_sched_getparam(PyObject *self, PyObject *args)
4851{
4852 pid_t pid;
4853 struct sched_param param;
4854 PyObject *res, *priority;
4855
4856 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
4857 return NULL;
4858 if (sched_getparam(pid, &param))
4859 return posix_error();
4860 res = PyStructSequence_New(&SchedParamType);
4861 if (!res)
4862 return NULL;
4863 priority = PyLong_FromLong(param.sched_priority);
4864 if (!priority) {
4865 Py_DECREF(res);
4866 return NULL;
4867 }
4868 PyStructSequence_SET_ITEM(res, 0, priority);
4869 return res;
4870}
4871
4872PyDoc_STRVAR(posix_sched_setparam__doc__,
4873"sched_setparam(pid, param)\n\n\
4874Set scheduling parameters for a process with PID *pid*.\n\
4875A PID of 0 means the calling process.");
4876
4877static PyObject *
4878posix_sched_setparam(PyObject *self, PyObject *args)
4879{
4880 pid_t pid;
4881 struct sched_param param;
4882
4883 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
4884 &pid, &convert_sched_param, &param))
4885 return NULL;
4886 if (sched_setparam(pid, &param))
4887 return posix_error();
4888 Py_RETURN_NONE;
4889}
4890
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004891#endif
4892
4893#ifdef HAVE_SCHED_RR_GET_INTERVAL
4894
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004895PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
4896"sched_rr_get_interval(pid) -> float\n\n\
4897Return the round-robin quantum for the process with PID *pid* in seconds.");
4898
4899static PyObject *
4900posix_sched_rr_get_interval(PyObject *self, PyObject *args)
4901{
4902 pid_t pid;
4903 struct timespec interval;
4904
4905 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
4906 return NULL;
4907 if (sched_rr_get_interval(pid, &interval))
4908 return posix_error();
4909 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
4910}
4911
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004912#endif
4913
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004914PyDoc_STRVAR(posix_sched_yield__doc__,
4915"sched_yield()\n\n\
4916Voluntarily relinquish the CPU.");
4917
4918static PyObject *
4919posix_sched_yield(PyObject *self, PyObject *noargs)
4920{
4921 if (sched_yield())
4922 return posix_error();
4923 Py_RETURN_NONE;
4924}
4925
Benjamin Peterson2740af82011-08-02 17:41:34 -05004926#ifdef HAVE_SCHED_SETAFFINITY
4927
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004928typedef struct {
4929 PyObject_HEAD;
4930 Py_ssize_t size;
4931 int ncpus;
4932 cpu_set_t *set;
4933} Py_cpu_set;
4934
4935static PyTypeObject cpu_set_type;
4936
4937static void
4938cpu_set_dealloc(Py_cpu_set *set)
4939{
4940 assert(set->set);
4941 CPU_FREE(set->set);
4942 Py_TYPE(set)->tp_free(set);
4943}
4944
4945static Py_cpu_set *
4946make_new_cpu_set(PyTypeObject *type, Py_ssize_t size)
4947{
4948 Py_cpu_set *set;
4949
4950 if (size < 0) {
4951 PyErr_SetString(PyExc_ValueError, "negative size");
4952 return NULL;
4953 }
4954 set = (Py_cpu_set *)type->tp_alloc(type, 0);
4955 if (!set)
4956 return NULL;
4957 set->ncpus = size;
4958 set->size = CPU_ALLOC_SIZE(size);
4959 set->set = CPU_ALLOC(size);
4960 if (!set->set) {
4961 type->tp_free(set);
4962 PyErr_NoMemory();
4963 return NULL;
4964 }
4965 CPU_ZERO_S(set->size, set->set);
4966 return set;
4967}
4968
4969static PyObject *
4970cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4971{
4972 int size;
4973
4974 if (!_PyArg_NoKeywords("cpu_set()", kwargs) ||
4975 !PyArg_ParseTuple(args, "i:cpu_set", &size))
4976 return NULL;
4977 return (PyObject *)make_new_cpu_set(type, size);
4978}
4979
4980static PyObject *
4981cpu_set_repr(Py_cpu_set *set)
4982{
4983 return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
Victor Stinner63941882011-09-29 00:42:28 +02004984}
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004985
4986static Py_ssize_t
4987cpu_set_len(Py_cpu_set *set)
4988{
4989 return set->ncpus;
4990}
4991
4992static int
4993_get_cpu(Py_cpu_set *set, const char *requester, PyObject *args)
4994{
4995 int cpu;
4996 if (!PyArg_ParseTuple(args, requester, &cpu))
4997 return -1;
4998 if (cpu < 0) {
4999 PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid");
5000 return -1;
5001 }
5002 if (cpu >= set->ncpus) {
5003 PyErr_SetString(PyExc_ValueError, "cpu too large for set");
5004 return -1;
5005 }
5006 return cpu;
5007}
5008
5009PyDoc_STRVAR(cpu_set_set_doc,
5010"cpu_set.set(i)\n\n\
5011Add CPU *i* to the set.");
5012
5013static PyObject *
5014cpu_set_set(Py_cpu_set *set, PyObject *args)
5015{
5016 int cpu = _get_cpu(set, "i|set", args);
5017 if (cpu == -1)
5018 return NULL;
5019 CPU_SET_S(cpu, set->size, set->set);
5020 Py_RETURN_NONE;
5021}
5022
5023PyDoc_STRVAR(cpu_set_count_doc,
5024"cpu_set.count() -> int\n\n\
5025Return the number of CPUs active in the set.");
5026
5027static PyObject *
5028cpu_set_count(Py_cpu_set *set, PyObject *noargs)
5029{
5030 return PyLong_FromLong(CPU_COUNT_S(set->size, set->set));
5031}
5032
5033PyDoc_STRVAR(cpu_set_clear_doc,
5034"cpu_set.clear(i)\n\n\
5035Remove CPU *i* from the set.");
5036
5037static PyObject *
5038cpu_set_clear(Py_cpu_set *set, PyObject *args)
5039{
5040 int cpu = _get_cpu(set, "i|clear", args);
5041 if (cpu == -1)
5042 return NULL;
5043 CPU_CLR_S(cpu, set->size, set->set);
5044 Py_RETURN_NONE;
5045}
5046
5047PyDoc_STRVAR(cpu_set_isset_doc,
5048"cpu_set.isset(i) -> bool\n\n\
5049Test if CPU *i* is in the set.");
5050
5051static PyObject *
5052cpu_set_isset(Py_cpu_set *set, PyObject *args)
5053{
5054 int cpu = _get_cpu(set, "i|isset", args);
5055 if (cpu == -1)
5056 return NULL;
5057 if (CPU_ISSET_S(cpu, set->size, set->set))
5058 Py_RETURN_TRUE;
5059 Py_RETURN_FALSE;
5060}
5061
5062PyDoc_STRVAR(cpu_set_zero_doc,
5063"cpu_set.zero()\n\n\
5064Clear the cpu_set.");
5065
5066static PyObject *
5067cpu_set_zero(Py_cpu_set *set, PyObject *noargs)
5068{
5069 CPU_ZERO_S(set->size, set->set);
5070 Py_RETURN_NONE;
5071}
5072
5073static PyObject *
5074cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op)
5075{
5076 int eq;
5077
Brian Curtindfc80e32011-08-10 20:28:54 -05005078 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type)
5079 Py_RETURN_NOTIMPLEMENTED;
5080
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005081 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
5082 if ((op == Py_EQ) ? eq : !eq)
5083 Py_RETURN_TRUE;
5084 else
5085 Py_RETURN_FALSE;
5086}
5087
5088#define CPU_SET_BINOP(name, op) \
5089 static PyObject * \
5090 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
5091 if (res) { \
5092 Py_INCREF(res); \
5093 } \
5094 else { \
Benjamin Petersone870fe62011-08-02 17:44:26 -05005095 res = make_new_cpu_set(&cpu_set_type, left->ncpus); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005096 if (!res) \
5097 return NULL; \
5098 } \
Benjamin Peterson9b374bf2011-08-02 18:22:30 -05005099 if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005100 Py_DECREF(res); \
Brian Curtindfc80e32011-08-10 20:28:54 -05005101 Py_RETURN_NOTIMPLEMENTED; \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005102 } \
Benjamin Peterson8f7bdd32011-08-02 18:34:30 -05005103 assert(left->size == right->size && right->size == res->size); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005104 op(res->size, res->set, left->set, right->set); \
5105 return (PyObject *)res; \
5106 } \
5107 static PyObject * \
5108 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
5109 return do_cpu_set_##name(left, right, NULL); \
5110 } \
5111 static PyObject * \
5112 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
5113 return do_cpu_set_##name(left, right, left); \
5114 } \
5115
5116CPU_SET_BINOP(and, CPU_AND_S)
5117CPU_SET_BINOP(or, CPU_OR_S)
5118CPU_SET_BINOP(xor, CPU_XOR_S)
5119#undef CPU_SET_BINOP
5120
5121PyDoc_STRVAR(cpu_set_doc,
5122"cpu_set(size)\n\n\
5123Create an empty mask of CPUs.");
5124
5125static PyNumberMethods cpu_set_as_number = {
5126 0, /*nb_add*/
5127 0, /*nb_subtract*/
5128 0, /*nb_multiply*/
5129 0, /*nb_remainder*/
5130 0, /*nb_divmod*/
5131 0, /*nb_power*/
5132 0, /*nb_negative*/
5133 0, /*nb_positive*/
5134 0, /*nb_absolute*/
5135 0, /*nb_bool*/
5136 0, /*nb_invert*/
5137 0, /*nb_lshift*/
5138 0, /*nb_rshift*/
5139 (binaryfunc)cpu_set_and, /*nb_and*/
5140 (binaryfunc)cpu_set_xor, /*nb_xor*/
5141 (binaryfunc)cpu_set_or, /*nb_or*/
5142 0, /*nb_int*/
5143 0, /*nb_reserved*/
5144 0, /*nb_float*/
5145 0, /*nb_inplace_add*/
5146 0, /*nb_inplace_subtract*/
5147 0, /*nb_inplace_multiply*/
5148 0, /*nb_inplace_remainder*/
5149 0, /*nb_inplace_power*/
5150 0, /*nb_inplace_lshift*/
5151 0, /*nb_inplace_rshift*/
5152 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
5153 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
5154 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
5155};
5156
5157static PySequenceMethods cpu_set_as_sequence = {
5158 (lenfunc)cpu_set_len, /* sq_length */
5159};
5160
5161static PyMethodDef cpu_set_methods[] = {
5162 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
5163 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
5164 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
5165 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
5166 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
5167 {NULL, NULL} /* sentinel */
5168};
5169
5170static PyTypeObject cpu_set_type = {
5171 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5172 "posix.cpu_set", /* tp_name */
5173 sizeof(Py_cpu_set), /* tp_basicsize */
5174 0, /* tp_itemsize */
5175 /* methods */
5176 (destructor)cpu_set_dealloc, /* tp_dealloc */
5177 0, /* tp_print */
5178 0, /* tp_getattr */
5179 0, /* tp_setattr */
5180 0, /* tp_reserved */
5181 (reprfunc)cpu_set_repr, /* tp_repr */
5182 &cpu_set_as_number, /* tp_as_number */
5183 &cpu_set_as_sequence, /* tp_as_sequence */
5184 0, /* tp_as_mapping */
5185 PyObject_HashNotImplemented, /* tp_hash */
5186 0, /* tp_call */
5187 0, /* tp_str */
5188 PyObject_GenericGetAttr, /* tp_getattro */
5189 0, /* tp_setattro */
5190 0, /* tp_as_buffer */
5191 Py_TPFLAGS_DEFAULT, /* tp_flags */
5192 cpu_set_doc, /* tp_doc */
5193 0, /* tp_traverse */
5194 0, /* tp_clear */
5195 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5196 0, /* tp_weaklistoffset */
5197 0, /* tp_iter */
5198 0, /* tp_iternext */
5199 cpu_set_methods, /* tp_methods */
5200 0, /* tp_members */
5201 0, /* tp_getset */
5202 0, /* tp_base */
5203 0, /* tp_dict */
5204 0, /* tp_descr_get */
5205 0, /* tp_descr_set */
5206 0, /* tp_dictoffset */
5207 0, /* tp_init */
5208 PyType_GenericAlloc, /* tp_alloc */
5209 cpu_set_new, /* tp_new */
5210 PyObject_Del, /* tp_free */
5211};
5212
5213PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5214"sched_setaffinity(pid, cpu_set)\n\n\
5215Set the affinity of the process with PID *pid* to *cpu_set*.");
5216
5217static PyObject *
5218posix_sched_setaffinity(PyObject *self, PyObject *args)
5219{
5220 pid_t pid;
5221 Py_cpu_set *cpu_set;
5222
Benjamin Petersona17a5d62011-08-09 16:49:13 -05005223 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005224 &pid, &cpu_set_type, &cpu_set))
5225 return NULL;
5226 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5227 return posix_error();
5228 Py_RETURN_NONE;
5229}
5230
5231PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5232"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5233Return the affinity of the process with PID *pid*.\n\
5234The returned cpu_set will be of size *ncpus*.");
5235
5236static PyObject *
5237posix_sched_getaffinity(PyObject *self, PyObject *args)
5238{
5239 pid_t pid;
5240 int ncpus;
5241 Py_cpu_set *res;
5242
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005243 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005244 &pid, &ncpus))
5245 return NULL;
5246 res = make_new_cpu_set(&cpu_set_type, ncpus);
5247 if (!res)
5248 return NULL;
5249 if (sched_getaffinity(pid, res->size, res->set)) {
5250 Py_DECREF(res);
5251 return posix_error();
5252 }
5253 return (PyObject *)res;
5254}
5255
Benjamin Peterson2740af82011-08-02 17:41:34 -05005256#endif /* HAVE_SCHED_SETAFFINITY */
5257
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005258#endif /* HAVE_SCHED_H */
5259
Neal Norwitzb59798b2003-03-21 01:43:31 +00005260/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005261/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5262#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005263#define DEV_PTY_FILE "/dev/ptc"
5264#define HAVE_DEV_PTMX
5265#else
5266#define DEV_PTY_FILE "/dev/ptmx"
5267#endif
5268
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005269#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005270#ifdef HAVE_PTY_H
5271#include <pty.h>
5272#else
5273#ifdef HAVE_LIBUTIL_H
5274#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005275#else
5276#ifdef HAVE_UTIL_H
5277#include <util.h>
5278#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005279#endif /* HAVE_LIBUTIL_H */
5280#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005281#ifdef HAVE_STROPTS_H
5282#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005283#endif
5284#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005285
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005286#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005287PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005288"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005289Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005290
5291static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005292posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005293{
Victor Stinner8c62be82010-05-06 00:08:46 +00005294 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005295#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005296 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005297#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005298#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005299 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005300#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005301 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005302#endif
5303#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005304
Thomas Wouters70c21a12000-07-14 14:28:33 +00005305#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005306 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5307 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005308#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005309 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5310 if (slave_name == NULL)
5311 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005312
Victor Stinner8c62be82010-05-06 00:08:46 +00005313 slave_fd = open(slave_name, O_RDWR);
5314 if (slave_fd < 0)
5315 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005316#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005317 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5318 if (master_fd < 0)
5319 return posix_error();
5320 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5321 /* change permission of slave */
5322 if (grantpt(master_fd) < 0) {
5323 PyOS_setsig(SIGCHLD, sig_saved);
5324 return posix_error();
5325 }
5326 /* unlock slave */
5327 if (unlockpt(master_fd) < 0) {
5328 PyOS_setsig(SIGCHLD, sig_saved);
5329 return posix_error();
5330 }
5331 PyOS_setsig(SIGCHLD, sig_saved);
5332 slave_name = ptsname(master_fd); /* get name of slave */
5333 if (slave_name == NULL)
5334 return posix_error();
5335 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5336 if (slave_fd < 0)
5337 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005338#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005339 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5340 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005341#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005342 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005343#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005344#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005345#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005346
Victor Stinner8c62be82010-05-06 00:08:46 +00005347 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005348
Fred Drake8cef4cf2000-06-28 16:40:38 +00005349}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005350#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005351
5352#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005353PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005354"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005355Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5356Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005357To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005358
5359static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005360posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005361{
Victor Stinner8c62be82010-05-06 00:08:46 +00005362 int master_fd = -1, result = 0;
5363 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005364
Victor Stinner8c62be82010-05-06 00:08:46 +00005365 _PyImport_AcquireLock();
5366 pid = forkpty(&master_fd, NULL, NULL, NULL);
5367 if (pid == 0) {
5368 /* child: this clobbers and resets the import lock. */
5369 PyOS_AfterFork();
5370 } else {
5371 /* parent: release the import lock. */
5372 result = _PyImport_ReleaseLock();
5373 }
5374 if (pid == -1)
5375 return posix_error();
5376 if (result < 0) {
5377 /* Don't clobber the OSError if the fork failed. */
5378 PyErr_SetString(PyExc_RuntimeError,
5379 "not holding the import lock");
5380 return NULL;
5381 }
5382 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005383}
5384#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005385
Ross Lagerwall7807c352011-03-17 20:20:30 +02005386
Guido van Rossumad0ee831995-03-01 10:34:45 +00005387#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005388PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005389"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005390Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005391
Barry Warsaw53699e91996-12-10 23:23:01 +00005392static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005393posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005394{
Victor Stinner8c62be82010-05-06 00:08:46 +00005395 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005396}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005397#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005398
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005399
Guido van Rossumad0ee831995-03-01 10:34:45 +00005400#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005401PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005402"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005403Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005404
Barry Warsaw53699e91996-12-10 23:23:01 +00005405static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005406posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005407{
Victor Stinner8c62be82010-05-06 00:08:46 +00005408 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005409}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005410#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005411
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005412
Guido van Rossumad0ee831995-03-01 10:34:45 +00005413#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005414PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005415"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005416Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005417
Barry Warsaw53699e91996-12-10 23:23:01 +00005418static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005419posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005420{
Victor Stinner8c62be82010-05-06 00:08:46 +00005421 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005422}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005423#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005424
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005425
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005426PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005427"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005428Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005429
Barry Warsaw53699e91996-12-10 23:23:01 +00005430static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005431posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005432{
Victor Stinner8c62be82010-05-06 00:08:46 +00005433 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005434}
5435
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005436#ifdef HAVE_GETGROUPLIST
5437PyDoc_STRVAR(posix_getgrouplist__doc__,
5438"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5439Returns a list of groups to which a user belongs.\n\n\
5440 user: username to lookup\n\
5441 group: base group id of the user");
5442
5443static PyObject *
5444posix_getgrouplist(PyObject *self, PyObject *args)
5445{
5446#ifdef NGROUPS_MAX
5447#define MAX_GROUPS NGROUPS_MAX
5448#else
5449 /* defined to be 16 on Solaris7, so this should be a small number */
5450#define MAX_GROUPS 64
5451#endif
5452
5453 const char *user;
5454 int i, ngroups;
5455 PyObject *list;
5456#ifdef __APPLE__
5457 int *groups, basegid;
5458#else
5459 gid_t *groups, basegid;
5460#endif
5461 ngroups = MAX_GROUPS;
5462
5463 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5464 return NULL;
5465
5466#ifdef __APPLE__
5467 groups = PyMem_Malloc(ngroups * sizeof(int));
5468#else
5469 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5470#endif
5471 if (groups == NULL)
5472 return PyErr_NoMemory();
5473
5474 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5475 PyMem_Del(groups);
5476 return posix_error();
5477 }
5478
5479 list = PyList_New(ngroups);
5480 if (list == NULL) {
5481 PyMem_Del(groups);
5482 return NULL;
5483 }
5484
5485 for (i = 0; i < ngroups; i++) {
5486 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5487 if (o == NULL) {
5488 Py_DECREF(list);
5489 PyMem_Del(groups);
5490 return NULL;
5491 }
5492 PyList_SET_ITEM(list, i, o);
5493 }
5494
5495 PyMem_Del(groups);
5496
5497 return list;
5498}
5499#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005500
Fred Drakec9680921999-12-13 16:37:25 +00005501#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005502PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005503"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005504Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005505
5506static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005507posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005508{
5509 PyObject *result = NULL;
5510
Fred Drakec9680921999-12-13 16:37:25 +00005511#ifdef NGROUPS_MAX
5512#define MAX_GROUPS NGROUPS_MAX
5513#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005514 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005515#define MAX_GROUPS 64
5516#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005517 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005518
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005519 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005520 * This is a helper variable to store the intermediate result when
5521 * that happens.
5522 *
5523 * To keep the code readable the OSX behaviour is unconditional,
5524 * according to the POSIX spec this should be safe on all unix-y
5525 * systems.
5526 */
5527 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005528 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005529
Victor Stinner8c62be82010-05-06 00:08:46 +00005530 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005531 if (n < 0) {
5532 if (errno == EINVAL) {
5533 n = getgroups(0, NULL);
5534 if (n == -1) {
5535 return posix_error();
5536 }
5537 if (n == 0) {
5538 /* Avoid malloc(0) */
5539 alt_grouplist = grouplist;
5540 } else {
5541 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5542 if (alt_grouplist == NULL) {
5543 errno = EINVAL;
5544 return posix_error();
5545 }
5546 n = getgroups(n, alt_grouplist);
5547 if (n == -1) {
5548 PyMem_Free(alt_grouplist);
5549 return posix_error();
5550 }
5551 }
5552 } else {
5553 return posix_error();
5554 }
5555 }
5556 result = PyList_New(n);
5557 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005558 int i;
5559 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005560 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005561 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005562 Py_DECREF(result);
5563 result = NULL;
5564 break;
Fred Drakec9680921999-12-13 16:37:25 +00005565 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005566 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005567 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005568 }
5569
5570 if (alt_grouplist != grouplist) {
5571 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005572 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005573
Fred Drakec9680921999-12-13 16:37:25 +00005574 return result;
5575}
5576#endif
5577
Antoine Pitroub7572f02009-12-02 20:46:48 +00005578#ifdef HAVE_INITGROUPS
5579PyDoc_STRVAR(posix_initgroups__doc__,
5580"initgroups(username, gid) -> None\n\n\
5581Call the system initgroups() to initialize the group access list with all of\n\
5582the groups of which the specified username is a member, plus the specified\n\
5583group id.");
5584
5585static PyObject *
5586posix_initgroups(PyObject *self, PyObject *args)
5587{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005588 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005589 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005590 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005591 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005592
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005593 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5594 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005595 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005596 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005597
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005598 res = initgroups(username, (gid_t) gid);
5599 Py_DECREF(oname);
5600 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005601 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005602
Victor Stinner8c62be82010-05-06 00:08:46 +00005603 Py_INCREF(Py_None);
5604 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005605}
5606#endif
5607
Martin v. Löwis606edc12002-06-13 21:09:11 +00005608#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005609PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005610"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005611Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005612
5613static PyObject *
5614posix_getpgid(PyObject *self, PyObject *args)
5615{
Victor Stinner8c62be82010-05-06 00:08:46 +00005616 pid_t pid, pgid;
5617 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5618 return NULL;
5619 pgid = getpgid(pid);
5620 if (pgid < 0)
5621 return posix_error();
5622 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005623}
5624#endif /* HAVE_GETPGID */
5625
5626
Guido van Rossumb6775db1994-08-01 11:34:53 +00005627#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005628PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005629"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005630Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005631
Barry Warsaw53699e91996-12-10 23:23:01 +00005632static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005633posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005634{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005635#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005636 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005637#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005638 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005639#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005640}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005641#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005642
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005643
Guido van Rossumb6775db1994-08-01 11:34:53 +00005644#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005645PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005646"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005647Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005648
Barry Warsaw53699e91996-12-10 23:23:01 +00005649static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005650posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005651{
Guido van Rossum64933891994-10-20 21:56:42 +00005652#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005653 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005654#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005655 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005656#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005657 return posix_error();
5658 Py_INCREF(Py_None);
5659 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005660}
5661
Guido van Rossumb6775db1994-08-01 11:34:53 +00005662#endif /* HAVE_SETPGRP */
5663
Guido van Rossumad0ee831995-03-01 10:34:45 +00005664#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005665
5666#ifdef MS_WINDOWS
5667#include <tlhelp32.h>
5668
5669static PyObject*
5670win32_getppid()
5671{
5672 HANDLE snapshot;
5673 pid_t mypid;
5674 PyObject* result = NULL;
5675 BOOL have_record;
5676 PROCESSENTRY32 pe;
5677
5678 mypid = getpid(); /* This function never fails */
5679
5680 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5681 if (snapshot == INVALID_HANDLE_VALUE)
5682 return PyErr_SetFromWindowsErr(GetLastError());
5683
5684 pe.dwSize = sizeof(pe);
5685 have_record = Process32First(snapshot, &pe);
5686 while (have_record) {
5687 if (mypid == (pid_t)pe.th32ProcessID) {
5688 /* We could cache the ulong value in a static variable. */
5689 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
5690 break;
5691 }
5692
5693 have_record = Process32Next(snapshot, &pe);
5694 }
5695
5696 /* If our loop exits and our pid was not found (result will be NULL)
5697 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
5698 * error anyway, so let's raise it. */
5699 if (!result)
5700 result = PyErr_SetFromWindowsErr(GetLastError());
5701
5702 CloseHandle(snapshot);
5703
5704 return result;
5705}
5706#endif /*MS_WINDOWS*/
5707
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005708PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005709"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005710Return the parent's process id. If the parent process has already exited,\n\
5711Windows machines will still return its id; others systems will return the id\n\
5712of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005713
Barry Warsaw53699e91996-12-10 23:23:01 +00005714static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005715posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005716{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005717#ifdef MS_WINDOWS
5718 return win32_getppid();
5719#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005720 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005721#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005722}
5723#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005724
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005725
Fred Drake12c6e2d1999-12-14 21:25:03 +00005726#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005727PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005728"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005729Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005730
5731static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005732posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005733{
Victor Stinner8c62be82010-05-06 00:08:46 +00005734 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005735#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005736 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02005737 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005738
5739 if (GetUserNameW(user_name, &num_chars)) {
5740 /* num_chars is the number of unicode chars plus null terminator */
5741 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005742 }
5743 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005744 result = PyErr_SetFromWindowsErr(GetLastError());
5745#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005746 char *name;
5747 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005748
Victor Stinner8c62be82010-05-06 00:08:46 +00005749 errno = 0;
5750 name = getlogin();
5751 if (name == NULL) {
5752 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005753 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005754 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005755 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005756 }
5757 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005758 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005759 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005760#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005761 return result;
5762}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005763#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005764
Guido van Rossumad0ee831995-03-01 10:34:45 +00005765#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005766PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005767"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005768Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005769
Barry Warsaw53699e91996-12-10 23:23:01 +00005770static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005771posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005772{
Victor Stinner8c62be82010-05-06 00:08:46 +00005773 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005774}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005775#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005776
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005777
Guido van Rossumad0ee831995-03-01 10:34:45 +00005778#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005779PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005780"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005781Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005782
Barry Warsaw53699e91996-12-10 23:23:01 +00005783static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005784posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005785{
Victor Stinner8c62be82010-05-06 00:08:46 +00005786 pid_t pid;
5787 int sig;
5788 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5789 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005790#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005791 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5792 APIRET rc;
5793 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005794 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005795
5796 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5797 APIRET rc;
5798 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005799 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005800
5801 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005802 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005803#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005804 if (kill(pid, sig) == -1)
5805 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005806#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005807 Py_INCREF(Py_None);
5808 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005809}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005810#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005811
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005812#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005813PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005814"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005815Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005816
5817static PyObject *
5818posix_killpg(PyObject *self, PyObject *args)
5819{
Victor Stinner8c62be82010-05-06 00:08:46 +00005820 int sig;
5821 pid_t pgid;
5822 /* XXX some man pages make the `pgid` parameter an int, others
5823 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5824 take the same type. Moreover, pid_t is always at least as wide as
5825 int (else compilation of this module fails), which is safe. */
5826 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5827 return NULL;
5828 if (killpg(pgid, sig) == -1)
5829 return posix_error();
5830 Py_INCREF(Py_None);
5831 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005832}
5833#endif
5834
Brian Curtineb24d742010-04-12 17:16:38 +00005835#ifdef MS_WINDOWS
5836PyDoc_STRVAR(win32_kill__doc__,
5837"kill(pid, sig)\n\n\
5838Kill a process with a signal.");
5839
5840static PyObject *
5841win32_kill(PyObject *self, PyObject *args)
5842{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005843 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005844 DWORD pid, sig, err;
5845 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005846
Victor Stinner8c62be82010-05-06 00:08:46 +00005847 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5848 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005849
Victor Stinner8c62be82010-05-06 00:08:46 +00005850 /* Console processes which share a common console can be sent CTRL+C or
5851 CTRL+BREAK events, provided they handle said events. */
5852 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5853 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5854 err = GetLastError();
5855 PyErr_SetFromWindowsErr(err);
5856 }
5857 else
5858 Py_RETURN_NONE;
5859 }
Brian Curtineb24d742010-04-12 17:16:38 +00005860
Victor Stinner8c62be82010-05-06 00:08:46 +00005861 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5862 attempt to open and terminate the process. */
5863 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5864 if (handle == NULL) {
5865 err = GetLastError();
5866 return PyErr_SetFromWindowsErr(err);
5867 }
Brian Curtineb24d742010-04-12 17:16:38 +00005868
Victor Stinner8c62be82010-05-06 00:08:46 +00005869 if (TerminateProcess(handle, sig) == 0) {
5870 err = GetLastError();
5871 result = PyErr_SetFromWindowsErr(err);
5872 } else {
5873 Py_INCREF(Py_None);
5874 result = Py_None;
5875 }
Brian Curtineb24d742010-04-12 17:16:38 +00005876
Victor Stinner8c62be82010-05-06 00:08:46 +00005877 CloseHandle(handle);
5878 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005879}
5880#endif /* MS_WINDOWS */
5881
Guido van Rossumc0125471996-06-28 18:55:32 +00005882#ifdef HAVE_PLOCK
5883
5884#ifdef HAVE_SYS_LOCK_H
5885#include <sys/lock.h>
5886#endif
5887
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005888PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005889"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005890Lock program segments into memory.");
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_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005894{
Victor Stinner8c62be82010-05-06 00:08:46 +00005895 int op;
5896 if (!PyArg_ParseTuple(args, "i:plock", &op))
5897 return NULL;
5898 if (plock(op) == -1)
5899 return posix_error();
5900 Py_INCREF(Py_None);
5901 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005902}
5903#endif
5904
Guido van Rossumb6775db1994-08-01 11:34:53 +00005905#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005906PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005907"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005908Set the current process's user id.");
5909
Barry Warsaw53699e91996-12-10 23:23:01 +00005910static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005911posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005912{
Victor Stinner8c62be82010-05-06 00:08:46 +00005913 long uid_arg;
5914 uid_t uid;
5915 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5916 return NULL;
5917 uid = uid_arg;
5918 if (uid != uid_arg) {
5919 PyErr_SetString(PyExc_OverflowError, "user id too big");
5920 return NULL;
5921 }
5922 if (setuid(uid) < 0)
5923 return posix_error();
5924 Py_INCREF(Py_None);
5925 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005926}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005927#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005928
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005929
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005930#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005931PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005932"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005933Set the current process's effective user id.");
5934
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005935static PyObject *
5936posix_seteuid (PyObject *self, PyObject *args)
5937{
Victor Stinner8c62be82010-05-06 00:08:46 +00005938 long euid_arg;
5939 uid_t euid;
5940 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5941 return NULL;
5942 euid = euid_arg;
5943 if (euid != euid_arg) {
5944 PyErr_SetString(PyExc_OverflowError, "user id too big");
5945 return NULL;
5946 }
5947 if (seteuid(euid) < 0) {
5948 return posix_error();
5949 } else {
5950 Py_INCREF(Py_None);
5951 return Py_None;
5952 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005953}
5954#endif /* HAVE_SETEUID */
5955
5956#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005957PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005958"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005959Set the current process's effective group id.");
5960
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005961static PyObject *
5962posix_setegid (PyObject *self, PyObject *args)
5963{
Victor Stinner8c62be82010-05-06 00:08:46 +00005964 long egid_arg;
5965 gid_t egid;
5966 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5967 return NULL;
5968 egid = egid_arg;
5969 if (egid != egid_arg) {
5970 PyErr_SetString(PyExc_OverflowError, "group id too big");
5971 return NULL;
5972 }
5973 if (setegid(egid) < 0) {
5974 return posix_error();
5975 } else {
5976 Py_INCREF(Py_None);
5977 return Py_None;
5978 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005979}
5980#endif /* HAVE_SETEGID */
5981
5982#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005983PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005984"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005985Set the current process's real and effective user ids.");
5986
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005987static PyObject *
5988posix_setreuid (PyObject *self, PyObject *args)
5989{
Victor Stinner8c62be82010-05-06 00:08:46 +00005990 long ruid_arg, euid_arg;
5991 uid_t ruid, euid;
5992 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5993 return NULL;
5994 if (ruid_arg == -1)
5995 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5996 else
5997 ruid = ruid_arg; /* otherwise, assign from our long */
5998 if (euid_arg == -1)
5999 euid = (uid_t)-1;
6000 else
6001 euid = euid_arg;
6002 if ((euid_arg != -1 && euid != euid_arg) ||
6003 (ruid_arg != -1 && ruid != ruid_arg)) {
6004 PyErr_SetString(PyExc_OverflowError, "user id too big");
6005 return NULL;
6006 }
6007 if (setreuid(ruid, euid) < 0) {
6008 return posix_error();
6009 } else {
6010 Py_INCREF(Py_None);
6011 return Py_None;
6012 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006013}
6014#endif /* HAVE_SETREUID */
6015
6016#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006017PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00006018"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006019Set the current process's real and effective group ids.");
6020
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006021static PyObject *
6022posix_setregid (PyObject *self, PyObject *args)
6023{
Victor Stinner8c62be82010-05-06 00:08:46 +00006024 long rgid_arg, egid_arg;
6025 gid_t rgid, egid;
6026 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
6027 return NULL;
6028 if (rgid_arg == -1)
6029 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
6030 else
6031 rgid = rgid_arg; /* otherwise, assign from our long */
6032 if (egid_arg == -1)
6033 egid = (gid_t)-1;
6034 else
6035 egid = egid_arg;
6036 if ((egid_arg != -1 && egid != egid_arg) ||
6037 (rgid_arg != -1 && rgid != rgid_arg)) {
6038 PyErr_SetString(PyExc_OverflowError, "group id too big");
6039 return NULL;
6040 }
6041 if (setregid(rgid, egid) < 0) {
6042 return posix_error();
6043 } else {
6044 Py_INCREF(Py_None);
6045 return Py_None;
6046 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006047}
6048#endif /* HAVE_SETREGID */
6049
Guido van Rossumb6775db1994-08-01 11:34:53 +00006050#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006051PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006052"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006053Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006054
Barry Warsaw53699e91996-12-10 23:23:01 +00006055static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006056posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006057{
Victor Stinner8c62be82010-05-06 00:08:46 +00006058 long gid_arg;
6059 gid_t gid;
6060 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
6061 return NULL;
6062 gid = gid_arg;
6063 if (gid != gid_arg) {
6064 PyErr_SetString(PyExc_OverflowError, "group id too big");
6065 return NULL;
6066 }
6067 if (setgid(gid) < 0)
6068 return posix_error();
6069 Py_INCREF(Py_None);
6070 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006071}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006072#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006073
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006074#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006075PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006076"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006077Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006078
6079static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006080posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006081{
Victor Stinner8c62be82010-05-06 00:08:46 +00006082 int i, len;
6083 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006084
Victor Stinner8c62be82010-05-06 00:08:46 +00006085 if (!PySequence_Check(groups)) {
6086 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6087 return NULL;
6088 }
6089 len = PySequence_Size(groups);
6090 if (len > MAX_GROUPS) {
6091 PyErr_SetString(PyExc_ValueError, "too many groups");
6092 return NULL;
6093 }
6094 for(i = 0; i < len; i++) {
6095 PyObject *elem;
6096 elem = PySequence_GetItem(groups, i);
6097 if (!elem)
6098 return NULL;
6099 if (!PyLong_Check(elem)) {
6100 PyErr_SetString(PyExc_TypeError,
6101 "groups must be integers");
6102 Py_DECREF(elem);
6103 return NULL;
6104 } else {
6105 unsigned long x = PyLong_AsUnsignedLong(elem);
6106 if (PyErr_Occurred()) {
6107 PyErr_SetString(PyExc_TypeError,
6108 "group id too big");
6109 Py_DECREF(elem);
6110 return NULL;
6111 }
6112 grouplist[i] = x;
6113 /* read back the value to see if it fitted in gid_t */
6114 if (grouplist[i] != x) {
6115 PyErr_SetString(PyExc_TypeError,
6116 "group id too big");
6117 Py_DECREF(elem);
6118 return NULL;
6119 }
6120 }
6121 Py_DECREF(elem);
6122 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006123
Victor Stinner8c62be82010-05-06 00:08:46 +00006124 if (setgroups(len, grouplist) < 0)
6125 return posix_error();
6126 Py_INCREF(Py_None);
6127 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006128}
6129#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006130
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006131#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6132static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006133wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006134{
Victor Stinner8c62be82010-05-06 00:08:46 +00006135 PyObject *result;
6136 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006137 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006138
Victor Stinner8c62be82010-05-06 00:08:46 +00006139 if (pid == -1)
6140 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006141
Victor Stinner8c62be82010-05-06 00:08:46 +00006142 if (struct_rusage == NULL) {
6143 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6144 if (m == NULL)
6145 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006146 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00006147 Py_DECREF(m);
6148 if (struct_rusage == NULL)
6149 return NULL;
6150 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006151
Victor Stinner8c62be82010-05-06 00:08:46 +00006152 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6153 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6154 if (!result)
6155 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006156
6157#ifndef doubletime
6158#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6159#endif
6160
Victor Stinner8c62be82010-05-06 00:08:46 +00006161 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006162 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00006163 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006164 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006165#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006166 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6167 SET_INT(result, 2, ru->ru_maxrss);
6168 SET_INT(result, 3, ru->ru_ixrss);
6169 SET_INT(result, 4, ru->ru_idrss);
6170 SET_INT(result, 5, ru->ru_isrss);
6171 SET_INT(result, 6, ru->ru_minflt);
6172 SET_INT(result, 7, ru->ru_majflt);
6173 SET_INT(result, 8, ru->ru_nswap);
6174 SET_INT(result, 9, ru->ru_inblock);
6175 SET_INT(result, 10, ru->ru_oublock);
6176 SET_INT(result, 11, ru->ru_msgsnd);
6177 SET_INT(result, 12, ru->ru_msgrcv);
6178 SET_INT(result, 13, ru->ru_nsignals);
6179 SET_INT(result, 14, ru->ru_nvcsw);
6180 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006181#undef SET_INT
6182
Victor Stinner8c62be82010-05-06 00:08:46 +00006183 if (PyErr_Occurred()) {
6184 Py_DECREF(result);
6185 return NULL;
6186 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006187
Victor Stinner8c62be82010-05-06 00:08:46 +00006188 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006189}
6190#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6191
6192#ifdef HAVE_WAIT3
6193PyDoc_STRVAR(posix_wait3__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006194"wait3(options) -> (pid, status, rusage)\n\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006195Wait for completion of a child process.");
6196
6197static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006198posix_wait3(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006199{
Victor Stinner8c62be82010-05-06 00:08:46 +00006200 pid_t pid;
6201 int options;
6202 struct rusage ru;
6203 WAIT_TYPE status;
6204 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006205
Victor Stinner4195b5c2012-02-08 23:03:19 +01006206 if (!PyArg_ParseTuple(args, "i:wait3", &options))
Victor Stinner8c62be82010-05-06 00:08:46 +00006207 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006208
Victor Stinner8c62be82010-05-06 00:08:46 +00006209 Py_BEGIN_ALLOW_THREADS
6210 pid = wait3(&status, options, &ru);
6211 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006212
Victor Stinner4195b5c2012-02-08 23:03:19 +01006213 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006214}
6215#endif /* HAVE_WAIT3 */
6216
6217#ifdef HAVE_WAIT4
6218PyDoc_STRVAR(posix_wait4__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006219"wait4(pid, options) -> (pid, status, rusage)\n\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006220Wait for completion of a given child process.");
6221
6222static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006223posix_wait4(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006224{
Victor Stinner8c62be82010-05-06 00:08:46 +00006225 pid_t pid;
6226 int options;
6227 struct rusage ru;
6228 WAIT_TYPE status;
6229 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006230
Victor Stinner4195b5c2012-02-08 23:03:19 +01006231 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
Victor Stinner8c62be82010-05-06 00:08:46 +00006232 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006233
Victor Stinner8c62be82010-05-06 00:08:46 +00006234 Py_BEGIN_ALLOW_THREADS
6235 pid = wait4(pid, &status, options, &ru);
6236 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006237
Victor Stinner4195b5c2012-02-08 23:03:19 +01006238 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006239}
6240#endif /* HAVE_WAIT4 */
6241
Ross Lagerwall7807c352011-03-17 20:20:30 +02006242#if defined(HAVE_WAITID) && !defined(__APPLE__)
6243PyDoc_STRVAR(posix_waitid__doc__,
6244"waitid(idtype, id, options) -> waitid_result\n\n\
6245Wait for the completion of one or more child processes.\n\n\
6246idtype can be P_PID, P_PGID or P_ALL.\n\
6247id specifies the pid to wait on.\n\
6248options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6249or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6250Returns either waitid_result or None if WNOHANG is specified and there are\n\
6251no children in a waitable state.");
6252
6253static PyObject *
6254posix_waitid(PyObject *self, PyObject *args)
6255{
6256 PyObject *result;
6257 idtype_t idtype;
6258 id_t id;
6259 int options, res;
6260 siginfo_t si;
6261 si.si_pid = 0;
6262 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6263 return NULL;
6264 Py_BEGIN_ALLOW_THREADS
6265 res = waitid(idtype, id, &si, options);
6266 Py_END_ALLOW_THREADS
6267 if (res == -1)
6268 return posix_error();
6269
6270 if (si.si_pid == 0)
6271 Py_RETURN_NONE;
6272
6273 result = PyStructSequence_New(&WaitidResultType);
6274 if (!result)
6275 return NULL;
6276
6277 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6278 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6279 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6280 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6281 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6282 if (PyErr_Occurred()) {
6283 Py_DECREF(result);
6284 return NULL;
6285 }
6286
6287 return result;
6288}
6289#endif
6290
Guido van Rossumb6775db1994-08-01 11:34:53 +00006291#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006292PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006293"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006294Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006295
Barry Warsaw53699e91996-12-10 23:23:01 +00006296static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006297posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006298{
Victor Stinner8c62be82010-05-06 00:08:46 +00006299 pid_t pid;
6300 int options;
6301 WAIT_TYPE status;
6302 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006303
Victor Stinner8c62be82010-05-06 00:08:46 +00006304 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6305 return NULL;
6306 Py_BEGIN_ALLOW_THREADS
6307 pid = waitpid(pid, &status, options);
6308 Py_END_ALLOW_THREADS
6309 if (pid == -1)
6310 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006311
Victor Stinner8c62be82010-05-06 00:08:46 +00006312 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006313}
6314
Tim Petersab034fa2002-02-01 11:27:43 +00006315#elif defined(HAVE_CWAIT)
6316
6317/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006318PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006319"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006320"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006321
6322static PyObject *
6323posix_waitpid(PyObject *self, PyObject *args)
6324{
Victor Stinner8c62be82010-05-06 00:08:46 +00006325 Py_intptr_t pid;
6326 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006327
Victor Stinner8c62be82010-05-06 00:08:46 +00006328 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6329 return NULL;
6330 Py_BEGIN_ALLOW_THREADS
6331 pid = _cwait(&status, pid, options);
6332 Py_END_ALLOW_THREADS
6333 if (pid == -1)
6334 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006335
Victor Stinner8c62be82010-05-06 00:08:46 +00006336 /* shift the status left a byte so this is more like the POSIX waitpid */
6337 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006338}
6339#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006340
Guido van Rossumad0ee831995-03-01 10:34:45 +00006341#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006342PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006343"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006344Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006345
Barry Warsaw53699e91996-12-10 23:23:01 +00006346static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006347posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006348{
Victor Stinner8c62be82010-05-06 00:08:46 +00006349 pid_t pid;
6350 WAIT_TYPE status;
6351 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006352
Victor Stinner8c62be82010-05-06 00:08:46 +00006353 Py_BEGIN_ALLOW_THREADS
6354 pid = wait(&status);
6355 Py_END_ALLOW_THREADS
6356 if (pid == -1)
6357 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006358
Victor Stinner8c62be82010-05-06 00:08:46 +00006359 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006360}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006361#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006362
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006363
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006364PyDoc_STRVAR(posix_lstat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006365"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006366Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006367
Barry Warsaw53699e91996-12-10 23:23:01 +00006368static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006369posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006370{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006371#ifdef HAVE_LSTAT
Victor Stinner4195b5c2012-02-08 23:03:19 +01006372 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006373#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006374#ifdef MS_WINDOWS
Victor Stinner4195b5c2012-02-08 23:03:19 +01006375 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006376 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006377#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01006378 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006379#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006380#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006381}
6382
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006383
Guido van Rossumb6775db1994-08-01 11:34:53 +00006384#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006385PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006386"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006387Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006388
Barry Warsaw53699e91996-12-10 23:23:01 +00006389static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006390posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006391{
Victor Stinner8c62be82010-05-06 00:08:46 +00006392 PyObject* v;
6393 char buf[MAXPATHLEN];
6394 PyObject *opath;
6395 char *path;
6396 int n;
6397 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006398
Victor Stinner8c62be82010-05-06 00:08:46 +00006399 if (!PyArg_ParseTuple(args, "O&:readlink",
6400 PyUnicode_FSConverter, &opath))
6401 return NULL;
6402 path = PyBytes_AsString(opath);
6403 v = PySequence_GetItem(args, 0);
6404 if (v == NULL) {
6405 Py_DECREF(opath);
6406 return NULL;
6407 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006408
Victor Stinner8c62be82010-05-06 00:08:46 +00006409 if (PyUnicode_Check(v)) {
6410 arg_is_unicode = 1;
6411 }
6412 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006413
Victor Stinner8c62be82010-05-06 00:08:46 +00006414 Py_BEGIN_ALLOW_THREADS
6415 n = readlink(path, buf, (int) sizeof buf);
6416 Py_END_ALLOW_THREADS
6417 if (n < 0)
6418 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006419
Victor Stinner8c62be82010-05-06 00:08:46 +00006420 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006421 if (arg_is_unicode)
6422 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6423 else
6424 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006425}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006426#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006427
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006428
Brian Curtin52173d42010-12-02 18:29:18 +00006429#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006430PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006431"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006432Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006433
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006434static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006435posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006436{
Victor Stinner8c62be82010-05-06 00:08:46 +00006437 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006438}
6439#endif /* HAVE_SYMLINK */
6440
Brian Curtind40e6f72010-07-08 21:39:08 +00006441#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6442
6443PyDoc_STRVAR(win_readlink__doc__,
6444"readlink(path) -> path\n\n\
6445Return a string representing the path to which the symbolic link points.");
6446
Brian Curtind40e6f72010-07-08 21:39:08 +00006447/* Windows readlink implementation */
6448static PyObject *
6449win_readlink(PyObject *self, PyObject *args)
6450{
6451 wchar_t *path;
6452 DWORD n_bytes_returned;
6453 DWORD io_result;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006454 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00006455 HANDLE reparse_point_handle;
6456
6457 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6458 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6459 wchar_t *print_name;
6460
6461 if (!PyArg_ParseTuple(args,
Victor Stinnereb5657a2011-09-30 01:44:27 +02006462 "U:readlink",
6463 &po))
6464 return NULL;
6465 path = PyUnicode_AsUnicode(po);
6466 if (path == NULL)
Brian Curtind40e6f72010-07-08 21:39:08 +00006467 return NULL;
6468
6469 /* First get a handle to the reparse point */
6470 Py_BEGIN_ALLOW_THREADS
6471 reparse_point_handle = CreateFileW(
6472 path,
6473 0,
6474 0,
6475 0,
6476 OPEN_EXISTING,
6477 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6478 0);
6479 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006480
Brian Curtind40e6f72010-07-08 21:39:08 +00006481 if (reparse_point_handle==INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006482 return win32_error_object("readlink", po);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006483
Brian Curtind40e6f72010-07-08 21:39:08 +00006484 Py_BEGIN_ALLOW_THREADS
6485 /* New call DeviceIoControl to read the reparse point */
6486 io_result = DeviceIoControl(
6487 reparse_point_handle,
6488 FSCTL_GET_REPARSE_POINT,
6489 0, 0, /* in buffer */
6490 target_buffer, sizeof(target_buffer),
6491 &n_bytes_returned,
6492 0 /* we're not using OVERLAPPED_IO */
6493 );
6494 CloseHandle(reparse_point_handle);
6495 Py_END_ALLOW_THREADS
6496
6497 if (io_result==0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006498 return win32_error_object("readlink", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00006499
6500 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6501 {
6502 PyErr_SetString(PyExc_ValueError,
6503 "not a symbolic link");
6504 return NULL;
6505 }
Brian Curtin74e45612010-07-09 15:58:59 +00006506 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6507 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6508
6509 result = PyUnicode_FromWideChar(print_name,
6510 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006511 return result;
6512}
6513
6514#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6515
Brian Curtin52173d42010-12-02 18:29:18 +00006516#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006517
6518/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6519static int has_CreateSymbolicLinkW = 0;
6520static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6521static int
6522check_CreateSymbolicLinkW()
6523{
6524 HINSTANCE hKernel32;
6525 /* only recheck */
6526 if (has_CreateSymbolicLinkW)
6527 return has_CreateSymbolicLinkW;
Martin v. Löwis50590f12012-01-14 17:54:09 +01006528 hKernel32 = GetModuleHandleW(L"KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006529 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6530 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006531 if (Py_CreateSymbolicLinkW)
6532 has_CreateSymbolicLinkW = 1;
6533 return has_CreateSymbolicLinkW;
6534}
6535
6536PyDoc_STRVAR(win_symlink__doc__,
6537"symlink(src, dst, target_is_directory=False)\n\n\
6538Create a symbolic link pointing to src named dst.\n\
6539target_is_directory is required if the target is to be interpreted as\n\
6540a directory.\n\
6541This function requires Windows 6.0 or greater, and raises a\n\
6542NotImplementedError otherwise.");
6543
6544static PyObject *
6545win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6546{
6547 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006548 PyObject *osrc, *odest;
6549 PyObject *usrc = NULL, *udest = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006550 wchar_t *wsrc, *wdest;
Brian Curtind40e6f72010-07-08 21:39:08 +00006551 int target_is_directory = 0;
6552 DWORD res;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006553
Brian Curtind40e6f72010-07-08 21:39:08 +00006554 if (!check_CreateSymbolicLinkW())
6555 {
6556 /* raise NotImplementedError */
6557 return PyErr_Format(PyExc_NotImplementedError,
6558 "CreateSymbolicLinkW not found");
6559 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006560 if (!PyArg_ParseTupleAndKeywords(
6561 args, kwargs, "OO|i:symlink", kwlist,
6562 &osrc, &odest, &target_is_directory))
Brian Curtind40e6f72010-07-08 21:39:08 +00006563 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006564
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006565 usrc = win32_decode_filename(osrc);
6566 if (!usrc)
6567 return NULL;
6568 udest = win32_decode_filename(odest);
6569 if (!udest)
6570 goto error;
6571
Brian Curtin3b4499c2010-12-28 14:31:47 +00006572 if (win32_can_symlink == 0)
6573 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6574
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006575 wsrc = PyUnicode_AsUnicode(usrc);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006576 if (wsrc == NULL)
6577 goto error;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006578 wdest = PyUnicode_AsUnicode(udest);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006579 if (wsrc == NULL)
6580 goto error;
6581
Brian Curtind40e6f72010-07-08 21:39:08 +00006582 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006583 res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory);
Brian Curtind40e6f72010-07-08 21:39:08 +00006584 Py_END_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006585
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006586 Py_DECREF(usrc);
6587 Py_DECREF(udest);
Brian Curtind40e6f72010-07-08 21:39:08 +00006588 if (!res)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006589 return win32_error_object("symlink", osrc);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006590
Brian Curtind40e6f72010-07-08 21:39:08 +00006591 Py_INCREF(Py_None);
6592 return Py_None;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006593
6594error:
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006595 Py_XDECREF(usrc);
6596 Py_XDECREF(udest);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006597 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00006598}
Brian Curtin52173d42010-12-02 18:29:18 +00006599#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006600
6601#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006602#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6603static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006604system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006605{
6606 ULONG value = 0;
6607
6608 Py_BEGIN_ALLOW_THREADS
6609 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6610 Py_END_ALLOW_THREADS
6611
6612 return value;
6613}
6614
6615static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006616posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006617{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006618 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006619 return Py_BuildValue("ddddd",
6620 (double)0 /* t.tms_utime / HZ */,
6621 (double)0 /* t.tms_stime / HZ */,
6622 (double)0 /* t.tms_cutime / HZ */,
6623 (double)0 /* t.tms_cstime / HZ */,
6624 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006625}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006626#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006627#define NEED_TICKS_PER_SECOND
6628static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006629static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006630posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006631{
Victor Stinner8c62be82010-05-06 00:08:46 +00006632 struct tms t;
6633 clock_t c;
6634 errno = 0;
6635 c = times(&t);
6636 if (c == (clock_t) -1)
6637 return posix_error();
6638 return Py_BuildValue("ddddd",
6639 (double)t.tms_utime / ticks_per_second,
6640 (double)t.tms_stime / ticks_per_second,
6641 (double)t.tms_cutime / ticks_per_second,
6642 (double)t.tms_cstime / ticks_per_second,
6643 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006644}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006645#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006646#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006647
6648
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006649#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006650#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006651static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006652posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006653{
Victor Stinner8c62be82010-05-06 00:08:46 +00006654 FILETIME create, exit, kernel, user;
6655 HANDLE hProc;
6656 hProc = GetCurrentProcess();
6657 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6658 /* The fields of a FILETIME structure are the hi and lo part
6659 of a 64-bit value expressed in 100 nanosecond units.
6660 1e7 is one second in such units; 1e-7 the inverse.
6661 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6662 */
6663 return Py_BuildValue(
6664 "ddddd",
6665 (double)(user.dwHighDateTime*429.4967296 +
6666 user.dwLowDateTime*1e-7),
6667 (double)(kernel.dwHighDateTime*429.4967296 +
6668 kernel.dwLowDateTime*1e-7),
6669 (double)0,
6670 (double)0,
6671 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006672}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006673#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006674
6675#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006676PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006677"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006678Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006679#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006680
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006681
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006682#ifdef HAVE_GETSID
6683PyDoc_STRVAR(posix_getsid__doc__,
6684"getsid(pid) -> sid\n\n\
6685Call the system call getsid().");
6686
6687static PyObject *
6688posix_getsid(PyObject *self, PyObject *args)
6689{
Victor Stinner8c62be82010-05-06 00:08:46 +00006690 pid_t pid;
6691 int sid;
6692 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
6693 return NULL;
6694 sid = getsid(pid);
6695 if (sid < 0)
6696 return posix_error();
6697 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006698}
6699#endif /* HAVE_GETSID */
6700
6701
Guido van Rossumb6775db1994-08-01 11:34:53 +00006702#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006703PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006704"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006705Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006706
Barry Warsaw53699e91996-12-10 23:23:01 +00006707static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006708posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006709{
Victor Stinner8c62be82010-05-06 00:08:46 +00006710 if (setsid() < 0)
6711 return posix_error();
6712 Py_INCREF(Py_None);
6713 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006714}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006715#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006716
Guido van Rossumb6775db1994-08-01 11:34:53 +00006717#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006718PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006719"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006720Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006721
Barry Warsaw53699e91996-12-10 23:23:01 +00006722static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006723posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006724{
Victor Stinner8c62be82010-05-06 00:08:46 +00006725 pid_t pid;
6726 int pgrp;
6727 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6728 return NULL;
6729 if (setpgid(pid, pgrp) < 0)
6730 return posix_error();
6731 Py_INCREF(Py_None);
6732 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006733}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006734#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006735
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006736
Guido van Rossumb6775db1994-08-01 11:34:53 +00006737#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006738PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006739"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006740Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006741
Barry Warsaw53699e91996-12-10 23:23:01 +00006742static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006743posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006744{
Victor Stinner8c62be82010-05-06 00:08:46 +00006745 int fd;
6746 pid_t pgid;
6747 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6748 return NULL;
6749 pgid = tcgetpgrp(fd);
6750 if (pgid < 0)
6751 return posix_error();
6752 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006753}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006754#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006755
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006756
Guido van Rossumb6775db1994-08-01 11:34:53 +00006757#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006758PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006759"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006760Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006761
Barry Warsaw53699e91996-12-10 23:23:01 +00006762static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006763posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006764{
Victor Stinner8c62be82010-05-06 00:08:46 +00006765 int fd;
6766 pid_t pgid;
6767 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6768 return NULL;
6769 if (tcsetpgrp(fd, pgid) < 0)
6770 return posix_error();
6771 Py_INCREF(Py_None);
6772 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006773}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006774#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006775
Guido van Rossum687dd131993-05-17 08:34:16 +00006776/* Functions acting on file descriptors */
6777
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006778PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006779"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006780Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006781
Barry Warsaw53699e91996-12-10 23:23:01 +00006782static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006783posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006784{
Victor Stinner8c62be82010-05-06 00:08:46 +00006785 PyObject *ofile;
6786 char *file;
6787 int flag;
6788 int mode = 0777;
6789 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006790
6791#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006792 PyObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006793 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02006794 wchar_t *wpath = PyUnicode_AsUnicode(po);
6795 if (wpath == NULL)
6796 return NULL;
6797
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006799 fd = _wopen(wpath, flag, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00006800 Py_END_ALLOW_THREADS
6801 if (fd < 0)
6802 return posix_error();
6803 return PyLong_FromLong((long)fd);
6804 }
6805 /* Drop the argument parsing error as narrow strings
6806 are also valid. */
6807 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006808#endif
6809
Victor Stinner26de69d2011-06-17 15:15:38 +02006810 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006811 PyUnicode_FSConverter, &ofile,
6812 &flag, &mode))
6813 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006814#ifdef MS_WINDOWS
6815 if (win32_warn_bytes_api()) {
6816 Py_DECREF(ofile);
6817 return NULL;
6818 }
6819#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006820 file = PyBytes_AsString(ofile);
6821 Py_BEGIN_ALLOW_THREADS
6822 fd = open(file, flag, mode);
6823 Py_END_ALLOW_THREADS
6824 if (fd < 0)
6825 return posix_error_with_allocated_filename(ofile);
6826 Py_DECREF(ofile);
6827 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006828}
6829
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006830
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006831PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006832"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006833Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006834
Barry Warsaw53699e91996-12-10 23:23:01 +00006835static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006836posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006837{
Victor Stinner8c62be82010-05-06 00:08:46 +00006838 int fd, res;
6839 if (!PyArg_ParseTuple(args, "i:close", &fd))
6840 return NULL;
6841 if (!_PyVerify_fd(fd))
6842 return posix_error();
6843 Py_BEGIN_ALLOW_THREADS
6844 res = close(fd);
6845 Py_END_ALLOW_THREADS
6846 if (res < 0)
6847 return posix_error();
6848 Py_INCREF(Py_None);
6849 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006850}
6851
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006852
Victor Stinner8c62be82010-05-06 00:08:46 +00006853PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006854"closerange(fd_low, fd_high)\n\n\
6855Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6856
6857static PyObject *
6858posix_closerange(PyObject *self, PyObject *args)
6859{
Victor Stinner8c62be82010-05-06 00:08:46 +00006860 int fd_from, fd_to, i;
6861 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6862 return NULL;
6863 Py_BEGIN_ALLOW_THREADS
6864 for (i = fd_from; i < fd_to; i++)
6865 if (_PyVerify_fd(i))
6866 close(i);
6867 Py_END_ALLOW_THREADS
6868 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006869}
6870
6871
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006872PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006873"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006874Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006875
Barry Warsaw53699e91996-12-10 23:23:01 +00006876static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006877posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006878{
Victor Stinner8c62be82010-05-06 00:08:46 +00006879 int fd;
6880 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6881 return NULL;
6882 if (!_PyVerify_fd(fd))
6883 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006884 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006885 if (fd < 0)
6886 return posix_error();
6887 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006888}
6889
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006890
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006891PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006892"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006893Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006894
Barry Warsaw53699e91996-12-10 23:23:01 +00006895static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006896posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006897{
Victor Stinner8c62be82010-05-06 00:08:46 +00006898 int fd, fd2, res;
6899 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6900 return NULL;
6901 if (!_PyVerify_fd_dup2(fd, fd2))
6902 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006903 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006904 if (res < 0)
6905 return posix_error();
6906 Py_INCREF(Py_None);
6907 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006908}
6909
Ross Lagerwall7807c352011-03-17 20:20:30 +02006910#ifdef HAVE_LOCKF
6911PyDoc_STRVAR(posix_lockf__doc__,
6912"lockf(fd, cmd, len)\n\n\
6913Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6914fd is an open file descriptor.\n\
6915cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6916F_TEST.\n\
6917len specifies the section of the file to lock.");
6918
6919static PyObject *
6920posix_lockf(PyObject *self, PyObject *args)
6921{
6922 int fd, cmd, res;
6923 off_t len;
6924 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6925 &fd, &cmd, _parse_off_t, &len))
6926 return NULL;
6927
6928 Py_BEGIN_ALLOW_THREADS
6929 res = lockf(fd, cmd, len);
6930 Py_END_ALLOW_THREADS
6931
6932 if (res < 0)
6933 return posix_error();
6934
6935 Py_RETURN_NONE;
6936}
6937#endif
6938
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006939
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006940PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006941"lseek(fd, pos, how) -> newpos\n\n\
Victor Stinnere83f8992011-12-17 23:15:09 +01006942Set the current position of a file descriptor.\n\
6943Return the new cursor position in bytes, starting from the beginning.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006944
Barry Warsaw53699e91996-12-10 23:23:01 +00006945static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006946posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006947{
Victor Stinner8c62be82010-05-06 00:08:46 +00006948 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006949#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006950 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006951#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006952 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006953#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006954 PyObject *posobj;
6955 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006956 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006957#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6959 switch (how) {
6960 case 0: how = SEEK_SET; break;
6961 case 1: how = SEEK_CUR; break;
6962 case 2: how = SEEK_END; break;
6963 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006964#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006965
Ross Lagerwall8e749672011-03-17 21:54:07 +02006966#if !defined(HAVE_LARGEFILE_SUPPORT)
6967 pos = PyLong_AsLong(posobj);
6968#else
6969 pos = PyLong_AsLongLong(posobj);
6970#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006971 if (PyErr_Occurred())
6972 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006973
Victor Stinner8c62be82010-05-06 00:08:46 +00006974 if (!_PyVerify_fd(fd))
6975 return posix_error();
6976 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006977#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006978 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006979#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006980 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006981#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006982 Py_END_ALLOW_THREADS
6983 if (res < 0)
6984 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006985
6986#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006987 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006988#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006989 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006990#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006991}
6992
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006993
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006994PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006995"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006996Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006997
Barry Warsaw53699e91996-12-10 23:23:01 +00006998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006999posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007000{
Victor Stinner8c62be82010-05-06 00:08:46 +00007001 int fd, size;
7002 Py_ssize_t n;
7003 PyObject *buffer;
7004 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
7005 return NULL;
7006 if (size < 0) {
7007 errno = EINVAL;
7008 return posix_error();
7009 }
7010 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7011 if (buffer == NULL)
7012 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00007013 if (!_PyVerify_fd(fd)) {
7014 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00007016 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007017 Py_BEGIN_ALLOW_THREADS
7018 n = read(fd, PyBytes_AS_STRING(buffer), size);
7019 Py_END_ALLOW_THREADS
7020 if (n < 0) {
7021 Py_DECREF(buffer);
7022 return posix_error();
7023 }
7024 if (n != size)
7025 _PyBytes_Resize(&buffer, n);
7026 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00007027}
7028
Ross Lagerwall7807c352011-03-17 20:20:30 +02007029#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
7030 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007031static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007032iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
7033{
7034 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007035 Py_ssize_t blen, total = 0;
7036
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007037 *iov = PyMem_New(struct iovec, cnt);
7038 if (*iov == NULL) {
7039 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007040 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007041 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007042
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007043 *buf = PyMem_New(Py_buffer, cnt);
7044 if (*buf == NULL) {
7045 PyMem_Del(*iov);
7046 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007047 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007048 }
7049
7050 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007051 PyObject *item = PySequence_GetItem(seq, i);
7052 if (item == NULL)
7053 goto fail;
7054 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
7055 Py_DECREF(item);
7056 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007057 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007058 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007059 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007060 blen = (*buf)[i].len;
7061 (*iov)[i].iov_len = blen;
7062 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007063 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007064 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007065
7066fail:
7067 PyMem_Del(*iov);
7068 for (j = 0; j < i; j++) {
7069 PyBuffer_Release(&(*buf)[j]);
7070 }
7071 PyMem_Del(*buf);
7072 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007073}
7074
7075static void
7076iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
7077{
7078 int i;
7079 PyMem_Del(iov);
7080 for (i = 0; i < cnt; i++) {
7081 PyBuffer_Release(&buf[i]);
7082 }
7083 PyMem_Del(buf);
7084}
7085#endif
7086
Ross Lagerwall7807c352011-03-17 20:20:30 +02007087#ifdef HAVE_READV
7088PyDoc_STRVAR(posix_readv__doc__,
7089"readv(fd, buffers) -> bytesread\n\n\
7090Read from a file descriptor into a number of writable buffers. buffers\n\
7091is an arbitrary sequence of writable buffers.\n\
7092Returns the total number of bytes read.");
7093
7094static PyObject *
7095posix_readv(PyObject *self, PyObject *args)
7096{
7097 int fd, cnt;
7098 Py_ssize_t n;
7099 PyObject *seq;
7100 struct iovec *iov;
7101 Py_buffer *buf;
7102
7103 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
7104 return NULL;
7105 if (!PySequence_Check(seq)) {
7106 PyErr_SetString(PyExc_TypeError,
7107 "readv() arg 2 must be a sequence");
7108 return NULL;
7109 }
7110 cnt = PySequence_Size(seq);
7111
7112 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
7113 return NULL;
7114
7115 Py_BEGIN_ALLOW_THREADS
7116 n = readv(fd, iov, cnt);
7117 Py_END_ALLOW_THREADS
7118
7119 iov_cleanup(iov, buf, cnt);
7120 return PyLong_FromSsize_t(n);
7121}
7122#endif
7123
7124#ifdef HAVE_PREAD
7125PyDoc_STRVAR(posix_pread__doc__,
7126"pread(fd, buffersize, offset) -> string\n\n\
7127Read from a file descriptor, fd, at a position of offset. It will read up\n\
7128to buffersize number of bytes. The file offset remains unchanged.");
7129
7130static PyObject *
7131posix_pread(PyObject *self, PyObject *args)
7132{
7133 int fd, size;
7134 off_t offset;
7135 Py_ssize_t n;
7136 PyObject *buffer;
7137 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
7138 return NULL;
7139
7140 if (size < 0) {
7141 errno = EINVAL;
7142 return posix_error();
7143 }
7144 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7145 if (buffer == NULL)
7146 return NULL;
7147 if (!_PyVerify_fd(fd)) {
7148 Py_DECREF(buffer);
7149 return posix_error();
7150 }
7151 Py_BEGIN_ALLOW_THREADS
7152 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
7153 Py_END_ALLOW_THREADS
7154 if (n < 0) {
7155 Py_DECREF(buffer);
7156 return posix_error();
7157 }
7158 if (n != size)
7159 _PyBytes_Resize(&buffer, n);
7160 return buffer;
7161}
7162#endif
7163
7164PyDoc_STRVAR(posix_write__doc__,
7165"write(fd, string) -> byteswritten\n\n\
7166Write a string to a file descriptor.");
7167
7168static PyObject *
7169posix_write(PyObject *self, PyObject *args)
7170{
7171 Py_buffer pbuf;
7172 int fd;
7173 Py_ssize_t size, len;
7174
7175 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7176 return NULL;
7177 if (!_PyVerify_fd(fd)) {
7178 PyBuffer_Release(&pbuf);
7179 return posix_error();
7180 }
7181 len = pbuf.len;
7182 Py_BEGIN_ALLOW_THREADS
7183#if defined(MS_WIN64) || defined(MS_WINDOWS)
7184 if (len > INT_MAX)
7185 len = INT_MAX;
7186 size = write(fd, pbuf.buf, (int)len);
7187#else
7188 size = write(fd, pbuf.buf, len);
7189#endif
7190 Py_END_ALLOW_THREADS
7191 PyBuffer_Release(&pbuf);
7192 if (size < 0)
7193 return posix_error();
7194 return PyLong_FromSsize_t(size);
7195}
7196
7197#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007198PyDoc_STRVAR(posix_sendfile__doc__,
7199"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7200sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7201 -> byteswritten\n\
7202Copy nbytes bytes from file descriptor in to file descriptor out.");
7203
7204static PyObject *
7205posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7206{
7207 int in, out;
7208 Py_ssize_t ret;
7209 off_t offset;
7210
7211#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7212#ifndef __APPLE__
7213 Py_ssize_t len;
7214#endif
7215 PyObject *headers = NULL, *trailers = NULL;
7216 Py_buffer *hbuf, *tbuf;
7217 off_t sbytes;
7218 struct sf_hdtr sf;
7219 int flags = 0;
7220 sf.headers = NULL;
7221 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007222 static char *keywords[] = {"out", "in",
7223 "offset", "count",
7224 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007225
7226#ifdef __APPLE__
7227 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007228 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007229#else
7230 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007231 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007232#endif
7233 &headers, &trailers, &flags))
7234 return NULL;
7235 if (headers != NULL) {
7236 if (!PySequence_Check(headers)) {
7237 PyErr_SetString(PyExc_TypeError,
7238 "sendfile() headers must be a sequence or None");
7239 return NULL;
7240 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007241 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007242 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007243 if (sf.hdr_cnt > 0 &&
7244 !(i = iov_setup(&(sf.headers), &hbuf,
7245 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007246 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007247#ifdef __APPLE__
7248 sbytes += i;
7249#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007250 }
7251 }
7252 if (trailers != NULL) {
7253 if (!PySequence_Check(trailers)) {
7254 PyErr_SetString(PyExc_TypeError,
7255 "sendfile() trailers must be a sequence or None");
7256 return NULL;
7257 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007258 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007259 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007260 if (sf.trl_cnt > 0 &&
7261 !(i = iov_setup(&(sf.trailers), &tbuf,
7262 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007263 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007264#ifdef __APPLE__
7265 sbytes += i;
7266#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007267 }
7268 }
7269
7270 Py_BEGIN_ALLOW_THREADS
7271#ifdef __APPLE__
7272 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7273#else
7274 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7275#endif
7276 Py_END_ALLOW_THREADS
7277
7278 if (sf.headers != NULL)
7279 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7280 if (sf.trailers != NULL)
7281 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7282
7283 if (ret < 0) {
7284 if ((errno == EAGAIN) || (errno == EBUSY)) {
7285 if (sbytes != 0) {
7286 // some data has been sent
7287 goto done;
7288 }
7289 else {
7290 // no data has been sent; upper application is supposed
7291 // to retry on EAGAIN or EBUSY
7292 return posix_error();
7293 }
7294 }
7295 return posix_error();
7296 }
7297 goto done;
7298
7299done:
7300 #if !defined(HAVE_LARGEFILE_SUPPORT)
7301 return Py_BuildValue("l", sbytes);
7302 #else
7303 return Py_BuildValue("L", sbytes);
7304 #endif
7305
7306#else
7307 Py_ssize_t count;
7308 PyObject *offobj;
7309 static char *keywords[] = {"out", "in",
7310 "offset", "count", NULL};
7311 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7312 keywords, &out, &in, &offobj, &count))
7313 return NULL;
7314#ifdef linux
7315 if (offobj == Py_None) {
7316 Py_BEGIN_ALLOW_THREADS
7317 ret = sendfile(out, in, NULL, count);
7318 Py_END_ALLOW_THREADS
7319 if (ret < 0)
7320 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007321 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007322 }
7323#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007324 if (!_parse_off_t(offobj, &offset))
7325 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007326 Py_BEGIN_ALLOW_THREADS
7327 ret = sendfile(out, in, &offset, count);
7328 Py_END_ALLOW_THREADS
7329 if (ret < 0)
7330 return posix_error();
7331 return Py_BuildValue("n", ret);
7332#endif
7333}
7334#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007335
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007336PyDoc_STRVAR(posix_fstat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007337"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007338Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007339
Barry Warsaw53699e91996-12-10 23:23:01 +00007340static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007341posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007342{
Victor Stinner8c62be82010-05-06 00:08:46 +00007343 int fd;
7344 STRUCT_STAT st;
7345 int res;
Victor Stinner4195b5c2012-02-08 23:03:19 +01007346 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Victor Stinner8c62be82010-05-06 00:08:46 +00007347 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007348#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007349 /* on OpenVMS we must ensure that all bytes are written to the file */
7350 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007351#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007352 if (!_PyVerify_fd(fd))
7353 return posix_error();
7354 Py_BEGIN_ALLOW_THREADS
7355 res = FSTAT(fd, &st);
7356 Py_END_ALLOW_THREADS
7357 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007358#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007359 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007360#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007361 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007362#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007363 }
Tim Peters5aa91602002-01-30 05:46:57 +00007364
Victor Stinner4195b5c2012-02-08 23:03:19 +01007365 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007366}
7367
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007368PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007369"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007370Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007371connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007372
7373static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007374posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007375{
Victor Stinner8c62be82010-05-06 00:08:46 +00007376 int fd;
7377 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7378 return NULL;
7379 if (!_PyVerify_fd(fd))
7380 return PyBool_FromLong(0);
7381 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007382}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007383
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007384#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007385PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007386"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007387Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007388
Barry Warsaw53699e91996-12-10 23:23:01 +00007389static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007390posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007391{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007392#if defined(PYOS_OS2)
7393 HFILE read, write;
7394 APIRET rc;
7395
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007396 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007397 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007398 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007399
7400 return Py_BuildValue("(ii)", read, write);
7401#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007402#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007403 int fds[2];
7404 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007405 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007406 if (res != 0)
7407 return posix_error();
7408 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007409#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007410 HANDLE read, write;
7411 int read_fd, write_fd;
7412 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007413 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007414 if (!ok)
7415 return win32_error("CreatePipe", NULL);
7416 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7417 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7418 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007419#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007420#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007421}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007422#endif /* HAVE_PIPE */
7423
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007424#ifdef HAVE_PIPE2
7425PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007426"pipe2(flags) -> (read_end, write_end)\n\n\
7427Create a pipe with flags set atomically.\n\
7428flags can be constructed by ORing together one or more of these values:\n\
7429O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007430");
7431
7432static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007433posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007434{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007435 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007436 int fds[2];
7437 int res;
7438
Charles-François Natali368f34b2011-06-06 19:49:47 +02007439 flags = PyLong_AsLong(arg);
7440 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007441 return NULL;
7442
7443 res = pipe2(fds, flags);
7444 if (res != 0)
7445 return posix_error();
7446 return Py_BuildValue("(ii)", fds[0], fds[1]);
7447}
7448#endif /* HAVE_PIPE2 */
7449
Ross Lagerwall7807c352011-03-17 20:20:30 +02007450#ifdef HAVE_WRITEV
7451PyDoc_STRVAR(posix_writev__doc__,
7452"writev(fd, buffers) -> byteswritten\n\n\
7453Write the contents of buffers to a file descriptor, where buffers is an\n\
7454arbitrary sequence of buffers.\n\
7455Returns the total bytes written.");
7456
7457static PyObject *
7458posix_writev(PyObject *self, PyObject *args)
7459{
7460 int fd, cnt;
7461 Py_ssize_t res;
7462 PyObject *seq;
7463 struct iovec *iov;
7464 Py_buffer *buf;
7465 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7466 return NULL;
7467 if (!PySequence_Check(seq)) {
7468 PyErr_SetString(PyExc_TypeError,
7469 "writev() arg 2 must be a sequence");
7470 return NULL;
7471 }
7472 cnt = PySequence_Size(seq);
7473
7474 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7475 return NULL;
7476 }
7477
7478 Py_BEGIN_ALLOW_THREADS
7479 res = writev(fd, iov, cnt);
7480 Py_END_ALLOW_THREADS
7481
7482 iov_cleanup(iov, buf, cnt);
7483 return PyLong_FromSsize_t(res);
7484}
7485#endif
7486
7487#ifdef HAVE_PWRITE
7488PyDoc_STRVAR(posix_pwrite__doc__,
7489"pwrite(fd, string, offset) -> byteswritten\n\n\
7490Write string to a file descriptor, fd, from offset, leaving the file\n\
7491offset unchanged.");
7492
7493static PyObject *
7494posix_pwrite(PyObject *self, PyObject *args)
7495{
7496 Py_buffer pbuf;
7497 int fd;
7498 off_t offset;
7499 Py_ssize_t size;
7500
7501 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7502 return NULL;
7503
7504 if (!_PyVerify_fd(fd)) {
7505 PyBuffer_Release(&pbuf);
7506 return posix_error();
7507 }
7508 Py_BEGIN_ALLOW_THREADS
7509 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7510 Py_END_ALLOW_THREADS
7511 PyBuffer_Release(&pbuf);
7512 if (size < 0)
7513 return posix_error();
7514 return PyLong_FromSsize_t(size);
7515}
7516#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007517
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007518#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007519PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007520"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007521Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007522
Barry Warsaw53699e91996-12-10 23:23:01 +00007523static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007524posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007525{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007526 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007527 char *filename;
7528 int mode = 0666;
7529 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007530 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7531 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007532 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007533 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007534 Py_BEGIN_ALLOW_THREADS
7535 res = mkfifo(filename, mode);
7536 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007537 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007538 if (res < 0)
7539 return posix_error();
7540 Py_INCREF(Py_None);
7541 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007542}
7543#endif
7544
7545
Neal Norwitz11690112002-07-30 01:08:28 +00007546#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007547PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007548"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007549Create a filesystem node (file, device special file or named pipe)\n\
7550named filename. mode specifies both the permissions to use and the\n\
7551type of node to be created, being combined (bitwise OR) with one of\n\
7552S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007553device defines the newly created device special file (probably using\n\
7554os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007555
7556
7557static PyObject *
7558posix_mknod(PyObject *self, PyObject *args)
7559{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007560 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007561 char *filename;
7562 int mode = 0600;
7563 int device = 0;
7564 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007565 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7566 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007567 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007568 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007569 Py_BEGIN_ALLOW_THREADS
7570 res = mknod(filename, mode, device);
7571 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007572 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007573 if (res < 0)
7574 return posix_error();
7575 Py_INCREF(Py_None);
7576 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007577}
7578#endif
7579
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007580#ifdef HAVE_DEVICE_MACROS
7581PyDoc_STRVAR(posix_major__doc__,
7582"major(device) -> major number\n\
7583Extracts a device major number from a raw device number.");
7584
7585static PyObject *
7586posix_major(PyObject *self, PyObject *args)
7587{
Victor Stinner8c62be82010-05-06 00:08:46 +00007588 int device;
7589 if (!PyArg_ParseTuple(args, "i:major", &device))
7590 return NULL;
7591 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007592}
7593
7594PyDoc_STRVAR(posix_minor__doc__,
7595"minor(device) -> minor number\n\
7596Extracts a device minor number from a raw device number.");
7597
7598static PyObject *
7599posix_minor(PyObject *self, PyObject *args)
7600{
Victor Stinner8c62be82010-05-06 00:08:46 +00007601 int device;
7602 if (!PyArg_ParseTuple(args, "i:minor", &device))
7603 return NULL;
7604 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007605}
7606
7607PyDoc_STRVAR(posix_makedev__doc__,
7608"makedev(major, minor) -> device number\n\
7609Composes a raw device number from the major and minor device numbers.");
7610
7611static PyObject *
7612posix_makedev(PyObject *self, PyObject *args)
7613{
Victor Stinner8c62be82010-05-06 00:08:46 +00007614 int major, minor;
7615 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7616 return NULL;
7617 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007618}
7619#endif /* device macros */
7620
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007621
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007622#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007623PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007624"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007625Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007626
Barry Warsaw53699e91996-12-10 23:23:01 +00007627static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007628posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007629{
Victor Stinner8c62be82010-05-06 00:08:46 +00007630 int fd;
7631 off_t length;
7632 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007633
Ross Lagerwall7807c352011-03-17 20:20:30 +02007634 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00007635 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007636
Victor Stinner8c62be82010-05-06 00:08:46 +00007637 Py_BEGIN_ALLOW_THREADS
7638 res = ftruncate(fd, length);
7639 Py_END_ALLOW_THREADS
7640 if (res < 0)
7641 return posix_error();
7642 Py_INCREF(Py_None);
7643 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007644}
7645#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007646
Ross Lagerwall7807c352011-03-17 20:20:30 +02007647#ifdef HAVE_TRUNCATE
7648PyDoc_STRVAR(posix_truncate__doc__,
7649"truncate(path, length)\n\n\
7650Truncate the file given by path to length bytes.");
7651
7652static PyObject *
7653posix_truncate(PyObject *self, PyObject *args)
7654{
7655 PyObject *opath;
7656 const char *path;
7657 off_t length;
7658 int res;
7659
7660 if (!PyArg_ParseTuple(args, "O&O&:truncate",
7661 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
7662 return NULL;
7663 path = PyBytes_AsString(opath);
7664
7665 Py_BEGIN_ALLOW_THREADS
7666 res = truncate(path, length);
7667 Py_END_ALLOW_THREADS
7668 Py_DECREF(opath);
7669 if (res < 0)
7670 return posix_error();
7671 Py_RETURN_NONE;
7672}
7673#endif
7674
7675#ifdef HAVE_POSIX_FALLOCATE
7676PyDoc_STRVAR(posix_posix_fallocate__doc__,
7677"posix_fallocate(fd, offset, len)\n\n\
7678Ensures that enough disk space is allocated for the file specified by fd\n\
7679starting from offset and continuing for len bytes.");
7680
7681static PyObject *
7682posix_posix_fallocate(PyObject *self, PyObject *args)
7683{
7684 off_t len, offset;
7685 int res, fd;
7686
7687 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
7688 &fd, _parse_off_t, &offset, _parse_off_t, &len))
7689 return NULL;
7690
7691 Py_BEGIN_ALLOW_THREADS
7692 res = posix_fallocate(fd, offset, len);
7693 Py_END_ALLOW_THREADS
7694 if (res != 0) {
7695 errno = res;
7696 return posix_error();
7697 }
7698 Py_RETURN_NONE;
7699}
7700#endif
7701
7702#ifdef HAVE_POSIX_FADVISE
7703PyDoc_STRVAR(posix_posix_fadvise__doc__,
7704"posix_fadvise(fd, offset, len, advice)\n\n\
7705Announces an intention to access data in a specific pattern thus allowing\n\
7706the kernel to make optimizations.\n\
7707The advice applies to the region of the file specified by fd starting at\n\
7708offset and continuing for len bytes.\n\
7709advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
7710POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
7711POSIX_FADV_DONTNEED.");
7712
7713static PyObject *
7714posix_posix_fadvise(PyObject *self, PyObject *args)
7715{
7716 off_t len, offset;
7717 int res, fd, advice;
7718
7719 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
7720 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
7721 return NULL;
7722
7723 Py_BEGIN_ALLOW_THREADS
7724 res = posix_fadvise(fd, offset, len, advice);
7725 Py_END_ALLOW_THREADS
7726 if (res != 0) {
7727 errno = res;
7728 return posix_error();
7729 }
7730 Py_RETURN_NONE;
7731}
7732#endif
7733
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007734#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007735PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007736"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007737Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007738
Fred Drake762e2061999-08-26 17:23:54 +00007739/* Save putenv() parameters as values here, so we can collect them when they
7740 * get re-set with another call for the same key. */
7741static PyObject *posix_putenv_garbage;
7742
Tim Peters5aa91602002-01-30 05:46:57 +00007743static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007744posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007745{
Victor Stinner84ae1182010-05-06 22:05:07 +00007746 PyObject *newstr = NULL;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007747#ifdef MS_WINDOWS
Victor Stinner65170952011-11-22 22:16:17 +01007748 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007749 wchar_t *newenv;
Victor Stinner65170952011-11-22 22:16:17 +01007750
Victor Stinner8c62be82010-05-06 00:08:46 +00007751 if (!PyArg_ParseTuple(args,
Victor Stinner9d3b93b2011-11-22 02:27:30 +01007752 "UU:putenv",
Victor Stinner65170952011-11-22 22:16:17 +01007753 &os1, &os2))
Victor Stinner8c62be82010-05-06 00:08:46 +00007754 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007755
Victor Stinner65170952011-11-22 22:16:17 +01007756 newstr = PyUnicode_FromFormat("%U=%U", os1, os2);
Victor Stinner84ae1182010-05-06 22:05:07 +00007757 if (newstr == NULL) {
7758 PyErr_NoMemory();
7759 goto error;
7760 }
Victor Stinner65170952011-11-22 22:16:17 +01007761 if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) {
7762 PyErr_Format(PyExc_ValueError,
7763 "the environment variable is longer than %u characters",
7764 _MAX_ENV);
7765 goto error;
7766 }
7767
Victor Stinner8c62be82010-05-06 00:08:46 +00007768 newenv = PyUnicode_AsUnicode(newstr);
Victor Stinnereb5657a2011-09-30 01:44:27 +02007769 if (newenv == NULL)
7770 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007771 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007772 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007773 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007774 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007775#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007776 PyObject *os1, *os2;
7777 char *s1, *s2;
7778 char *newenv;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007779
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007780 if (!PyArg_ParseTuple(args,
7781 "O&O&:putenv",
7782 PyUnicode_FSConverter, &os1,
7783 PyUnicode_FSConverter, &os2))
7784 return NULL;
7785 s1 = PyBytes_AsString(os1);
7786 s2 = PyBytes_AsString(os2);
Guido van Rossumad0ee831995-03-01 10:34:45 +00007787
Victor Stinner65170952011-11-22 22:16:17 +01007788 newstr = PyBytes_FromFormat("%s=%s", s1, s2);
Victor Stinner9d3b93b2011-11-22 02:27:30 +01007789 if (newstr == NULL) {
7790 PyErr_NoMemory();
7791 goto error;
7792 }
7793
Victor Stinner8c62be82010-05-06 00:08:46 +00007794 newenv = PyBytes_AS_STRING(newstr);
Victor Stinner8c62be82010-05-06 00:08:46 +00007795 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007796 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007797 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007798 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007799#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007800
Victor Stinner8c62be82010-05-06 00:08:46 +00007801 /* Install the first arg and newstr in posix_putenv_garbage;
7802 * this will cause previous value to be collected. This has to
7803 * happen after the real putenv() call because the old value
7804 * was still accessible until then. */
Victor Stinner65170952011-11-22 22:16:17 +01007805 if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 /* really not much we can do; just leak */
7807 PyErr_Clear();
7808 }
7809 else {
7810 Py_DECREF(newstr);
7811 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007812
Martin v. Löwis011e8422009-05-05 04:43:17 +00007813#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007814 Py_DECREF(os1);
7815 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007816#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007817 Py_RETURN_NONE;
7818
7819error:
7820#ifndef MS_WINDOWS
7821 Py_DECREF(os1);
7822 Py_DECREF(os2);
7823#endif
7824 Py_XDECREF(newstr);
7825 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00007826}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007827#endif /* putenv */
7828
Guido van Rossumc524d952001-10-19 01:31:59 +00007829#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007830PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007831"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007832Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007833
7834static PyObject *
7835posix_unsetenv(PyObject *self, PyObject *args)
7836{
Victor Stinner65170952011-11-22 22:16:17 +01007837 PyObject *name;
Victor Stinner984890f2011-11-24 13:53:38 +01007838#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01007839 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01007840#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007841
7842 if (!PyArg_ParseTuple(args, "O&:unsetenv",
Guido van Rossumc524d952001-10-19 01:31:59 +00007843
Victor Stinner65170952011-11-22 22:16:17 +01007844 PyUnicode_FSConverter, &name))
Guido van Rossumc524d952001-10-19 01:31:59 +00007845 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00007846
Victor Stinner984890f2011-11-24 13:53:38 +01007847#ifdef HAVE_BROKEN_UNSETENV
7848 unsetenv(PyBytes_AS_STRING(name));
7849#else
Victor Stinner65170952011-11-22 22:16:17 +01007850 err = unsetenv(PyBytes_AS_STRING(name));
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06007851 if (err) {
Benjamin Petersone8eb0e82011-11-22 23:14:47 -06007852 Py_DECREF(name);
Victor Stinner60b385e2011-11-22 22:01:28 +01007853 return posix_error();
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06007854 }
Victor Stinner984890f2011-11-24 13:53:38 +01007855#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007856
Victor Stinner8c62be82010-05-06 00:08:46 +00007857 /* Remove the key from posix_putenv_garbage;
7858 * this will cause it to be collected. This has to
7859 * happen after the real unsetenv() call because the
7860 * old value was still accessible until then.
7861 */
Victor Stinner65170952011-11-22 22:16:17 +01007862 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007863 /* really not much we can do; just leak */
7864 PyErr_Clear();
7865 }
Victor Stinner65170952011-11-22 22:16:17 +01007866 Py_DECREF(name);
Victor Stinner84ae1182010-05-06 22:05:07 +00007867 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007868}
7869#endif /* unsetenv */
7870
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007871PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007872"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007873Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007874
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007875static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007876posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007877{
Victor Stinner8c62be82010-05-06 00:08:46 +00007878 int code;
7879 char *message;
7880 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7881 return NULL;
7882 message = strerror(code);
7883 if (message == NULL) {
7884 PyErr_SetString(PyExc_ValueError,
7885 "strerror() argument out of range");
7886 return NULL;
7887 }
Victor Stinner1b579672011-12-17 05:47:23 +01007888 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007889}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007890
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007891
Guido van Rossumc9641791998-08-04 15:26:23 +00007892#ifdef HAVE_SYS_WAIT_H
7893
Fred Drake106c1a02002-04-23 15:58:02 +00007894#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007895PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007896"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007897Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007898
7899static PyObject *
7900posix_WCOREDUMP(PyObject *self, PyObject *args)
7901{
Victor Stinner8c62be82010-05-06 00:08:46 +00007902 WAIT_TYPE status;
7903 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007904
Victor Stinner8c62be82010-05-06 00:08:46 +00007905 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7906 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007907
Victor Stinner8c62be82010-05-06 00:08:46 +00007908 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007909}
7910#endif /* WCOREDUMP */
7911
7912#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007913PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007914"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007915Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007916job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007917
7918static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007919posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007920{
Victor Stinner8c62be82010-05-06 00:08:46 +00007921 WAIT_TYPE status;
7922 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007923
Victor Stinner8c62be82010-05-06 00:08:46 +00007924 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7925 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007926
Victor Stinner8c62be82010-05-06 00:08:46 +00007927 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007928}
7929#endif /* WIFCONTINUED */
7930
Guido van Rossumc9641791998-08-04 15:26:23 +00007931#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007932PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007933"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007934Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007935
7936static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007937posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007938{
Victor Stinner8c62be82010-05-06 00:08:46 +00007939 WAIT_TYPE status;
7940 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007941
Victor Stinner8c62be82010-05-06 00:08:46 +00007942 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7943 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007944
Victor Stinner8c62be82010-05-06 00:08:46 +00007945 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007946}
7947#endif /* WIFSTOPPED */
7948
7949#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007950PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007951"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007952Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007953
7954static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007955posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007956{
Victor Stinner8c62be82010-05-06 00:08:46 +00007957 WAIT_TYPE status;
7958 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007959
Victor Stinner8c62be82010-05-06 00:08:46 +00007960 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7961 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007962
Victor Stinner8c62be82010-05-06 00:08:46 +00007963 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007964}
7965#endif /* WIFSIGNALED */
7966
7967#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007968PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007969"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007970Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007971system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007972
7973static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007974posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007975{
Victor Stinner8c62be82010-05-06 00:08:46 +00007976 WAIT_TYPE status;
7977 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007978
Victor Stinner8c62be82010-05-06 00:08:46 +00007979 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7980 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007981
Victor Stinner8c62be82010-05-06 00:08:46 +00007982 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007983}
7984#endif /* WIFEXITED */
7985
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007986#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007987PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007988"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007989Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007990
7991static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007992posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007993{
Victor Stinner8c62be82010-05-06 00:08:46 +00007994 WAIT_TYPE status;
7995 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007996
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7998 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007999
Victor Stinner8c62be82010-05-06 00:08:46 +00008000 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008001}
8002#endif /* WEXITSTATUS */
8003
8004#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008005PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008006"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008007Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008008value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008009
8010static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008011posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008012{
Victor Stinner8c62be82010-05-06 00:08:46 +00008013 WAIT_TYPE status;
8014 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008015
Victor Stinner8c62be82010-05-06 00:08:46 +00008016 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
8017 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008018
Victor Stinner8c62be82010-05-06 00:08:46 +00008019 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008020}
8021#endif /* WTERMSIG */
8022
8023#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008024PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008025"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008026Return the signal that stopped the process that provided\n\
8027the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008028
8029static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008030posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008031{
Victor Stinner8c62be82010-05-06 00:08:46 +00008032 WAIT_TYPE status;
8033 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008034
Victor Stinner8c62be82010-05-06 00:08:46 +00008035 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
8036 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008037
Victor Stinner8c62be82010-05-06 00:08:46 +00008038 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008039}
8040#endif /* WSTOPSIG */
8041
8042#endif /* HAVE_SYS_WAIT_H */
8043
8044
Thomas Wouters477c8d52006-05-27 19:21:47 +00008045#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00008046#ifdef _SCO_DS
8047/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
8048 needed definitions in sys/statvfs.h */
8049#define _SVID3
8050#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008051#include <sys/statvfs.h>
8052
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008053static PyObject*
8054_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008055 PyObject *v = PyStructSequence_New(&StatVFSResultType);
8056 if (v == NULL)
8057 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008058
8059#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008060 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8061 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8062 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
8063 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
8064 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
8065 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
8066 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
8067 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
8068 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8069 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008070#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008071 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8072 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8073 PyStructSequence_SET_ITEM(v, 2,
8074 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
8075 PyStructSequence_SET_ITEM(v, 3,
8076 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
8077 PyStructSequence_SET_ITEM(v, 4,
8078 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
8079 PyStructSequence_SET_ITEM(v, 5,
8080 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
8081 PyStructSequence_SET_ITEM(v, 6,
8082 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
8083 PyStructSequence_SET_ITEM(v, 7,
8084 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
8085 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8086 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008087#endif
8088
Victor Stinner8c62be82010-05-06 00:08:46 +00008089 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008090}
8091
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008092PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008093"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008094Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008095
8096static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008097posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008098{
Victor Stinner8c62be82010-05-06 00:08:46 +00008099 int fd, res;
8100 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008101
Victor Stinner8c62be82010-05-06 00:08:46 +00008102 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
8103 return NULL;
8104 Py_BEGIN_ALLOW_THREADS
8105 res = fstatvfs(fd, &st);
8106 Py_END_ALLOW_THREADS
8107 if (res != 0)
8108 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008109
Victor Stinner8c62be82010-05-06 00:08:46 +00008110 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008111}
Thomas Wouters477c8d52006-05-27 19:21:47 +00008112#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008113
8114
Thomas Wouters477c8d52006-05-27 19:21:47 +00008115#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008116#include <sys/statvfs.h>
8117
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008118PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008119"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008120Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008121
8122static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008123posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008124{
Victor Stinner6fa67772011-09-20 04:04:33 +02008125 PyObject *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00008126 int res;
8127 struct statvfs st;
Victor Stinner6fa67772011-09-20 04:04:33 +02008128 if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00008129 return NULL;
8130 Py_BEGIN_ALLOW_THREADS
Victor Stinner6fa67772011-09-20 04:04:33 +02008131 res = statvfs(PyBytes_AS_STRING(path), &st);
Victor Stinner8c62be82010-05-06 00:08:46 +00008132 Py_END_ALLOW_THREADS
Victor Stinner6fa67772011-09-20 04:04:33 +02008133 if (res != 0) {
8134 posix_error_with_filename(PyBytes_AS_STRING(path));
8135 Py_DECREF(path);
8136 return NULL;
8137 }
8138 Py_DECREF(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008139
Victor Stinner8c62be82010-05-06 00:08:46 +00008140 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008141}
8142#endif /* HAVE_STATVFS */
8143
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008144#ifdef MS_WINDOWS
8145PyDoc_STRVAR(win32__getdiskusage__doc__,
8146"_getdiskusage(path) -> (total, free)\n\n\
8147Return disk usage statistics about the given path as (total, free) tuple.");
8148
8149static PyObject *
8150win32__getdiskusage(PyObject *self, PyObject *args)
8151{
8152 BOOL retval;
8153 ULARGE_INTEGER _, total, free;
Victor Stinner6139c1b2011-11-09 22:14:14 +01008154 const wchar_t *path;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008155
Victor Stinner6139c1b2011-11-09 22:14:14 +01008156 if (! PyArg_ParseTuple(args, "u", &path))
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008157 return NULL;
8158
8159 Py_BEGIN_ALLOW_THREADS
Victor Stinner6139c1b2011-11-09 22:14:14 +01008160 retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008161 Py_END_ALLOW_THREADS
8162 if (retval == 0)
8163 return PyErr_SetFromWindowsErr(0);
8164
8165 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8166}
8167#endif
8168
8169
Fred Drakec9680921999-12-13 16:37:25 +00008170/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8171 * It maps strings representing configuration variable names to
8172 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008173 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008174 * rarely-used constants. There are three separate tables that use
8175 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008176 *
8177 * This code is always included, even if none of the interfaces that
8178 * need it are included. The #if hackery needed to avoid it would be
8179 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008180 */
8181struct constdef {
8182 char *name;
8183 long value;
8184};
8185
Fred Drake12c6e2d1999-12-14 21:25:03 +00008186static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008187conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008188 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008189{
Christian Heimes217cfd12007-12-02 14:31:20 +00008190 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008191 *valuep = PyLong_AS_LONG(arg);
8192 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008193 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008194 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008195 /* look up the value in the table using a binary search */
8196 size_t lo = 0;
8197 size_t mid;
8198 size_t hi = tablesize;
8199 int cmp;
8200 const char *confname;
8201 if (!PyUnicode_Check(arg)) {
8202 PyErr_SetString(PyExc_TypeError,
8203 "configuration names must be strings or integers");
8204 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008205 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008206 confname = _PyUnicode_AsString(arg);
8207 if (confname == NULL)
8208 return 0;
8209 while (lo < hi) {
8210 mid = (lo + hi) / 2;
8211 cmp = strcmp(confname, table[mid].name);
8212 if (cmp < 0)
8213 hi = mid;
8214 else if (cmp > 0)
8215 lo = mid + 1;
8216 else {
8217 *valuep = table[mid].value;
8218 return 1;
8219 }
8220 }
8221 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8222 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008224}
8225
8226
8227#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8228static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008229#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008230 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008231#endif
8232#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008233 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008234#endif
Fred Drakec9680921999-12-13 16:37:25 +00008235#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008236 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008237#endif
8238#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008239 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008240#endif
8241#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008242 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008243#endif
8244#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008245 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008246#endif
8247#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008248 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008249#endif
8250#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008251 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008252#endif
8253#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008254 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008255#endif
8256#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008257 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008258#endif
8259#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008260 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008261#endif
8262#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008263 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008264#endif
8265#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008266 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008267#endif
8268#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008269 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008270#endif
8271#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008272 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008273#endif
8274#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008275 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008276#endif
8277#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008278 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008279#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008280#ifdef _PC_ACL_ENABLED
8281 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8282#endif
8283#ifdef _PC_MIN_HOLE_SIZE
8284 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8285#endif
8286#ifdef _PC_ALLOC_SIZE_MIN
8287 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8288#endif
8289#ifdef _PC_REC_INCR_XFER_SIZE
8290 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8291#endif
8292#ifdef _PC_REC_MAX_XFER_SIZE
8293 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8294#endif
8295#ifdef _PC_REC_MIN_XFER_SIZE
8296 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8297#endif
8298#ifdef _PC_REC_XFER_ALIGN
8299 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8300#endif
8301#ifdef _PC_SYMLINK_MAX
8302 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8303#endif
8304#ifdef _PC_XATTR_ENABLED
8305 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8306#endif
8307#ifdef _PC_XATTR_EXISTS
8308 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8309#endif
8310#ifdef _PC_TIMESTAMP_RESOLUTION
8311 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8312#endif
Fred Drakec9680921999-12-13 16:37:25 +00008313};
8314
Fred Drakec9680921999-12-13 16:37:25 +00008315static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008316conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008317{
8318 return conv_confname(arg, valuep, posix_constants_pathconf,
8319 sizeof(posix_constants_pathconf)
8320 / sizeof(struct constdef));
8321}
8322#endif
8323
8324#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008325PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008326"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008327Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008328If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008329
8330static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008331posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008332{
8333 PyObject *result = NULL;
8334 int name, fd;
8335
Fred Drake12c6e2d1999-12-14 21:25:03 +00008336 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8337 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008338 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008339
Stefan Krah0e803b32010-11-26 16:16:47 +00008340 errno = 0;
8341 limit = fpathconf(fd, name);
8342 if (limit == -1 && errno != 0)
8343 posix_error();
8344 else
8345 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008346 }
8347 return result;
8348}
8349#endif
8350
8351
8352#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008353PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008354"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008355Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008356If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008357
8358static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008359posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008360{
8361 PyObject *result = NULL;
8362 int name;
8363 char *path;
8364
8365 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8366 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008367 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008368
Victor Stinner8c62be82010-05-06 00:08:46 +00008369 errno = 0;
8370 limit = pathconf(path, name);
8371 if (limit == -1 && errno != 0) {
8372 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008373 /* could be a path or name problem */
8374 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008375 else
Stefan Krah99439262010-11-26 12:58:05 +00008376 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008377 }
8378 else
8379 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008380 }
8381 return result;
8382}
8383#endif
8384
8385#ifdef HAVE_CONFSTR
8386static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008387#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008388 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008389#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008390#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008391 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008392#endif
8393#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008394 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008395#endif
Fred Draked86ed291999-12-15 15:34:33 +00008396#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008397 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008398#endif
8399#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008400 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008401#endif
8402#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008403 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008404#endif
8405#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008406 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008407#endif
Fred Drakec9680921999-12-13 16:37:25 +00008408#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008409 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008410#endif
8411#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008412 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008413#endif
8414#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008415 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008416#endif
8417#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008418 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008419#endif
8420#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008421 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008422#endif
8423#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008424 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008425#endif
8426#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008427 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008428#endif
8429#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008430 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008431#endif
Fred Draked86ed291999-12-15 15:34:33 +00008432#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008433 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008434#endif
Fred Drakec9680921999-12-13 16:37:25 +00008435#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008436 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008437#endif
Fred Draked86ed291999-12-15 15:34:33 +00008438#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008439 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008440#endif
8441#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008442 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008443#endif
8444#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008445 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008446#endif
8447#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008448 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008449#endif
Fred Drakec9680921999-12-13 16:37:25 +00008450#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008451 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008452#endif
8453#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008454 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008455#endif
8456#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008457 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008458#endif
8459#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008460 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008461#endif
8462#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008463 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008464#endif
8465#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008466 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008467#endif
8468#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008469 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008470#endif
8471#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008472 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008473#endif
8474#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008475 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008476#endif
8477#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008478 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008479#endif
8480#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008481 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008482#endif
8483#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008484 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008485#endif
8486#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008487 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008488#endif
8489#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008490 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008491#endif
8492#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008493 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008494#endif
8495#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008496 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008497#endif
Fred Draked86ed291999-12-15 15:34:33 +00008498#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008499 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008500#endif
8501#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008502 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008503#endif
8504#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008505 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008506#endif
8507#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008508 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008509#endif
8510#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008511 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008512#endif
8513#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008514 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008515#endif
8516#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008517 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008518#endif
8519#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008520 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008521#endif
8522#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008523 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008524#endif
8525#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008526 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008527#endif
8528#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008529 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008530#endif
8531#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008532 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008533#endif
8534#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008535 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008536#endif
Fred Drakec9680921999-12-13 16:37:25 +00008537};
8538
8539static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008540conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008541{
8542 return conv_confname(arg, valuep, posix_constants_confstr,
8543 sizeof(posix_constants_confstr)
8544 / sizeof(struct constdef));
8545}
8546
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008547PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008548"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008549Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008550
8551static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008552posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008553{
8554 PyObject *result = NULL;
8555 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008556 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008557 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008558
Victor Stinnercb043522010-09-10 23:49:04 +00008559 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8560 return NULL;
8561
8562 errno = 0;
8563 len = confstr(name, buffer, sizeof(buffer));
8564 if (len == 0) {
8565 if (errno) {
8566 posix_error();
8567 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008568 }
8569 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008570 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008571 }
8572 }
Victor Stinnercb043522010-09-10 23:49:04 +00008573
8574 if ((unsigned int)len >= sizeof(buffer)) {
8575 char *buf = PyMem_Malloc(len);
8576 if (buf == NULL)
8577 return PyErr_NoMemory();
8578 confstr(name, buf, len);
8579 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8580 PyMem_Free(buf);
8581 }
8582 else
8583 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008584 return result;
8585}
8586#endif
8587
8588
8589#ifdef HAVE_SYSCONF
8590static struct constdef posix_constants_sysconf[] = {
8591#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008592 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008593#endif
8594#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008595 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008596#endif
8597#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008598 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008599#endif
8600#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008601 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008602#endif
8603#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008604 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008605#endif
8606#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008607 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008608#endif
8609#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008610 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008611#endif
8612#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008613 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008614#endif
8615#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008616 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008617#endif
8618#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008619 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008620#endif
Fred Draked86ed291999-12-15 15:34:33 +00008621#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008622 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008623#endif
8624#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008625 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008626#endif
Fred Drakec9680921999-12-13 16:37:25 +00008627#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008628 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008629#endif
Fred Drakec9680921999-12-13 16:37:25 +00008630#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008631 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008632#endif
8633#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008634 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008635#endif
8636#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008637 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008638#endif
8639#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008640 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008641#endif
8642#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008643 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008644#endif
Fred Draked86ed291999-12-15 15:34:33 +00008645#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008646 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008647#endif
Fred Drakec9680921999-12-13 16:37:25 +00008648#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008649 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008650#endif
8651#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008652 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008653#endif
8654#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008655 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008656#endif
8657#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008658 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008659#endif
8660#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008661 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008662#endif
Fred Draked86ed291999-12-15 15:34:33 +00008663#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00008664 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008665#endif
Fred Drakec9680921999-12-13 16:37:25 +00008666#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008667 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008668#endif
8669#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008670 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008671#endif
8672#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008673 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008674#endif
8675#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008676 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008677#endif
8678#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008679 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008680#endif
8681#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008682 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008683#endif
8684#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008685 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008686#endif
8687#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008688 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008689#endif
8690#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008691 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008692#endif
8693#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008694 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008695#endif
8696#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008697 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008698#endif
8699#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008700 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008701#endif
8702#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008703 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008704#endif
8705#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008706 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008707#endif
8708#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008709 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008710#endif
8711#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008712 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008713#endif
8714#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008715 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008716#endif
8717#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008718 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008719#endif
8720#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008721 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008722#endif
8723#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008724 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008725#endif
8726#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008727 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008728#endif
8729#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008730 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008731#endif
8732#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008733 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008734#endif
Fred Draked86ed291999-12-15 15:34:33 +00008735#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008736 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008737#endif
Fred Drakec9680921999-12-13 16:37:25 +00008738#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008739 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008740#endif
8741#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008742 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008743#endif
8744#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008745 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008746#endif
Fred Draked86ed291999-12-15 15:34:33 +00008747#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008748 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008749#endif
Fred Drakec9680921999-12-13 16:37:25 +00008750#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008751 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008752#endif
Fred Draked86ed291999-12-15 15:34:33 +00008753#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008754 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008755#endif
8756#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008757 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008758#endif
Fred Drakec9680921999-12-13 16:37:25 +00008759#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008760 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008761#endif
8762#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008763 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008764#endif
8765#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008766 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008767#endif
8768#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008769 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008770#endif
Fred Draked86ed291999-12-15 15:34:33 +00008771#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008772 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008773#endif
Fred Drakec9680921999-12-13 16:37:25 +00008774#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008775 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008776#endif
8777#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008778 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008779#endif
8780#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008781 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008782#endif
8783#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008784 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008785#endif
8786#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008787 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008788#endif
8789#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008790 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008791#endif
8792#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008793 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008794#endif
Fred Draked86ed291999-12-15 15:34:33 +00008795#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008796 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008797#endif
Fred Drakec9680921999-12-13 16:37:25 +00008798#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008799 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008800#endif
8801#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008802 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008803#endif
Fred Draked86ed291999-12-15 15:34:33 +00008804#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008805 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008806#endif
Fred Drakec9680921999-12-13 16:37:25 +00008807#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008808 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008809#endif
8810#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008811 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008812#endif
8813#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008814 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008815#endif
8816#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008817 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008818#endif
8819#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008820 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008821#endif
8822#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008823 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008824#endif
8825#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008826 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008827#endif
8828#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008829 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008830#endif
8831#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008832 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008833#endif
Fred Draked86ed291999-12-15 15:34:33 +00008834#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008835 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008836#endif
8837#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008838 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008839#endif
Fred Drakec9680921999-12-13 16:37:25 +00008840#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008841 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008842#endif
8843#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008844 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008845#endif
8846#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008847 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008848#endif
8849#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008850 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008851#endif
8852#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008853 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008854#endif
8855#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008856 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008857#endif
8858#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008859 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008860#endif
8861#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008862 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008863#endif
8864#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008865 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008866#endif
8867#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008868 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008869#endif
8870#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008871 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008872#endif
8873#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008874 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008875#endif
8876#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008877 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008878#endif
8879#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008880 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008881#endif
8882#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008883 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008884#endif
8885#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008886 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008887#endif
8888#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008889 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008890#endif
8891#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008892 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008893#endif
8894#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008895 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008896#endif
8897#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008898 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008899#endif
8900#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008901 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008902#endif
8903#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008904 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008905#endif
8906#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008907 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008908#endif
8909#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008910 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008911#endif
8912#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008913 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008914#endif
8915#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008916 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008917#endif
8918#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008919 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008920#endif
8921#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008922 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008923#endif
8924#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008925 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008926#endif
8927#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008928 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008929#endif
8930#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008931 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008932#endif
8933#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008934 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008935#endif
8936#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008937 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008938#endif
8939#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008940 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008941#endif
8942#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008943 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008944#endif
Fred Draked86ed291999-12-15 15:34:33 +00008945#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008946 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008947#endif
Fred Drakec9680921999-12-13 16:37:25 +00008948#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008949 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008950#endif
8951#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008952 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008953#endif
8954#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008955 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008956#endif
8957#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008958 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008959#endif
8960#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008961 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008962#endif
8963#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008964 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008965#endif
8966#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008967 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008968#endif
8969#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008970 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008971#endif
8972#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008973 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008974#endif
8975#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008976 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008977#endif
8978#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008979 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008980#endif
8981#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008982 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008983#endif
8984#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008985 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008986#endif
8987#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008988 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008989#endif
8990#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008991 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008992#endif
8993#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008994 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008995#endif
8996#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008997 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008998#endif
8999#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009000 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00009001#endif
9002#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009003 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009004#endif
9005#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009006 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009007#endif
9008#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009009 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009010#endif
9011#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009012 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009013#endif
9014#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009015 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009016#endif
9017#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009018 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009019#endif
9020#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00009021 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00009022#endif
9023#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009024 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009025#endif
9026#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009027 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009028#endif
9029#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009030 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009031#endif
9032#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009033 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009034#endif
9035#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00009036 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00009037#endif
9038#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009039 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009040#endif
9041#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00009042 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00009043#endif
9044#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009045 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009046#endif
9047#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00009048 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00009049#endif
9050#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00009051 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00009052#endif
9053#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00009054 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00009055#endif
9056#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009057 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00009058#endif
9059#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009060 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009061#endif
9062#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00009063 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00009064#endif
9065#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00009066 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00009067#endif
9068#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009069 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009070#endif
9071#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009072 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009073#endif
9074#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00009075 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00009076#endif
9077#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00009078 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00009079#endif
9080#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00009081 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00009082#endif
9083};
9084
9085static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009086conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009087{
9088 return conv_confname(arg, valuep, posix_constants_sysconf,
9089 sizeof(posix_constants_sysconf)
9090 / sizeof(struct constdef));
9091}
9092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009093PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009094"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009095Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009096
9097static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009098posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009099{
9100 PyObject *result = NULL;
9101 int name;
9102
9103 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
9104 int value;
9105
9106 errno = 0;
9107 value = sysconf(name);
9108 if (value == -1 && errno != 0)
9109 posix_error();
9110 else
Christian Heimes217cfd12007-12-02 14:31:20 +00009111 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00009112 }
9113 return result;
9114}
9115#endif
9116
9117
Fred Drakebec628d1999-12-15 18:31:10 +00009118/* This code is used to ensure that the tables of configuration value names
9119 * are in sorted order as required by conv_confname(), and also to build the
9120 * the exported dictionaries that are used to publish information about the
9121 * names available on the host platform.
9122 *
9123 * Sorting the table at runtime ensures that the table is properly ordered
9124 * when used, even for platforms we're not able to test on. It also makes
9125 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009126 */
Fred Drakebec628d1999-12-15 18:31:10 +00009127
9128static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009129cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009130{
9131 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009132 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009133 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009134 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009135
9136 return strcmp(c1->name, c2->name);
9137}
9138
9139static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009140setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009141 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009142{
Fred Drakebec628d1999-12-15 18:31:10 +00009143 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009144 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009145
9146 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9147 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009148 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009149 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009150
Barry Warsaw3155db32000-04-13 15:20:40 +00009151 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009152 PyObject *o = PyLong_FromLong(table[i].value);
9153 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9154 Py_XDECREF(o);
9155 Py_DECREF(d);
9156 return -1;
9157 }
9158 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009159 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009160 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009161}
9162
Fred Drakebec628d1999-12-15 18:31:10 +00009163/* Return -1 on failure, 0 on success. */
9164static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009165setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009166{
9167#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009168 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009169 sizeof(posix_constants_pathconf)
9170 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009171 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009172 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009173#endif
9174#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009175 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009176 sizeof(posix_constants_confstr)
9177 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009178 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009179 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009180#endif
9181#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009182 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009183 sizeof(posix_constants_sysconf)
9184 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009185 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009186 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009187#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009188 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009189}
Fred Draked86ed291999-12-15 15:34:33 +00009190
9191
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009192PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009193"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009194Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009195in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009196
9197static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009198posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009199{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009200 abort();
9201 /*NOTREACHED*/
9202 Py_FatalError("abort() called from Python code didn't abort!");
9203 return NULL;
9204}
Fred Drakebec628d1999-12-15 18:31:10 +00009205
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009206#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009207PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009208"startfile(filepath [, operation]) - Start a file with its associated\n\
9209application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009210\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009211When \"operation\" is not specified or \"open\", this acts like\n\
9212double-clicking the file in Explorer, or giving the file name as an\n\
9213argument to the DOS \"start\" command: the file is opened with whatever\n\
9214application (if any) its extension is associated.\n\
9215When another \"operation\" is given, it specifies what should be done with\n\
9216the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009217\n\
9218startfile returns as soon as the associated application is launched.\n\
9219There is no option to wait for the application to close, and no way\n\
9220to retrieve the application's exit status.\n\
9221\n\
9222The filepath is relative to the current directory. If you want to use\n\
9223an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009224the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009225
9226static PyObject *
9227win32_startfile(PyObject *self, PyObject *args)
9228{
Victor Stinner8c62be82010-05-06 00:08:46 +00009229 PyObject *ofilepath;
9230 char *filepath;
9231 char *operation = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02009232 wchar_t *wpath, *woperation;
Victor Stinner8c62be82010-05-06 00:08:46 +00009233 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009234
Victor Stinnereb5657a2011-09-30 01:44:27 +02009235 PyObject *unipath, *uoperation = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009236 if (!PyArg_ParseTuple(args, "U|s:startfile",
9237 &unipath, &operation)) {
9238 PyErr_Clear();
9239 goto normal;
9240 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009241
Victor Stinner8c62be82010-05-06 00:08:46 +00009242 if (operation) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009243 uoperation = PyUnicode_DecodeASCII(operation,
Victor Stinner8c62be82010-05-06 00:08:46 +00009244 strlen(operation), NULL);
Victor Stinnereb5657a2011-09-30 01:44:27 +02009245 if (!uoperation) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009246 PyErr_Clear();
9247 operation = NULL;
9248 goto normal;
9249 }
9250 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009251
Victor Stinnereb5657a2011-09-30 01:44:27 +02009252 wpath = PyUnicode_AsUnicode(unipath);
9253 if (wpath == NULL)
9254 goto normal;
9255 if (uoperation) {
9256 woperation = PyUnicode_AsUnicode(uoperation);
9257 if (woperation == NULL)
9258 goto normal;
9259 }
9260 else
9261 woperation = NULL;
9262
Victor Stinner8c62be82010-05-06 00:08:46 +00009263 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02009264 rc = ShellExecuteW((HWND)0, woperation, wpath,
9265 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +00009266 Py_END_ALLOW_THREADS
9267
Victor Stinnereb5657a2011-09-30 01:44:27 +02009268 Py_XDECREF(uoperation);
Victor Stinner8c62be82010-05-06 00:08:46 +00009269 if (rc <= (HINSTANCE)32) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009270 win32_error_object("startfile", unipath);
9271 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009272 }
9273 Py_INCREF(Py_None);
9274 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009275
9276normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009277 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9278 PyUnicode_FSConverter, &ofilepath,
9279 &operation))
9280 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01009281 if (win32_warn_bytes_api()) {
9282 Py_DECREF(ofilepath);
9283 return NULL;
9284 }
Victor Stinner8c62be82010-05-06 00:08:46 +00009285 filepath = PyBytes_AsString(ofilepath);
9286 Py_BEGIN_ALLOW_THREADS
9287 rc = ShellExecute((HWND)0, operation, filepath,
9288 NULL, NULL, SW_SHOWNORMAL);
9289 Py_END_ALLOW_THREADS
9290 if (rc <= (HINSTANCE)32) {
9291 PyObject *errval = win32_error("startfile", filepath);
9292 Py_DECREF(ofilepath);
9293 return errval;
9294 }
9295 Py_DECREF(ofilepath);
9296 Py_INCREF(Py_None);
9297 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009298}
9299#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009300
Martin v. Löwis438b5342002-12-27 10:16:42 +00009301#ifdef HAVE_GETLOADAVG
9302PyDoc_STRVAR(posix_getloadavg__doc__,
9303"getloadavg() -> (float, float, float)\n\n\
9304Return the number of processes in the system run queue averaged over\n\
9305the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9306was unobtainable");
9307
9308static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009309posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009310{
9311 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009312 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009313 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9314 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009315 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009316 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009317}
9318#endif
9319
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009320#ifdef MS_WINDOWS
9321
9322PyDoc_STRVAR(win32_urandom__doc__,
9323"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009324Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009325
9326typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
9327 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
9328 DWORD dwFlags );
9329typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
9330 BYTE *pbBuffer );
9331
9332static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00009333/* This handle is never explicitly released. Instead, the operating
9334 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009335static HCRYPTPROV hCryptProv = 0;
9336
Tim Peters4ad82172004-08-30 17:02:04 +00009337static PyObject*
9338win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009339{
Victor Stinner8c62be82010-05-06 00:08:46 +00009340 int howMany;
9341 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009342
Victor Stinner8c62be82010-05-06 00:08:46 +00009343 /* Read arguments */
9344 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9345 return NULL;
9346 if (howMany < 0)
9347 return PyErr_Format(PyExc_ValueError,
9348 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009349
Victor Stinner8c62be82010-05-06 00:08:46 +00009350 if (hCryptProv == 0) {
9351 HINSTANCE hAdvAPI32 = NULL;
9352 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009353
Victor Stinner8c62be82010-05-06 00:08:46 +00009354 /* Obtain handle to the DLL containing CryptoAPI
9355 This should not fail */
Martin v. Löwis50590f12012-01-14 17:54:09 +01009356 hAdvAPI32 = GetModuleHandleW(L"advapi32.dll");
Victor Stinner8c62be82010-05-06 00:08:46 +00009357 if(hAdvAPI32 == NULL)
9358 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009359
Victor Stinner8c62be82010-05-06 00:08:46 +00009360 /* Obtain pointers to the CryptoAPI functions
9361 This will fail on some early versions of Win95 */
9362 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
9363 hAdvAPI32,
9364 "CryptAcquireContextA");
9365 if (pCryptAcquireContext == NULL)
9366 return PyErr_Format(PyExc_NotImplementedError,
9367 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009368
Victor Stinner8c62be82010-05-06 00:08:46 +00009369 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
9370 hAdvAPI32, "CryptGenRandom");
9371 if (pCryptGenRandom == NULL)
9372 return PyErr_Format(PyExc_NotImplementedError,
9373 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009374
Victor Stinner8c62be82010-05-06 00:08:46 +00009375 /* Acquire context */
9376 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
9377 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
9378 return win32_error("CryptAcquireContext", NULL);
9379 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009380
Victor Stinner8c62be82010-05-06 00:08:46 +00009381 /* Allocate bytes */
9382 result = PyBytes_FromStringAndSize(NULL, howMany);
9383 if (result != NULL) {
9384 /* Get random data */
9385 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
9386 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
9387 PyBytes_AS_STRING(result))) {
9388 Py_DECREF(result);
9389 return win32_error("CryptGenRandom", NULL);
9390 }
9391 }
9392 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009393}
9394#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009395
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009396PyDoc_STRVAR(device_encoding__doc__,
9397"device_encoding(fd) -> str\n\n\
9398Return a string describing the encoding of the device\n\
9399if the output is a terminal; else return None.");
9400
9401static PyObject *
9402device_encoding(PyObject *self, PyObject *args)
9403{
Victor Stinner8c62be82010-05-06 00:08:46 +00009404 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02009405#if defined(MS_WINDOWS) || defined(MS_WIN64)
9406 UINT cp;
9407#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009408 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9409 return NULL;
9410 if (!_PyVerify_fd(fd) || !isatty(fd)) {
9411 Py_INCREF(Py_None);
9412 return Py_None;
9413 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009414#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02009415 if (fd == 0)
9416 cp = GetConsoleCP();
9417 else if (fd == 1 || fd == 2)
9418 cp = GetConsoleOutputCP();
9419 else
9420 cp = 0;
9421 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
9422 has no console */
9423 if (cp != 0)
9424 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009425#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00009426 {
9427 char *codeset = nl_langinfo(CODESET);
9428 if (codeset != NULL && codeset[0] != 0)
9429 return PyUnicode_FromString(codeset);
9430 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009431#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009432 Py_INCREF(Py_None);
9433 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009434}
9435
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009436#ifdef __VMS
9437/* Use openssl random routine */
9438#include <openssl/rand.h>
9439PyDoc_STRVAR(vms_urandom__doc__,
9440"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009441Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009442
9443static PyObject*
9444vms_urandom(PyObject *self, PyObject *args)
9445{
Victor Stinner8c62be82010-05-06 00:08:46 +00009446 int howMany;
9447 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009448
Victor Stinner8c62be82010-05-06 00:08:46 +00009449 /* Read arguments */
9450 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9451 return NULL;
9452 if (howMany < 0)
9453 return PyErr_Format(PyExc_ValueError,
9454 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009455
Victor Stinner8c62be82010-05-06 00:08:46 +00009456 /* Allocate bytes */
9457 result = PyBytes_FromStringAndSize(NULL, howMany);
9458 if (result != NULL) {
9459 /* Get random data */
9460 if (RAND_pseudo_bytes((unsigned char*)
9461 PyBytes_AS_STRING(result),
9462 howMany) < 0) {
9463 Py_DECREF(result);
9464 return PyErr_Format(PyExc_ValueError,
9465 "RAND_pseudo_bytes");
9466 }
9467 }
9468 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009469}
9470#endif
9471
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009472#ifdef HAVE_SETRESUID
9473PyDoc_STRVAR(posix_setresuid__doc__,
9474"setresuid(ruid, euid, suid)\n\n\
9475Set the current process's real, effective, and saved user ids.");
9476
9477static PyObject*
9478posix_setresuid (PyObject *self, PyObject *args)
9479{
Victor Stinner8c62be82010-05-06 00:08:46 +00009480 /* We assume uid_t is no larger than a long. */
9481 long ruid, euid, suid;
9482 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9483 return NULL;
9484 if (setresuid(ruid, euid, suid) < 0)
9485 return posix_error();
9486 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009487}
9488#endif
9489
9490#ifdef HAVE_SETRESGID
9491PyDoc_STRVAR(posix_setresgid__doc__,
9492"setresgid(rgid, egid, sgid)\n\n\
9493Set the current process's real, effective, and saved group ids.");
9494
9495static PyObject*
9496posix_setresgid (PyObject *self, PyObject *args)
9497{
Victor Stinner8c62be82010-05-06 00:08:46 +00009498 /* We assume uid_t is no larger than a long. */
9499 long rgid, egid, sgid;
9500 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9501 return NULL;
9502 if (setresgid(rgid, egid, sgid) < 0)
9503 return posix_error();
9504 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009505}
9506#endif
9507
9508#ifdef HAVE_GETRESUID
9509PyDoc_STRVAR(posix_getresuid__doc__,
9510"getresuid() -> (ruid, euid, suid)\n\n\
9511Get tuple of the current process's real, effective, and saved user ids.");
9512
9513static PyObject*
9514posix_getresuid (PyObject *self, PyObject *noargs)
9515{
Victor Stinner8c62be82010-05-06 00:08:46 +00009516 uid_t ruid, euid, suid;
9517 long l_ruid, l_euid, l_suid;
9518 if (getresuid(&ruid, &euid, &suid) < 0)
9519 return posix_error();
9520 /* Force the values into long's as we don't know the size of uid_t. */
9521 l_ruid = ruid;
9522 l_euid = euid;
9523 l_suid = suid;
9524 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009525}
9526#endif
9527
9528#ifdef HAVE_GETRESGID
9529PyDoc_STRVAR(posix_getresgid__doc__,
9530"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009531Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009532
9533static PyObject*
9534posix_getresgid (PyObject *self, PyObject *noargs)
9535{
Victor Stinner8c62be82010-05-06 00:08:46 +00009536 uid_t rgid, egid, sgid;
9537 long l_rgid, l_egid, l_sgid;
9538 if (getresgid(&rgid, &egid, &sgid) < 0)
9539 return posix_error();
9540 /* Force the values into long's as we don't know the size of uid_t. */
9541 l_rgid = rgid;
9542 l_egid = egid;
9543 l_sgid = sgid;
9544 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009545}
9546#endif
9547
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009548/* Posix *at family of functions:
9549 faccessat, fchmodat, fchownat, fstatat, futimesat,
9550 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9551 unlinkat, utimensat, mkfifoat */
9552
9553#ifdef HAVE_FACCESSAT
9554PyDoc_STRVAR(posix_faccessat__doc__,
9555"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9556Like access() but if path is relative, it is taken as relative to dirfd.\n\
9557flags is optional and can be constructed by ORing together zero or more\n\
9558of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9559If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9560is interpreted relative to the current working directory.");
9561
9562static PyObject *
9563posix_faccessat(PyObject *self, PyObject *args)
9564{
9565 PyObject *opath;
9566 char *path;
9567 int mode;
9568 int res;
9569 int dirfd, flags = 0;
9570 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9571 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9572 return NULL;
9573 path = PyBytes_AsString(opath);
9574 Py_BEGIN_ALLOW_THREADS
9575 res = faccessat(dirfd, path, mode, flags);
9576 Py_END_ALLOW_THREADS
9577 Py_DECREF(opath);
9578 return PyBool_FromLong(res == 0);
9579}
9580#endif
9581
9582#ifdef HAVE_FCHMODAT
9583PyDoc_STRVAR(posix_fchmodat__doc__,
9584"fchmodat(dirfd, path, mode, flags=0)\n\n\
9585Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9586flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9587If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9588is interpreted relative to the current working directory.");
9589
9590static PyObject *
9591posix_fchmodat(PyObject *self, PyObject *args)
9592{
9593 int dirfd, mode, res;
9594 int flags = 0;
9595 PyObject *opath;
9596 char *path;
9597
9598 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9599 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9600 return NULL;
9601
9602 path = PyBytes_AsString(opath);
9603
9604 Py_BEGIN_ALLOW_THREADS
9605 res = fchmodat(dirfd, path, mode, flags);
9606 Py_END_ALLOW_THREADS
9607 Py_DECREF(opath);
9608 if (res < 0)
9609 return posix_error();
9610 Py_RETURN_NONE;
9611}
9612#endif /* HAVE_FCHMODAT */
9613
9614#ifdef HAVE_FCHOWNAT
9615PyDoc_STRVAR(posix_fchownat__doc__,
9616"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9617Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9618flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9619If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9620is interpreted relative to the current working directory.");
9621
9622static PyObject *
9623posix_fchownat(PyObject *self, PyObject *args)
9624{
9625 PyObject *opath;
9626 int dirfd, res;
9627 long uid, gid;
9628 int flags = 0;
9629 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009630
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009631 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9632 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9633 return NULL;
9634
9635 path = PyBytes_AsString(opath);
9636
9637 Py_BEGIN_ALLOW_THREADS
9638 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9639 Py_END_ALLOW_THREADS
9640 Py_DECREF(opath);
9641 if (res < 0)
9642 return posix_error();
9643 Py_RETURN_NONE;
9644}
9645#endif /* HAVE_FCHOWNAT */
9646
9647#ifdef HAVE_FSTATAT
9648PyDoc_STRVAR(posix_fstatat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01009649"fstatat(dirfd, path, flags=0) -> stat result\n\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009650Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9651flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9652If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9653is interpreted relative to the current working directory.");
9654
9655static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01009656posix_fstatat(PyObject *self, PyObject *args)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009657{
9658 PyObject *opath;
9659 char *path;
9660 STRUCT_STAT st;
9661 int dirfd, res, flags = 0;
9662
Victor Stinner4195b5c2012-02-08 23:03:19 +01009663 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9664 &dirfd, PyUnicode_FSConverter, &opath, &flags))
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009665 return NULL;
9666 path = PyBytes_AsString(opath);
9667
9668 Py_BEGIN_ALLOW_THREADS
9669 res = fstatat(dirfd, path, &st, flags);
9670 Py_END_ALLOW_THREADS
9671 Py_DECREF(opath);
9672 if (res != 0)
9673 return posix_error();
9674
Victor Stinner4195b5c2012-02-08 23:03:19 +01009675 return _pystat_fromstructstat(&st);
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009676}
9677#endif
9678
9679#ifdef HAVE_FUTIMESAT
9680PyDoc_STRVAR(posix_futimesat__doc__,
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009681"futimesat(dirfd, path[, (atime, mtime)])\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009682Like utime() but if path is relative, it is taken as relative to dirfd.\n\
9683If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9684is interpreted relative to the current working directory.");
9685
9686static PyObject *
9687posix_futimesat(PyObject *self, PyObject *args)
9688{
9689 PyObject *opath;
9690 char *path;
9691 int res, dirfd;
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009692 PyObject* arg = Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009693 time_t atime, mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01009694 long ansec, mnsec;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009695
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009696 if (!PyArg_ParseTuple(args, "iO&|O:futimesat",
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009697 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9698 return NULL;
9699 path = PyBytes_AsString(opath);
9700 if (arg == Py_None) {
9701 /* optional time values not given */
9702 Py_BEGIN_ALLOW_THREADS
9703 res = futimesat(dirfd, path, NULL);
9704 Py_END_ALLOW_THREADS
9705 }
9706 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9707 PyErr_SetString(PyExc_TypeError,
9708 "futimesat() arg 3 must be a tuple (atime, mtime)");
9709 Py_DECREF(opath);
9710 return NULL;
9711 }
9712 else {
9713 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Victor Stinnera2f7c002012-02-08 03:36:25 +01009714 &atime, &ansec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009715 Py_DECREF(opath);
9716 return NULL;
9717 }
9718 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Victor Stinnera2f7c002012-02-08 03:36:25 +01009719 &mtime, &mnsec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009720 Py_DECREF(opath);
9721 return NULL;
9722 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009723
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009724 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009725 {
9726#ifdef HAVE_UTIMENSAT
9727 struct timespec buf[2];
9728 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01009729 buf[0].tv_nsec = ansec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009730 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01009731 buf[1].tv_nsec = mnsec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009732 res = utimensat(dirfd, path, buf, 0);
9733#else
9734 struct timeval buf[2];
9735 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01009736 buf[0].tv_usec = ansec / 1000;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009737 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01009738 buf[1].tv_usec = mnsec / 1000;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009739 res = futimesat(dirfd, path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009740#endif
9741 }
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009742 Py_END_ALLOW_THREADS
9743 }
9744 Py_DECREF(opath);
9745 if (res < 0) {
9746 return posix_error();
9747 }
9748 Py_RETURN_NONE;
9749}
9750#endif
9751
9752#ifdef HAVE_LINKAT
9753PyDoc_STRVAR(posix_linkat__doc__,
9754"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9755Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9756and if dstpath is relative, it is taken as relative to dstfd.\n\
9757flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9758If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9759srcpath is interpreted relative to the current working directory. This\n\
9760also applies for dstpath.");
9761
9762static PyObject *
9763posix_linkat(PyObject *self, PyObject *args)
9764{
9765 PyObject *osrc, *odst;
9766 char *src, *dst;
9767 int res, srcfd, dstfd;
9768 int flags = 0;
9769
9770 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9771 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9772 return NULL;
9773 src = PyBytes_AsString(osrc);
9774 dst = PyBytes_AsString(odst);
9775 Py_BEGIN_ALLOW_THREADS
9776 res = linkat(srcfd, src, dstfd, dst, flags);
9777 Py_END_ALLOW_THREADS
9778 Py_DECREF(osrc);
9779 Py_DECREF(odst);
9780 if (res < 0)
9781 return posix_error();
9782 Py_RETURN_NONE;
9783}
9784#endif /* HAVE_LINKAT */
9785
9786#ifdef HAVE_MKDIRAT
9787PyDoc_STRVAR(posix_mkdirat__doc__,
9788"mkdirat(dirfd, path, mode=0o777)\n\n\
9789Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9790If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9791is interpreted relative to the current working directory.");
9792
9793static PyObject *
9794posix_mkdirat(PyObject *self, PyObject *args)
9795{
9796 int res, dirfd;
9797 PyObject *opath;
9798 char *path;
9799 int mode = 0777;
9800
9801 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9802 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9803 return NULL;
9804 path = PyBytes_AsString(opath);
9805 Py_BEGIN_ALLOW_THREADS
9806 res = mkdirat(dirfd, path, mode);
9807 Py_END_ALLOW_THREADS
9808 Py_DECREF(opath);
9809 if (res < 0)
9810 return posix_error();
9811 Py_RETURN_NONE;
9812}
9813#endif
9814
9815#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9816PyDoc_STRVAR(posix_mknodat__doc__,
9817"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9818Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9819If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9820is interpreted relative to the current working directory.");
9821
9822static PyObject *
9823posix_mknodat(PyObject *self, PyObject *args)
9824{
9825 PyObject *opath;
9826 char *filename;
9827 int mode = 0600;
9828 int device = 0;
9829 int res, dirfd;
9830 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9831 PyUnicode_FSConverter, &opath, &mode, &device))
9832 return NULL;
9833 filename = PyBytes_AS_STRING(opath);
9834 Py_BEGIN_ALLOW_THREADS
9835 res = mknodat(dirfd, filename, mode, device);
9836 Py_END_ALLOW_THREADS
9837 Py_DECREF(opath);
9838 if (res < 0)
9839 return posix_error();
9840 Py_RETURN_NONE;
9841}
9842#endif
9843
9844#ifdef HAVE_OPENAT
9845PyDoc_STRVAR(posix_openat__doc__,
9846"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9847Like open() but if path is relative, it is taken as relative to dirfd.\n\
9848If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9849is interpreted relative to the current working directory.");
9850
9851static PyObject *
9852posix_openat(PyObject *self, PyObject *args)
9853{
9854 PyObject *ofile;
9855 char *file;
9856 int flag, dirfd, fd;
9857 int mode = 0777;
9858
9859 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9860 &dirfd, PyUnicode_FSConverter, &ofile,
9861 &flag, &mode))
9862 return NULL;
9863 file = PyBytes_AsString(ofile);
9864 Py_BEGIN_ALLOW_THREADS
9865 fd = openat(dirfd, file, flag, mode);
9866 Py_END_ALLOW_THREADS
9867 Py_DECREF(ofile);
9868 if (fd < 0)
9869 return posix_error();
9870 return PyLong_FromLong((long)fd);
9871}
9872#endif
9873
9874#ifdef HAVE_READLINKAT
9875PyDoc_STRVAR(posix_readlinkat__doc__,
9876"readlinkat(dirfd, path) -> path\n\n\
9877Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9878If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9879is interpreted relative to the current working directory.");
9880
9881static PyObject *
9882posix_readlinkat(PyObject *self, PyObject *args)
9883{
9884 PyObject *v, *opath;
9885 char buf[MAXPATHLEN];
9886 char *path;
9887 int n, dirfd;
9888 int arg_is_unicode = 0;
9889
9890 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9891 &dirfd, PyUnicode_FSConverter, &opath))
9892 return NULL;
9893 path = PyBytes_AsString(opath);
9894 v = PySequence_GetItem(args, 1);
9895 if (v == NULL) {
9896 Py_DECREF(opath);
9897 return NULL;
9898 }
9899
9900 if (PyUnicode_Check(v)) {
9901 arg_is_unicode = 1;
9902 }
9903 Py_DECREF(v);
9904
9905 Py_BEGIN_ALLOW_THREADS
9906 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9907 Py_END_ALLOW_THREADS
9908 Py_DECREF(opath);
9909 if (n < 0)
9910 return posix_error();
9911
9912 if (arg_is_unicode)
9913 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9914 else
9915 return PyBytes_FromStringAndSize(buf, n);
9916}
9917#endif /* HAVE_READLINKAT */
9918
9919#ifdef HAVE_RENAMEAT
9920PyDoc_STRVAR(posix_renameat__doc__,
9921"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9922Like rename() but if oldpath is relative, it is taken as relative to\n\
9923olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9924If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9925oldpath is interpreted relative to the current working directory. This\n\
9926also applies for newpath.");
9927
9928static PyObject *
9929posix_renameat(PyObject *self, PyObject *args)
9930{
9931 int res;
9932 PyObject *opathold, *opathnew;
9933 char *opath, *npath;
9934 int oldfd, newfd;
9935
9936 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9937 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9938 return NULL;
9939 opath = PyBytes_AsString(opathold);
9940 npath = PyBytes_AsString(opathnew);
9941 Py_BEGIN_ALLOW_THREADS
9942 res = renameat(oldfd, opath, newfd, npath);
9943 Py_END_ALLOW_THREADS
9944 Py_DECREF(opathold);
9945 Py_DECREF(opathnew);
9946 if (res < 0)
9947 return posix_error();
9948 Py_RETURN_NONE;
9949}
9950#endif
9951
9952#if HAVE_SYMLINKAT
9953PyDoc_STRVAR(posix_symlinkat__doc__,
9954"symlinkat(src, dstfd, dst)\n\n\
9955Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9956If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9957is interpreted relative to the current working directory.");
9958
9959static PyObject *
9960posix_symlinkat(PyObject *self, PyObject *args)
9961{
9962 int res, dstfd;
9963 PyObject *osrc, *odst;
9964 char *src, *dst;
9965
9966 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9967 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9968 return NULL;
9969 src = PyBytes_AsString(osrc);
9970 dst = PyBytes_AsString(odst);
9971 Py_BEGIN_ALLOW_THREADS
9972 res = symlinkat(src, dstfd, dst);
9973 Py_END_ALLOW_THREADS
9974 Py_DECREF(osrc);
9975 Py_DECREF(odst);
9976 if (res < 0)
9977 return posix_error();
9978 Py_RETURN_NONE;
9979}
9980#endif /* HAVE_SYMLINKAT */
9981
9982#ifdef HAVE_UNLINKAT
9983PyDoc_STRVAR(posix_unlinkat__doc__,
9984"unlinkat(dirfd, path, flags=0)\n\n\
9985Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9986flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9987specified, unlinkat() behaves like rmdir().\n\
9988If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9989is interpreted relative to the current working directory.");
9990
9991static PyObject *
9992posix_unlinkat(PyObject *self, PyObject *args)
9993{
9994 int dirfd, res, flags = 0;
9995 PyObject *opath;
9996 char *path;
9997
9998 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9999 &dirfd, PyUnicode_FSConverter, &opath, &flags))
10000 return NULL;
10001 path = PyBytes_AsString(opath);
10002 Py_BEGIN_ALLOW_THREADS
10003 res = unlinkat(dirfd, path, flags);
10004 Py_END_ALLOW_THREADS
10005 Py_DECREF(opath);
10006 if (res < 0)
10007 return posix_error();
10008 Py_RETURN_NONE;
10009}
10010#endif
10011
10012#ifdef HAVE_UTIMENSAT
10013PyDoc_STRVAR(posix_utimensat__doc__,
Brian Curtin569b4942011-11-07 16:09:20 -060010014"utimensat(dirfd, path[, atime=(atime_sec, atime_nsec),\n\
10015 mtime=(mtime_sec, mtime_nsec), flags=0])\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010016utimensat(dirfd, path, None, None, flags)\n\n\
10017Updates the timestamps of a file with nanosecond precision. If path is\n\
10018relative, it is taken as relative to dirfd.\n\
Brian Curtin569b4942011-11-07 16:09:20 -060010019If atime and mtime are both None, which is the default, set atime and\n\
10020mtime to the current time.\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010021flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
10022If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10023is interpreted relative to the current working directory.\n\
10024If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
10025current time.\n\
10026If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
10027
10028static PyObject *
Brian Curtin569b4942011-11-07 16:09:20 -060010029posix_utimensat(PyObject *self, PyObject *args, PyObject *kwargs)
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010030{
10031 PyObject *opath;
10032 char *path;
10033 int res, dirfd, flags = 0;
Brian Curtin569b4942011-11-07 16:09:20 -060010034 PyObject *atime = Py_None;
10035 PyObject *mtime = Py_None;
10036
10037 static char *kwlist[] = {"dirfd", "path", "atime", "mtime", "flags", NULL};
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010038
10039 struct timespec buf[2];
10040
Brian Curtin569b4942011-11-07 16:09:20 -060010041 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO&|OOi:utimensat", kwlist,
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010042 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
10043 return NULL;
10044 path = PyBytes_AsString(opath);
10045 if (atime == Py_None && mtime == Py_None) {
10046 /* optional time values not given */
10047 Py_BEGIN_ALLOW_THREADS
10048 res = utimensat(dirfd, path, NULL, flags);
10049 Py_END_ALLOW_THREADS
10050 }
10051 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
10052 PyErr_SetString(PyExc_TypeError,
10053 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
10054 Py_DECREF(opath);
10055 return NULL;
10056 }
10057 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
10058 PyErr_SetString(PyExc_TypeError,
10059 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
10060 Py_DECREF(opath);
10061 return NULL;
10062 }
10063 else {
10064 if (!PyArg_ParseTuple(atime, "ll:utimensat",
10065 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
10066 Py_DECREF(opath);
10067 return NULL;
10068 }
10069 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
10070 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
10071 Py_DECREF(opath);
10072 return NULL;
10073 }
10074 Py_BEGIN_ALLOW_THREADS
10075 res = utimensat(dirfd, path, buf, flags);
10076 Py_END_ALLOW_THREADS
10077 }
10078 Py_DECREF(opath);
10079 if (res < 0) {
10080 return posix_error();
10081 }
10082 Py_RETURN_NONE;
10083}
10084#endif
10085
10086#ifdef HAVE_MKFIFOAT
10087PyDoc_STRVAR(posix_mkfifoat__doc__,
10088"mkfifoat(dirfd, path, mode=0o666)\n\n\
10089Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
10090If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10091is interpreted relative to the current working directory.");
10092
10093static PyObject *
10094posix_mkfifoat(PyObject *self, PyObject *args)
10095{
10096 PyObject *opath;
10097 char *filename;
10098 int mode = 0666;
10099 int res, dirfd;
10100 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
10101 &dirfd, PyUnicode_FSConverter, &opath, &mode))
10102 return NULL;
10103 filename = PyBytes_AS_STRING(opath);
10104 Py_BEGIN_ALLOW_THREADS
10105 res = mkfifoat(dirfd, filename, mode);
10106 Py_END_ALLOW_THREADS
10107 Py_DECREF(opath);
10108 if (res < 0)
10109 return posix_error();
10110 Py_RETURN_NONE;
10111}
10112#endif
10113
Benjamin Peterson9428d532011-09-14 11:45:52 -040010114#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040010115
10116static int
10117try_getxattr(const char *path, const char *name,
10118 ssize_t (*get)(const char *, const char *, void *, size_t),
10119 Py_ssize_t buf_size, PyObject **res)
10120{
10121 PyObject *value;
10122 Py_ssize_t len;
10123
10124 assert(buf_size <= XATTR_SIZE_MAX);
10125 value = PyBytes_FromStringAndSize(NULL, buf_size);
10126 if (!value)
10127 return 0;
10128 Py_BEGIN_ALLOW_THREADS;
10129 len = get(path, name, PyBytes_AS_STRING(value), buf_size);
10130 Py_END_ALLOW_THREADS;
10131 if (len < 0) {
10132 Py_DECREF(value);
10133 if (errno == ERANGE) {
10134 value = NULL;
10135 }
10136 else {
10137 posix_error();
10138 return 0;
10139 }
10140 }
10141 else if (len != buf_size) {
10142 /* Can only shrink. */
10143 _PyBytes_Resize(&value, len);
10144 }
10145 *res = value;
10146 return 1;
10147}
10148
10149static PyObject *
10150getxattr_common(const char *path, PyObject *name_obj,
10151 ssize_t (*get)(const char *, const char *, void *, size_t))
10152{
10153 PyObject *value;
10154 const char *name = PyBytes_AS_STRING(name_obj);
10155
10156 /* Try a small value first. */
10157 if (!try_getxattr(path, name, get, 128, &value))
10158 return NULL;
10159 if (value)
10160 return value;
10161 /* Now the maximum possible one. */
10162 if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value))
10163 return NULL;
10164 assert(value);
10165 return value;
10166}
10167
10168PyDoc_STRVAR(posix_getxattr__doc__,
10169"getxattr(path, attr) -> value\n\n\
10170Return the value of extended attribute *name* on *path*.");
10171
10172static PyObject *
10173posix_getxattr(PyObject *self, PyObject *args)
10174{
10175 PyObject *path, *res, *name;
10176
10177 if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path,
10178 PyUnicode_FSConverter, &name))
10179 return NULL;
10180 res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr);
10181 Py_DECREF(path);
10182 Py_DECREF(name);
10183 return res;
10184}
10185
10186PyDoc_STRVAR(posix_lgetxattr__doc__,
10187"lgetxattr(path, attr) -> value\n\n\
10188Like getxattr but don't follow symlinks.");
10189
10190static PyObject *
10191posix_lgetxattr(PyObject *self, PyObject *args)
10192{
10193 PyObject *path, *res, *name;
10194
10195 if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path,
10196 PyUnicode_FSConverter, &name))
10197 return NULL;
10198 res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr);
10199 Py_DECREF(path);
10200 Py_DECREF(name);
10201 return res;
10202}
10203
10204static ssize_t
10205wrap_fgetxattr(const char *path, const char *name, void *value, size_t size)
10206{
10207 /* Hack to share code. */
10208 return fgetxattr((int)(Py_uintptr_t)path, name, value, size);
10209}
10210
10211PyDoc_STRVAR(posix_fgetxattr__doc__,
10212"fgetxattr(fd, attr) -> value\n\n\
10213Like getxattr but operate on a fd instead of a path.");
10214
10215static PyObject *
10216posix_fgetxattr(PyObject *self, PyObject *args)
10217{
10218 PyObject *res, *name;
10219 int fd;
10220
10221 if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name))
10222 return NULL;
10223 res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr);
10224 Py_DECREF(name);
10225 return res;
10226}
10227
10228PyDoc_STRVAR(posix_setxattr__doc__,
10229"setxattr(path, attr, value, flags=0)\n\n\
10230Set extended attribute *attr* on *path* to *value*.");
10231
10232static PyObject *
10233posix_setxattr(PyObject *self, PyObject *args)
10234{
10235 PyObject *path, *name;
10236 Py_buffer data;
10237 int flags = 0, err;
10238
10239 if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter,
10240 &path, PyUnicode_FSConverter, &name, &data, &flags))
10241 return NULL;
10242 Py_BEGIN_ALLOW_THREADS;
10243 err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10244 data.buf, data.len, flags);
10245 Py_END_ALLOW_THREADS;
10246 Py_DECREF(path);
10247 Py_DECREF(name);
10248 PyBuffer_Release(&data);
10249 if (err)
10250 return posix_error();
10251 Py_RETURN_NONE;
10252}
10253
10254PyDoc_STRVAR(posix_lsetxattr__doc__,
10255"lsetxattr(path, attr, value, flags=0)\n\n\
10256Like setxattr but don't follow symlinks.");
10257
10258static PyObject *
10259posix_lsetxattr(PyObject *self, PyObject *args)
10260{
10261 PyObject *path, *name;
10262 Py_buffer data;
10263 int flags = 0, err;
10264
10265 if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter,
10266 &path, PyUnicode_FSConverter, &name, &data, &flags))
10267 return NULL;
10268 Py_BEGIN_ALLOW_THREADS;
10269 err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10270 data.buf, data.len, flags);
10271 Py_END_ALLOW_THREADS;
10272 Py_DECREF(path);
10273 Py_DECREF(name);
10274 PyBuffer_Release(&data);
10275 if (err)
10276 return posix_error();
10277 Py_RETURN_NONE;
10278}
10279
10280PyDoc_STRVAR(posix_fsetxattr__doc__,
10281"fsetxattr(fd, attr, value, flags=0)\n\n\
10282Like setxattr but operates on *fd* instead of a path.");
10283
10284static PyObject *
10285posix_fsetxattr(PyObject *self, PyObject *args)
10286{
10287 Py_buffer data;
10288 const char *name;
10289 int fd, flags = 0, err;
10290
10291 if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter,
10292 &name, &data, &flags))
10293 return NULL;
10294 Py_BEGIN_ALLOW_THREADS;
10295 err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags);
10296 Py_END_ALLOW_THREADS;
10297 Py_DECREF(name);
10298 PyBuffer_Release(&data);
10299 if (err)
10300 return posix_error();
10301 Py_RETURN_NONE;
10302}
10303
10304PyDoc_STRVAR(posix_removexattr__doc__,
10305"removexattr(path, attr)\n\n\
10306Remove extended attribute *attr* on *path*.");
10307
10308static PyObject *
10309posix_removexattr(PyObject *self, PyObject *args)
10310{
10311 PyObject *path, *name;
10312 int err;
10313
10314 if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path,
10315 PyUnicode_FSConverter, &name))
10316 return NULL;
10317 Py_BEGIN_ALLOW_THREADS;
10318 err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10319 Py_END_ALLOW_THREADS;
10320 Py_DECREF(path);
10321 Py_DECREF(name);
10322 if (err)
10323 return posix_error();
10324 Py_RETURN_NONE;
10325}
10326
10327PyDoc_STRVAR(posix_lremovexattr__doc__,
10328"lremovexattr(path, attr)\n\n\
10329Like removexattr but don't follow symlinks.");
10330
10331static PyObject *
10332posix_lremovexattr(PyObject *self, PyObject *args)
10333{
10334 PyObject *path, *name;
10335 int err;
10336
10337 if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path,
10338 PyUnicode_FSConverter, &name))
10339 return NULL;
10340 Py_BEGIN_ALLOW_THREADS;
10341 err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10342 Py_END_ALLOW_THREADS;
10343 Py_DECREF(path);
10344 Py_DECREF(name);
10345 if (err)
10346 return posix_error();
10347 Py_RETURN_NONE;
10348}
10349
10350PyDoc_STRVAR(posix_fremovexattr__doc__,
10351"fremovexattr(fd, attr)\n\n\
10352Like removexattr but operates on a file descriptor.");
10353
10354static PyObject *
10355posix_fremovexattr(PyObject *self, PyObject *args)
10356{
10357 PyObject *name;
10358 int fd, err;
10359
10360 if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd,
10361 PyUnicode_FSConverter, &name))
10362 return NULL;
10363 Py_BEGIN_ALLOW_THREADS;
10364 err = fremovexattr(fd, PyBytes_AS_STRING(name));
10365 Py_END_ALLOW_THREADS;
10366 Py_DECREF(name);
10367 if (err)
10368 return posix_error();
10369 Py_RETURN_NONE;
10370}
10371
10372static Py_ssize_t
10373try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t),
10374 Py_ssize_t buf_size, char **buf)
10375{
10376 Py_ssize_t len;
10377
10378 *buf = PyMem_MALLOC(buf_size);
10379 if (!*buf) {
10380 PyErr_NoMemory();
10381 return -1;
10382 }
10383 Py_BEGIN_ALLOW_THREADS;
10384 len = list(path, *buf, buf_size);
10385 Py_END_ALLOW_THREADS;
10386 if (len < 0) {
10387 PyMem_FREE(*buf);
10388 if (errno != ERANGE)
10389 posix_error();
10390 return -1;
10391 }
10392 return len;
10393}
10394
10395static PyObject *
10396listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t))
10397{
10398 PyObject *res, *attr;
10399 Py_ssize_t len, err, start, i;
10400 char *buf;
10401
10402 len = try_listxattr(path, list, 256, &buf);
10403 if (len < 0) {
10404 if (PyErr_Occurred())
10405 return NULL;
10406 len = try_listxattr(path, list, XATTR_LIST_MAX, &buf);
10407 if (len < 0)
10408 return NULL;
10409 }
10410 res = PyList_New(0);
10411 if (!res) {
10412 PyMem_FREE(buf);
10413 return NULL;
10414 }
10415 for (start = i = 0; i < len; i++) {
10416 if (!buf[i]) {
10417 attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start);
10418 if (!attr) {
10419 Py_DECREF(res);
10420 PyMem_FREE(buf);
10421 return NULL;
10422 }
10423 err = PyList_Append(res, attr);
10424 Py_DECREF(attr);
10425 if (err) {
10426 Py_DECREF(res);
10427 PyMem_FREE(buf);
10428 return NULL;
10429 }
10430 start = i + 1;
10431 }
10432 }
10433 PyMem_FREE(buf);
10434 return res;
10435}
10436
10437PyDoc_STRVAR(posix_listxattr__doc__,
10438"listxattr(path)\n\n\
10439Return a list of extended attributes on *path*.");
10440
10441static PyObject *
10442posix_listxattr(PyObject *self, PyObject *args)
10443{
10444 PyObject *path, *res;
10445
10446 if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path))
10447 return NULL;
10448 res = listxattr_common(PyBytes_AS_STRING(path), listxattr);
10449 Py_DECREF(path);
10450 return res;
10451}
10452
10453PyDoc_STRVAR(posix_llistxattr__doc__,
10454"llistxattr(path)\n\n\
10455Like listxattr but don't follow symlinks..");
10456
10457static PyObject *
10458posix_llistxattr(PyObject *self, PyObject *args)
10459{
10460 PyObject *path, *res;
10461
10462 if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path))
10463 return NULL;
10464 res = listxattr_common(PyBytes_AS_STRING(path), llistxattr);
10465 Py_DECREF(path);
10466 return res;
10467}
10468
10469static ssize_t
10470wrap_flistxattr(const char *path, char *buf, size_t len)
10471{
10472 /* Hack to share code. */
10473 return flistxattr((int)(Py_uintptr_t)path, buf, len);
10474}
10475
10476PyDoc_STRVAR(posix_flistxattr__doc__,
10477"flistxattr(path)\n\n\
10478Like flistxattr but operates on a file descriptor.");
10479
10480static PyObject *
10481posix_flistxattr(PyObject *self, PyObject *args)
10482{
10483 long fd;
10484
10485 if (!PyArg_ParseTuple(args, "i:flistxattr", &fd))
10486 return NULL;
10487 return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr);
10488}
10489
Benjamin Peterson9428d532011-09-14 11:45:52 -040010490#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040010491
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010492
10493/* Terminal size querying */
10494
10495static PyTypeObject TerminalSizeType;
10496
10497PyDoc_STRVAR(TerminalSize_docstring,
10498 "A tuple of (columns, lines) for holding terminal window size");
10499
10500static PyStructSequence_Field TerminalSize_fields[] = {
10501 {"columns", "width of the terminal window in characters"},
10502 {"lines", "height of the terminal window in characters"},
10503 {NULL, NULL}
10504};
10505
10506static PyStructSequence_Desc TerminalSize_desc = {
10507 "os.terminal_size",
10508 TerminalSize_docstring,
10509 TerminalSize_fields,
10510 2,
10511};
10512
10513#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
10514PyDoc_STRVAR(termsize__doc__,
10515 "Return the size of the terminal window as (columns, lines).\n" \
10516 "\n" \
10517 "The optional argument fd (default standard output) specifies\n" \
10518 "which file descriptor should be queried.\n" \
10519 "\n" \
10520 "If the file descriptor is not connected to a terminal, an OSError\n" \
10521 "is thrown.\n" \
10522 "\n" \
10523 "This function will only be defined if an implementation is\n" \
10524 "available for this system.\n" \
10525 "\n" \
10526 "shutil.get_terminal_size is the high-level function which should \n" \
10527 "normally be used, os.get_terminal_size is the low-level implementation.");
10528
10529static PyObject*
10530get_terminal_size(PyObject *self, PyObject *args)
10531{
10532 int columns, lines;
10533 PyObject *termsize;
10534
10535 int fd = fileno(stdout);
10536 /* Under some conditions stdout may not be connected and
10537 * fileno(stdout) may point to an invalid file descriptor. For example
10538 * GUI apps don't have valid standard streams by default.
10539 *
10540 * If this happens, and the optional fd argument is not present,
10541 * the ioctl below will fail returning EBADF. This is what we want.
10542 */
10543
10544 if (!PyArg_ParseTuple(args, "|i", &fd))
10545 return NULL;
10546
10547#ifdef TERMSIZE_USE_IOCTL
10548 {
10549 struct winsize w;
10550 if (ioctl(fd, TIOCGWINSZ, &w))
10551 return PyErr_SetFromErrno(PyExc_OSError);
10552 columns = w.ws_col;
10553 lines = w.ws_row;
10554 }
10555#endif /* TERMSIZE_USE_IOCTL */
10556
10557#ifdef TERMSIZE_USE_CONIO
10558 {
10559 DWORD nhandle;
10560 HANDLE handle;
10561 CONSOLE_SCREEN_BUFFER_INFO csbi;
10562 switch (fd) {
10563 case 0: nhandle = STD_INPUT_HANDLE;
10564 break;
10565 case 1: nhandle = STD_OUTPUT_HANDLE;
10566 break;
10567 case 2: nhandle = STD_ERROR_HANDLE;
10568 break;
10569 default:
10570 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
10571 }
10572 handle = GetStdHandle(nhandle);
10573 if (handle == NULL)
10574 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
10575 if (handle == INVALID_HANDLE_VALUE)
10576 return PyErr_SetFromWindowsErr(0);
10577
10578 if (!GetConsoleScreenBufferInfo(handle, &csbi))
10579 return PyErr_SetFromWindowsErr(0);
10580
10581 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
10582 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
10583 }
10584#endif /* TERMSIZE_USE_CONIO */
10585
10586 termsize = PyStructSequence_New(&TerminalSizeType);
10587 if (termsize == NULL)
10588 return NULL;
10589 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
10590 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
10591 if (PyErr_Occurred()) {
10592 Py_DECREF(termsize);
10593 return NULL;
10594 }
10595 return termsize;
10596}
10597#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
10598
10599
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010600static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010601 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010602#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010603 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010604#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010605 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010606#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010607 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010608#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +000010609 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010610#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010611 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010612#endif /* HAVE_FCHMOD */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010613#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010614 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010615#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +000010616#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010617 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010618#endif /* HAVE_LCHMOD */
10619#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010620 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010621#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +000010622#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010623 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010624#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010625#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010626 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010627#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +000010628#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +000010629 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +000010630#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010631#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +000010632 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010633#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010634#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +000010635 {"getcwd", (PyCFunction)posix_getcwd_unicode,
10636 METH_NOARGS, posix_getcwd__doc__},
10637 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
10638 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010639#endif
10640#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010641 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010642#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +000010643 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +000010644#ifdef HAVE_FDOPENDIR
Charles-François Natali77940902012-02-06 19:54:48 +010010645 {"flistdir", posix_flistdir, METH_VARARGS, posix_flistdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +000010646#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +010010647 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010648 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010649#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010650 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum775f4da1993-01-09 17:18:52 +000010651#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010652#ifdef HAVE_GETPRIORITY
10653 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10654#endif /* HAVE_GETPRIORITY */
10655#ifdef HAVE_SETPRIORITY
10656 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10657#endif /* HAVE_SETPRIORITY */
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010658#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010659 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010660#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010661#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010662 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010663#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010664 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
Antoine Pitrouf3b2d882012-01-30 22:08:52 +010010665 {"replace", posix_replace, METH_VARARGS, posix_replace__doc__},
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010666 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
Victor Stinner4195b5c2012-02-08 23:03:19 +010010667 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010668 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +000010669#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010670 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010671#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010672#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010673 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010674 win_symlink__doc__},
10675#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossum85e3b011991-06-03 12:42:10 +000010676#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010677 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010678#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010679 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010680#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010681 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010682#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010683 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10684 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
10685 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010686#ifdef HAVE_FUTIMES
10687 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
10688#endif
10689#ifdef HAVE_LUTIMES
10690 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
10691#endif
10692#ifdef HAVE_FUTIMENS
10693 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
10694#endif
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000010695#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010696 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010697#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010698 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010699#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010700 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
10701 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010702#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010703#ifdef HAVE_FEXECVE
10704 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
10705#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000010706#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010707 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10708 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010709#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010710 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10711 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010712#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010713#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010714#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010715 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010716#endif /* HAVE_FORK1 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010717#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010718 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010719#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010720#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010721#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010722 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10723 {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__},
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010724#endif
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010725#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010726 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010727#endif
10728#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010729 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010730#endif
10731#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010732 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010733#endif
10734#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010735 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010736#endif
10737#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010738 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010739#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010740 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010741#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010742 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10743 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10744#endif
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010745#endif /* HAVE_SCHED_H */
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010746#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010747 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010748#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010749#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010750 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010751#endif /* HAVE_FORKPTY */
Guido van Rossum3b066191991-06-04 19:40:25 +000010752#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010753 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +000010754#endif /* HAVE_GETEGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010755#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010756 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossum46003ff1992-05-15 11:05:24 +000010757#endif /* HAVE_GETEUID */
10758#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010759 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010760#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010761#ifdef HAVE_GETGROUPLIST
10762 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10763#endif
Fred Drakec9680921999-12-13 16:37:25 +000010764#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010765 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010766#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010767 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010768#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010769 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010770#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010771#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010772 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010773#endif /* HAVE_GETPPID */
10774#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010775 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010776#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010777#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010778 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010779#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010780#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010781 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010782#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010783#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010784 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010785#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010786#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010787 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010788#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010789#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010790 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10791 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000010792 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010793#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010794#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010795 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010796#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010797#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010798 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010799#endif /* HAVE_SETEUID */
10800#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010801 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010802#endif /* HAVE_SETEGID */
10803#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010804 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010805#endif /* HAVE_SETREUID */
10806#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010807 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010808#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010809#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010810 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010811#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010812#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010813 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010814#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010815#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010816 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010817#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010818#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010819 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010820#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010821#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010822 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010823#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010824#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010825 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010826#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010827#ifdef HAVE_WAIT3
Victor Stinner4195b5c2012-02-08 23:03:19 +010010828 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010829#endif /* HAVE_WAIT3 */
10830#ifdef HAVE_WAIT4
Victor Stinner4195b5c2012-02-08 23:03:19 +010010831 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010832#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010833#if defined(HAVE_WAITID) && !defined(__APPLE__)
10834 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10835#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010836#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010837 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010838#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010839#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010840 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010841#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010842#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010843 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010844#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010845#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010846 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010847#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010848#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010849 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010850#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010851#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010852 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010853#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000010854 {"open", posix_open, METH_VARARGS, posix_open__doc__},
10855 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10856 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10857 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10858 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10859 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010860#ifdef HAVE_LOCKF
10861 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10862#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010863 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10864 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010865#ifdef HAVE_READV
10866 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10867#endif
10868#ifdef HAVE_PREAD
10869 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10870#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010871 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010872#ifdef HAVE_WRITEV
10873 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10874#endif
10875#ifdef HAVE_PWRITE
10876 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10877#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010878#ifdef HAVE_SENDFILE
10879 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10880 posix_sendfile__doc__},
10881#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +010010882 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010883 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010884#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010885 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010886#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010887#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010888 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010889#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010890#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000010891 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010892#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010893#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000010894 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010895#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010896#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010897 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10898 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10899 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010900#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010901#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010902 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010903#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010904#ifdef HAVE_TRUNCATE
10905 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
10906#endif
10907#ifdef HAVE_POSIX_FALLOCATE
10908 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10909#endif
10910#ifdef HAVE_POSIX_FADVISE
10911 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10912#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010913#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010914 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010915#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010916#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010917 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010918#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010919 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010920#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010921 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010922#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010923#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010924 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010925#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010926#ifdef HAVE_SYNC
10927 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10928#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010929#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010930 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010931#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010932#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010933#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010934 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010935#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010936#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010937 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010938#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010939#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010940 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010941#endif /* WIFSTOPPED */
10942#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010943 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010944#endif /* WIFSIGNALED */
10945#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010946 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010947#endif /* WIFEXITED */
10948#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010949 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010950#endif /* WEXITSTATUS */
10951#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010952 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010953#endif /* WTERMSIG */
10954#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010955 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010956#endif /* WSTOPSIG */
10957#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010958#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010959 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010960#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010961#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010962 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010963#endif
Fred Drakec9680921999-12-13 16:37:25 +000010964#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010965 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010966#endif
10967#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010968 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010969#endif
10970#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010971 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010972#endif
10973#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010974 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010975#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010976 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010977#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010978 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010979 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000010980 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010981 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010982 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010983#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010984#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010985 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010986#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010987 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010988 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010989 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010990 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +000010991 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010992 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010993#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010994 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010995#endif
10996#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010997 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010998#endif
10999#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000011000 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011001#endif
11002#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000011003 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011004#endif
11005
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011006/* posix *at family of functions */
11007#ifdef HAVE_FACCESSAT
11008 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
11009#endif
11010#ifdef HAVE_FCHMODAT
11011 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
11012#endif /* HAVE_FCHMODAT */
11013#ifdef HAVE_FCHOWNAT
11014 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
11015#endif /* HAVE_FCHOWNAT */
11016#ifdef HAVE_FSTATAT
Victor Stinner4195b5c2012-02-08 23:03:19 +010011017 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011018#endif
11019#ifdef HAVE_FUTIMESAT
11020 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
11021#endif
11022#ifdef HAVE_LINKAT
11023 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
11024#endif /* HAVE_LINKAT */
11025#ifdef HAVE_MKDIRAT
11026 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
11027#endif
11028#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
11029 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
11030#endif
11031#ifdef HAVE_OPENAT
11032 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
11033#endif
11034#ifdef HAVE_READLINKAT
11035 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
11036#endif /* HAVE_READLINKAT */
11037#ifdef HAVE_RENAMEAT
11038 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
11039#endif
11040#if HAVE_SYMLINKAT
11041 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
11042#endif /* HAVE_SYMLINKAT */
11043#ifdef HAVE_UNLINKAT
11044 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
11045#endif
11046#ifdef HAVE_UTIMENSAT
Jesus Cead03a4912011-11-08 17:28:04 +010011047 {"utimensat", (PyCFunction)posix_utimensat,
11048 METH_VARARGS | METH_KEYWORDS,
Brian Curtin569b4942011-11-07 16:09:20 -060011049 posix_utimensat__doc__},
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011050#endif
11051#ifdef HAVE_MKFIFOAT
11052 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
11053#endif
Benjamin Peterson9428d532011-09-14 11:45:52 -040011054#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040011055 {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__},
11056 {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__},
11057 {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__},
11058 {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__},
11059 {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__},
11060 {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__},
11061 {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__},
11062 {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__},
11063 {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__},
11064 {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__},
11065 {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__},
11066 {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__},
11067#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011068#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
11069 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
11070#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011071 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011072};
11073
11074
Barry Warsaw4a342091996-12-19 23:50:02 +000011075static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011076ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000011077{
Victor Stinner8c62be82010-05-06 00:08:46 +000011078 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000011079}
11080
Guido van Rossumd48f2521997-12-05 22:19:34 +000011081#if defined(PYOS_OS2)
11082/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000011083static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000011084{
11085 APIRET rc;
11086 ULONG values[QSV_MAX+1];
11087 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000011088 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000011089
11090 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000011091 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011092 Py_END_ALLOW_THREADS
11093
11094 if (rc != NO_ERROR) {
11095 os2_error(rc);
11096 return -1;
11097 }
11098
Fred Drake4d1e64b2002-04-15 19:40:07 +000011099 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
11100 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
11101 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
11102 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
11103 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
11104 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
11105 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011106
11107 switch (values[QSV_VERSION_MINOR]) {
11108 case 0: ver = "2.00"; break;
11109 case 10: ver = "2.10"; break;
11110 case 11: ver = "2.11"; break;
11111 case 30: ver = "3.00"; break;
11112 case 40: ver = "4.00"; break;
11113 case 50: ver = "5.00"; break;
11114 default:
Tim Peters885d4572001-11-28 20:27:42 +000011115 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000011116 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000011117 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011118 ver = &tmp[0];
11119 }
11120
11121 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000011122 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000011123 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011124
11125 /* Add Indicator of Which Drive was Used to Boot the System */
11126 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
11127 tmp[1] = ':';
11128 tmp[2] = '\0';
11129
Fred Drake4d1e64b2002-04-15 19:40:07 +000011130 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011131}
11132#endif
11133
Brian Curtin52173d42010-12-02 18:29:18 +000011134#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011135static int
Brian Curtin52173d42010-12-02 18:29:18 +000011136enable_symlink()
11137{
11138 HANDLE tok;
11139 TOKEN_PRIVILEGES tok_priv;
11140 LUID luid;
11141 int meth_idx = 0;
11142
11143 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011144 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011145
11146 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011147 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011148
11149 tok_priv.PrivilegeCount = 1;
11150 tok_priv.Privileges[0].Luid = luid;
11151 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
11152
11153 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
11154 sizeof(TOKEN_PRIVILEGES),
11155 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011156 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011157
Brian Curtin3b4499c2010-12-28 14:31:47 +000011158 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
11159 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000011160}
11161#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
11162
Barry Warsaw4a342091996-12-19 23:50:02 +000011163static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011164all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000011165{
Guido van Rossum94f6f721999-01-06 18:42:14 +000011166#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011167 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011168#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011169#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011170 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011171#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011172#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011173 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011174#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011175#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011176 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011177#endif
Fred Drakec9680921999-12-13 16:37:25 +000011178#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011179 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000011180#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011181#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011182 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011183#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011184#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000011185 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011186#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011187#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000011188 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011189#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011190#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000011191 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011192#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011193#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011194 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011195#endif
11196#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011197 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011198#endif
11199#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000011200 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011201#endif
11202#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000011203 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011204#endif
11205#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011206 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011207#endif
11208#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000011209 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011210#endif
11211#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011212 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011213#endif
11214#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011215 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011216#endif
11217#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011218 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011219#endif
11220#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000011221 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011222#endif
11223#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011224 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011225#endif
11226#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000011227 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011228#endif
11229#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011230 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011231#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000011232#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011233 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011234#endif
11235#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000011236 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011237#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011238#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011239 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011240#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000011241#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011242 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011243#endif
11244#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011245 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011246#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011247#ifdef PRIO_PROCESS
11248 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
11249#endif
11250#ifdef PRIO_PGRP
11251 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
11252#endif
11253#ifdef PRIO_USER
11254 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
11255#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020011256#ifdef O_CLOEXEC
11257 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
11258#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011259/* posix - constants for *at functions */
11260#ifdef AT_SYMLINK_NOFOLLOW
11261 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
11262#endif
11263#ifdef AT_EACCESS
11264 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
11265#endif
11266#ifdef AT_FDCWD
11267 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
11268#endif
11269#ifdef AT_REMOVEDIR
11270 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
11271#endif
11272#ifdef AT_SYMLINK_FOLLOW
11273 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
11274#endif
11275#ifdef UTIME_NOW
11276 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11277#endif
11278#ifdef UTIME_OMIT
11279 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11280#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011281
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011282
Tim Peters5aa91602002-01-30 05:46:57 +000011283/* MS Windows */
11284#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011285 /* Don't inherit in child processes. */
11286 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011287#endif
11288#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000011289 /* Optimize for short life (keep in memory). */
11290 /* MS forgot to define this one with a non-underscore form too. */
11291 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011292#endif
11293#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011294 /* Automatically delete when last handle is closed. */
11295 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011296#endif
11297#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000011298 /* Optimize for random access. */
11299 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011300#endif
11301#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011302 /* Optimize for sequential access. */
11303 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011304#endif
11305
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011306/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011307#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011308 /* Send a SIGIO signal whenever input or output
11309 becomes available on file descriptor */
11310 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011311#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011312#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011313 /* Direct disk access. */
11314 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011315#endif
11316#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000011317 /* Must be a directory. */
11318 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011319#endif
11320#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000011321 /* Do not follow links. */
11322 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011323#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011324#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011325 /* Do not update the access time. */
11326 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011327#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000011328
Victor Stinner8c62be82010-05-06 00:08:46 +000011329 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011330#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011331 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011332#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011333#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011334 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011335#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011336#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011337 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011338#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011339#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000011340 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011341#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011342#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000011343 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011344#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011345#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000011346 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011347#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011348#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000011349 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011350#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011351#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000011352 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011353#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011354#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011355 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011356#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011357#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011358 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011359#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011360#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011361 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011362#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011363#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011364 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011365#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011366#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000011367 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011368#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011369#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000011370 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011371#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011372#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011373 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011374#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011375#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011376 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011377#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011378#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000011379 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011380#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011381
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011382 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011383#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011384 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011385#endif /* ST_RDONLY */
11386#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011387 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011388#endif /* ST_NOSUID */
11389
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011390 /* FreeBSD sendfile() constants */
11391#ifdef SF_NODISKIO
11392 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
11393#endif
11394#ifdef SF_MNOWAIT
11395 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
11396#endif
11397#ifdef SF_SYNC
11398 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
11399#endif
11400
Ross Lagerwall7807c352011-03-17 20:20:30 +020011401 /* constants for posix_fadvise */
11402#ifdef POSIX_FADV_NORMAL
11403 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
11404#endif
11405#ifdef POSIX_FADV_SEQUENTIAL
11406 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
11407#endif
11408#ifdef POSIX_FADV_RANDOM
11409 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
11410#endif
11411#ifdef POSIX_FADV_NOREUSE
11412 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
11413#endif
11414#ifdef POSIX_FADV_WILLNEED
11415 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
11416#endif
11417#ifdef POSIX_FADV_DONTNEED
11418 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
11419#endif
11420
11421 /* constants for waitid */
11422#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
11423 if (ins(d, "P_PID", (long)P_PID)) return -1;
11424 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
11425 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
11426#endif
11427#ifdef WEXITED
11428 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
11429#endif
11430#ifdef WNOWAIT
11431 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
11432#endif
11433#ifdef WSTOPPED
11434 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
11435#endif
11436#ifdef CLD_EXITED
11437 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
11438#endif
11439#ifdef CLD_DUMPED
11440 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
11441#endif
11442#ifdef CLD_TRAPPED
11443 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
11444#endif
11445#ifdef CLD_CONTINUED
11446 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
11447#endif
11448
11449 /* constants for lockf */
11450#ifdef F_LOCK
11451 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
11452#endif
11453#ifdef F_TLOCK
11454 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
11455#endif
11456#ifdef F_ULOCK
11457 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
11458#endif
11459#ifdef F_TEST
11460 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
11461#endif
11462
11463 /* constants for futimens */
11464#ifdef UTIME_NOW
11465 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11466#endif
11467#ifdef UTIME_OMIT
11468 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11469#endif
11470
Guido van Rossum246bc171999-02-01 23:54:31 +000011471#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011472#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000011473 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
11474 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
11475 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
11476 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
11477 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
11478 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
11479 if (ins(d, "P_PM", (long)P_PM)) return -1;
11480 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
11481 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
11482 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
11483 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
11484 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
11485 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
11486 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
11487 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
11488 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
11489 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
11490 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
11491 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
11492 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011493#else
Victor Stinner8c62be82010-05-06 00:08:46 +000011494 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
11495 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
11496 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
11497 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
11498 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000011499#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011500#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000011501
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011502#ifdef HAVE_SCHED_H
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011503 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011504 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
11505 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
11506#ifdef SCHED_SPORADIC
11507 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
11508#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011509#ifdef SCHED_BATCH
11510 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
11511#endif
11512#ifdef SCHED_IDLE
11513 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
11514#endif
11515#ifdef SCHED_RESET_ON_FORK
11516 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
11517#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011518#ifdef SCHED_SYS
11519 if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1;
11520#endif
11521#ifdef SCHED_IA
11522 if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1;
11523#endif
11524#ifdef SCHED_FSS
11525 if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1;
11526#endif
11527#ifdef SCHED_FX
11528 if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1;
11529#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011530#endif
11531
Benjamin Peterson9428d532011-09-14 11:45:52 -040011532#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040011533 if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1;
11534 if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1;
11535 if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1;
11536#endif
11537
Victor Stinner8b905bd2011-10-25 13:34:04 +020011538#ifdef RTLD_LAZY
11539 if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1;
11540#endif
11541#ifdef RTLD_NOW
11542 if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1;
11543#endif
11544#ifdef RTLD_GLOBAL
11545 if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1;
11546#endif
11547#ifdef RTLD_LOCAL
11548 if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1;
11549#endif
11550#ifdef RTLD_NODELETE
11551 if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1;
11552#endif
11553#ifdef RTLD_NOLOAD
11554 if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1;
11555#endif
11556#ifdef RTLD_DEEPBIND
11557 if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1;
11558#endif
11559
Guido van Rossumd48f2521997-12-05 22:19:34 +000011560#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000011561 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011562#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011563 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000011564}
11565
11566
Tim Peters5aa91602002-01-30 05:46:57 +000011567#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011568#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011569#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011570
11571#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011572#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011573#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011574
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011575#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000011576#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011577#define MODNAME "posix"
11578#endif
11579
Martin v. Löwis1a214512008-06-11 05:26:20 +000011580static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000011581 PyModuleDef_HEAD_INIT,
11582 MODNAME,
11583 posix__doc__,
11584 -1,
11585 posix_methods,
11586 NULL,
11587 NULL,
11588 NULL,
11589 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000011590};
11591
11592
Mark Hammondfe51c6d2002-08-02 02:27:13 +000011593PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011594INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000011595{
Victor Stinner8c62be82010-05-06 00:08:46 +000011596 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000011597
Brian Curtin52173d42010-12-02 18:29:18 +000011598#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011599 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000011600#endif
11601
Victor Stinner8c62be82010-05-06 00:08:46 +000011602 m = PyModule_Create(&posixmodule);
11603 if (m == NULL)
11604 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000011605
Victor Stinner8c62be82010-05-06 00:08:46 +000011606 /* Initialize environ dictionary */
11607 v = convertenviron();
11608 Py_XINCREF(v);
11609 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
11610 return NULL;
11611 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000011612
Victor Stinner8c62be82010-05-06 00:08:46 +000011613 if (all_ins(m))
11614 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000011615
Victor Stinner8c62be82010-05-06 00:08:46 +000011616 if (setup_confname_tables(m))
11617 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000011618
Victor Stinner8c62be82010-05-06 00:08:46 +000011619 Py_INCREF(PyExc_OSError);
11620 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000011621
Benjamin Peterson2740af82011-08-02 17:41:34 -050011622#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011623 if (PyType_Ready(&cpu_set_type) < 0)
11624 return NULL;
11625 Py_INCREF(&cpu_set_type);
11626 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
Benjamin Peterson2740af82011-08-02 17:41:34 -050011627#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011628
Guido van Rossumb3d39562000-01-31 18:41:26 +000011629#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011630 if (posix_putenv_garbage == NULL)
11631 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000011632#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011633
Victor Stinner8c62be82010-05-06 00:08:46 +000011634 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020011635#if defined(HAVE_WAITID) && !defined(__APPLE__)
11636 waitid_result_desc.name = MODNAME ".waitid_result";
11637 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11638#endif
11639
Victor Stinner8c62be82010-05-06 00:08:46 +000011640 stat_result_desc.name = MODNAME ".stat_result";
11641 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
11642 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
11643 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11644 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11645 structseq_new = StatResultType.tp_new;
11646 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011647
Victor Stinner8c62be82010-05-06 00:08:46 +000011648 statvfs_result_desc.name = MODNAME ".statvfs_result";
11649 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011650#ifdef NEED_TICKS_PER_SECOND
11651# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000011652 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011653# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000011654 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011655# else
Victor Stinner8c62be82010-05-06 00:08:46 +000011656 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011657# endif
11658#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011659
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050011660#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011661 sched_param_desc.name = MODNAME ".sched_param";
11662 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11663 SchedParamType.tp_new = sched_param_new;
11664#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011665
11666 /* initialize TerminalSize_info */
11667 PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc);
11668 Py_INCREF(&TerminalSizeType);
Victor Stinner8c62be82010-05-06 00:08:46 +000011669 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020011670#if defined(HAVE_WAITID) && !defined(__APPLE__)
11671 Py_INCREF((PyObject*) &WaitidResultType);
11672 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
11673#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011674 Py_INCREF((PyObject*) &StatResultType);
11675 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
11676 Py_INCREF((PyObject*) &StatVFSResultType);
11677 PyModule_AddObject(m, "statvfs_result",
11678 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011679
11680#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011681 Py_INCREF(&SchedParamType);
11682 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011683#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011684 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011685
11686#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000011687 /*
11688 * Step 2 of weak-linking support on Mac OS X.
11689 *
11690 * The code below removes functions that are not available on the
11691 * currently active platform.
11692 *
11693 * This block allow one to use a python binary that was build on
11694 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
11695 * OSX 10.4.
11696 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011697#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011698 if (fstatvfs == NULL) {
11699 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
11700 return NULL;
11701 }
11702 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011703#endif /* HAVE_FSTATVFS */
11704
11705#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011706 if (statvfs == NULL) {
11707 if (PyObject_DelAttrString(m, "statvfs") == -1) {
11708 return NULL;
11709 }
11710 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011711#endif /* HAVE_STATVFS */
11712
11713# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011714 if (lchown == NULL) {
11715 if (PyObject_DelAttrString(m, "lchown") == -1) {
11716 return NULL;
11717 }
11718 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011719#endif /* HAVE_LCHOWN */
11720
11721
11722#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011723
11724 PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
11725
Victor Stinner8c62be82010-05-06 00:08:46 +000011726 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011727
Guido van Rossumb6775db1994-08-01 11:34:53 +000011728}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011729
11730#ifdef __cplusplus
11731}
11732#endif