blob: 8aea64008377d4b1d245f2a049d4fa801ca32762 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000018 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000019 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
49#define INCL_DOS
50#define INCL_DOSERRORS
51#define INCL_DOSPROCESS
52#define INCL_NOPMAPI
53#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000054#if defined(PYCC_GCC)
55#include <ctype.h>
56#include <io.h>
57#include <stdio.h>
58#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000060#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#endif
62
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#endif /* HAVE_SYS_TYPES_H */
66
67#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070
Guido van Rossum36bc6801995-06-14 22:54:23 +000071#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000072#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000073#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000076#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000078
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#ifdef HAVE_FCNTL_H
80#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000081#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000082
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083#ifdef HAVE_GRP_H
84#include <grp.h>
85#endif
86
Barry Warsaw5676bd12003-01-07 20:57:09 +000087#ifdef HAVE_SYSEXITS_H
88#include <sysexits.h>
89#endif /* HAVE_SYSEXITS_H */
90
Anthony Baxter8a560de2004-10-13 15:30:56 +000091#ifdef HAVE_SYS_LOADAVG_H
92#include <sys/loadavg.h>
93#endif
94
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000095#ifdef HAVE_LANGINFO_H
96#include <langinfo.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000107#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000114#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000119#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000120#define HAVE_WAIT 1
121#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000124#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#define HAVE_EXECV 1
126#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000127#define HAVE_SYSTEM 1
128#define HAVE_CWAIT 1
129#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000130#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000131#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000132#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
133/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000134#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000135/* Unix functions that the configure script doesn't check for */
136#define HAVE_EXECV 1
137#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000138#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000139#define HAVE_FORK1 1
140#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#define HAVE_GETCWD 1
142#define HAVE_GETEGID 1
143#define HAVE_GETEUID 1
144#define HAVE_GETGID 1
145#define HAVE_GETPPID 1
146#define HAVE_GETUID 1
147#define HAVE_KILL 1
148#define HAVE_OPENDIR 1
149#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000150#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000151#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000152#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000153#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#endif /* _MSC_VER */
155#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000156#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000157#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000158
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000160
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000161#if defined(__sgi)&&_COMPILER_VERSION>=700
162/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
163 (default) */
164extern char *ctermid_r(char *);
165#endif
166
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000167#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000170#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000171#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000172extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000173#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000176#endif
177#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000178extern int chdir(char *);
179extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000180#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000181extern int chdir(const char *);
182extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000183#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000184#ifdef __BORLANDC__
185extern int chmod(const char *, int);
186#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000188#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000189/*#ifdef HAVE_FCHMOD
190extern int fchmod(int, mode_t);
191#endif*/
192/*#ifdef HAVE_LCHMOD
193extern int lchmod(const char *, mode_t);
194#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000195extern int chown(const char *, uid_t, gid_t);
196extern char *getcwd(char *, int);
197extern char *strerror(int);
198extern int link(const char *, const char *);
199extern int rename(const char *, const char *);
200extern int stat(const char *, struct stat *);
201extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000203extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000204#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000206extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000207#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000209
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000210#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#ifdef HAVE_UTIME_H
213#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000216#ifdef HAVE_SYS_UTIME_H
217#include <sys/utime.h>
218#define HAVE_UTIME_H /* pretend we do for the rest of this file */
219#endif /* HAVE_SYS_UTIME_H */
220
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#ifdef HAVE_SYS_TIMES_H
222#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000223#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224
225#ifdef HAVE_SYS_PARAM_H
226#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000227#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228
229#ifdef HAVE_SYS_UTSNAME_H
230#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000231#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#define NAMLEN(dirent) strlen((dirent)->d_name)
236#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000237#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#include <direct.h>
239#define NAMLEN(dirent) strlen((dirent)->d_name)
240#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000243#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000244#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000249#endif
250#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000255#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258#endif
259#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000261#endif
262#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000264#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000265#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000266#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000268#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000269#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270
Guido van Rossumd48f2521997-12-05 22:19:34 +0000271#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000273#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274
Tim Petersbc2e10e2002-03-03 23:17:02 +0000275#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000276#if defined(PATH_MAX) && PATH_MAX > 1024
277#define MAXPATHLEN PATH_MAX
278#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000279#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000280#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000281#endif /* MAXPATHLEN */
282
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000283#ifdef UNION_WAIT
284/* Emulate some macros on systems that have a union instead of macros */
285
286#ifndef WIFEXITED
287#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
288#endif
289
290#ifndef WEXITSTATUS
291#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
292#endif
293
294#ifndef WTERMSIG
295#define WTERMSIG(u_wait) ((u_wait).w_termsig)
296#endif
297
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298#define WAIT_TYPE union wait
299#define WAIT_STATUS_INT(s) (s.w_status)
300
301#else /* !UNION_WAIT */
302#define WAIT_TYPE int
303#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000304#endif /* UNION_WAIT */
305
Greg Wardb48bc172000-03-01 21:51:56 +0000306/* Don't use the "_r" form if we don't need it (also, won't have a
307 prototype for it, at least on Solaris -- maybe others as well?). */
308#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
309#define USE_CTERMID_R
310#endif
311
Fred Drake699f3522000-06-29 21:12:41 +0000312/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000313#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000314#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000315# define STAT win32_stat
316# define FSTAT win32_fstat
317# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000318#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000319# define STAT stat
320# define FSTAT fstat
321# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000322#endif
323
Tim Peters11b23062003-04-23 02:39:17 +0000324#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000325#include <sys/mkdev.h>
326#else
327#if defined(MAJOR_IN_SYSMACROS)
328#include <sys/sysmacros.h>
329#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000330#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
331#include <sys/mkdev.h>
332#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000333#endif
Fred Drake699f3522000-06-29 21:12:41 +0000334
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000335#if defined _MSC_VER && _MSC_VER >= 1400
336/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
337 * valid and throw an assertion if it isn't.
338 * Normally, an invalid fd is likely to be a C program error and therefore
339 * an assertion can be useful, but it does contradict the POSIX standard
340 * which for write(2) states:
341 * "Otherwise, -1 shall be returned and errno set to indicate the error."
342 * "[EBADF] The fildes argument is not a valid file descriptor open for
343 * writing."
344 * Furthermore, python allows the user to enter any old integer
345 * as a fd and should merely raise a python exception on error.
346 * The Microsoft CRT doesn't provide an official way to check for the
347 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000348 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000349 * internal structures involved.
350 * The structures below must be updated for each version of visual studio
351 * according to the file internal.h in the CRT source, until MS comes
352 * up with a less hacky way to do this.
353 * (all of this is to avoid globally modifying the CRT behaviour using
354 * _set_invalid_parameter_handler() and _CrtSetReportMode())
355 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000356/* The actual size of the structure is determined at runtime.
357 * Only the first items must be present.
358 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000359typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000360 intptr_t osfhnd;
361 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000362} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000363
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000364extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000365#define IOINFO_L2E 5
366#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
367#define IOINFO_ARRAYS 64
368#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
369#define FOPEN 0x01
370#define _NO_CONSOLE_FILENO (intptr_t)-2
371
372/* This function emulates what the windows CRT does to validate file handles */
373int
374_PyVerify_fd(int fd)
375{
Victor Stinner8c62be82010-05-06 00:08:46 +0000376 const int i1 = fd >> IOINFO_L2E;
377 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000378
Victor Stinner8c62be82010-05-06 00:08:46 +0000379 static int sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000380
Victor Stinner8c62be82010-05-06 00:08:46 +0000381 /* Determine the actual size of the ioinfo structure,
382 * as used by the CRT loaded in memory
383 */
384 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
385 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
386 }
387 if (sizeof_ioinfo == 0) {
388 /* This should not happen... */
389 goto fail;
390 }
391
392 /* See that it isn't a special CLEAR fileno */
393 if (fd != _NO_CONSOLE_FILENO) {
394 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
395 * we check pointer validity and other info
396 */
397 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
398 /* finally, check that the file is open */
399 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
400 if (info->osfile & FOPEN) {
401 return 1;
402 }
403 }
404 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000405 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000406 errno = EBADF;
407 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000408}
409
410/* the special case of checking dup2. The target fd must be in a sensible range */
411static int
412_PyVerify_fd_dup2(int fd1, int fd2)
413{
Victor Stinner8c62be82010-05-06 00:08:46 +0000414 if (!_PyVerify_fd(fd1))
415 return 0;
416 if (fd2 == _NO_CONSOLE_FILENO)
417 return 0;
418 if ((unsigned)fd2 < _NHANDLE_)
419 return 1;
420 else
421 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000422}
423#else
424/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
425#define _PyVerify_fd_dup2(A, B) (1)
426#endif
427
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000429#ifdef WITH_NEXT_FRAMEWORK
430/* On Darwin/MacOSX a shared library or framework has no access to
431** environ directly, we must obtain it with _NSGetEnviron().
432*/
433#include <crt_externs.h>
434static char **environ;
435#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000437#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438
Barry Warsaw53699e91996-12-10 23:23:01 +0000439static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000440convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
Victor Stinner8c62be82010-05-06 00:08:46 +0000442 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000443#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000444 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000445#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000446 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000447#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000448#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000449 APIRET rc;
450 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
451#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000452
Victor Stinner8c62be82010-05-06 00:08:46 +0000453 d = PyDict_New();
454 if (d == NULL)
455 return NULL;
456#ifdef WITH_NEXT_FRAMEWORK
457 if (environ == NULL)
458 environ = *_NSGetEnviron();
459#endif
460#ifdef MS_WINDOWS
461 /* _wenviron must be initialized in this way if the program is started
462 through main() instead of wmain(). */
463 _wgetenv(L"");
464 if (_wenviron == NULL)
465 return d;
466 /* This part ignores errors */
467 for (e = _wenviron; *e != NULL; e++) {
468 PyObject *k;
469 PyObject *v;
470 wchar_t *p = wcschr(*e, L'=');
471 if (p == NULL)
472 continue;
473 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
474 if (k == NULL) {
475 PyErr_Clear();
476 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000477 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000478 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
479 if (v == NULL) {
480 PyErr_Clear();
481 Py_DECREF(k);
482 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000483 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000484 if (PyDict_GetItem(d, k) == NULL) {
485 if (PyDict_SetItem(d, k, v) != 0)
486 PyErr_Clear();
487 }
488 Py_DECREF(k);
489 Py_DECREF(v);
490 }
491#else
492 if (environ == NULL)
493 return d;
494 /* This part ignores errors */
495 for (e = environ; *e != NULL; e++) {
496 PyObject *k;
497 PyObject *v;
498 char *p = strchr(*e, '=');
499 if (p == NULL)
500 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000501 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000502 if (k == NULL) {
503 PyErr_Clear();
504 continue;
505 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000506 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000507 if (v == NULL) {
508 PyErr_Clear();
509 Py_DECREF(k);
510 continue;
511 }
512 if (PyDict_GetItem(d, k) == NULL) {
513 if (PyDict_SetItem(d, k, v) != 0)
514 PyErr_Clear();
515 }
516 Py_DECREF(k);
517 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000518 }
519#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000520#if defined(PYOS_OS2)
521 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
522 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
523 PyObject *v = PyBytes_FromString(buffer);
524 PyDict_SetItemString(d, "BEGINLIBPATH", v);
525 Py_DECREF(v);
526 }
527 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
528 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
529 PyObject *v = PyBytes_FromString(buffer);
530 PyDict_SetItemString(d, "ENDLIBPATH", v);
531 Py_DECREF(v);
532 }
533#endif
534 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000535}
536
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537/* Set a POSIX-specific error from errno, and return NULL */
538
Barry Warsawd58d7641998-07-23 16:14:40 +0000539static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000540posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000541{
Victor Stinner8c62be82010-05-06 00:08:46 +0000542 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543}
Barry Warsawd58d7641998-07-23 16:14:40 +0000544static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000545posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000546{
Victor Stinner8c62be82010-05-06 00:08:46 +0000547 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000548}
549
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000550#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000551static PyObject *
552posix_error_with_unicode_filename(Py_UNICODE* name)
553{
Victor Stinner8c62be82010-05-06 00:08:46 +0000554 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000555}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000556#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000557
558
Mark Hammondef8b6542001-05-13 08:04:26 +0000559static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000560posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000561{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000562 PyObject *name_str, *rc;
563 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
564 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000565 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000566 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
567 name_str);
568 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000569 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000570}
571
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000572#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000573static PyObject *
574win32_error(char* function, char* filename)
575{
Victor Stinner8c62be82010-05-06 00:08:46 +0000576 /* XXX We should pass the function name along in the future.
577 (winreg.c also wants to pass the function name.)
578 This would however require an additional param to the
579 Windows error object, which is non-trivial.
580 */
581 errno = GetLastError();
582 if (filename)
583 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
584 else
585 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000586}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000587
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000588static PyObject *
589win32_error_unicode(char* function, Py_UNICODE* filename)
590{
Victor Stinner8c62be82010-05-06 00:08:46 +0000591 /* XXX - see win32_error for comments on 'function' */
592 errno = GetLastError();
593 if (filename)
594 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
595 else
596 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000597}
598
Thomas Wouters477c8d52006-05-27 19:21:47 +0000599static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000600convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000601{
Victor Stinner8c62be82010-05-06 00:08:46 +0000602 if (PyUnicode_CheckExact(*param))
603 Py_INCREF(*param);
604 else if (PyUnicode_Check(*param))
605 /* For a Unicode subtype that's not a Unicode object,
606 return a true Unicode object with the same data. */
607 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
608 PyUnicode_GET_SIZE(*param));
609 else
610 *param = PyUnicode_FromEncodedObject(*param,
611 Py_FileSystemDefaultEncoding,
612 "strict");
613 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000614}
615
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000616#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617
Guido van Rossumd48f2521997-12-05 22:19:34 +0000618#if defined(PYOS_OS2)
619/**********************************************************************
620 * Helper Function to Trim and Format OS/2 Messages
621 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000622static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000623os2_formatmsg(char *msgbuf, int msglen, char *reason)
624{
625 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
626
627 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
628 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
629
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000630 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000631 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
632 }
633
634 /* Add Optional Reason Text */
635 if (reason) {
636 strcat(msgbuf, " : ");
637 strcat(msgbuf, reason);
638 }
639}
640
641/**********************************************************************
642 * Decode an OS/2 Operating System Error Code
643 *
644 * A convenience function to lookup an OS/2 error code and return a
645 * text message we can use to raise a Python exception.
646 *
647 * Notes:
648 * The messages for errors returned from the OS/2 kernel reside in
649 * the file OSO001.MSG in the \OS2 directory hierarchy.
650 *
651 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000652static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000653os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
654{
655 APIRET rc;
656 ULONG msglen;
657
658 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
659 Py_BEGIN_ALLOW_THREADS
660 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
661 errorcode, "oso001.msg", &msglen);
662 Py_END_ALLOW_THREADS
663
664 if (rc == NO_ERROR)
665 os2_formatmsg(msgbuf, msglen, reason);
666 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000667 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000668 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000669
670 return msgbuf;
671}
672
673/* Set an OS/2-specific error and return NULL. OS/2 kernel
674 errors are not in a global variable e.g. 'errno' nor are
675 they congruent with posix error numbers. */
676
Victor Stinner8c62be82010-05-06 00:08:46 +0000677static PyObject *
678os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000679{
680 char text[1024];
681 PyObject *v;
682
683 os2_strerror(text, sizeof(text), code, "");
684
685 v = Py_BuildValue("(is)", code, text);
686 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000687 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000688 Py_DECREF(v);
689 }
690 return NULL; /* Signal to Python that an Exception is Pending */
691}
692
693#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694
695/* POSIX generic methods */
696
Barry Warsaw53699e91996-12-10 23:23:01 +0000697static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000698posix_fildes(PyObject *fdobj, int (*func)(int))
699{
Victor Stinner8c62be82010-05-06 00:08:46 +0000700 int fd;
701 int res;
702 fd = PyObject_AsFileDescriptor(fdobj);
703 if (fd < 0)
704 return NULL;
705 if (!_PyVerify_fd(fd))
706 return posix_error();
707 Py_BEGIN_ALLOW_THREADS
708 res = (*func)(fd);
709 Py_END_ALLOW_THREADS
710 if (res < 0)
711 return posix_error();
712 Py_INCREF(Py_None);
713 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000714}
Guido van Rossum21142a01999-01-08 21:05:37 +0000715
716static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000717posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718{
Victor Stinner8c62be82010-05-06 00:08:46 +0000719 PyObject *opath1 = NULL;
720 char *path1;
721 int res;
722 if (!PyArg_ParseTuple(args, format,
723 PyUnicode_FSConverter, &opath1))
724 return NULL;
725 path1 = PyBytes_AsString(opath1);
726 Py_BEGIN_ALLOW_THREADS
727 res = (*func)(path1);
728 Py_END_ALLOW_THREADS
729 if (res < 0)
730 return posix_error_with_allocated_filename(opath1);
731 Py_DECREF(opath1);
732 Py_INCREF(Py_None);
733 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000734}
735
Barry Warsaw53699e91996-12-10 23:23:01 +0000736static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000737posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000738 char *format,
739 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000740{
Victor Stinner8c62be82010-05-06 00:08:46 +0000741 PyObject *opath1 = NULL, *opath2 = NULL;
742 char *path1, *path2;
743 int res;
744 if (!PyArg_ParseTuple(args, format,
745 PyUnicode_FSConverter, &opath1,
746 PyUnicode_FSConverter, &opath2)) {
747 return NULL;
748 }
749 path1 = PyBytes_AsString(opath1);
750 path2 = PyBytes_AsString(opath2);
751 Py_BEGIN_ALLOW_THREADS
752 res = (*func)(path1, path2);
753 Py_END_ALLOW_THREADS
754 Py_DECREF(opath1);
755 Py_DECREF(opath2);
756 if (res != 0)
757 /* XXX how to report both path1 and path2??? */
758 return posix_error();
759 Py_INCREF(Py_None);
760 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000761}
762
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000763#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000764static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000765win32_1str(PyObject* args, char* func,
766 char* format, BOOL (__stdcall *funcA)(LPCSTR),
767 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000768{
Victor Stinner8c62be82010-05-06 00:08:46 +0000769 PyObject *uni;
770 char *ansi;
771 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000772
Victor Stinner8c62be82010-05-06 00:08:46 +0000773 if (!PyArg_ParseTuple(args, wformat, &uni))
774 PyErr_Clear();
775 else {
776 Py_BEGIN_ALLOW_THREADS
777 result = funcW(PyUnicode_AsUnicode(uni));
778 Py_END_ALLOW_THREADS
779 if (!result)
780 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
781 Py_INCREF(Py_None);
782 return Py_None;
783 }
784 if (!PyArg_ParseTuple(args, format, &ansi))
785 return NULL;
786 Py_BEGIN_ALLOW_THREADS
787 result = funcA(ansi);
788 Py_END_ALLOW_THREADS
789 if (!result)
790 return win32_error(func, ansi);
791 Py_INCREF(Py_None);
792 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000793
794}
795
796/* This is a reimplementation of the C library's chdir function,
797 but one that produces Win32 errors instead of DOS error codes.
798 chdir is essentially a wrapper around SetCurrentDirectory; however,
799 it also needs to set "magic" environment variables indicating
800 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000801static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000802win32_chdir(LPCSTR path)
803{
Victor Stinner8c62be82010-05-06 00:08:46 +0000804 char new_path[MAX_PATH+1];
805 int result;
806 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000807
Victor Stinner8c62be82010-05-06 00:08:46 +0000808 if(!SetCurrentDirectoryA(path))
809 return FALSE;
810 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
811 if (!result)
812 return FALSE;
813 /* In the ANSI API, there should not be any paths longer
814 than MAX_PATH. */
815 assert(result <= MAX_PATH+1);
816 if (strncmp(new_path, "\\\\", 2) == 0 ||
817 strncmp(new_path, "//", 2) == 0)
818 /* UNC path, nothing to do. */
819 return TRUE;
820 env[1] = new_path[0];
821 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000822}
823
824/* The Unicode version differs from the ANSI version
825 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000826static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000827win32_wchdir(LPCWSTR path)
828{
Victor Stinner8c62be82010-05-06 00:08:46 +0000829 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
830 int result;
831 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000832
Victor Stinner8c62be82010-05-06 00:08:46 +0000833 if(!SetCurrentDirectoryW(path))
834 return FALSE;
835 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
836 if (!result)
837 return FALSE;
838 if (result > MAX_PATH+1) {
839 new_path = malloc(result * sizeof(wchar_t));
840 if (!new_path) {
841 SetLastError(ERROR_OUTOFMEMORY);
842 return FALSE;
843 }
844 result = GetCurrentDirectoryW(result, new_path);
845 if (!result) {
846 free(new_path);
847 return FALSE;
848 }
849 }
850 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
851 wcsncmp(new_path, L"//", 2) == 0)
852 /* UNC path, nothing to do. */
853 return TRUE;
854 env[1] = new_path[0];
855 result = SetEnvironmentVariableW(env, new_path);
856 if (new_path != _new_path)
857 free(new_path);
858 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859}
860#endif
861
Martin v. Löwis14694662006-02-03 12:54:16 +0000862#ifdef MS_WINDOWS
863/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
864 - time stamps are restricted to second resolution
865 - file modification times suffer from forth-and-back conversions between
866 UTC and local time
867 Therefore, we implement our own stat, based on the Win32 API directly.
868*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000869#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000870
871struct win32_stat{
872 int st_dev;
873 __int64 st_ino;
874 unsigned short st_mode;
875 int st_nlink;
876 int st_uid;
877 int st_gid;
878 int st_rdev;
879 __int64 st_size;
880 int st_atime;
881 int st_atime_nsec;
882 int st_mtime;
883 int st_mtime_nsec;
884 int st_ctime;
885 int st_ctime_nsec;
886};
887
888static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
889
890static void
891FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
892{
Victor Stinner8c62be82010-05-06 00:08:46 +0000893 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
894 /* Cannot simply cast and dereference in_ptr,
895 since it might not be aligned properly */
896 __int64 in;
897 memcpy(&in, in_ptr, sizeof(in));
898 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
899 /* XXX Win32 supports time stamps past 2038; we currently don't */
900 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000901}
902
Thomas Wouters477c8d52006-05-27 19:21:47 +0000903static void
904time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
905{
Victor Stinner8c62be82010-05-06 00:08:46 +0000906 /* XXX endianness */
907 __int64 out;
908 out = time_in + secs_between_epochs;
909 out = out * 10000000 + nsec_in / 100;
910 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000911}
912
Martin v. Löwis14694662006-02-03 12:54:16 +0000913/* Below, we *know* that ugo+r is 0444 */
914#if _S_IREAD != 0400
915#error Unsupported C library
916#endif
917static int
918attributes_to_mode(DWORD attr)
919{
Victor Stinner8c62be82010-05-06 00:08:46 +0000920 int m = 0;
921 if (attr & FILE_ATTRIBUTE_DIRECTORY)
922 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
923 else
924 m |= _S_IFREG;
925 if (attr & FILE_ATTRIBUTE_READONLY)
926 m |= 0444;
927 else
928 m |= 0666;
929 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +0000930}
931
932static int
933attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
934{
Victor Stinner8c62be82010-05-06 00:08:46 +0000935 memset(result, 0, sizeof(*result));
936 result->st_mode = attributes_to_mode(info->dwFileAttributes);
937 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
938 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
939 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
940 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +0000941
Victor Stinner8c62be82010-05-06 00:08:46 +0000942 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +0000943}
944
Guido van Rossumd8faa362007-04-27 19:54:29 +0000945static BOOL
946attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
947{
Victor Stinner8c62be82010-05-06 00:08:46 +0000948 HANDLE hFindFile;
949 WIN32_FIND_DATAA FileData;
950 hFindFile = FindFirstFileA(pszFile, &FileData);
951 if (hFindFile == INVALID_HANDLE_VALUE)
952 return FALSE;
953 FindClose(hFindFile);
954 pfad->dwFileAttributes = FileData.dwFileAttributes;
955 pfad->ftCreationTime = FileData.ftCreationTime;
956 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
957 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
958 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
959 pfad->nFileSizeLow = FileData.nFileSizeLow;
960 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000961}
962
963static BOOL
964attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
965{
Victor Stinner8c62be82010-05-06 00:08:46 +0000966 HANDLE hFindFile;
967 WIN32_FIND_DATAW FileData;
968 hFindFile = FindFirstFileW(pszFile, &FileData);
969 if (hFindFile == INVALID_HANDLE_VALUE)
970 return FALSE;
971 FindClose(hFindFile);
972 pfad->dwFileAttributes = FileData.dwFileAttributes;
973 pfad->ftCreationTime = FileData.ftCreationTime;
974 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
975 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
976 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
977 pfad->nFileSizeLow = FileData.nFileSizeLow;
978 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000979}
980
Victor Stinner8c62be82010-05-06 00:08:46 +0000981static int
Martin v. Löwis14694662006-02-03 12:54:16 +0000982win32_stat(const char* path, struct win32_stat *result)
983{
Victor Stinner8c62be82010-05-06 00:08:46 +0000984 WIN32_FILE_ATTRIBUTE_DATA info;
985 int code;
986 char *dot;
987 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
988 if (GetLastError() != ERROR_SHARING_VIOLATION) {
989 /* Protocol violation: we explicitly clear errno, instead of
990 setting it to a POSIX error. Callers should use GetLastError. */
991 errno = 0;
992 return -1;
993 } else {
994 /* Could not get attributes on open file. Fall back to
995 reading the directory. */
996 if (!attributes_from_dir(path, &info)) {
997 /* Very strange. This should not fail now */
998 errno = 0;
999 return -1;
1000 }
1001 }
1002 }
1003 code = attribute_data_to_stat(&info, result);
1004 if (code != 0)
1005 return code;
1006 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1007 dot = strrchr(path, '.');
1008 if (dot) {
1009 if (stricmp(dot, ".bat") == 0 ||
1010 stricmp(dot, ".cmd") == 0 ||
1011 stricmp(dot, ".exe") == 0 ||
1012 stricmp(dot, ".com") == 0)
1013 result->st_mode |= 0111;
1014 }
1015 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001016}
1017
Victor Stinner8c62be82010-05-06 00:08:46 +00001018static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001019win32_wstat(const wchar_t* path, struct win32_stat *result)
1020{
Victor Stinner8c62be82010-05-06 00:08:46 +00001021 int code;
1022 const wchar_t *dot;
1023 WIN32_FILE_ATTRIBUTE_DATA info;
1024 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1025 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1026 /* Protocol violation: we explicitly clear errno, instead of
1027 setting it to a POSIX error. Callers should use GetLastError. */
1028 errno = 0;
1029 return -1;
1030 } else {
1031 /* Could not get attributes on open file. Fall back to
1032 reading the directory. */
1033 if (!attributes_from_dir_w(path, &info)) {
1034 /* Very strange. This should not fail now */
1035 errno = 0;
1036 return -1;
1037 }
1038 }
1039 }
1040 code = attribute_data_to_stat(&info, result);
1041 if (code < 0)
1042 return code;
1043 /* Set IFEXEC if it is an .exe, .bat, ... */
1044 dot = wcsrchr(path, '.');
1045 if (dot) {
1046 if (_wcsicmp(dot, L".bat") == 0 ||
1047 _wcsicmp(dot, L".cmd") == 0 ||
1048 _wcsicmp(dot, L".exe") == 0 ||
1049 _wcsicmp(dot, L".com") == 0)
1050 result->st_mode |= 0111;
1051 }
1052 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001053}
1054
1055static int
1056win32_fstat(int file_number, struct win32_stat *result)
1057{
Victor Stinner8c62be82010-05-06 00:08:46 +00001058 BY_HANDLE_FILE_INFORMATION info;
1059 HANDLE h;
1060 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001061
Victor Stinner8c62be82010-05-06 00:08:46 +00001062 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001063
Victor Stinner8c62be82010-05-06 00:08:46 +00001064 /* Protocol violation: we explicitly clear errno, instead of
1065 setting it to a POSIX error. Callers should use GetLastError. */
1066 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001067
Victor Stinner8c62be82010-05-06 00:08:46 +00001068 if (h == INVALID_HANDLE_VALUE) {
1069 /* This is really a C library error (invalid file handle).
1070 We set the Win32 error to the closes one matching. */
1071 SetLastError(ERROR_INVALID_HANDLE);
1072 return -1;
1073 }
1074 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001075
Victor Stinner8c62be82010-05-06 00:08:46 +00001076 type = GetFileType(h);
1077 if (type == FILE_TYPE_UNKNOWN) {
1078 DWORD error = GetLastError();
1079 if (error != 0) {
1080 return -1;
1081 }
1082 /* else: valid but unknown file */
1083 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001084
Victor Stinner8c62be82010-05-06 00:08:46 +00001085 if (type != FILE_TYPE_DISK) {
1086 if (type == FILE_TYPE_CHAR)
1087 result->st_mode = _S_IFCHR;
1088 else if (type == FILE_TYPE_PIPE)
1089 result->st_mode = _S_IFIFO;
1090 return 0;
1091 }
1092
1093 if (!GetFileInformationByHandle(h, &info)) {
1094 return -1;
1095 }
1096
1097 /* similar to stat() */
1098 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1099 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1100 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1101 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1102 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1103 /* specific to fstat() */
1104 result->st_nlink = info.nNumberOfLinks;
1105 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1106 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001107}
1108
1109#endif /* MS_WINDOWS */
1110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001111PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001112"stat_result: Result from stat or lstat.\n\n\
1113This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001114 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001115or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1116\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001117Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1118or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001119\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001120See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001121
1122static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001123 {"st_mode", "protection bits"},
1124 {"st_ino", "inode"},
1125 {"st_dev", "device"},
1126 {"st_nlink", "number of hard links"},
1127 {"st_uid", "user ID of owner"},
1128 {"st_gid", "group ID of owner"},
1129 {"st_size", "total size, in bytes"},
1130 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1131 {NULL, "integer time of last access"},
1132 {NULL, "integer time of last modification"},
1133 {NULL, "integer time of last change"},
1134 {"st_atime", "time of last access"},
1135 {"st_mtime", "time of last modification"},
1136 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001137#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001138 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001139#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001140#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001141 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001142#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001143#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001144 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001145#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001146#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001147 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001148#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001149#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001150 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001151#endif
1152#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001153 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001154#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001155 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001156};
1157
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001158#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001159#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001160#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001161#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001162#endif
1163
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001164#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001165#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1166#else
1167#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1168#endif
1169
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001170#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001171#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1172#else
1173#define ST_RDEV_IDX ST_BLOCKS_IDX
1174#endif
1175
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001176#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1177#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1178#else
1179#define ST_FLAGS_IDX ST_RDEV_IDX
1180#endif
1181
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001182#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001183#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001184#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001185#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001186#endif
1187
1188#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1189#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1190#else
1191#define ST_BIRTHTIME_IDX ST_GEN_IDX
1192#endif
1193
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001194static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001195 "stat_result", /* name */
1196 stat_result__doc__, /* doc */
1197 stat_result_fields,
1198 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001199};
1200
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001201PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001202"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1203This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001204 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001205or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001206\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001207See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001208
1209static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001210 {"f_bsize", },
1211 {"f_frsize", },
1212 {"f_blocks", },
1213 {"f_bfree", },
1214 {"f_bavail", },
1215 {"f_files", },
1216 {"f_ffree", },
1217 {"f_favail", },
1218 {"f_flag", },
1219 {"f_namemax",},
1220 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001221};
1222
1223static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001224 "statvfs_result", /* name */
1225 statvfs_result__doc__, /* doc */
1226 statvfs_result_fields,
1227 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001228};
1229
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001230static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001231static PyTypeObject StatResultType;
1232static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001233static newfunc structseq_new;
1234
1235static PyObject *
1236statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1237{
Victor Stinner8c62be82010-05-06 00:08:46 +00001238 PyStructSequence *result;
1239 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001240
Victor Stinner8c62be82010-05-06 00:08:46 +00001241 result = (PyStructSequence*)structseq_new(type, args, kwds);
1242 if (!result)
1243 return NULL;
1244 /* If we have been initialized from a tuple,
1245 st_?time might be set to None. Initialize it
1246 from the int slots. */
1247 for (i = 7; i <= 9; i++) {
1248 if (result->ob_item[i+3] == Py_None) {
1249 Py_DECREF(Py_None);
1250 Py_INCREF(result->ob_item[i]);
1251 result->ob_item[i+3] = result->ob_item[i];
1252 }
1253 }
1254 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001255}
1256
1257
1258
1259/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001260static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001261
1262PyDoc_STRVAR(stat_float_times__doc__,
1263"stat_float_times([newval]) -> oldval\n\n\
1264Determine whether os.[lf]stat represents time stamps as float objects.\n\
1265If newval is True, future calls to stat() return floats, if it is False,\n\
1266future calls return ints. \n\
1267If newval is omitted, return the current setting.\n");
1268
1269static PyObject*
1270stat_float_times(PyObject* self, PyObject *args)
1271{
Victor Stinner8c62be82010-05-06 00:08:46 +00001272 int newval = -1;
1273 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1274 return NULL;
1275 if (newval == -1)
1276 /* Return old value */
1277 return PyBool_FromLong(_stat_float_times);
1278 _stat_float_times = newval;
1279 Py_INCREF(Py_None);
1280 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001281}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001282
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001283static void
1284fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1285{
Victor Stinner8c62be82010-05-06 00:08:46 +00001286 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001287#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001288 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001289#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001290 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001291#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001292 if (!ival)
1293 return;
1294 if (_stat_float_times) {
1295 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1296 } else {
1297 fval = ival;
1298 Py_INCREF(fval);
1299 }
1300 PyStructSequence_SET_ITEM(v, index, ival);
1301 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001302}
1303
Tim Peters5aa91602002-01-30 05:46:57 +00001304/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001305 (used by posix_stat() and posix_fstat()) */
1306static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001307_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001308{
Victor Stinner8c62be82010-05-06 00:08:46 +00001309 unsigned long ansec, mnsec, cnsec;
1310 PyObject *v = PyStructSequence_New(&StatResultType);
1311 if (v == NULL)
1312 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001313
Victor Stinner8c62be82010-05-06 00:08:46 +00001314 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001315#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001316 PyStructSequence_SET_ITEM(v, 1,
1317 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001318#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001319 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001320#endif
1321#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001322 PyStructSequence_SET_ITEM(v, 2,
1323 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001324#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001325 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001326#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001327 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1328 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1329 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001330#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001331 PyStructSequence_SET_ITEM(v, 6,
1332 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001333#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001334 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001335#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001336
Martin v. Löwis14694662006-02-03 12:54:16 +00001337#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001338 ansec = st->st_atim.tv_nsec;
1339 mnsec = st->st_mtim.tv_nsec;
1340 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001341#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001342 ansec = st->st_atimespec.tv_nsec;
1343 mnsec = st->st_mtimespec.tv_nsec;
1344 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001345#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001346 ansec = st->st_atime_nsec;
1347 mnsec = st->st_mtime_nsec;
1348 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001349#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001350 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001351#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001352 fill_time(v, 7, st->st_atime, ansec);
1353 fill_time(v, 8, st->st_mtime, mnsec);
1354 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001355
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001356#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001357 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1358 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001359#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001360#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001361 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1362 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001363#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001364#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001365 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1366 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001367#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001368#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001369 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1370 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001371#endif
1372#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001373 {
1374 PyObject *val;
1375 unsigned long bsec,bnsec;
1376 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001377#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001378 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001379#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001380 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001381#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001382 if (_stat_float_times) {
1383 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1384 } else {
1385 val = PyLong_FromLong((long)bsec);
1386 }
1387 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1388 val);
1389 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001390#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001391#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001392 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1393 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001394#endif
Fred Drake699f3522000-06-29 21:12:41 +00001395
Victor Stinner8c62be82010-05-06 00:08:46 +00001396 if (PyErr_Occurred()) {
1397 Py_DECREF(v);
1398 return NULL;
1399 }
Fred Drake699f3522000-06-29 21:12:41 +00001400
Victor Stinner8c62be82010-05-06 00:08:46 +00001401 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001402}
1403
Martin v. Löwisd8948722004-06-02 09:57:56 +00001404#ifdef MS_WINDOWS
1405
1406/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1407 where / can be used in place of \ and the trailing slash is optional.
1408 Both SERVER and SHARE must have at least one character.
1409*/
1410
1411#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1412#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001413#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001414#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001415#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001416
Tim Peters4ad82172004-08-30 17:02:04 +00001417static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001418IsUNCRootA(char *path, int pathlen)
1419{
Victor Stinner8c62be82010-05-06 00:08:46 +00001420 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001421
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001423
Victor Stinner8c62be82010-05-06 00:08:46 +00001424 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1425 /* minimum UNCRoot is \\x\y */
1426 return FALSE;
1427 for (i = 2; i < pathlen ; i++)
1428 if (ISSLASH(path[i])) break;
1429 if (i == 2 || i == pathlen)
1430 /* do not allow \\\SHARE or \\SERVER */
1431 return FALSE;
1432 share = i+1;
1433 for (i = share; i < pathlen; i++)
1434 if (ISSLASH(path[i])) break;
1435 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001436
Victor Stinner8c62be82010-05-06 00:08:46 +00001437 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001438}
1439
Tim Peters4ad82172004-08-30 17:02:04 +00001440static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001441IsUNCRootW(Py_UNICODE *path, int pathlen)
1442{
Victor Stinner8c62be82010-05-06 00:08:46 +00001443 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001444
Victor Stinner8c62be82010-05-06 00:08:46 +00001445 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001446
Victor Stinner8c62be82010-05-06 00:08:46 +00001447 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1448 /* minimum UNCRoot is \\x\y */
1449 return FALSE;
1450 for (i = 2; i < pathlen ; i++)
1451 if (ISSLASH(path[i])) break;
1452 if (i == 2 || i == pathlen)
1453 /* do not allow \\\SHARE or \\SERVER */
1454 return FALSE;
1455 share = i+1;
1456 for (i = share; i < pathlen; i++)
1457 if (ISSLASH(path[i])) break;
1458 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001459
Victor Stinner8c62be82010-05-06 00:08:46 +00001460 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001461}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001462#endif /* MS_WINDOWS */
1463
Barry Warsaw53699e91996-12-10 23:23:01 +00001464static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001465posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001466 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001467#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001468 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001469#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001470 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001471#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001472 char *wformat,
1473 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001474{
Victor Stinner8c62be82010-05-06 00:08:46 +00001475 STRUCT_STAT st;
1476 PyObject *opath;
1477 char *path;
1478 int res;
1479 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001480
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001481#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001482 PyUnicodeObject *po;
1483 if (PyArg_ParseTuple(args, wformat, &po)) {
1484 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001485
Victor Stinner8c62be82010-05-06 00:08:46 +00001486 Py_BEGIN_ALLOW_THREADS
1487 /* PyUnicode_AS_UNICODE result OK without
1488 thread lock as it is a simple dereference. */
1489 res = wstatfunc(wpath, &st);
1490 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001491
Victor Stinner8c62be82010-05-06 00:08:46 +00001492 if (res != 0)
1493 return win32_error_unicode("stat", wpath);
1494 return _pystat_fromstructstat(&st);
1495 }
1496 /* Drop the argument parsing error as narrow strings
1497 are also valid. */
1498 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001499#endif
1500
Victor Stinner8c62be82010-05-06 00:08:46 +00001501 if (!PyArg_ParseTuple(args, format,
1502 PyUnicode_FSConverter, &opath))
1503 return NULL;
1504 path = PyBytes_AsString(opath);
1505 Py_BEGIN_ALLOW_THREADS
1506 res = (*statfunc)(path, &st);
1507 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001508
Victor Stinner8c62be82010-05-06 00:08:46 +00001509 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001510#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001511 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001512#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001513 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001514#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001515 }
1516 else
1517 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001518
Victor Stinner8c62be82010-05-06 00:08:46 +00001519 Py_DECREF(opath);
1520 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001521}
1522
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001523/* POSIX methods */
1524
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001525PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001526"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001527Use the real uid/gid to test for access to a path. Note that most\n\
1528operations will use the effective uid/gid, therefore this routine can\n\
1529be used in a suid/sgid environment to test if the invoking user has the\n\
1530specified access to the path. The mode argument can be F_OK to test\n\
1531existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001532
1533static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001534posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001535{
Victor Stinner8c62be82010-05-06 00:08:46 +00001536 PyObject *opath;
1537 char *path;
1538 int mode;
1539
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001540#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001541 DWORD attr;
1542 PyUnicodeObject *po;
1543 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1544 Py_BEGIN_ALLOW_THREADS
1545 /* PyUnicode_AS_UNICODE OK without thread lock as
1546 it is a simple dereference. */
1547 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1548 Py_END_ALLOW_THREADS
1549 goto finish;
1550 }
1551 /* Drop the argument parsing error as narrow strings
1552 are also valid. */
1553 PyErr_Clear();
1554 if (!PyArg_ParseTuple(args, "O&i:access",
1555 PyUnicode_FSConverter, &opath, &mode))
1556 return NULL;
1557 path = PyBytes_AsString(opath);
1558 Py_BEGIN_ALLOW_THREADS
1559 attr = GetFileAttributesA(path);
1560 Py_END_ALLOW_THREADS
1561 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001562finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001563 if (attr == 0xFFFFFFFF)
1564 /* File does not exist, or cannot read attributes */
1565 return PyBool_FromLong(0);
1566 /* Access is possible if either write access wasn't requested, or
1567 the file isn't read-only, or if it's a directory, as there are
1568 no read-only directories on Windows. */
1569 return PyBool_FromLong(!(mode & 2)
1570 || !(attr & FILE_ATTRIBUTE_READONLY)
1571 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001572#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001573 int res;
1574 if (!PyArg_ParseTuple(args, "O&i:access",
1575 PyUnicode_FSConverter, &opath, &mode))
1576 return NULL;
1577 path = PyBytes_AsString(opath);
1578 Py_BEGIN_ALLOW_THREADS
1579 res = access(path, mode);
1580 Py_END_ALLOW_THREADS
1581 Py_DECREF(opath);
1582 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001583#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001584}
1585
Guido van Rossumd371ff11999-01-25 16:12:23 +00001586#ifndef F_OK
1587#define F_OK 0
1588#endif
1589#ifndef R_OK
1590#define R_OK 4
1591#endif
1592#ifndef W_OK
1593#define W_OK 2
1594#endif
1595#ifndef X_OK
1596#define X_OK 1
1597#endif
1598
1599#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001600PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001601"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001602Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001603
1604static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001605posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001606{
Victor Stinner8c62be82010-05-06 00:08:46 +00001607 int id;
1608 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001609
Victor Stinner8c62be82010-05-06 00:08:46 +00001610 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1611 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001612
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001613#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001614 /* file descriptor 0 only, the default input device (stdin) */
1615 if (id == 0) {
1616 ret = ttyname();
1617 }
1618 else {
1619 ret = NULL;
1620 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001621#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001623#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001624 if (ret == NULL)
1625 return posix_error();
1626 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001627}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001628#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001629
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001630#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001631PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001632"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001633Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001634
1635static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001636posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001637{
Victor Stinner8c62be82010-05-06 00:08:46 +00001638 char *ret;
1639 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001640
Greg Wardb48bc172000-03-01 21:51:56 +00001641#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001642 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001643#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001644 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001645#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001646 if (ret == NULL)
1647 return posix_error();
1648 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001649}
1650#endif
1651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001652PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001653"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001654Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001655
Barry Warsaw53699e91996-12-10 23:23:01 +00001656static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001657posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001658{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001659#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001660 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001661#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001662 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001663#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001664 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001665#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001666 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001667#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001668}
1669
Fred Drake4d1e64b2002-04-15 19:40:07 +00001670#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001671PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001672"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001673Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001674opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001675
1676static PyObject *
1677posix_fchdir(PyObject *self, PyObject *fdobj)
1678{
Victor Stinner8c62be82010-05-06 00:08:46 +00001679 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001680}
1681#endif /* HAVE_FCHDIR */
1682
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001683
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001684PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001685"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001686Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001687
Barry Warsaw53699e91996-12-10 23:23:01 +00001688static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001689posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001690{
Victor Stinner8c62be82010-05-06 00:08:46 +00001691 PyObject *opath = NULL;
1692 char *path = NULL;
1693 int i;
1694 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001695#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001696 DWORD attr;
1697 PyUnicodeObject *po;
1698 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1699 Py_BEGIN_ALLOW_THREADS
1700 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1701 if (attr != 0xFFFFFFFF) {
1702 if (i & _S_IWRITE)
1703 attr &= ~FILE_ATTRIBUTE_READONLY;
1704 else
1705 attr |= FILE_ATTRIBUTE_READONLY;
1706 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1707 }
1708 else
1709 res = 0;
1710 Py_END_ALLOW_THREADS
1711 if (!res)
1712 return win32_error_unicode("chmod",
1713 PyUnicode_AS_UNICODE(po));
1714 Py_INCREF(Py_None);
1715 return Py_None;
1716 }
1717 /* Drop the argument parsing error as narrow strings
1718 are also valid. */
1719 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001720
Victor Stinner8c62be82010-05-06 00:08:46 +00001721 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1722 &opath, &i))
1723 return NULL;
1724 path = PyBytes_AsString(opath);
1725 Py_BEGIN_ALLOW_THREADS
1726 attr = GetFileAttributesA(path);
1727 if (attr != 0xFFFFFFFF) {
1728 if (i & _S_IWRITE)
1729 attr &= ~FILE_ATTRIBUTE_READONLY;
1730 else
1731 attr |= FILE_ATTRIBUTE_READONLY;
1732 res = SetFileAttributesA(path, attr);
1733 }
1734 else
1735 res = 0;
1736 Py_END_ALLOW_THREADS
1737 if (!res) {
1738 win32_error("chmod", path);
1739 Py_DECREF(opath);
1740 return NULL;
1741 }
1742 Py_DECREF(opath);
1743 Py_INCREF(Py_None);
1744 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001745#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001746 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1747 &opath, &i))
1748 return NULL;
1749 path = PyBytes_AsString(opath);
1750 Py_BEGIN_ALLOW_THREADS
1751 res = chmod(path, i);
1752 Py_END_ALLOW_THREADS
1753 if (res < 0)
1754 return posix_error_with_allocated_filename(opath);
1755 Py_DECREF(opath);
1756 Py_INCREF(Py_None);
1757 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001758#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001759}
1760
Christian Heimes4e30a842007-11-30 22:12:06 +00001761#ifdef HAVE_FCHMOD
1762PyDoc_STRVAR(posix_fchmod__doc__,
1763"fchmod(fd, mode)\n\n\
1764Change the access permissions of the file given by file\n\
1765descriptor fd.");
1766
1767static PyObject *
1768posix_fchmod(PyObject *self, PyObject *args)
1769{
Victor Stinner8c62be82010-05-06 00:08:46 +00001770 int fd, mode, res;
1771 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1772 return NULL;
1773 Py_BEGIN_ALLOW_THREADS
1774 res = fchmod(fd, mode);
1775 Py_END_ALLOW_THREADS
1776 if (res < 0)
1777 return posix_error();
1778 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001779}
1780#endif /* HAVE_FCHMOD */
1781
1782#ifdef HAVE_LCHMOD
1783PyDoc_STRVAR(posix_lchmod__doc__,
1784"lchmod(path, mode)\n\n\
1785Change the access permissions of a file. If path is a symlink, this\n\
1786affects the link itself rather than the target.");
1787
1788static PyObject *
1789posix_lchmod(PyObject *self, PyObject *args)
1790{
Victor Stinner8c62be82010-05-06 00:08:46 +00001791 PyObject *opath;
1792 char *path;
1793 int i;
1794 int res;
1795 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1796 &opath, &i))
1797 return NULL;
1798 path = PyBytes_AsString(opath);
1799 Py_BEGIN_ALLOW_THREADS
1800 res = lchmod(path, i);
1801 Py_END_ALLOW_THREADS
1802 if (res < 0)
1803 return posix_error_with_allocated_filename(opath);
1804 Py_DECREF(opath);
1805 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001806}
1807#endif /* HAVE_LCHMOD */
1808
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001809
Thomas Wouterscf297e42007-02-23 15:07:44 +00001810#ifdef HAVE_CHFLAGS
1811PyDoc_STRVAR(posix_chflags__doc__,
1812"chflags(path, flags)\n\n\
1813Set file flags.");
1814
1815static PyObject *
1816posix_chflags(PyObject *self, PyObject *args)
1817{
Victor Stinner8c62be82010-05-06 00:08:46 +00001818 PyObject *opath;
1819 char *path;
1820 unsigned long flags;
1821 int res;
1822 if (!PyArg_ParseTuple(args, "O&k:chflags",
1823 PyUnicode_FSConverter, &opath, &flags))
1824 return NULL;
1825 path = PyBytes_AsString(opath);
1826 Py_BEGIN_ALLOW_THREADS
1827 res = chflags(path, flags);
1828 Py_END_ALLOW_THREADS
1829 if (res < 0)
1830 return posix_error_with_allocated_filename(opath);
1831 Py_DECREF(opath);
1832 Py_INCREF(Py_None);
1833 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001834}
1835#endif /* HAVE_CHFLAGS */
1836
1837#ifdef HAVE_LCHFLAGS
1838PyDoc_STRVAR(posix_lchflags__doc__,
1839"lchflags(path, flags)\n\n\
1840Set file flags.\n\
1841This function will not follow symbolic links.");
1842
1843static PyObject *
1844posix_lchflags(PyObject *self, PyObject *args)
1845{
Victor Stinner8c62be82010-05-06 00:08:46 +00001846 PyObject *opath;
1847 char *path;
1848 unsigned long flags;
1849 int res;
1850 if (!PyArg_ParseTuple(args, "O&k:lchflags",
1851 PyUnicode_FSConverter, &opath, &flags))
1852 return NULL;
1853 path = PyBytes_AsString(opath);
1854 Py_BEGIN_ALLOW_THREADS
1855 res = lchflags(path, flags);
1856 Py_END_ALLOW_THREADS
1857 if (res < 0)
1858 return posix_error_with_allocated_filename(opath);
1859 Py_DECREF(opath);
1860 Py_INCREF(Py_None);
1861 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001862}
1863#endif /* HAVE_LCHFLAGS */
1864
Martin v. Löwis244edc82001-10-04 22:44:26 +00001865#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001866PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001867"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001868Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001869
1870static PyObject *
1871posix_chroot(PyObject *self, PyObject *args)
1872{
Victor Stinner8c62be82010-05-06 00:08:46 +00001873 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001874}
1875#endif
1876
Guido van Rossum21142a01999-01-08 21:05:37 +00001877#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001878PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001879"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001880force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001881
1882static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001883posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001884{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001885 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001886}
1887#endif /* HAVE_FSYNC */
1888
1889#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001890
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001891#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001892extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1893#endif
1894
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001895PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001896"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001897force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001898 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001899
1900static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001901posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001902{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001903 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001904}
1905#endif /* HAVE_FDATASYNC */
1906
1907
Fredrik Lundh10723342000-07-10 16:38:09 +00001908#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001909PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001910"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001911Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001912
Barry Warsaw53699e91996-12-10 23:23:01 +00001913static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001914posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001915{
Victor Stinner8c62be82010-05-06 00:08:46 +00001916 PyObject *opath;
1917 char *path;
1918 long uid, gid;
1919 int res;
1920 if (!PyArg_ParseTuple(args, "O&ll:chown",
1921 PyUnicode_FSConverter, &opath,
1922 &uid, &gid))
1923 return NULL;
1924 path = PyBytes_AsString(opath);
1925 Py_BEGIN_ALLOW_THREADS
1926 res = chown(path, (uid_t) uid, (gid_t) gid);
1927 Py_END_ALLOW_THREADS
1928 if (res < 0)
1929 return posix_error_with_allocated_filename(opath);
1930 Py_DECREF(opath);
1931 Py_INCREF(Py_None);
1932 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001933}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001934#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001935
Christian Heimes4e30a842007-11-30 22:12:06 +00001936#ifdef HAVE_FCHOWN
1937PyDoc_STRVAR(posix_fchown__doc__,
1938"fchown(fd, uid, gid)\n\n\
1939Change the owner and group id of the file given by file descriptor\n\
1940fd to the numeric uid and gid.");
1941
1942static PyObject *
1943posix_fchown(PyObject *self, PyObject *args)
1944{
Victor Stinner8c62be82010-05-06 00:08:46 +00001945 int fd;
1946 long uid, gid;
1947 int res;
1948 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
1949 return NULL;
1950 Py_BEGIN_ALLOW_THREADS
1951 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1952 Py_END_ALLOW_THREADS
1953 if (res < 0)
1954 return posix_error();
1955 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001956}
1957#endif /* HAVE_FCHOWN */
1958
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001959#ifdef HAVE_LCHOWN
1960PyDoc_STRVAR(posix_lchown__doc__,
1961"lchown(path, uid, gid)\n\n\
1962Change the owner and group id of path to the numeric uid and gid.\n\
1963This function will not follow symbolic links.");
1964
1965static PyObject *
1966posix_lchown(PyObject *self, PyObject *args)
1967{
Victor Stinner8c62be82010-05-06 00:08:46 +00001968 PyObject *opath;
1969 char *path;
1970 long uid, gid;
1971 int res;
1972 if (!PyArg_ParseTuple(args, "O&ll:lchown",
1973 PyUnicode_FSConverter, &opath,
1974 &uid, &gid))
1975 return NULL;
1976 path = PyBytes_AsString(opath);
1977 Py_BEGIN_ALLOW_THREADS
1978 res = lchown(path, (uid_t) uid, (gid_t) gid);
1979 Py_END_ALLOW_THREADS
1980 if (res < 0)
1981 return posix_error_with_allocated_filename(opath);
1982 Py_DECREF(opath);
1983 Py_INCREF(Py_None);
1984 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001985}
1986#endif /* HAVE_LCHOWN */
1987
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001988
Guido van Rossum36bc6801995-06-14 22:54:23 +00001989#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00001990static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001991posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001992{
Victor Stinner8c62be82010-05-06 00:08:46 +00001993 char buf[1026];
1994 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001995
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001996#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001997 if (!use_bytes) {
1998 wchar_t wbuf[1026];
1999 wchar_t *wbuf2 = wbuf;
2000 PyObject *resobj;
2001 DWORD len;
2002 Py_BEGIN_ALLOW_THREADS
2003 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2004 /* If the buffer is large enough, len does not include the
2005 terminating \0. If the buffer is too small, len includes
2006 the space needed for the terminator. */
2007 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2008 wbuf2 = malloc(len * sizeof(wchar_t));
2009 if (wbuf2)
2010 len = GetCurrentDirectoryW(len, wbuf2);
2011 }
2012 Py_END_ALLOW_THREADS
2013 if (!wbuf2) {
2014 PyErr_NoMemory();
2015 return NULL;
2016 }
2017 if (!len) {
2018 if (wbuf2 != wbuf) free(wbuf2);
2019 return win32_error("getcwdu", NULL);
2020 }
2021 resobj = PyUnicode_FromWideChar(wbuf2, len);
2022 if (wbuf2 != wbuf) free(wbuf2);
2023 return resobj;
2024 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002025#endif
2026
Victor Stinner8c62be82010-05-06 00:08:46 +00002027 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002028#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002029 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002030#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002031 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002032#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002033 Py_END_ALLOW_THREADS
2034 if (res == NULL)
2035 return posix_error();
2036 if (use_bytes)
2037 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002038 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002039}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002040
2041PyDoc_STRVAR(posix_getcwd__doc__,
2042"getcwd() -> path\n\n\
2043Return a unicode string representing the current working directory.");
2044
2045static PyObject *
2046posix_getcwd_unicode(PyObject *self)
2047{
2048 return posix_getcwd(0);
2049}
2050
2051PyDoc_STRVAR(posix_getcwdb__doc__,
2052"getcwdb() -> path\n\n\
2053Return a bytes string representing the current working directory.");
2054
2055static PyObject *
2056posix_getcwd_bytes(PyObject *self)
2057{
2058 return posix_getcwd(1);
2059}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002060#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002061
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002062
Guido van Rossumb6775db1994-08-01 11:34:53 +00002063#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002064PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002065"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002066Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002067
Barry Warsaw53699e91996-12-10 23:23:01 +00002068static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002069posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002070{
Victor Stinner8c62be82010-05-06 00:08:46 +00002071 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002072}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002073#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002074
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002075
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002076PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002077"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002078Return a list containing the names of the entries in the directory.\n\
2079\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00002080 path: path of directory to list\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002081\n\
2082The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002083entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002084
Barry Warsaw53699e91996-12-10 23:23:01 +00002085static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002086posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002087{
Victor Stinner8c62be82010-05-06 00:08:46 +00002088 /* XXX Should redo this putting the (now four) versions of opendir
2089 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002090#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002091
Victor Stinner8c62be82010-05-06 00:08:46 +00002092 PyObject *d, *v;
2093 HANDLE hFindFile;
2094 BOOL result;
2095 WIN32_FIND_DATA FileData;
2096 PyObject *opath;
2097 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2098 char *bufptr = namebuf;
2099 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002100
Victor Stinner8c62be82010-05-06 00:08:46 +00002101 PyObject *po;
2102 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2103 WIN32_FIND_DATAW wFileData;
2104 Py_UNICODE *wnamebuf;
2105 /* Overallocate for \\*.*\0 */
2106 len = PyUnicode_GET_SIZE(po);
2107 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2108 if (!wnamebuf) {
2109 PyErr_NoMemory();
2110 return NULL;
2111 }
2112 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2113 if (len > 0) {
2114 Py_UNICODE wch = wnamebuf[len-1];
2115 if (wch != L'/' && wch != L'\\' && wch != L':')
2116 wnamebuf[len++] = L'\\';
2117 wcscpy(wnamebuf + len, L"*.*");
2118 }
2119 if ((d = PyList_New(0)) == NULL) {
2120 free(wnamebuf);
2121 return NULL;
2122 }
2123 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2124 if (hFindFile == INVALID_HANDLE_VALUE) {
2125 int error = GetLastError();
2126 if (error == ERROR_FILE_NOT_FOUND) {
2127 free(wnamebuf);
2128 return d;
2129 }
2130 Py_DECREF(d);
2131 win32_error_unicode("FindFirstFileW", wnamebuf);
2132 free(wnamebuf);
2133 return NULL;
2134 }
2135 do {
2136 /* Skip over . and .. */
2137 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2138 wcscmp(wFileData.cFileName, L"..") != 0) {
2139 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2140 if (v == NULL) {
2141 Py_DECREF(d);
2142 d = NULL;
2143 break;
2144 }
2145 if (PyList_Append(d, v) != 0) {
2146 Py_DECREF(v);
2147 Py_DECREF(d);
2148 d = NULL;
2149 break;
2150 }
2151 Py_DECREF(v);
2152 }
2153 Py_BEGIN_ALLOW_THREADS
2154 result = FindNextFileW(hFindFile, &wFileData);
2155 Py_END_ALLOW_THREADS
2156 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2157 it got to the end of the directory. */
2158 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2159 Py_DECREF(d);
2160 win32_error_unicode("FindNextFileW", wnamebuf);
2161 FindClose(hFindFile);
2162 free(wnamebuf);
2163 return NULL;
2164 }
2165 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002166
Victor Stinner8c62be82010-05-06 00:08:46 +00002167 if (FindClose(hFindFile) == FALSE) {
2168 Py_DECREF(d);
2169 win32_error_unicode("FindClose", wnamebuf);
2170 free(wnamebuf);
2171 return NULL;
2172 }
2173 free(wnamebuf);
2174 return d;
2175 }
2176 /* Drop the argument parsing error as narrow strings
2177 are also valid. */
2178 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002179
Victor Stinner8c62be82010-05-06 00:08:46 +00002180 if (!PyArg_ParseTuple(args, "O&:listdir",
2181 PyUnicode_FSConverter, &opath))
2182 return NULL;
2183 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2184 PyErr_SetString(PyExc_ValueError, "path too long");
2185 Py_DECREF(opath);
2186 return NULL;
2187 }
2188 strcpy(namebuf, PyBytes_AsString(opath));
2189 len = PyObject_Size(opath);
2190 if (len > 0) {
2191 char ch = namebuf[len-1];
2192 if (ch != SEP && ch != ALTSEP && ch != ':')
2193 namebuf[len++] = '/';
2194 strcpy(namebuf + len, "*.*");
2195 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002196
Victor Stinner8c62be82010-05-06 00:08:46 +00002197 if ((d = PyList_New(0)) == NULL)
2198 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002199
Victor Stinner8c62be82010-05-06 00:08:46 +00002200 hFindFile = FindFirstFile(namebuf, &FileData);
2201 if (hFindFile == INVALID_HANDLE_VALUE) {
2202 int error = GetLastError();
2203 if (error == ERROR_FILE_NOT_FOUND)
2204 return d;
2205 Py_DECREF(d);
2206 return win32_error("FindFirstFile", namebuf);
2207 }
2208 do {
2209 /* Skip over . and .. */
2210 if (strcmp(FileData.cFileName, ".") != 0 &&
2211 strcmp(FileData.cFileName, "..") != 0) {
2212 v = PyBytes_FromString(FileData.cFileName);
2213 if (v == NULL) {
2214 Py_DECREF(d);
2215 d = NULL;
2216 break;
2217 }
2218 if (PyList_Append(d, v) != 0) {
2219 Py_DECREF(v);
2220 Py_DECREF(d);
2221 d = NULL;
2222 break;
2223 }
2224 Py_DECREF(v);
2225 }
2226 Py_BEGIN_ALLOW_THREADS
2227 result = FindNextFile(hFindFile, &FileData);
2228 Py_END_ALLOW_THREADS
2229 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2230 it got to the end of the directory. */
2231 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2232 Py_DECREF(d);
2233 win32_error("FindNextFile", namebuf);
2234 FindClose(hFindFile);
2235 return NULL;
2236 }
2237 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002238
Victor Stinner8c62be82010-05-06 00:08:46 +00002239 if (FindClose(hFindFile) == FALSE) {
2240 Py_DECREF(d);
2241 return win32_error("FindClose", namebuf);
2242 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002243
Victor Stinner8c62be82010-05-06 00:08:46 +00002244 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002245
Tim Peters0bb44a42000-09-15 07:44:49 +00002246#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002247
2248#ifndef MAX_PATH
2249#define MAX_PATH CCHMAXPATH
2250#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002251 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002252 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002253 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002254 PyObject *d, *v;
2255 char namebuf[MAX_PATH+5];
2256 HDIR hdir = 1;
2257 ULONG srchcnt = 1;
2258 FILEFINDBUF3 ep;
2259 APIRET rc;
2260
Victor Stinner8c62be82010-05-06 00:08:46 +00002261 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002262 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002263 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002264 name = PyBytes_AsString(oname);
2265 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002266 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002267 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002268 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002269 return NULL;
2270 }
2271 strcpy(namebuf, name);
2272 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002273 if (*pt == ALTSEP)
2274 *pt = SEP;
2275 if (namebuf[len-1] != SEP)
2276 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002277 strcpy(namebuf + len, "*.*");
2278
Neal Norwitz6c913782007-10-14 03:23:09 +00002279 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002280 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002281 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002282 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002283
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002284 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2285 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002286 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002287 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2288 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2289 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002290
2291 if (rc != NO_ERROR) {
2292 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002293 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002294 }
2295
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002296 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002297 do {
2298 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002299 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002300 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002301
2302 strcpy(namebuf, ep.achName);
2303
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002304 /* Leave Case of Name Alone -- In Native Form */
2305 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002306
Christian Heimes72b710a2008-05-26 13:28:38 +00002307 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002308 if (v == NULL) {
2309 Py_DECREF(d);
2310 d = NULL;
2311 break;
2312 }
2313 if (PyList_Append(d, v) != 0) {
2314 Py_DECREF(v);
2315 Py_DECREF(d);
2316 d = NULL;
2317 break;
2318 }
2319 Py_DECREF(v);
2320 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2321 }
2322
Victor Stinnerdcb24032010-04-22 12:08:36 +00002323 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002324 return d;
2325#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002326 PyObject *oname;
2327 char *name;
2328 PyObject *d, *v;
2329 DIR *dirp;
2330 struct dirent *ep;
2331 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002332
Victor Stinner8c62be82010-05-06 00:08:46 +00002333 errno = 0;
2334 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2335 arg_is_unicode = 0;
2336 PyErr_Clear();
2337 }
2338 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
2339 return NULL;
2340 name = PyBytes_AsString(oname);
2341 if ((dirp = opendir(name)) == NULL) {
2342 return posix_error_with_allocated_filename(oname);
2343 }
2344 if ((d = PyList_New(0)) == NULL) {
2345 closedir(dirp);
2346 Py_DECREF(oname);
2347 return NULL;
2348 }
2349 for (;;) {
2350 errno = 0;
2351 Py_BEGIN_ALLOW_THREADS
2352 ep = readdir(dirp);
2353 Py_END_ALLOW_THREADS
2354 if (ep == NULL) {
2355 if (errno == 0) {
2356 break;
2357 } else {
2358 closedir(dirp);
2359 Py_DECREF(d);
2360 return posix_error_with_allocated_filename(oname);
2361 }
2362 }
2363 if (ep->d_name[0] == '.' &&
2364 (NAMLEN(ep) == 1 ||
2365 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2366 continue;
2367 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
2368 if (v == NULL) {
2369 Py_DECREF(d);
2370 d = NULL;
2371 break;
2372 }
2373 if (arg_is_unicode) {
2374 PyObject *w;
Just van Rossum46c97842003-02-25 21:42:15 +00002375
Victor Stinner8c62be82010-05-06 00:08:46 +00002376 w = PyUnicode_FromEncodedObject(v,
2377 Py_FileSystemDefaultEncoding,
2378 "surrogateescape");
2379 Py_DECREF(v);
2380 if (w != NULL)
2381 v = w;
2382 else {
2383 /* Encoding failed to decode ASCII bytes.
2384 Raise exception. */
2385 Py_DECREF(d);
2386 d = NULL;
2387 break;
2388 }
2389 }
2390 if (PyList_Append(d, v) != 0) {
2391 Py_DECREF(v);
2392 Py_DECREF(d);
2393 d = NULL;
2394 break;
2395 }
2396 Py_DECREF(v);
2397 }
2398 closedir(dirp);
2399 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002400
Victor Stinner8c62be82010-05-06 00:08:46 +00002401 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002402
Tim Peters0bb44a42000-09-15 07:44:49 +00002403#endif /* which OS */
2404} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002405
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002406#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002407/* A helper function for abspath on win32 */
2408static PyObject *
2409posix__getfullpathname(PyObject *self, PyObject *args)
2410{
Victor Stinner8c62be82010-05-06 00:08:46 +00002411 PyObject *opath;
2412 char *path;
2413 char outbuf[MAX_PATH*2];
2414 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002415#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002416 PyUnicodeObject *po;
2417 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2418 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2419 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2420 Py_UNICODE *wtemp;
2421 DWORD result;
2422 PyObject *v;
2423 result = GetFullPathNameW(wpath,
2424 sizeof(woutbuf)/sizeof(woutbuf[0]),
2425 woutbuf, &wtemp);
2426 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2427 woutbufp = malloc(result * sizeof(Py_UNICODE));
2428 if (!woutbufp)
2429 return PyErr_NoMemory();
2430 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2431 }
2432 if (result)
2433 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2434 else
2435 v = win32_error_unicode("GetFullPathNameW", wpath);
2436 if (woutbufp != woutbuf)
2437 free(woutbufp);
2438 return v;
2439 }
2440 /* Drop the argument parsing error as narrow strings
2441 are also valid. */
2442 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002443
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002444#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002445 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2446 PyUnicode_FSConverter, &opath))
2447 return NULL;
2448 path = PyBytes_AsString(opath);
2449 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2450 outbuf, &temp)) {
2451 win32_error("GetFullPathName", path);
2452 Py_DECREF(opath);
2453 return NULL;
2454 }
2455 Py_DECREF(opath);
2456 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2457 return PyUnicode_Decode(outbuf, strlen(outbuf),
2458 Py_FileSystemDefaultEncoding, NULL);
2459 }
2460 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002461} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002462#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002463
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002464PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002465"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002466Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002467
Barry Warsaw53699e91996-12-10 23:23:01 +00002468static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002469posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002470{
Victor Stinner8c62be82010-05-06 00:08:46 +00002471 int res;
2472 PyObject *opath;
2473 char *path;
2474 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002475
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002476#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002477 PyUnicodeObject *po;
2478 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2479 Py_BEGIN_ALLOW_THREADS
2480 /* PyUnicode_AS_UNICODE OK without thread lock as
2481 it is a simple dereference. */
2482 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2483 Py_END_ALLOW_THREADS
2484 if (!res)
2485 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2486 Py_INCREF(Py_None);
2487 return Py_None;
2488 }
2489 /* Drop the argument parsing error as narrow strings
2490 are also valid. */
2491 PyErr_Clear();
2492 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2493 PyUnicode_FSConverter, &opath, &mode))
2494 return NULL;
2495 path = PyBytes_AsString(opath);
2496 Py_BEGIN_ALLOW_THREADS
2497 /* PyUnicode_AS_UNICODE OK without thread lock as
2498 it is a simple dereference. */
2499 res = CreateDirectoryA(path, NULL);
2500 Py_END_ALLOW_THREADS
2501 if (!res) {
2502 win32_error("mkdir", path);
2503 Py_DECREF(opath);
2504 return NULL;
2505 }
2506 Py_DECREF(opath);
2507 Py_INCREF(Py_None);
2508 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002509#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002510
Victor Stinner8c62be82010-05-06 00:08:46 +00002511 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2512 PyUnicode_FSConverter, &opath, &mode))
2513 return NULL;
2514 path = PyBytes_AsString(opath);
2515 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002516#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002517 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002518#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002519 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002520#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002521 Py_END_ALLOW_THREADS
2522 if (res < 0)
2523 return posix_error_with_allocated_filename(opath);
2524 Py_DECREF(opath);
2525 Py_INCREF(Py_None);
2526 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002527#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002528}
2529
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002530
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002531/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2532#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002533#include <sys/resource.h>
2534#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002535
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002536
2537#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002538PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002539"nice(inc) -> new_priority\n\n\
2540Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002541
Barry Warsaw53699e91996-12-10 23:23:01 +00002542static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002543posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002544{
Victor Stinner8c62be82010-05-06 00:08:46 +00002545 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002546
Victor Stinner8c62be82010-05-06 00:08:46 +00002547 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2548 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002549
Victor Stinner8c62be82010-05-06 00:08:46 +00002550 /* There are two flavours of 'nice': one that returns the new
2551 priority (as required by almost all standards out there) and the
2552 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2553 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002554
Victor Stinner8c62be82010-05-06 00:08:46 +00002555 If we are of the nice family that returns the new priority, we
2556 need to clear errno before the call, and check if errno is filled
2557 before calling posix_error() on a returnvalue of -1, because the
2558 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002559
Victor Stinner8c62be82010-05-06 00:08:46 +00002560 errno = 0;
2561 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002562#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002563 if (value == 0)
2564 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002565#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002566 if (value == -1 && errno != 0)
2567 /* either nice() or getpriority() returned an error */
2568 return posix_error();
2569 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002570}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002571#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002572
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002573PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002574"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002575Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002576
Barry Warsaw53699e91996-12-10 23:23:01 +00002577static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002578posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002579{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002580#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002581 PyObject *o1, *o2;
2582 char *p1, *p2;
2583 BOOL result;
2584 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2585 goto error;
2586 if (!convert_to_unicode(&o1))
2587 goto error;
2588 if (!convert_to_unicode(&o2)) {
2589 Py_DECREF(o1);
2590 goto error;
2591 }
2592 Py_BEGIN_ALLOW_THREADS
2593 result = MoveFileW(PyUnicode_AsUnicode(o1),
2594 PyUnicode_AsUnicode(o2));
2595 Py_END_ALLOW_THREADS
2596 Py_DECREF(o1);
2597 Py_DECREF(o2);
2598 if (!result)
2599 return win32_error("rename", NULL);
2600 Py_INCREF(Py_None);
2601 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002602error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002603 PyErr_Clear();
2604 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2605 return NULL;
2606 Py_BEGIN_ALLOW_THREADS
2607 result = MoveFileA(p1, p2);
2608 Py_END_ALLOW_THREADS
2609 if (!result)
2610 return win32_error("rename", NULL);
2611 Py_INCREF(Py_None);
2612 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002613#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002614 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002615#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002616}
2617
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002618
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002619PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002620"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002621Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002622
Barry Warsaw53699e91996-12-10 23:23:01 +00002623static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002624posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002625{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002626#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002627 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002628#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002629 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002630#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002631}
2632
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002633
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002634PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002635"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002636Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002637
Barry Warsaw53699e91996-12-10 23:23:01 +00002638static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002639posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002640{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002641#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002642 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002643#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002644 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002645#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002646}
2647
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002648
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002649#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002650PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002651"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002652Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002653
Barry Warsaw53699e91996-12-10 23:23:01 +00002654static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002655posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002656{
Victor Stinner8c62be82010-05-06 00:08:46 +00002657 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002658#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002659 wchar_t *command;
2660 if (!PyArg_ParseTuple(args, "u:system", &command))
2661 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002662
Victor Stinner8c62be82010-05-06 00:08:46 +00002663 Py_BEGIN_ALLOW_THREADS
2664 sts = _wsystem(command);
2665 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00002666#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002667 PyObject *command_obj;
2668 char *command;
2669 if (!PyArg_ParseTuple(args, "O&:system",
2670 PyUnicode_FSConverter, &command_obj))
2671 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002672
Victor Stinner8c62be82010-05-06 00:08:46 +00002673 command = PyBytes_AsString(command_obj);
2674 Py_BEGIN_ALLOW_THREADS
2675 sts = system(command);
2676 Py_END_ALLOW_THREADS
2677 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00002678#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002679 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002680}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002681#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002682
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002683
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002684PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002685"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002686Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002687
Barry Warsaw53699e91996-12-10 23:23:01 +00002688static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002689posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002690{
Victor Stinner8c62be82010-05-06 00:08:46 +00002691 int i;
2692 if (!PyArg_ParseTuple(args, "i:umask", &i))
2693 return NULL;
2694 i = (int)umask(i);
2695 if (i < 0)
2696 return posix_error();
2697 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002698}
2699
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002700
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002701PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002702"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002703Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002704
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002705PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002706"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002707Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002708
Barry Warsaw53699e91996-12-10 23:23:01 +00002709static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002710posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002711{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002712#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002713 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002714#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002715 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002716#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002717}
2718
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002719
Guido van Rossumb6775db1994-08-01 11:34:53 +00002720#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002721PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002722"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002723Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002724
Barry Warsaw53699e91996-12-10 23:23:01 +00002725static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002726posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002727{
Victor Stinner8c62be82010-05-06 00:08:46 +00002728 struct utsname u;
2729 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002730
Victor Stinner8c62be82010-05-06 00:08:46 +00002731 Py_BEGIN_ALLOW_THREADS
2732 res = uname(&u);
2733 Py_END_ALLOW_THREADS
2734 if (res < 0)
2735 return posix_error();
2736 return Py_BuildValue("(sssss)",
2737 u.sysname,
2738 u.nodename,
2739 u.release,
2740 u.version,
2741 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002742}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002743#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002744
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002745static int
2746extract_time(PyObject *t, long* sec, long* usec)
2747{
Victor Stinner8c62be82010-05-06 00:08:46 +00002748 long intval;
2749 if (PyFloat_Check(t)) {
2750 double tval = PyFloat_AsDouble(t);
2751 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
2752 if (!intobj)
2753 return -1;
2754 intval = PyLong_AsLong(intobj);
2755 Py_DECREF(intobj);
2756 if (intval == -1 && PyErr_Occurred())
2757 return -1;
2758 *sec = intval;
2759 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2760 if (*usec < 0)
2761 /* If rounding gave us a negative number,
2762 truncate. */
2763 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002764 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00002765 }
2766 intval = PyLong_AsLong(t);
2767 if (intval == -1 && PyErr_Occurred())
2768 return -1;
2769 *sec = intval;
2770 *usec = 0;
2771 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002772}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002773
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002774PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002775"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002776utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002777Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002778second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002779
Barry Warsaw53699e91996-12-10 23:23:01 +00002780static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002781posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002782{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002783#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002784 PyObject *arg;
2785 PyUnicodeObject *obwpath;
2786 wchar_t *wpath = NULL;
2787 PyObject *oapath;
2788 char *apath;
2789 HANDLE hFile;
2790 long atimesec, mtimesec, ausec, musec;
2791 FILETIME atime, mtime;
2792 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002793
Victor Stinner8c62be82010-05-06 00:08:46 +00002794 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2795 wpath = PyUnicode_AS_UNICODE(obwpath);
2796 Py_BEGIN_ALLOW_THREADS
2797 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2798 NULL, OPEN_EXISTING,
2799 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2800 Py_END_ALLOW_THREADS
2801 if (hFile == INVALID_HANDLE_VALUE)
2802 return win32_error_unicode("utime", wpath);
2803 } else
2804 /* Drop the argument parsing error as narrow strings
2805 are also valid. */
2806 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002807
Victor Stinner8c62be82010-05-06 00:08:46 +00002808 if (!wpath) {
2809 if (!PyArg_ParseTuple(args, "O&O:utime",
2810 PyUnicode_FSConverter, &oapath, &arg))
2811 return NULL;
2812 apath = PyBytes_AsString(oapath);
2813 Py_BEGIN_ALLOW_THREADS
2814 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2815 NULL, OPEN_EXISTING,
2816 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2817 Py_END_ALLOW_THREADS
2818 if (hFile == INVALID_HANDLE_VALUE) {
2819 win32_error("utime", apath);
2820 Py_DECREF(oapath);
2821 return NULL;
2822 }
2823 Py_DECREF(oapath);
2824 }
2825
2826 if (arg == Py_None) {
2827 SYSTEMTIME now;
2828 GetSystemTime(&now);
2829 if (!SystemTimeToFileTime(&now, &mtime) ||
2830 !SystemTimeToFileTime(&now, &atime)) {
2831 win32_error("utime", NULL);
2832 goto done;
2833 }
2834 }
2835 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2836 PyErr_SetString(PyExc_TypeError,
2837 "utime() arg 2 must be a tuple (atime, mtime)");
2838 goto done;
2839 }
2840 else {
2841 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2842 &atimesec, &ausec) == -1)
2843 goto done;
2844 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2845 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2846 &mtimesec, &musec) == -1)
2847 goto done;
2848 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2849 }
2850 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2851 /* Avoid putting the file name into the error here,
2852 as that may confuse the user into believing that
2853 something is wrong with the file, when it also
2854 could be the time stamp that gives a problem. */
2855 win32_error("utime", NULL);
2856 }
2857 Py_INCREF(Py_None);
2858 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002859done:
Victor Stinner8c62be82010-05-06 00:08:46 +00002860 CloseHandle(hFile);
2861 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002862#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002863
Victor Stinner8c62be82010-05-06 00:08:46 +00002864 PyObject *opath;
2865 char *path;
2866 long atime, mtime, ausec, musec;
2867 int res;
2868 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002869
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002870#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00002871 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002872#define ATIME buf[0].tv_sec
2873#define MTIME buf[1].tv_sec
2874#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002875/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00002876 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002877#define ATIME buf.actime
2878#define MTIME buf.modtime
2879#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002880#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00002881 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002882#define ATIME buf[0]
2883#define MTIME buf[1]
2884#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002885#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002886
Mark Hammond817c9292003-12-03 01:22:38 +00002887
Victor Stinner8c62be82010-05-06 00:08:46 +00002888 if (!PyArg_ParseTuple(args, "O&O:utime",
2889 PyUnicode_FSConverter, &opath, &arg))
2890 return NULL;
2891 path = PyBytes_AsString(opath);
2892 if (arg == Py_None) {
2893 /* optional time values not given */
2894 Py_BEGIN_ALLOW_THREADS
2895 res = utime(path, NULL);
2896 Py_END_ALLOW_THREADS
2897 }
2898 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2899 PyErr_SetString(PyExc_TypeError,
2900 "utime() arg 2 must be a tuple (atime, mtime)");
2901 Py_DECREF(opath);
2902 return NULL;
2903 }
2904 else {
2905 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2906 &atime, &ausec) == -1) {
2907 Py_DECREF(opath);
2908 return NULL;
2909 }
2910 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2911 &mtime, &musec) == -1) {
2912 Py_DECREF(opath);
2913 return NULL;
2914 }
2915 ATIME = atime;
2916 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002917#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00002918 buf[0].tv_usec = ausec;
2919 buf[1].tv_usec = musec;
2920 Py_BEGIN_ALLOW_THREADS
2921 res = utimes(path, buf);
2922 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002923#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002924 Py_BEGIN_ALLOW_THREADS
2925 res = utime(path, UTIME_ARG);
2926 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002927#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00002928 }
2929 if (res < 0) {
2930 return posix_error_with_allocated_filename(opath);
2931 }
2932 Py_DECREF(opath);
2933 Py_INCREF(Py_None);
2934 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002935#undef UTIME_ARG
2936#undef ATIME
2937#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002938#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002939}
2940
Guido van Rossum85e3b011991-06-03 12:42:10 +00002941
Guido van Rossum3b066191991-06-04 19:40:25 +00002942/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002943
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002944PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002945"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002946Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002947
Barry Warsaw53699e91996-12-10 23:23:01 +00002948static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002949posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002950{
Victor Stinner8c62be82010-05-06 00:08:46 +00002951 int sts;
2952 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
2953 return NULL;
2954 _exit(sts);
2955 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002956}
2957
Martin v. Löwis114619e2002-10-07 06:44:21 +00002958#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2959static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002960free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002961{
Victor Stinner8c62be82010-05-06 00:08:46 +00002962 Py_ssize_t i;
2963 for (i = 0; i < count; i++)
2964 PyMem_Free(array[i]);
2965 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002966}
Martin v. Löwis011e8422009-05-05 04:43:17 +00002967
Antoine Pitrou69f71142009-05-24 21:25:49 +00002968static
Martin v. Löwis011e8422009-05-05 04:43:17 +00002969int fsconvert_strdup(PyObject *o, char**out)
2970{
Victor Stinner8c62be82010-05-06 00:08:46 +00002971 PyObject *bytes;
2972 Py_ssize_t size;
2973 if (!PyUnicode_FSConverter(o, &bytes))
2974 return 0;
2975 size = PyBytes_GET_SIZE(bytes);
2976 *out = PyMem_Malloc(size+1);
2977 if (!*out)
2978 return 0;
2979 memcpy(*out, PyBytes_AsString(bytes), size+1);
2980 Py_DECREF(bytes);
2981 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002982}
Martin v. Löwis114619e2002-10-07 06:44:21 +00002983#endif
2984
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002985
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002986#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002987PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002988"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002989Execute an executable path with arguments, replacing current process.\n\
2990\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00002991 path: path of executable file\n\
2992 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002993
Barry Warsaw53699e91996-12-10 23:23:01 +00002994static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002995posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002996{
Victor Stinner8c62be82010-05-06 00:08:46 +00002997 PyObject *opath;
2998 char *path;
2999 PyObject *argv;
3000 char **argvlist;
3001 Py_ssize_t i, argc;
3002 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003003
Victor Stinner8c62be82010-05-06 00:08:46 +00003004 /* execv has two arguments: (path, argv), where
3005 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003006
Victor Stinner8c62be82010-05-06 00:08:46 +00003007 if (!PyArg_ParseTuple(args, "O&O:execv",
3008 PyUnicode_FSConverter,
3009 &opath, &argv))
3010 return NULL;
3011 path = PyBytes_AsString(opath);
3012 if (PyList_Check(argv)) {
3013 argc = PyList_Size(argv);
3014 getitem = PyList_GetItem;
3015 }
3016 else if (PyTuple_Check(argv)) {
3017 argc = PyTuple_Size(argv);
3018 getitem = PyTuple_GetItem;
3019 }
3020 else {
3021 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3022 Py_DECREF(opath);
3023 return NULL;
3024 }
3025 if (argc < 1) {
3026 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3027 Py_DECREF(opath);
3028 return NULL;
3029 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003030
Victor Stinner8c62be82010-05-06 00:08:46 +00003031 argvlist = PyMem_NEW(char *, argc+1);
3032 if (argvlist == NULL) {
3033 Py_DECREF(opath);
3034 return PyErr_NoMemory();
3035 }
3036 for (i = 0; i < argc; i++) {
3037 if (!fsconvert_strdup((*getitem)(argv, i),
3038 &argvlist[i])) {
3039 free_string_array(argvlist, i);
3040 PyErr_SetString(PyExc_TypeError,
3041 "execv() arg 2 must contain only strings");
3042 Py_DECREF(opath);
3043 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003044
Victor Stinner8c62be82010-05-06 00:08:46 +00003045 }
3046 }
3047 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003048
Victor Stinner8c62be82010-05-06 00:08:46 +00003049 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003050
Victor Stinner8c62be82010-05-06 00:08:46 +00003051 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003052
Victor Stinner8c62be82010-05-06 00:08:46 +00003053 free_string_array(argvlist, argc);
3054 Py_DECREF(opath);
3055 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003056}
3057
Victor Stinner13bb71c2010-04-23 21:41:56 +00003058static char**
3059parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3060{
Victor Stinner8c62be82010-05-06 00:08:46 +00003061 char **envlist;
3062 Py_ssize_t i, pos, envc;
3063 PyObject *keys=NULL, *vals=NULL;
3064 PyObject *key, *val, *key2, *val2;
3065 char *p, *k, *v;
3066 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003067
Victor Stinner8c62be82010-05-06 00:08:46 +00003068 i = PyMapping_Size(env);
3069 if (i < 0)
3070 return NULL;
3071 envlist = PyMem_NEW(char *, i + 1);
3072 if (envlist == NULL) {
3073 PyErr_NoMemory();
3074 return NULL;
3075 }
3076 envc = 0;
3077 keys = PyMapping_Keys(env);
3078 vals = PyMapping_Values(env);
3079 if (!keys || !vals)
3080 goto error;
3081 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3082 PyErr_Format(PyExc_TypeError,
3083 "env.keys() or env.values() is not a list");
3084 goto error;
3085 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003086
Victor Stinner8c62be82010-05-06 00:08:46 +00003087 for (pos = 0; pos < i; pos++) {
3088 key = PyList_GetItem(keys, pos);
3089 val = PyList_GetItem(vals, pos);
3090 if (!key || !val)
3091 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003092
Victor Stinner8c62be82010-05-06 00:08:46 +00003093 if (PyUnicode_FSConverter(key, &key2) == 0)
3094 goto error;
3095 if (PyUnicode_FSConverter(val, &val2) == 0) {
3096 Py_DECREF(key2);
3097 goto error;
3098 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003099
3100#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003101 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3102 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003103#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003104 k = PyBytes_AsString(key2);
3105 v = PyBytes_AsString(val2);
3106 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003107
Victor Stinner8c62be82010-05-06 00:08:46 +00003108 p = PyMem_NEW(char, len);
3109 if (p == NULL) {
3110 PyErr_NoMemory();
3111 Py_DECREF(key2);
3112 Py_DECREF(val2);
3113 goto error;
3114 }
3115 PyOS_snprintf(p, len, "%s=%s", k, v);
3116 envlist[envc++] = p;
3117 Py_DECREF(key2);
3118 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003119#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003120 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003121#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003122 }
3123 Py_DECREF(vals);
3124 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003125
Victor Stinner8c62be82010-05-06 00:08:46 +00003126 envlist[envc] = 0;
3127 *envc_ptr = envc;
3128 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003129
3130error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003131 Py_XDECREF(keys);
3132 Py_XDECREF(vals);
3133 while (--envc >= 0)
3134 PyMem_DEL(envlist[envc]);
3135 PyMem_DEL(envlist);
3136 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003137}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003138
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003139PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003140"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003141Execute a path with arguments and environment, replacing current process.\n\
3142\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003143 path: path of executable file\n\
3144 args: tuple or list of arguments\n\
3145 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003146
Barry Warsaw53699e91996-12-10 23:23:01 +00003147static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003148posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003149{
Victor Stinner8c62be82010-05-06 00:08:46 +00003150 PyObject *opath;
3151 char *path;
3152 PyObject *argv, *env;
3153 char **argvlist;
3154 char **envlist;
3155 Py_ssize_t i, argc, envc;
3156 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3157 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003158
Victor Stinner8c62be82010-05-06 00:08:46 +00003159 /* execve has three arguments: (path, argv, env), where
3160 argv is a list or tuple of strings and env is a dictionary
3161 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003162
Victor Stinner8c62be82010-05-06 00:08:46 +00003163 if (!PyArg_ParseTuple(args, "O&OO:execve",
3164 PyUnicode_FSConverter,
3165 &opath, &argv, &env))
3166 return NULL;
3167 path = PyBytes_AsString(opath);
3168 if (PyList_Check(argv)) {
3169 argc = PyList_Size(argv);
3170 getitem = PyList_GetItem;
3171 }
3172 else if (PyTuple_Check(argv)) {
3173 argc = PyTuple_Size(argv);
3174 getitem = PyTuple_GetItem;
3175 }
3176 else {
3177 PyErr_SetString(PyExc_TypeError,
3178 "execve() arg 2 must be a tuple or list");
3179 goto fail_0;
3180 }
3181 if (!PyMapping_Check(env)) {
3182 PyErr_SetString(PyExc_TypeError,
3183 "execve() arg 3 must be a mapping object");
3184 goto fail_0;
3185 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003186
Victor Stinner8c62be82010-05-06 00:08:46 +00003187 argvlist = PyMem_NEW(char *, argc+1);
3188 if (argvlist == NULL) {
3189 PyErr_NoMemory();
3190 goto fail_0;
3191 }
3192 for (i = 0; i < argc; i++) {
3193 if (!fsconvert_strdup((*getitem)(argv, i),
3194 &argvlist[i]))
3195 {
3196 lastarg = i;
3197 goto fail_1;
3198 }
3199 }
3200 lastarg = argc;
3201 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003202
Victor Stinner8c62be82010-05-06 00:08:46 +00003203 envlist = parse_envlist(env, &envc);
3204 if (envlist == NULL)
3205 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003206
Victor Stinner8c62be82010-05-06 00:08:46 +00003207 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003208
Victor Stinner8c62be82010-05-06 00:08:46 +00003209 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003210
Victor Stinner8c62be82010-05-06 00:08:46 +00003211 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003212
Victor Stinner8c62be82010-05-06 00:08:46 +00003213 while (--envc >= 0)
3214 PyMem_DEL(envlist[envc]);
3215 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003216 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003217 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003218 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003219 Py_DECREF(opath);
3220 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003221}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003222#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003223
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003224
Guido van Rossuma1065681999-01-25 23:20:23 +00003225#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003226PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003227"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003228Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003229\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003230 mode: mode of process creation\n\
3231 path: path of executable file\n\
3232 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003233
3234static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003235posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003236{
Victor Stinner8c62be82010-05-06 00:08:46 +00003237 PyObject *opath;
3238 char *path;
3239 PyObject *argv;
3240 char **argvlist;
3241 int mode, i;
3242 Py_ssize_t argc;
3243 Py_intptr_t spawnval;
3244 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003245
Victor Stinner8c62be82010-05-06 00:08:46 +00003246 /* spawnv has three arguments: (mode, path, argv), where
3247 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003248
Victor Stinner8c62be82010-05-06 00:08:46 +00003249 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3250 PyUnicode_FSConverter,
3251 &opath, &argv))
3252 return NULL;
3253 path = PyBytes_AsString(opath);
3254 if (PyList_Check(argv)) {
3255 argc = PyList_Size(argv);
3256 getitem = PyList_GetItem;
3257 }
3258 else if (PyTuple_Check(argv)) {
3259 argc = PyTuple_Size(argv);
3260 getitem = PyTuple_GetItem;
3261 }
3262 else {
3263 PyErr_SetString(PyExc_TypeError,
3264 "spawnv() arg 2 must be a tuple or list");
3265 Py_DECREF(opath);
3266 return NULL;
3267 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003268
Victor Stinner8c62be82010-05-06 00:08:46 +00003269 argvlist = PyMem_NEW(char *, argc+1);
3270 if (argvlist == NULL) {
3271 Py_DECREF(opath);
3272 return PyErr_NoMemory();
3273 }
3274 for (i = 0; i < argc; i++) {
3275 if (!fsconvert_strdup((*getitem)(argv, i),
3276 &argvlist[i])) {
3277 free_string_array(argvlist, i);
3278 PyErr_SetString(
3279 PyExc_TypeError,
3280 "spawnv() arg 2 must contain only strings");
3281 Py_DECREF(opath);
3282 return NULL;
3283 }
3284 }
3285 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003286
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003287#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003288 Py_BEGIN_ALLOW_THREADS
3289 spawnval = spawnv(mode, path, argvlist);
3290 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003291#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003292 if (mode == _OLD_P_OVERLAY)
3293 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003294
Victor Stinner8c62be82010-05-06 00:08:46 +00003295 Py_BEGIN_ALLOW_THREADS
3296 spawnval = _spawnv(mode, path, argvlist);
3297 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003298#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003299
Victor Stinner8c62be82010-05-06 00:08:46 +00003300 free_string_array(argvlist, argc);
3301 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003302
Victor Stinner8c62be82010-05-06 00:08:46 +00003303 if (spawnval == -1)
3304 return posix_error();
3305 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003306#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003307 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003308#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003309 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003310#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003311}
3312
3313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003314PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003315"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003316Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003317\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003318 mode: mode of process creation\n\
3319 path: path of executable file\n\
3320 args: tuple or list of arguments\n\
3321 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003322
3323static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003324posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003325{
Victor Stinner8c62be82010-05-06 00:08:46 +00003326 PyObject *opath;
3327 char *path;
3328 PyObject *argv, *env;
3329 char **argvlist;
3330 char **envlist;
3331 PyObject *res = NULL;
3332 int mode, envc;
3333 Py_ssize_t argc, i;
3334 Py_intptr_t spawnval;
3335 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3336 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003337
Victor Stinner8c62be82010-05-06 00:08:46 +00003338 /* spawnve has four arguments: (mode, path, argv, env), where
3339 argv is a list or tuple of strings and env is a dictionary
3340 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003341
Victor Stinner8c62be82010-05-06 00:08:46 +00003342 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3343 PyUnicode_FSConverter,
3344 &opath, &argv, &env))
3345 return NULL;
3346 path = PyBytes_AsString(opath);
3347 if (PyList_Check(argv)) {
3348 argc = PyList_Size(argv);
3349 getitem = PyList_GetItem;
3350 }
3351 else if (PyTuple_Check(argv)) {
3352 argc = PyTuple_Size(argv);
3353 getitem = PyTuple_GetItem;
3354 }
3355 else {
3356 PyErr_SetString(PyExc_TypeError,
3357 "spawnve() arg 2 must be a tuple or list");
3358 goto fail_0;
3359 }
3360 if (!PyMapping_Check(env)) {
3361 PyErr_SetString(PyExc_TypeError,
3362 "spawnve() arg 3 must be a mapping object");
3363 goto fail_0;
3364 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003365
Victor Stinner8c62be82010-05-06 00:08:46 +00003366 argvlist = PyMem_NEW(char *, argc+1);
3367 if (argvlist == NULL) {
3368 PyErr_NoMemory();
3369 goto fail_0;
3370 }
3371 for (i = 0; i < argc; i++) {
3372 if (!fsconvert_strdup((*getitem)(argv, i),
3373 &argvlist[i]))
3374 {
3375 lastarg = i;
3376 goto fail_1;
3377 }
3378 }
3379 lastarg = argc;
3380 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003381
Victor Stinner8c62be82010-05-06 00:08:46 +00003382 envlist = parse_envlist(env, &envc);
3383 if (envlist == NULL)
3384 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003385
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003386#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003387 Py_BEGIN_ALLOW_THREADS
3388 spawnval = spawnve(mode, path, argvlist, envlist);
3389 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003390#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003391 if (mode == _OLD_P_OVERLAY)
3392 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003393
Victor Stinner8c62be82010-05-06 00:08:46 +00003394 Py_BEGIN_ALLOW_THREADS
3395 spawnval = _spawnve(mode, path, argvlist, envlist);
3396 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003397#endif
Tim Peters25059d32001-12-07 20:35:43 +00003398
Victor Stinner8c62be82010-05-06 00:08:46 +00003399 if (spawnval == -1)
3400 (void) posix_error();
3401 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003402#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003403 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003404#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003405 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003406#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003407
Victor Stinner8c62be82010-05-06 00:08:46 +00003408 while (--envc >= 0)
3409 PyMem_DEL(envlist[envc]);
3410 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003411 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003412 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003413 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003414 Py_DECREF(opath);
3415 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003416}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003417
3418/* OS/2 supports spawnvp & spawnvpe natively */
3419#if defined(PYOS_OS2)
3420PyDoc_STRVAR(posix_spawnvp__doc__,
3421"spawnvp(mode, file, args)\n\n\
3422Execute the program 'file' in a new process, using the environment\n\
3423search path to find the file.\n\
3424\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003425 mode: mode of process creation\n\
3426 file: executable file name\n\
3427 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003428
3429static PyObject *
3430posix_spawnvp(PyObject *self, PyObject *args)
3431{
Victor Stinner8c62be82010-05-06 00:08:46 +00003432 PyObject *opath;
3433 char *path;
3434 PyObject *argv;
3435 char **argvlist;
3436 int mode, i, argc;
3437 Py_intptr_t spawnval;
3438 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003439
Victor Stinner8c62be82010-05-06 00:08:46 +00003440 /* spawnvp has three arguments: (mode, path, argv), where
3441 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003442
Victor Stinner8c62be82010-05-06 00:08:46 +00003443 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3444 PyUnicode_FSConverter,
3445 &opath, &argv))
3446 return NULL;
3447 path = PyBytes_AsString(opath);
3448 if (PyList_Check(argv)) {
3449 argc = PyList_Size(argv);
3450 getitem = PyList_GetItem;
3451 }
3452 else if (PyTuple_Check(argv)) {
3453 argc = PyTuple_Size(argv);
3454 getitem = PyTuple_GetItem;
3455 }
3456 else {
3457 PyErr_SetString(PyExc_TypeError,
3458 "spawnvp() arg 2 must be a tuple or list");
3459 Py_DECREF(opath);
3460 return NULL;
3461 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003462
Victor Stinner8c62be82010-05-06 00:08:46 +00003463 argvlist = PyMem_NEW(char *, argc+1);
3464 if (argvlist == NULL) {
3465 Py_DECREF(opath);
3466 return PyErr_NoMemory();
3467 }
3468 for (i = 0; i < argc; i++) {
3469 if (!fsconvert_strdup((*getitem)(argv, i),
3470 &argvlist[i])) {
3471 free_string_array(argvlist, i);
3472 PyErr_SetString(
3473 PyExc_TypeError,
3474 "spawnvp() arg 2 must contain only strings");
3475 Py_DECREF(opath);
3476 return NULL;
3477 }
3478 }
3479 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003480
Victor Stinner8c62be82010-05-06 00:08:46 +00003481 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003482#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003483 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003484#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003485 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003486#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003487 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003488
Victor Stinner8c62be82010-05-06 00:08:46 +00003489 free_string_array(argvlist, argc);
3490 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003491
Victor Stinner8c62be82010-05-06 00:08:46 +00003492 if (spawnval == -1)
3493 return posix_error();
3494 else
3495 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003496}
3497
3498
3499PyDoc_STRVAR(posix_spawnvpe__doc__,
3500"spawnvpe(mode, file, args, env)\n\n\
3501Execute the program 'file' in a new process, using the environment\n\
3502search path to find the file.\n\
3503\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003504 mode: mode of process creation\n\
3505 file: executable file name\n\
3506 args: tuple or list of arguments\n\
3507 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003508
3509static PyObject *
3510posix_spawnvpe(PyObject *self, PyObject *args)
3511{
Victor Stinner8c62be82010-05-06 00:08:46 +00003512 PyObject *opath
3513 char *path;
3514 PyObject *argv, *env;
3515 char **argvlist;
3516 char **envlist;
3517 PyObject *res=NULL;
3518 int mode, i, argc, envc;
3519 Py_intptr_t spawnval;
3520 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3521 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003522
Victor Stinner8c62be82010-05-06 00:08:46 +00003523 /* spawnvpe has four arguments: (mode, path, argv, env), where
3524 argv is a list or tuple of strings and env is a dictionary
3525 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003526
Victor Stinner8c62be82010-05-06 00:08:46 +00003527 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3528 PyUnicode_FSConverter,
3529 &opath, &argv, &env))
3530 return NULL;
3531 path = PyBytes_AsString(opath);
3532 if (PyList_Check(argv)) {
3533 argc = PyList_Size(argv);
3534 getitem = PyList_GetItem;
3535 }
3536 else if (PyTuple_Check(argv)) {
3537 argc = PyTuple_Size(argv);
3538 getitem = PyTuple_GetItem;
3539 }
3540 else {
3541 PyErr_SetString(PyExc_TypeError,
3542 "spawnvpe() arg 2 must be a tuple or list");
3543 goto fail_0;
3544 }
3545 if (!PyMapping_Check(env)) {
3546 PyErr_SetString(PyExc_TypeError,
3547 "spawnvpe() arg 3 must be a mapping object");
3548 goto fail_0;
3549 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003550
Victor Stinner8c62be82010-05-06 00:08:46 +00003551 argvlist = PyMem_NEW(char *, argc+1);
3552 if (argvlist == NULL) {
3553 PyErr_NoMemory();
3554 goto fail_0;
3555 }
3556 for (i = 0; i < argc; i++) {
3557 if (!fsconvert_strdup((*getitem)(argv, i),
3558 &argvlist[i]))
3559 {
3560 lastarg = i;
3561 goto fail_1;
3562 }
3563 }
3564 lastarg = argc;
3565 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003566
Victor Stinner8c62be82010-05-06 00:08:46 +00003567 envlist = parse_envlist(env, &envc);
3568 if (envlist == NULL)
3569 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003570
Victor Stinner8c62be82010-05-06 00:08:46 +00003571 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003572#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003573 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003574#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003575 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003576#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003577 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003578
Victor Stinner8c62be82010-05-06 00:08:46 +00003579 if (spawnval == -1)
3580 (void) posix_error();
3581 else
3582 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003583
Victor Stinner8c62be82010-05-06 00:08:46 +00003584 while (--envc >= 0)
3585 PyMem_DEL(envlist[envc]);
3586 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003587 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003588 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003589 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003590 Py_DECREF(opath);
3591 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003592}
3593#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003594#endif /* HAVE_SPAWNV */
3595
3596
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003597#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003598PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003599"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003600Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3601\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003602Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003603
3604static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003605posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003606{
Victor Stinner8c62be82010-05-06 00:08:46 +00003607 pid_t pid;
3608 int result = 0;
3609 _PyImport_AcquireLock();
3610 pid = fork1();
3611 if (pid == 0) {
3612 /* child: this clobbers and resets the import lock. */
3613 PyOS_AfterFork();
3614 } else {
3615 /* parent: release the import lock. */
3616 result = _PyImport_ReleaseLock();
3617 }
3618 if (pid == -1)
3619 return posix_error();
3620 if (result < 0) {
3621 /* Don't clobber the OSError if the fork failed. */
3622 PyErr_SetString(PyExc_RuntimeError,
3623 "not holding the import lock");
3624 return NULL;
3625 }
3626 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003627}
3628#endif
3629
3630
Guido van Rossumad0ee831995-03-01 10:34:45 +00003631#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003632PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003633"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003634Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003635Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003636
Barry Warsaw53699e91996-12-10 23:23:01 +00003637static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003638posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003639{
Victor Stinner8c62be82010-05-06 00:08:46 +00003640 pid_t pid;
3641 int result = 0;
3642 _PyImport_AcquireLock();
3643 pid = fork();
3644 if (pid == 0) {
3645 /* child: this clobbers and resets the import lock. */
3646 PyOS_AfterFork();
3647 } else {
3648 /* parent: release the import lock. */
3649 result = _PyImport_ReleaseLock();
3650 }
3651 if (pid == -1)
3652 return posix_error();
3653 if (result < 0) {
3654 /* Don't clobber the OSError if the fork failed. */
3655 PyErr_SetString(PyExc_RuntimeError,
3656 "not holding the import lock");
3657 return NULL;
3658 }
3659 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003660}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003661#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003662
Neal Norwitzb59798b2003-03-21 01:43:31 +00003663/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003664/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3665#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003666#define DEV_PTY_FILE "/dev/ptc"
3667#define HAVE_DEV_PTMX
3668#else
3669#define DEV_PTY_FILE "/dev/ptmx"
3670#endif
3671
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003672#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003673#ifdef HAVE_PTY_H
3674#include <pty.h>
3675#else
3676#ifdef HAVE_LIBUTIL_H
3677#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00003678#else
3679#ifdef HAVE_UTIL_H
3680#include <util.h>
3681#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003682#endif /* HAVE_LIBUTIL_H */
3683#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003684#ifdef HAVE_STROPTS_H
3685#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003686#endif
3687#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003688
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003689#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003690PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003691"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003692Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003693
3694static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003695posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003696{
Victor Stinner8c62be82010-05-06 00:08:46 +00003697 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003698#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00003699 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003700#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003701#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003702 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003703#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00003704 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003705#endif
3706#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003707
Thomas Wouters70c21a12000-07-14 14:28:33 +00003708#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00003709 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3710 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003711#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003712 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3713 if (slave_name == NULL)
3714 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00003715
Victor Stinner8c62be82010-05-06 00:08:46 +00003716 slave_fd = open(slave_name, O_RDWR);
3717 if (slave_fd < 0)
3718 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003719#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003720 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3721 if (master_fd < 0)
3722 return posix_error();
3723 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3724 /* change permission of slave */
3725 if (grantpt(master_fd) < 0) {
3726 PyOS_setsig(SIGCHLD, sig_saved);
3727 return posix_error();
3728 }
3729 /* unlock slave */
3730 if (unlockpt(master_fd) < 0) {
3731 PyOS_setsig(SIGCHLD, sig_saved);
3732 return posix_error();
3733 }
3734 PyOS_setsig(SIGCHLD, sig_saved);
3735 slave_name = ptsname(master_fd); /* get name of slave */
3736 if (slave_name == NULL)
3737 return posix_error();
3738 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3739 if (slave_fd < 0)
3740 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003741#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003742 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3743 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003744#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00003745 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003746#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003747#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003748#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003749
Victor Stinner8c62be82010-05-06 00:08:46 +00003750 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003751
Fred Drake8cef4cf2000-06-28 16:40:38 +00003752}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003753#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003754
3755#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003756PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003757"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003758Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3759Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003760To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003761
3762static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003763posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003764{
Victor Stinner8c62be82010-05-06 00:08:46 +00003765 int master_fd = -1, result = 0;
3766 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003767
Victor Stinner8c62be82010-05-06 00:08:46 +00003768 _PyImport_AcquireLock();
3769 pid = forkpty(&master_fd, NULL, NULL, NULL);
3770 if (pid == 0) {
3771 /* child: this clobbers and resets the import lock. */
3772 PyOS_AfterFork();
3773 } else {
3774 /* parent: release the import lock. */
3775 result = _PyImport_ReleaseLock();
3776 }
3777 if (pid == -1)
3778 return posix_error();
3779 if (result < 0) {
3780 /* Don't clobber the OSError if the fork failed. */
3781 PyErr_SetString(PyExc_RuntimeError,
3782 "not holding the import lock");
3783 return NULL;
3784 }
3785 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003786}
3787#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003788
Guido van Rossumad0ee831995-03-01 10:34:45 +00003789#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003790PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003791"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003792Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003793
Barry Warsaw53699e91996-12-10 23:23:01 +00003794static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003795posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003796{
Victor Stinner8c62be82010-05-06 00:08:46 +00003797 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003798}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003799#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003800
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003801
Guido van Rossumad0ee831995-03-01 10:34:45 +00003802#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003803PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003804"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003805Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003806
Barry Warsaw53699e91996-12-10 23:23:01 +00003807static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003808posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003809{
Victor Stinner8c62be82010-05-06 00:08:46 +00003810 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003811}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003812#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003813
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003814
Guido van Rossumad0ee831995-03-01 10:34:45 +00003815#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003816PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003817"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003818Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003819
Barry Warsaw53699e91996-12-10 23:23:01 +00003820static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003821posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003822{
Victor Stinner8c62be82010-05-06 00:08:46 +00003823 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003824}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003825#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003826
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003827
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003828PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003829"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003830Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003831
Barry Warsaw53699e91996-12-10 23:23:01 +00003832static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003833posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003834{
Victor Stinner8c62be82010-05-06 00:08:46 +00003835 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003836}
3837
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003838
Fred Drakec9680921999-12-13 16:37:25 +00003839#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003840PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003841"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003842Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003843
3844static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003845posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003846{
3847 PyObject *result = NULL;
3848
Fred Drakec9680921999-12-13 16:37:25 +00003849#ifdef NGROUPS_MAX
3850#define MAX_GROUPS NGROUPS_MAX
3851#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003852 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00003853#define MAX_GROUPS 64
3854#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003855 gid_t grouplist[MAX_GROUPS];
3856 int n;
Fred Drakec9680921999-12-13 16:37:25 +00003857
Victor Stinner8c62be82010-05-06 00:08:46 +00003858 n = getgroups(MAX_GROUPS, grouplist);
3859 if (n < 0)
3860 posix_error();
3861 else {
3862 result = PyList_New(n);
3863 if (result != NULL) {
3864 int i;
3865 for (i = 0; i < n; ++i) {
3866 PyObject *o = PyLong_FromLong((long)grouplist[i]);
3867 if (o == NULL) {
3868 Py_DECREF(result);
3869 result = NULL;
3870 break;
Fred Drakec9680921999-12-13 16:37:25 +00003871 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003872 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00003873 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003874 }
3875 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003876
Fred Drakec9680921999-12-13 16:37:25 +00003877 return result;
3878}
3879#endif
3880
Antoine Pitroub7572f02009-12-02 20:46:48 +00003881#ifdef HAVE_INITGROUPS
3882PyDoc_STRVAR(posix_initgroups__doc__,
3883"initgroups(username, gid) -> None\n\n\
3884Call the system initgroups() to initialize the group access list with all of\n\
3885the groups of which the specified username is a member, plus the specified\n\
3886group id.");
3887
3888static PyObject *
3889posix_initgroups(PyObject *self, PyObject *args)
3890{
Victor Stinner8c62be82010-05-06 00:08:46 +00003891 char *username;
3892 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00003893
Victor Stinner8c62be82010-05-06 00:08:46 +00003894 if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid))
3895 return NULL;
Antoine Pitroub7572f02009-12-02 20:46:48 +00003896
Victor Stinner8c62be82010-05-06 00:08:46 +00003897 if (initgroups(username, (gid_t) gid) == -1)
3898 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00003899
Victor Stinner8c62be82010-05-06 00:08:46 +00003900 Py_INCREF(Py_None);
3901 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00003902}
3903#endif
3904
Martin v. Löwis606edc12002-06-13 21:09:11 +00003905#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003906PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003907"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003908Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003909
3910static PyObject *
3911posix_getpgid(PyObject *self, PyObject *args)
3912{
Victor Stinner8c62be82010-05-06 00:08:46 +00003913 pid_t pid, pgid;
3914 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
3915 return NULL;
3916 pgid = getpgid(pid);
3917 if (pgid < 0)
3918 return posix_error();
3919 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003920}
3921#endif /* HAVE_GETPGID */
3922
3923
Guido van Rossumb6775db1994-08-01 11:34:53 +00003924#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003925PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003926"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003927Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003928
Barry Warsaw53699e91996-12-10 23:23:01 +00003929static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003930posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003931{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003932#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00003933 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003934#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00003935 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003936#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003937}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003938#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003939
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003940
Guido van Rossumb6775db1994-08-01 11:34:53 +00003941#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003942PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003943"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003944Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003945
Barry Warsaw53699e91996-12-10 23:23:01 +00003946static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003947posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003948{
Guido van Rossum64933891994-10-20 21:56:42 +00003949#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00003950 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003951#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00003952 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003953#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00003954 return posix_error();
3955 Py_INCREF(Py_None);
3956 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003957}
3958
Guido van Rossumb6775db1994-08-01 11:34:53 +00003959#endif /* HAVE_SETPGRP */
3960
Guido van Rossumad0ee831995-03-01 10:34:45 +00003961#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003962PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003963"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003964Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003965
Barry Warsaw53699e91996-12-10 23:23:01 +00003966static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003967posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003968{
Victor Stinner8c62be82010-05-06 00:08:46 +00003969 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003970}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003971#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003972
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003973
Fred Drake12c6e2d1999-12-14 21:25:03 +00003974#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003975PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003976"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003977Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003978
3979static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003980posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003981{
Victor Stinner8c62be82010-05-06 00:08:46 +00003982 PyObject *result = NULL;
3983 char *name;
3984 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003985
Victor Stinner8c62be82010-05-06 00:08:46 +00003986 errno = 0;
3987 name = getlogin();
3988 if (name == NULL) {
3989 if (errno)
3990 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00003991 else
Victor Stinner8c62be82010-05-06 00:08:46 +00003992 PyErr_SetString(PyExc_OSError,
3993 "unable to determine login name");
3994 }
3995 else
3996 result = PyUnicode_FromString(name);
3997 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003998
Fred Drake12c6e2d1999-12-14 21:25:03 +00003999 return result;
4000}
4001#endif
4002
Guido van Rossumad0ee831995-03-01 10:34:45 +00004003#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004004PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004005"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004006Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004007
Barry Warsaw53699e91996-12-10 23:23:01 +00004008static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004009posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004010{
Victor Stinner8c62be82010-05-06 00:08:46 +00004011 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004012}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004013#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004014
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004015
Guido van Rossumad0ee831995-03-01 10:34:45 +00004016#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004017PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004018"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004019Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004020
Barry Warsaw53699e91996-12-10 23:23:01 +00004021static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004022posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004023{
Victor Stinner8c62be82010-05-06 00:08:46 +00004024 pid_t pid;
4025 int sig;
4026 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4027 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004028#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004029 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4030 APIRET rc;
4031 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004032 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004033
4034 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4035 APIRET rc;
4036 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004037 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004038
4039 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004040 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004041#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004042 if (kill(pid, sig) == -1)
4043 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004044#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004045 Py_INCREF(Py_None);
4046 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004047}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004048#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004049
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004050#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004051PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004052"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004053Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004054
4055static PyObject *
4056posix_killpg(PyObject *self, PyObject *args)
4057{
Victor Stinner8c62be82010-05-06 00:08:46 +00004058 int sig;
4059 pid_t pgid;
4060 /* XXX some man pages make the `pgid` parameter an int, others
4061 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4062 take the same type. Moreover, pid_t is always at least as wide as
4063 int (else compilation of this module fails), which is safe. */
4064 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4065 return NULL;
4066 if (killpg(pgid, sig) == -1)
4067 return posix_error();
4068 Py_INCREF(Py_None);
4069 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004070}
4071#endif
4072
Brian Curtineb24d742010-04-12 17:16:38 +00004073#ifdef MS_WINDOWS
4074PyDoc_STRVAR(win32_kill__doc__,
4075"kill(pid, sig)\n\n\
4076Kill a process with a signal.");
4077
4078static PyObject *
4079win32_kill(PyObject *self, PyObject *args)
4080{
Victor Stinner8c62be82010-05-06 00:08:46 +00004081 PyObject *result, handle_obj;
4082 DWORD pid, sig, err;
4083 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004084
Victor Stinner8c62be82010-05-06 00:08:46 +00004085 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4086 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004087
Victor Stinner8c62be82010-05-06 00:08:46 +00004088 /* Console processes which share a common console can be sent CTRL+C or
4089 CTRL+BREAK events, provided they handle said events. */
4090 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4091 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4092 err = GetLastError();
4093 PyErr_SetFromWindowsErr(err);
4094 }
4095 else
4096 Py_RETURN_NONE;
4097 }
Brian Curtineb24d742010-04-12 17:16:38 +00004098
Victor Stinner8c62be82010-05-06 00:08:46 +00004099 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4100 attempt to open and terminate the process. */
4101 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4102 if (handle == NULL) {
4103 err = GetLastError();
4104 return PyErr_SetFromWindowsErr(err);
4105 }
Brian Curtineb24d742010-04-12 17:16:38 +00004106
Victor Stinner8c62be82010-05-06 00:08:46 +00004107 if (TerminateProcess(handle, sig) == 0) {
4108 err = GetLastError();
4109 result = PyErr_SetFromWindowsErr(err);
4110 } else {
4111 Py_INCREF(Py_None);
4112 result = Py_None;
4113 }
Brian Curtineb24d742010-04-12 17:16:38 +00004114
Victor Stinner8c62be82010-05-06 00:08:46 +00004115 CloseHandle(handle);
4116 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004117}
4118#endif /* MS_WINDOWS */
4119
Guido van Rossumc0125471996-06-28 18:55:32 +00004120#ifdef HAVE_PLOCK
4121
4122#ifdef HAVE_SYS_LOCK_H
4123#include <sys/lock.h>
4124#endif
4125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004126PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004127"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004128Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004129
Barry Warsaw53699e91996-12-10 23:23:01 +00004130static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004131posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004132{
Victor Stinner8c62be82010-05-06 00:08:46 +00004133 int op;
4134 if (!PyArg_ParseTuple(args, "i:plock", &op))
4135 return NULL;
4136 if (plock(op) == -1)
4137 return posix_error();
4138 Py_INCREF(Py_None);
4139 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004140}
4141#endif
4142
Guido van Rossumb6775db1994-08-01 11:34:53 +00004143#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004144PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004145"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004146Set the current process's user id.");
4147
Barry Warsaw53699e91996-12-10 23:23:01 +00004148static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004149posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004150{
Victor Stinner8c62be82010-05-06 00:08:46 +00004151 long uid_arg;
4152 uid_t uid;
4153 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4154 return NULL;
4155 uid = uid_arg;
4156 if (uid != uid_arg) {
4157 PyErr_SetString(PyExc_OverflowError, "user id too big");
4158 return NULL;
4159 }
4160 if (setuid(uid) < 0)
4161 return posix_error();
4162 Py_INCREF(Py_None);
4163 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004164}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004165#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004166
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004167
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004168#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004169PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004170"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004171Set the current process's effective user id.");
4172
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004173static PyObject *
4174posix_seteuid (PyObject *self, PyObject *args)
4175{
Victor Stinner8c62be82010-05-06 00:08:46 +00004176 long euid_arg;
4177 uid_t euid;
4178 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4179 return NULL;
4180 euid = euid_arg;
4181 if (euid != euid_arg) {
4182 PyErr_SetString(PyExc_OverflowError, "user id too big");
4183 return NULL;
4184 }
4185 if (seteuid(euid) < 0) {
4186 return posix_error();
4187 } else {
4188 Py_INCREF(Py_None);
4189 return Py_None;
4190 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004191}
4192#endif /* HAVE_SETEUID */
4193
4194#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004195PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004196"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004197Set the current process's effective group id.");
4198
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004199static PyObject *
4200posix_setegid (PyObject *self, PyObject *args)
4201{
Victor Stinner8c62be82010-05-06 00:08:46 +00004202 long egid_arg;
4203 gid_t egid;
4204 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4205 return NULL;
4206 egid = egid_arg;
4207 if (egid != egid_arg) {
4208 PyErr_SetString(PyExc_OverflowError, "group id too big");
4209 return NULL;
4210 }
4211 if (setegid(egid) < 0) {
4212 return posix_error();
4213 } else {
4214 Py_INCREF(Py_None);
4215 return Py_None;
4216 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004217}
4218#endif /* HAVE_SETEGID */
4219
4220#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004221PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004222"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004223Set the current process's real and effective user ids.");
4224
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004225static PyObject *
4226posix_setreuid (PyObject *self, PyObject *args)
4227{
Victor Stinner8c62be82010-05-06 00:08:46 +00004228 long ruid_arg, euid_arg;
4229 uid_t ruid, euid;
4230 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4231 return NULL;
4232 if (ruid_arg == -1)
4233 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4234 else
4235 ruid = ruid_arg; /* otherwise, assign from our long */
4236 if (euid_arg == -1)
4237 euid = (uid_t)-1;
4238 else
4239 euid = euid_arg;
4240 if ((euid_arg != -1 && euid != euid_arg) ||
4241 (ruid_arg != -1 && ruid != ruid_arg)) {
4242 PyErr_SetString(PyExc_OverflowError, "user id too big");
4243 return NULL;
4244 }
4245 if (setreuid(ruid, euid) < 0) {
4246 return posix_error();
4247 } else {
4248 Py_INCREF(Py_None);
4249 return Py_None;
4250 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004251}
4252#endif /* HAVE_SETREUID */
4253
4254#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004255PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004256"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004257Set the current process's real and effective group ids.");
4258
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004259static PyObject *
4260posix_setregid (PyObject *self, PyObject *args)
4261{
Victor Stinner8c62be82010-05-06 00:08:46 +00004262 long rgid_arg, egid_arg;
4263 gid_t rgid, egid;
4264 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4265 return NULL;
4266 if (rgid_arg == -1)
4267 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4268 else
4269 rgid = rgid_arg; /* otherwise, assign from our long */
4270 if (egid_arg == -1)
4271 egid = (gid_t)-1;
4272 else
4273 egid = egid_arg;
4274 if ((egid_arg != -1 && egid != egid_arg) ||
4275 (rgid_arg != -1 && rgid != rgid_arg)) {
4276 PyErr_SetString(PyExc_OverflowError, "group id too big");
4277 return NULL;
4278 }
4279 if (setregid(rgid, egid) < 0) {
4280 return posix_error();
4281 } else {
4282 Py_INCREF(Py_None);
4283 return Py_None;
4284 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004285}
4286#endif /* HAVE_SETREGID */
4287
Guido van Rossumb6775db1994-08-01 11:34:53 +00004288#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004289PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004290"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004291Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004292
Barry Warsaw53699e91996-12-10 23:23:01 +00004293static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004294posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004295{
Victor Stinner8c62be82010-05-06 00:08:46 +00004296 long gid_arg;
4297 gid_t gid;
4298 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4299 return NULL;
4300 gid = gid_arg;
4301 if (gid != gid_arg) {
4302 PyErr_SetString(PyExc_OverflowError, "group id too big");
4303 return NULL;
4304 }
4305 if (setgid(gid) < 0)
4306 return posix_error();
4307 Py_INCREF(Py_None);
4308 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004309}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004310#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004311
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004312#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004313PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004314"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004315Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004316
4317static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004318posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004319{
Victor Stinner8c62be82010-05-06 00:08:46 +00004320 int i, len;
4321 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004322
Victor Stinner8c62be82010-05-06 00:08:46 +00004323 if (!PySequence_Check(groups)) {
4324 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4325 return NULL;
4326 }
4327 len = PySequence_Size(groups);
4328 if (len > MAX_GROUPS) {
4329 PyErr_SetString(PyExc_ValueError, "too many groups");
4330 return NULL;
4331 }
4332 for(i = 0; i < len; i++) {
4333 PyObject *elem;
4334 elem = PySequence_GetItem(groups, i);
4335 if (!elem)
4336 return NULL;
4337 if (!PyLong_Check(elem)) {
4338 PyErr_SetString(PyExc_TypeError,
4339 "groups must be integers");
4340 Py_DECREF(elem);
4341 return NULL;
4342 } else {
4343 unsigned long x = PyLong_AsUnsignedLong(elem);
4344 if (PyErr_Occurred()) {
4345 PyErr_SetString(PyExc_TypeError,
4346 "group id too big");
4347 Py_DECREF(elem);
4348 return NULL;
4349 }
4350 grouplist[i] = x;
4351 /* read back the value to see if it fitted in gid_t */
4352 if (grouplist[i] != x) {
4353 PyErr_SetString(PyExc_TypeError,
4354 "group id too big");
4355 Py_DECREF(elem);
4356 return NULL;
4357 }
4358 }
4359 Py_DECREF(elem);
4360 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004361
Victor Stinner8c62be82010-05-06 00:08:46 +00004362 if (setgroups(len, grouplist) < 0)
4363 return posix_error();
4364 Py_INCREF(Py_None);
4365 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004366}
4367#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004368
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004369#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4370static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004371wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004372{
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 PyObject *result;
4374 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004375
Victor Stinner8c62be82010-05-06 00:08:46 +00004376 if (pid == -1)
4377 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004378
Victor Stinner8c62be82010-05-06 00:08:46 +00004379 if (struct_rusage == NULL) {
4380 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4381 if (m == NULL)
4382 return NULL;
4383 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4384 Py_DECREF(m);
4385 if (struct_rusage == NULL)
4386 return NULL;
4387 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004388
Victor Stinner8c62be82010-05-06 00:08:46 +00004389 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4390 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4391 if (!result)
4392 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004393
4394#ifndef doubletime
4395#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4396#endif
4397
Victor Stinner8c62be82010-05-06 00:08:46 +00004398 PyStructSequence_SET_ITEM(result, 0,
4399 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4400 PyStructSequence_SET_ITEM(result, 1,
4401 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004402#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004403 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4404 SET_INT(result, 2, ru->ru_maxrss);
4405 SET_INT(result, 3, ru->ru_ixrss);
4406 SET_INT(result, 4, ru->ru_idrss);
4407 SET_INT(result, 5, ru->ru_isrss);
4408 SET_INT(result, 6, ru->ru_minflt);
4409 SET_INT(result, 7, ru->ru_majflt);
4410 SET_INT(result, 8, ru->ru_nswap);
4411 SET_INT(result, 9, ru->ru_inblock);
4412 SET_INT(result, 10, ru->ru_oublock);
4413 SET_INT(result, 11, ru->ru_msgsnd);
4414 SET_INT(result, 12, ru->ru_msgrcv);
4415 SET_INT(result, 13, ru->ru_nsignals);
4416 SET_INT(result, 14, ru->ru_nvcsw);
4417 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004418#undef SET_INT
4419
Victor Stinner8c62be82010-05-06 00:08:46 +00004420 if (PyErr_Occurred()) {
4421 Py_DECREF(result);
4422 return NULL;
4423 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004424
Victor Stinner8c62be82010-05-06 00:08:46 +00004425 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004426}
4427#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4428
4429#ifdef HAVE_WAIT3
4430PyDoc_STRVAR(posix_wait3__doc__,
4431"wait3(options) -> (pid, status, rusage)\n\n\
4432Wait for completion of a child process.");
4433
4434static PyObject *
4435posix_wait3(PyObject *self, PyObject *args)
4436{
Victor Stinner8c62be82010-05-06 00:08:46 +00004437 pid_t pid;
4438 int options;
4439 struct rusage ru;
4440 WAIT_TYPE status;
4441 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004442
Victor Stinner8c62be82010-05-06 00:08:46 +00004443 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4444 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004445
Victor Stinner8c62be82010-05-06 00:08:46 +00004446 Py_BEGIN_ALLOW_THREADS
4447 pid = wait3(&status, options, &ru);
4448 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004449
Victor Stinner8c62be82010-05-06 00:08:46 +00004450 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004451}
4452#endif /* HAVE_WAIT3 */
4453
4454#ifdef HAVE_WAIT4
4455PyDoc_STRVAR(posix_wait4__doc__,
4456"wait4(pid, options) -> (pid, status, rusage)\n\n\
4457Wait for completion of a given child process.");
4458
4459static PyObject *
4460posix_wait4(PyObject *self, PyObject *args)
4461{
Victor Stinner8c62be82010-05-06 00:08:46 +00004462 pid_t pid;
4463 int options;
4464 struct rusage ru;
4465 WAIT_TYPE status;
4466 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004467
Victor Stinner8c62be82010-05-06 00:08:46 +00004468 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4469 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004470
Victor Stinner8c62be82010-05-06 00:08:46 +00004471 Py_BEGIN_ALLOW_THREADS
4472 pid = wait4(pid, &status, options, &ru);
4473 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004474
Victor Stinner8c62be82010-05-06 00:08:46 +00004475 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004476}
4477#endif /* HAVE_WAIT4 */
4478
Guido van Rossumb6775db1994-08-01 11:34:53 +00004479#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004480PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004481"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004482Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004483
Barry Warsaw53699e91996-12-10 23:23:01 +00004484static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004485posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004486{
Victor Stinner8c62be82010-05-06 00:08:46 +00004487 pid_t pid;
4488 int options;
4489 WAIT_TYPE status;
4490 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004491
Victor Stinner8c62be82010-05-06 00:08:46 +00004492 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4493 return NULL;
4494 Py_BEGIN_ALLOW_THREADS
4495 pid = waitpid(pid, &status, options);
4496 Py_END_ALLOW_THREADS
4497 if (pid == -1)
4498 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004499
Victor Stinner8c62be82010-05-06 00:08:46 +00004500 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004501}
4502
Tim Petersab034fa2002-02-01 11:27:43 +00004503#elif defined(HAVE_CWAIT)
4504
4505/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004506PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004507"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004508"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004509
4510static PyObject *
4511posix_waitpid(PyObject *self, PyObject *args)
4512{
Victor Stinner8c62be82010-05-06 00:08:46 +00004513 Py_intptr_t pid;
4514 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004515
Victor Stinner8c62be82010-05-06 00:08:46 +00004516 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4517 return NULL;
4518 Py_BEGIN_ALLOW_THREADS
4519 pid = _cwait(&status, pid, options);
4520 Py_END_ALLOW_THREADS
4521 if (pid == -1)
4522 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004523
Victor Stinner8c62be82010-05-06 00:08:46 +00004524 /* shift the status left a byte so this is more like the POSIX waitpid */
4525 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004526}
4527#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004528
Guido van Rossumad0ee831995-03-01 10:34:45 +00004529#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004530PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004531"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004532Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004533
Barry Warsaw53699e91996-12-10 23:23:01 +00004534static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004535posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004536{
Victor Stinner8c62be82010-05-06 00:08:46 +00004537 pid_t pid;
4538 WAIT_TYPE status;
4539 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004540
Victor Stinner8c62be82010-05-06 00:08:46 +00004541 Py_BEGIN_ALLOW_THREADS
4542 pid = wait(&status);
4543 Py_END_ALLOW_THREADS
4544 if (pid == -1)
4545 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004546
Victor Stinner8c62be82010-05-06 00:08:46 +00004547 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004548}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004549#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004550
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004551
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004552PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004553"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004554Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004555
Barry Warsaw53699e91996-12-10 23:23:01 +00004556static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004557posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004558{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004559#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00004560 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004561#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004562#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004563 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004564#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004565 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004566#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004567#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004568}
4569
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004570
Guido van Rossumb6775db1994-08-01 11:34:53 +00004571#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004572PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004573"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004574Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004575
Barry Warsaw53699e91996-12-10 23:23:01 +00004576static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004577posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004578{
Victor Stinner8c62be82010-05-06 00:08:46 +00004579 PyObject* v;
4580 char buf[MAXPATHLEN];
4581 PyObject *opath;
4582 char *path;
4583 int n;
4584 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004585
Victor Stinner8c62be82010-05-06 00:08:46 +00004586 if (!PyArg_ParseTuple(args, "O&:readlink",
4587 PyUnicode_FSConverter, &opath))
4588 return NULL;
4589 path = PyBytes_AsString(opath);
4590 v = PySequence_GetItem(args, 0);
4591 if (v == NULL) {
4592 Py_DECREF(opath);
4593 return NULL;
4594 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004595
Victor Stinner8c62be82010-05-06 00:08:46 +00004596 if (PyUnicode_Check(v)) {
4597 arg_is_unicode = 1;
4598 }
4599 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004600
Victor Stinner8c62be82010-05-06 00:08:46 +00004601 Py_BEGIN_ALLOW_THREADS
4602 n = readlink(path, buf, (int) sizeof buf);
4603 Py_END_ALLOW_THREADS
4604 if (n < 0)
4605 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004606
Victor Stinner8c62be82010-05-06 00:08:46 +00004607 Py_DECREF(opath);
4608 v = PyBytes_FromStringAndSize(buf, n);
4609 if (arg_is_unicode) {
4610 PyObject *w;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004611
Victor Stinner8c62be82010-05-06 00:08:46 +00004612 w = PyUnicode_FromEncodedObject(v,
4613 Py_FileSystemDefaultEncoding,
4614 "surrogateescape");
4615 if (w != NULL) {
4616 Py_DECREF(v);
4617 v = w;
4618 }
4619 else {
4620 v = NULL;
4621 }
4622 }
4623 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004624}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004625#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004626
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004627
Guido van Rossumb6775db1994-08-01 11:34:53 +00004628#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004629PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004630"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004631Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004632
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004633static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004634posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004635{
Victor Stinner8c62be82010-05-06 00:08:46 +00004636 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004637}
4638#endif /* HAVE_SYMLINK */
4639
4640
4641#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004642#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4643static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004644system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004645{
4646 ULONG value = 0;
4647
4648 Py_BEGIN_ALLOW_THREADS
4649 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4650 Py_END_ALLOW_THREADS
4651
4652 return value;
4653}
4654
4655static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004656posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004657{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004658 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00004659 return Py_BuildValue("ddddd",
4660 (double)0 /* t.tms_utime / HZ */,
4661 (double)0 /* t.tms_stime / HZ */,
4662 (double)0 /* t.tms_cutime / HZ */,
4663 (double)0 /* t.tms_cstime / HZ */,
4664 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00004665}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004666#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004667#define NEED_TICKS_PER_SECOND
4668static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004669static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004670posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004671{
Victor Stinner8c62be82010-05-06 00:08:46 +00004672 struct tms t;
4673 clock_t c;
4674 errno = 0;
4675 c = times(&t);
4676 if (c == (clock_t) -1)
4677 return posix_error();
4678 return Py_BuildValue("ddddd",
4679 (double)t.tms_utime / ticks_per_second,
4680 (double)t.tms_stime / ticks_per_second,
4681 (double)t.tms_cutime / ticks_per_second,
4682 (double)t.tms_cstime / ticks_per_second,
4683 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004684}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004685#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004686#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004687
4688
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004689#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004690#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004691static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004692posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004693{
Victor Stinner8c62be82010-05-06 00:08:46 +00004694 FILETIME create, exit, kernel, user;
4695 HANDLE hProc;
4696 hProc = GetCurrentProcess();
4697 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4698 /* The fields of a FILETIME structure are the hi and lo part
4699 of a 64-bit value expressed in 100 nanosecond units.
4700 1e7 is one second in such units; 1e-7 the inverse.
4701 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4702 */
4703 return Py_BuildValue(
4704 "ddddd",
4705 (double)(user.dwHighDateTime*429.4967296 +
4706 user.dwLowDateTime*1e-7),
4707 (double)(kernel.dwHighDateTime*429.4967296 +
4708 kernel.dwLowDateTime*1e-7),
4709 (double)0,
4710 (double)0,
4711 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004712}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004713#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004714
4715#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004716PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004717"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004718Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004719#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004720
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004721
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004722#ifdef HAVE_GETSID
4723PyDoc_STRVAR(posix_getsid__doc__,
4724"getsid(pid) -> sid\n\n\
4725Call the system call getsid().");
4726
4727static PyObject *
4728posix_getsid(PyObject *self, PyObject *args)
4729{
Victor Stinner8c62be82010-05-06 00:08:46 +00004730 pid_t pid;
4731 int sid;
4732 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
4733 return NULL;
4734 sid = getsid(pid);
4735 if (sid < 0)
4736 return posix_error();
4737 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004738}
4739#endif /* HAVE_GETSID */
4740
4741
Guido van Rossumb6775db1994-08-01 11:34:53 +00004742#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004743PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004744"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004745Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004746
Barry Warsaw53699e91996-12-10 23:23:01 +00004747static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004748posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004749{
Victor Stinner8c62be82010-05-06 00:08:46 +00004750 if (setsid() < 0)
4751 return posix_error();
4752 Py_INCREF(Py_None);
4753 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004754}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004755#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004756
Guido van Rossumb6775db1994-08-01 11:34:53 +00004757#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004758PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004759"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004760Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004761
Barry Warsaw53699e91996-12-10 23:23:01 +00004762static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004763posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004764{
Victor Stinner8c62be82010-05-06 00:08:46 +00004765 pid_t pid;
4766 int pgrp;
4767 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
4768 return NULL;
4769 if (setpgid(pid, pgrp) < 0)
4770 return posix_error();
4771 Py_INCREF(Py_None);
4772 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004773}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004774#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004775
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004776
Guido van Rossumb6775db1994-08-01 11:34:53 +00004777#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004778PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004779"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004780Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004781
Barry Warsaw53699e91996-12-10 23:23:01 +00004782static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004783posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004784{
Victor Stinner8c62be82010-05-06 00:08:46 +00004785 int fd;
4786 pid_t pgid;
4787 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
4788 return NULL;
4789 pgid = tcgetpgrp(fd);
4790 if (pgid < 0)
4791 return posix_error();
4792 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004793}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004794#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004795
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004796
Guido van Rossumb6775db1994-08-01 11:34:53 +00004797#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004798PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004799"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004800Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004801
Barry Warsaw53699e91996-12-10 23:23:01 +00004802static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004803posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004804{
Victor Stinner8c62be82010-05-06 00:08:46 +00004805 int fd;
4806 pid_t pgid;
4807 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
4808 return NULL;
4809 if (tcsetpgrp(fd, pgid) < 0)
4810 return posix_error();
4811 Py_INCREF(Py_None);
4812 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004813}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004814#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004815
Guido van Rossum687dd131993-05-17 08:34:16 +00004816/* Functions acting on file descriptors */
4817
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004818PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004819"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004820Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004821
Barry Warsaw53699e91996-12-10 23:23:01 +00004822static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004823posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004824{
Victor Stinner8c62be82010-05-06 00:08:46 +00004825 PyObject *ofile;
4826 char *file;
4827 int flag;
4828 int mode = 0777;
4829 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004830
4831#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004832 PyUnicodeObject *po;
4833 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4834 Py_BEGIN_ALLOW_THREADS
4835 /* PyUnicode_AS_UNICODE OK without thread
4836 lock as it is a simple dereference. */
4837 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4838 Py_END_ALLOW_THREADS
4839 if (fd < 0)
4840 return posix_error();
4841 return PyLong_FromLong((long)fd);
4842 }
4843 /* Drop the argument parsing error as narrow strings
4844 are also valid. */
4845 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004846#endif
4847
Victor Stinner8c62be82010-05-06 00:08:46 +00004848 if (!PyArg_ParseTuple(args, "O&i|i",
4849 PyUnicode_FSConverter, &ofile,
4850 &flag, &mode))
4851 return NULL;
4852 file = PyBytes_AsString(ofile);
4853 Py_BEGIN_ALLOW_THREADS
4854 fd = open(file, flag, mode);
4855 Py_END_ALLOW_THREADS
4856 if (fd < 0)
4857 return posix_error_with_allocated_filename(ofile);
4858 Py_DECREF(ofile);
4859 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004860}
4861
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004863PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004864"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004865Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004866
Barry Warsaw53699e91996-12-10 23:23:01 +00004867static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004868posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004869{
Victor Stinner8c62be82010-05-06 00:08:46 +00004870 int fd, res;
4871 if (!PyArg_ParseTuple(args, "i:close", &fd))
4872 return NULL;
4873 if (!_PyVerify_fd(fd))
4874 return posix_error();
4875 Py_BEGIN_ALLOW_THREADS
4876 res = close(fd);
4877 Py_END_ALLOW_THREADS
4878 if (res < 0)
4879 return posix_error();
4880 Py_INCREF(Py_None);
4881 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004882}
4883
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004884
Victor Stinner8c62be82010-05-06 00:08:46 +00004885PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00004886"closerange(fd_low, fd_high)\n\n\
4887Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4888
4889static PyObject *
4890posix_closerange(PyObject *self, PyObject *args)
4891{
Victor Stinner8c62be82010-05-06 00:08:46 +00004892 int fd_from, fd_to, i;
4893 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4894 return NULL;
4895 Py_BEGIN_ALLOW_THREADS
4896 for (i = fd_from; i < fd_to; i++)
4897 if (_PyVerify_fd(i))
4898 close(i);
4899 Py_END_ALLOW_THREADS
4900 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00004901}
4902
4903
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004904PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004905"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004906Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004907
Barry Warsaw53699e91996-12-10 23:23:01 +00004908static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004909posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004910{
Victor Stinner8c62be82010-05-06 00:08:46 +00004911 int fd;
4912 if (!PyArg_ParseTuple(args, "i:dup", &fd))
4913 return NULL;
4914 if (!_PyVerify_fd(fd))
4915 return posix_error();
4916 Py_BEGIN_ALLOW_THREADS
4917 fd = dup(fd);
4918 Py_END_ALLOW_THREADS
4919 if (fd < 0)
4920 return posix_error();
4921 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004922}
4923
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004924
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004925PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004926"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004927Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004928
Barry Warsaw53699e91996-12-10 23:23:01 +00004929static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004930posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004931{
Victor Stinner8c62be82010-05-06 00:08:46 +00004932 int fd, fd2, res;
4933 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
4934 return NULL;
4935 if (!_PyVerify_fd_dup2(fd, fd2))
4936 return posix_error();
4937 Py_BEGIN_ALLOW_THREADS
4938 res = dup2(fd, fd2);
4939 Py_END_ALLOW_THREADS
4940 if (res < 0)
4941 return posix_error();
4942 Py_INCREF(Py_None);
4943 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004944}
4945
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004946
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004947PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004948"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004949Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004950
Barry Warsaw53699e91996-12-10 23:23:01 +00004951static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004952posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004953{
Victor Stinner8c62be82010-05-06 00:08:46 +00004954 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004955#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00004956 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004957#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004958 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004959#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004960 PyObject *posobj;
4961 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
4962 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004963#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00004964 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4965 switch (how) {
4966 case 0: how = SEEK_SET; break;
4967 case 1: how = SEEK_CUR; break;
4968 case 2: how = SEEK_END; break;
4969 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004970#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004971
4972#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00004973 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004974#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004975 pos = PyLong_Check(posobj) ?
4976 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004977#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004978 if (PyErr_Occurred())
4979 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004980
Victor Stinner8c62be82010-05-06 00:08:46 +00004981 if (!_PyVerify_fd(fd))
4982 return posix_error();
4983 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004984#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00004985 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004986#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004987 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004988#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004989 Py_END_ALLOW_THREADS
4990 if (res < 0)
4991 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004992
4993#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00004994 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004995#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004996 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004997#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004998}
4999
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005000
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005001PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005002"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005003Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005004
Barry Warsaw53699e91996-12-10 23:23:01 +00005005static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005006posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005007{
Victor Stinner8c62be82010-05-06 00:08:46 +00005008 int fd, size;
5009 Py_ssize_t n;
5010 PyObject *buffer;
5011 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5012 return NULL;
5013 if (size < 0) {
5014 errno = EINVAL;
5015 return posix_error();
5016 }
5017 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5018 if (buffer == NULL)
5019 return NULL;
5020 if (!_PyVerify_fd(fd))
5021 return posix_error();
5022 Py_BEGIN_ALLOW_THREADS
5023 n = read(fd, PyBytes_AS_STRING(buffer), size);
5024 Py_END_ALLOW_THREADS
5025 if (n < 0) {
5026 Py_DECREF(buffer);
5027 return posix_error();
5028 }
5029 if (n != size)
5030 _PyBytes_Resize(&buffer, n);
5031 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005032}
5033
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005034
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005035PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005036"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005037Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005038
Barry Warsaw53699e91996-12-10 23:23:01 +00005039static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005040posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005041{
Victor Stinner8c62be82010-05-06 00:08:46 +00005042 Py_buffer pbuf;
5043 int fd;
5044 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005045
Victor Stinner8c62be82010-05-06 00:08:46 +00005046 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5047 return NULL;
5048 if (!_PyVerify_fd(fd))
5049 return posix_error();
5050 Py_BEGIN_ALLOW_THREADS
5051 size = write(fd, pbuf.buf, (size_t)pbuf.len);
5052 Py_END_ALLOW_THREADS
5053 PyBuffer_Release(&pbuf);
5054 if (size < 0)
5055 return posix_error();
5056 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005057}
5058
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005059
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005060PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005061"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005062Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005063
Barry Warsaw53699e91996-12-10 23:23:01 +00005064static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005065posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005066{
Victor Stinner8c62be82010-05-06 00:08:46 +00005067 int fd;
5068 STRUCT_STAT st;
5069 int res;
5070 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5071 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005072#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005073 /* on OpenVMS we must ensure that all bytes are written to the file */
5074 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005075#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005076 if (!_PyVerify_fd(fd))
5077 return posix_error();
5078 Py_BEGIN_ALLOW_THREADS
5079 res = FSTAT(fd, &st);
5080 Py_END_ALLOW_THREADS
5081 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005082#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005083 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005084#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005085 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005086#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005087 }
Tim Peters5aa91602002-01-30 05:46:57 +00005088
Victor Stinner8c62be82010-05-06 00:08:46 +00005089 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005090}
5091
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005092PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005093"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005094Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005095connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005096
5097static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005098posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005099{
Victor Stinner8c62be82010-05-06 00:08:46 +00005100 int fd;
5101 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5102 return NULL;
5103 if (!_PyVerify_fd(fd))
5104 return PyBool_FromLong(0);
5105 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005106}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005107
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005108#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005109PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005110"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005111Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005112
Barry Warsaw53699e91996-12-10 23:23:01 +00005113static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005114posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005115{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005116#if defined(PYOS_OS2)
5117 HFILE read, write;
5118 APIRET rc;
5119
Victor Stinner8c62be82010-05-06 00:08:46 +00005120 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005121 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005122 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005123 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005124 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005125
5126 return Py_BuildValue("(ii)", read, write);
5127#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005128#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005129 int fds[2];
5130 int res;
5131 Py_BEGIN_ALLOW_THREADS
5132 res = pipe(fds);
5133 Py_END_ALLOW_THREADS
5134 if (res != 0)
5135 return posix_error();
5136 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005137#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005138 HANDLE read, write;
5139 int read_fd, write_fd;
5140 BOOL ok;
5141 Py_BEGIN_ALLOW_THREADS
5142 ok = CreatePipe(&read, &write, NULL, 0);
5143 Py_END_ALLOW_THREADS
5144 if (!ok)
5145 return win32_error("CreatePipe", NULL);
5146 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5147 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5148 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005149#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005150#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005151}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005152#endif /* HAVE_PIPE */
5153
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005154
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005155#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005156PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005157"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005158Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005159
Barry Warsaw53699e91996-12-10 23:23:01 +00005160static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005161posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005162{
Victor Stinner8c62be82010-05-06 00:08:46 +00005163 char *filename;
5164 int mode = 0666;
5165 int res;
5166 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
5167 return NULL;
5168 Py_BEGIN_ALLOW_THREADS
5169 res = mkfifo(filename, mode);
5170 Py_END_ALLOW_THREADS
5171 if (res < 0)
5172 return posix_error();
5173 Py_INCREF(Py_None);
5174 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005175}
5176#endif
5177
5178
Neal Norwitz11690112002-07-30 01:08:28 +00005179#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005180PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005181"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005182Create a filesystem node (file, device special file or named pipe)\n\
5183named filename. mode specifies both the permissions to use and the\n\
5184type of node to be created, being combined (bitwise OR) with one of\n\
5185S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005186device defines the newly created device special file (probably using\n\
5187os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005188
5189
5190static PyObject *
5191posix_mknod(PyObject *self, PyObject *args)
5192{
Victor Stinner8c62be82010-05-06 00:08:46 +00005193 char *filename;
5194 int mode = 0600;
5195 int device = 0;
5196 int res;
5197 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
5198 return NULL;
5199 Py_BEGIN_ALLOW_THREADS
5200 res = mknod(filename, mode, device);
5201 Py_END_ALLOW_THREADS
5202 if (res < 0)
5203 return posix_error();
5204 Py_INCREF(Py_None);
5205 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005206}
5207#endif
5208
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005209#ifdef HAVE_DEVICE_MACROS
5210PyDoc_STRVAR(posix_major__doc__,
5211"major(device) -> major number\n\
5212Extracts a device major number from a raw device number.");
5213
5214static PyObject *
5215posix_major(PyObject *self, PyObject *args)
5216{
Victor Stinner8c62be82010-05-06 00:08:46 +00005217 int device;
5218 if (!PyArg_ParseTuple(args, "i:major", &device))
5219 return NULL;
5220 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005221}
5222
5223PyDoc_STRVAR(posix_minor__doc__,
5224"minor(device) -> minor number\n\
5225Extracts a device minor number from a raw device number.");
5226
5227static PyObject *
5228posix_minor(PyObject *self, PyObject *args)
5229{
Victor Stinner8c62be82010-05-06 00:08:46 +00005230 int device;
5231 if (!PyArg_ParseTuple(args, "i:minor", &device))
5232 return NULL;
5233 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005234}
5235
5236PyDoc_STRVAR(posix_makedev__doc__,
5237"makedev(major, minor) -> device number\n\
5238Composes a raw device number from the major and minor device numbers.");
5239
5240static PyObject *
5241posix_makedev(PyObject *self, PyObject *args)
5242{
Victor Stinner8c62be82010-05-06 00:08:46 +00005243 int major, minor;
5244 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5245 return NULL;
5246 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005247}
5248#endif /* device macros */
5249
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005250
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005251#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005252PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005253"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005254Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005255
Barry Warsaw53699e91996-12-10 23:23:01 +00005256static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005257posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005258{
Victor Stinner8c62be82010-05-06 00:08:46 +00005259 int fd;
5260 off_t length;
5261 int res;
5262 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005263
Victor Stinner8c62be82010-05-06 00:08:46 +00005264 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5265 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005266
5267#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005268 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005269#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005270 length = PyLong_Check(lenobj) ?
5271 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005272#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005273 if (PyErr_Occurred())
5274 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005275
Victor Stinner8c62be82010-05-06 00:08:46 +00005276 Py_BEGIN_ALLOW_THREADS
5277 res = ftruncate(fd, length);
5278 Py_END_ALLOW_THREADS
5279 if (res < 0)
5280 return posix_error();
5281 Py_INCREF(Py_None);
5282 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005283}
5284#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005285
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005286#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005287PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005288"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005289Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005290
Fred Drake762e2061999-08-26 17:23:54 +00005291/* Save putenv() parameters as values here, so we can collect them when they
5292 * get re-set with another call for the same key. */
5293static PyObject *posix_putenv_garbage;
5294
Tim Peters5aa91602002-01-30 05:46:57 +00005295static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005296posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005297{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005298#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005299 wchar_t *s1, *s2;
5300 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005301#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005302 PyObject *os1, *os2;
5303 char *s1, *s2;
5304 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005305#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005306 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005307 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005308
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005309#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005310 if (!PyArg_ParseTuple(args,
5311 "uu:putenv",
5312 &s1, &s2))
5313 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005314#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005315 if (!PyArg_ParseTuple(args,
5316 "O&O&:putenv",
5317 PyUnicode_FSConverter, &os1,
5318 PyUnicode_FSConverter, &os2))
5319 return NULL;
5320 s1 = PyBytes_AsString(os1);
5321 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005322#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005323
5324#if defined(PYOS_OS2)
5325 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5326 APIRET rc;
5327
Guido van Rossumd48f2521997-12-05 22:19:34 +00005328 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005329 if (rc != NO_ERROR) {
5330 os2_error(rc);
5331 goto error;
5332 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005333
5334 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5335 APIRET rc;
5336
Guido van Rossumd48f2521997-12-05 22:19:34 +00005337 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005338 if (rc != NO_ERROR) {
5339 os2_error(rc);
5340 goto error;
5341 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005342 } else {
5343#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005344 /* XXX This can leak memory -- not easy to fix :-( */
5345 /* len includes space for a trailing \0; the size arg to
5346 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005347#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005348 len = wcslen(s1) + wcslen(s2) + 2;
5349 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005350#else
Victor Stinner84ae1182010-05-06 22:05:07 +00005351 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00005352 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005353#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005354 if (newstr == NULL) {
5355 PyErr_NoMemory();
5356 goto error;
5357 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005358#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005359 newenv = PyUnicode_AsUnicode(newstr);
5360 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5361 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005362 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005363 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005364 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005365#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005366 newenv = PyBytes_AS_STRING(newstr);
5367 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5368 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005369 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005370 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005371 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005372#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005373
Victor Stinner8c62be82010-05-06 00:08:46 +00005374 /* Install the first arg and newstr in posix_putenv_garbage;
5375 * this will cause previous value to be collected. This has to
5376 * happen after the real putenv() call because the old value
5377 * was still accessible until then. */
5378 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00005379#ifdef MS_WINDOWS
5380 PyTuple_GET_ITEM(args, 0),
5381#else
5382 os1,
5383#endif
5384 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005385 /* really not much we can do; just leak */
5386 PyErr_Clear();
5387 }
5388 else {
5389 Py_DECREF(newstr);
5390 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005391
5392#if defined(PYOS_OS2)
5393 }
5394#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005395
Martin v. Löwis011e8422009-05-05 04:43:17 +00005396#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005397 Py_DECREF(os1);
5398 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005399#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005400 Py_RETURN_NONE;
5401
5402error:
5403#ifndef MS_WINDOWS
5404 Py_DECREF(os1);
5405 Py_DECREF(os2);
5406#endif
5407 Py_XDECREF(newstr);
5408 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005409}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005410#endif /* putenv */
5411
Guido van Rossumc524d952001-10-19 01:31:59 +00005412#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005413PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005414"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005415Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005416
5417static PyObject *
5418posix_unsetenv(PyObject *self, PyObject *args)
5419{
Victor Stinner84ae1182010-05-06 22:05:07 +00005420#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005421 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00005422
Victor Stinner8c62be82010-05-06 00:08:46 +00005423 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5424 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00005425#else
5426 PyObject *os1;
5427 char *s1;
5428
5429 if (!PyArg_ParseTuple(args, "O&:unsetenv",
5430 PyUnicode_FSConverter, &os1))
5431 return NULL;
5432 s1 = PyBytes_AsString(os1);
5433#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00005434
Victor Stinner8c62be82010-05-06 00:08:46 +00005435 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00005436
Victor Stinner8c62be82010-05-06 00:08:46 +00005437 /* Remove the key from posix_putenv_garbage;
5438 * this will cause it to be collected. This has to
5439 * happen after the real unsetenv() call because the
5440 * old value was still accessible until then.
5441 */
5442 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00005443#ifdef MS_WINDOWS
5444 PyTuple_GET_ITEM(args, 0)
5445#else
5446 os1
5447#endif
5448 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005449 /* really not much we can do; just leak */
5450 PyErr_Clear();
5451 }
Guido van Rossumc524d952001-10-19 01:31:59 +00005452
Victor Stinner84ae1182010-05-06 22:05:07 +00005453#ifndef MS_WINDOWS
5454 Py_DECREF(os1);
5455#endif
5456 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00005457}
5458#endif /* unsetenv */
5459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005460PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005461"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005462Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005463
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005464static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005465posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005466{
Victor Stinner8c62be82010-05-06 00:08:46 +00005467 int code;
5468 char *message;
5469 if (!PyArg_ParseTuple(args, "i:strerror", &code))
5470 return NULL;
5471 message = strerror(code);
5472 if (message == NULL) {
5473 PyErr_SetString(PyExc_ValueError,
5474 "strerror() argument out of range");
5475 return NULL;
5476 }
5477 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005478}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005479
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005480
Guido van Rossumc9641791998-08-04 15:26:23 +00005481#ifdef HAVE_SYS_WAIT_H
5482
Fred Drake106c1a02002-04-23 15:58:02 +00005483#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005484PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005485"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005486Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005487
5488static PyObject *
5489posix_WCOREDUMP(PyObject *self, PyObject *args)
5490{
Victor Stinner8c62be82010-05-06 00:08:46 +00005491 WAIT_TYPE status;
5492 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005493
Victor Stinner8c62be82010-05-06 00:08:46 +00005494 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
5495 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005496
Victor Stinner8c62be82010-05-06 00:08:46 +00005497 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005498}
5499#endif /* WCOREDUMP */
5500
5501#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005502PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005503"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005504Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005505job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005506
5507static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005508posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005509{
Victor Stinner8c62be82010-05-06 00:08:46 +00005510 WAIT_TYPE status;
5511 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005512
Victor Stinner8c62be82010-05-06 00:08:46 +00005513 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
5514 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005515
Victor Stinner8c62be82010-05-06 00:08:46 +00005516 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005517}
5518#endif /* WIFCONTINUED */
5519
Guido van Rossumc9641791998-08-04 15:26:23 +00005520#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005521PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005522"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005523Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005524
5525static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005526posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005527{
Victor Stinner8c62be82010-05-06 00:08:46 +00005528 WAIT_TYPE status;
5529 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005530
Victor Stinner8c62be82010-05-06 00:08:46 +00005531 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
5532 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005533
Victor Stinner8c62be82010-05-06 00:08:46 +00005534 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005535}
5536#endif /* WIFSTOPPED */
5537
5538#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005539PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005540"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005541Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005542
5543static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005544posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005545{
Victor Stinner8c62be82010-05-06 00:08:46 +00005546 WAIT_TYPE status;
5547 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005548
Victor Stinner8c62be82010-05-06 00:08:46 +00005549 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
5550 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005551
Victor Stinner8c62be82010-05-06 00:08:46 +00005552 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005553}
5554#endif /* WIFSIGNALED */
5555
5556#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005557PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005558"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005559Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005560system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005561
5562static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005563posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005564{
Victor Stinner8c62be82010-05-06 00:08:46 +00005565 WAIT_TYPE status;
5566 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005567
Victor Stinner8c62be82010-05-06 00:08:46 +00005568 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
5569 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005570
Victor Stinner8c62be82010-05-06 00:08:46 +00005571 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005572}
5573#endif /* WIFEXITED */
5574
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005575#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005576PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005577"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005578Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005579
5580static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005581posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005582{
Victor Stinner8c62be82010-05-06 00:08:46 +00005583 WAIT_TYPE status;
5584 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005585
Victor Stinner8c62be82010-05-06 00:08:46 +00005586 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
5587 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005588
Victor Stinner8c62be82010-05-06 00:08:46 +00005589 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005590}
5591#endif /* WEXITSTATUS */
5592
5593#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005594PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005595"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005596Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005597value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005598
5599static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005600posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005601{
Victor Stinner8c62be82010-05-06 00:08:46 +00005602 WAIT_TYPE status;
5603 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005604
Victor Stinner8c62be82010-05-06 00:08:46 +00005605 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
5606 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005607
Victor Stinner8c62be82010-05-06 00:08:46 +00005608 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005609}
5610#endif /* WTERMSIG */
5611
5612#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005613PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005614"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005615Return the signal that stopped the process that provided\n\
5616the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005617
5618static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005619posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005620{
Victor Stinner8c62be82010-05-06 00:08:46 +00005621 WAIT_TYPE status;
5622 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005623
Victor Stinner8c62be82010-05-06 00:08:46 +00005624 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
5625 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005626
Victor Stinner8c62be82010-05-06 00:08:46 +00005627 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005628}
5629#endif /* WSTOPSIG */
5630
5631#endif /* HAVE_SYS_WAIT_H */
5632
5633
Thomas Wouters477c8d52006-05-27 19:21:47 +00005634#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005635#ifdef _SCO_DS
5636/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5637 needed definitions in sys/statvfs.h */
5638#define _SVID3
5639#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005640#include <sys/statvfs.h>
5641
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005642static PyObject*
5643_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005644 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5645 if (v == NULL)
5646 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005647
5648#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005649 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5650 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5651 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5652 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5653 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5654 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5655 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5656 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5657 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5658 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005659#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005660 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5661 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5662 PyStructSequence_SET_ITEM(v, 2,
5663 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
5664 PyStructSequence_SET_ITEM(v, 3,
5665 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
5666 PyStructSequence_SET_ITEM(v, 4,
5667 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
5668 PyStructSequence_SET_ITEM(v, 5,
5669 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
5670 PyStructSequence_SET_ITEM(v, 6,
5671 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
5672 PyStructSequence_SET_ITEM(v, 7,
5673 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
5674 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5675 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005676#endif
5677
Victor Stinner8c62be82010-05-06 00:08:46 +00005678 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005679}
5680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005681PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005682"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005683Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005684
5685static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005686posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005687{
Victor Stinner8c62be82010-05-06 00:08:46 +00005688 int fd, res;
5689 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005690
Victor Stinner8c62be82010-05-06 00:08:46 +00005691 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
5692 return NULL;
5693 Py_BEGIN_ALLOW_THREADS
5694 res = fstatvfs(fd, &st);
5695 Py_END_ALLOW_THREADS
5696 if (res != 0)
5697 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005698
Victor Stinner8c62be82010-05-06 00:08:46 +00005699 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005700}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005701#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005702
5703
Thomas Wouters477c8d52006-05-27 19:21:47 +00005704#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005705#include <sys/statvfs.h>
5706
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005707PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005708"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005709Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005710
5711static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005712posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005713{
Victor Stinner8c62be82010-05-06 00:08:46 +00005714 char *path;
5715 int res;
5716 struct statvfs st;
5717 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
5718 return NULL;
5719 Py_BEGIN_ALLOW_THREADS
5720 res = statvfs(path, &st);
5721 Py_END_ALLOW_THREADS
5722 if (res != 0)
5723 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005724
Victor Stinner8c62be82010-05-06 00:08:46 +00005725 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005726}
5727#endif /* HAVE_STATVFS */
5728
Fred Drakec9680921999-12-13 16:37:25 +00005729/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5730 * It maps strings representing configuration variable names to
5731 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005732 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005733 * rarely-used constants. There are three separate tables that use
5734 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005735 *
5736 * This code is always included, even if none of the interfaces that
5737 * need it are included. The #if hackery needed to avoid it would be
5738 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005739 */
5740struct constdef {
5741 char *name;
5742 long value;
5743};
5744
Fred Drake12c6e2d1999-12-14 21:25:03 +00005745static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005746conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005747 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005748{
Christian Heimes217cfd12007-12-02 14:31:20 +00005749 if (PyLong_Check(arg)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005750 *valuep = PyLong_AS_LONG(arg);
5751 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005752 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005753 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00005754 /* look up the value in the table using a binary search */
5755 size_t lo = 0;
5756 size_t mid;
5757 size_t hi = tablesize;
5758 int cmp;
5759 const char *confname;
5760 if (!PyUnicode_Check(arg)) {
5761 PyErr_SetString(PyExc_TypeError,
5762 "configuration names must be strings or integers");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005763 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005764 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005765 confname = _PyUnicode_AsString(arg);
5766 if (confname == NULL)
5767 return 0;
5768 while (lo < hi) {
5769 mid = (lo + hi) / 2;
5770 cmp = strcmp(confname, table[mid].name);
5771 if (cmp < 0)
5772 hi = mid;
5773 else if (cmp > 0)
5774 lo = mid + 1;
5775 else {
5776 *valuep = table[mid].value;
5777 return 1;
5778 }
5779 }
5780 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
5781 return 0;
5782 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005783}
5784
5785
5786#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5787static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005788#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005789 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00005790#endif
5791#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005792 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00005793#endif
Fred Drakec9680921999-12-13 16:37:25 +00005794#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005795 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005796#endif
5797#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00005798 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00005799#endif
5800#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00005801 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00005802#endif
5803#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00005804 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00005805#endif
5806#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005807 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005808#endif
5809#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00005810 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00005811#endif
5812#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00005813 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00005814#endif
5815#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005816 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005817#endif
5818#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00005819 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00005820#endif
5821#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005822 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005823#endif
5824#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00005825 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00005826#endif
5827#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005828 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005829#endif
5830#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00005831 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00005832#endif
5833#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005834 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005835#endif
5836#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00005837 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00005838#endif
5839};
5840
Fred Drakec9680921999-12-13 16:37:25 +00005841static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005842conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005843{
5844 return conv_confname(arg, valuep, posix_constants_pathconf,
5845 sizeof(posix_constants_pathconf)
5846 / sizeof(struct constdef));
5847}
5848#endif
5849
5850#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005851PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005852"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005853Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005854If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005855
5856static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005857posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005858{
5859 PyObject *result = NULL;
5860 int name, fd;
5861
Fred Drake12c6e2d1999-12-14 21:25:03 +00005862 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5863 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005864 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00005865
Victor Stinner8c62be82010-05-06 00:08:46 +00005866 errno = 0;
5867 limit = fpathconf(fd, name);
5868 if (limit == -1 && errno != 0)
5869 posix_error();
5870 else
5871 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005872 }
5873 return result;
5874}
5875#endif
5876
5877
5878#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005879PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005880"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005881Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005882If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005883
5884static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005885posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005886{
5887 PyObject *result = NULL;
5888 int name;
5889 char *path;
5890
5891 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5892 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005893 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00005894
Victor Stinner8c62be82010-05-06 00:08:46 +00005895 errno = 0;
5896 limit = pathconf(path, name);
5897 if (limit == -1 && errno != 0) {
5898 if (errno == EINVAL)
5899 /* could be a path or name problem */
5900 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00005901 else
Victor Stinner8c62be82010-05-06 00:08:46 +00005902 posix_error_with_filename(path);
5903 }
5904 else
5905 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005906 }
5907 return result;
5908}
5909#endif
5910
5911#ifdef HAVE_CONFSTR
5912static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005913#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00005914 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00005915#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00005916#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00005917 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00005918#endif
5919#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00005920 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00005921#endif
Fred Draked86ed291999-12-15 15:34:33 +00005922#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00005923 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00005924#endif
5925#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00005926 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00005927#endif
5928#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00005929 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00005930#endif
5931#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00005932 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00005933#endif
Fred Drakec9680921999-12-13 16:37:25 +00005934#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005935 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005936#endif
5937#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005938 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005939#endif
5940#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005941 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005942#endif
5943#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005944 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005945#endif
5946#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005947 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005948#endif
5949#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005950 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005951#endif
5952#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005953 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005954#endif
5955#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005956 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005957#endif
Fred Draked86ed291999-12-15 15:34:33 +00005958#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00005959 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00005960#endif
Fred Drakec9680921999-12-13 16:37:25 +00005961#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00005962 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00005963#endif
Fred Draked86ed291999-12-15 15:34:33 +00005964#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00005965 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00005966#endif
5967#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00005968 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00005969#endif
5970#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00005971 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00005972#endif
5973#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00005974 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00005975#endif
Fred Drakec9680921999-12-13 16:37:25 +00005976#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005977 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005978#endif
5979#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005980 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005981#endif
5982#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005983 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005984#endif
5985#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005986 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005987#endif
5988#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005989 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005990#endif
5991#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005992 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005993#endif
5994#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005995 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005996#endif
5997#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005998 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005999#endif
6000#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006001 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006002#endif
6003#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006004 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006005#endif
6006#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006007 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006008#endif
6009#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006010 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006011#endif
6012#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006013 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006014#endif
6015#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006016 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006017#endif
6018#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006019 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006020#endif
6021#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006022 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006023#endif
Fred Draked86ed291999-12-15 15:34:33 +00006024#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006025 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006026#endif
6027#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006028 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006029#endif
6030#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006031 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006032#endif
6033#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006034 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006035#endif
6036#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006037 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006038#endif
6039#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006040 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006041#endif
6042#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006043 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006044#endif
6045#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006046 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006047#endif
6048#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006049 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006050#endif
6051#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006052 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006053#endif
6054#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006055 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006056#endif
6057#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006058 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006059#endif
6060#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006061 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006062#endif
Fred Drakec9680921999-12-13 16:37:25 +00006063};
6064
6065static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006066conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006067{
6068 return conv_confname(arg, valuep, posix_constants_confstr,
6069 sizeof(posix_constants_confstr)
6070 / sizeof(struct constdef));
6071}
6072
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006073PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006074"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006075Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006076
6077static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006078posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006079{
6080 PyObject *result = NULL;
6081 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006082 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006083
6084 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006085 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006086
Fred Drakec9680921999-12-13 16:37:25 +00006087 errno = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006088 len = confstr(name, buffer, sizeof(buffer));
6089 if (len == 0) {
6090 if (errno) {
6091 posix_error();
6092 }
6093 else {
6094 result = Py_None;
6095 Py_INCREF(Py_None);
6096 }
Fred Drakec9680921999-12-13 16:37:25 +00006097 }
6098 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00006099 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006100 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006101 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006102 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006103 }
6104 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006105 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006106 }
6107 }
6108 return result;
6109}
6110#endif
6111
6112
6113#ifdef HAVE_SYSCONF
6114static struct constdef posix_constants_sysconf[] = {
6115#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006116 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006117#endif
6118#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006119 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006120#endif
6121#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006122 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006123#endif
6124#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006125 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006126#endif
6127#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006128 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006129#endif
6130#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006131 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006132#endif
6133#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006134 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006135#endif
6136#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006137 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006138#endif
6139#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006140 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006141#endif
6142#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006143 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006144#endif
Fred Draked86ed291999-12-15 15:34:33 +00006145#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006146 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006147#endif
6148#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006149 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006150#endif
Fred Drakec9680921999-12-13 16:37:25 +00006151#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006152 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006153#endif
Fred Drakec9680921999-12-13 16:37:25 +00006154#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006155 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006156#endif
6157#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006158 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006159#endif
6160#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006161 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006162#endif
6163#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006164 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006165#endif
6166#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006167 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006168#endif
Fred Draked86ed291999-12-15 15:34:33 +00006169#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006170 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006171#endif
Fred Drakec9680921999-12-13 16:37:25 +00006172#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006173 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006174#endif
6175#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006176 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006177#endif
6178#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006179 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006180#endif
6181#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006182 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006183#endif
6184#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006185 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006186#endif
Fred Draked86ed291999-12-15 15:34:33 +00006187#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006188 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006189#endif
Fred Drakec9680921999-12-13 16:37:25 +00006190#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006191 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006192#endif
6193#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006194 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006195#endif
6196#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006197 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006198#endif
6199#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006200 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006201#endif
6202#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006203 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006204#endif
6205#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006206 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006207#endif
6208#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006209 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006210#endif
6211#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006212 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006213#endif
6214#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006215 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006216#endif
6217#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006218 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006219#endif
6220#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006221 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006222#endif
6223#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006224 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006225#endif
6226#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006227 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006228#endif
6229#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006230 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006231#endif
6232#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006233 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006234#endif
6235#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006236 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006237#endif
6238#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006239 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006240#endif
6241#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006242 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006243#endif
6244#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006245 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006246#endif
6247#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006248 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006249#endif
6250#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006251 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006252#endif
6253#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006254 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006255#endif
6256#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006257 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006258#endif
Fred Draked86ed291999-12-15 15:34:33 +00006259#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006260 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006261#endif
Fred Drakec9680921999-12-13 16:37:25 +00006262#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006263 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006264#endif
6265#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006266 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006267#endif
6268#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006269 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006270#endif
Fred Draked86ed291999-12-15 15:34:33 +00006271#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006272 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006273#endif
Fred Drakec9680921999-12-13 16:37:25 +00006274#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006275 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006276#endif
Fred Draked86ed291999-12-15 15:34:33 +00006277#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006278 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006279#endif
6280#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006281 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006282#endif
Fred Drakec9680921999-12-13 16:37:25 +00006283#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006284 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006285#endif
6286#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006287 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006288#endif
6289#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006290 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006291#endif
6292#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006293 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006294#endif
Fred Draked86ed291999-12-15 15:34:33 +00006295#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00006296 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00006297#endif
Fred Drakec9680921999-12-13 16:37:25 +00006298#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00006299 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00006300#endif
6301#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00006302 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00006303#endif
6304#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006305 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006306#endif
6307#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006308 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00006309#endif
6310#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00006311 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00006312#endif
6313#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00006314 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00006315#endif
6316#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00006317 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00006318#endif
Fred Draked86ed291999-12-15 15:34:33 +00006319#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00006320 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00006321#endif
Fred Drakec9680921999-12-13 16:37:25 +00006322#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006323 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006324#endif
6325#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006326 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006327#endif
Fred Draked86ed291999-12-15 15:34:33 +00006328#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006329 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006330#endif
Fred Drakec9680921999-12-13 16:37:25 +00006331#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006332 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006333#endif
6334#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006335 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006336#endif
6337#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006338 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006339#endif
6340#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006341 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006342#endif
6343#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006344 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006345#endif
6346#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006347 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006348#endif
6349#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006350 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006351#endif
6352#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006353 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00006354#endif
6355#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006356 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00006357#endif
Fred Draked86ed291999-12-15 15:34:33 +00006358#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006359 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00006360#endif
6361#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006362 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00006363#endif
Fred Drakec9680921999-12-13 16:37:25 +00006364#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00006365 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00006366#endif
6367#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006368 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006369#endif
6370#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006371 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006372#endif
6373#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006374 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006375#endif
6376#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006377 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006378#endif
6379#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006380 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006381#endif
6382#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00006383 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00006384#endif
6385#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00006386 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00006387#endif
6388#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006389 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00006390#endif
6391#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006392 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00006393#endif
6394#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00006395 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00006396#endif
6397#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006398 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00006399#endif
6400#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006401 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00006402#endif
6403#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00006404 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00006405#endif
6406#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00006407 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00006408#endif
6409#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00006410 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00006411#endif
6412#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00006413 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00006414#endif
6415#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006416 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006417#endif
6418#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00006419 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006420#endif
6421#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00006422 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00006423#endif
6424#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006425 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006426#endif
6427#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006428 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006429#endif
6430#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00006431 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00006432#endif
6433#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006434 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006435#endif
6436#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006437 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006438#endif
6439#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00006440 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00006441#endif
6442#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00006443 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00006444#endif
6445#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006446 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006447#endif
6448#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006449 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006450#endif
6451#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006452 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00006453#endif
6454#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006455 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006456#endif
6457#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006458 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006459#endif
6460#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006461 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006462#endif
6463#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006464 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006465#endif
6466#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006467 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006468#endif
Fred Draked86ed291999-12-15 15:34:33 +00006469#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00006470 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00006471#endif
Fred Drakec9680921999-12-13 16:37:25 +00006472#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00006473 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00006474#endif
6475#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006476 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006477#endif
6478#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00006479 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00006480#endif
6481#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006482 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006483#endif
6484#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006485 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006486#endif
6487#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00006488 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00006489#endif
6490#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00006491 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00006492#endif
6493#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006494 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006495#endif
6496#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00006497 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00006498#endif
6499#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006500 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006501#endif
6502#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006504#endif
6505#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006506 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00006507#endif
6508#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00006509 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00006510#endif
6511#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00006512 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00006513#endif
6514#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00006515 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00006516#endif
6517#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006518 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006519#endif
6520#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006521 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006522#endif
6523#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006524 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00006525#endif
6526#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006527 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006528#endif
6529#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006530 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006531#endif
6532#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006533 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006534#endif
6535#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006536 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006537#endif
6538#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006539 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006540#endif
6541#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006542 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006543#endif
6544#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00006545 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00006546#endif
6547#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006548 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006549#endif
6550#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006551 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006552#endif
6553#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006554 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006555#endif
6556#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006557 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006558#endif
6559#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00006560 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00006561#endif
6562#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00006563 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00006564#endif
6565#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00006566 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00006567#endif
6568#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00006569 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00006570#endif
6571#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00006572 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00006573#endif
6574#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00006575 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00006576#endif
6577#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00006578 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00006579#endif
6580#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00006581 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00006582#endif
6583#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00006584 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00006585#endif
6586#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00006587 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00006588#endif
6589#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00006590 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00006591#endif
6592#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006593 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006594#endif
6595#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006596 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006597#endif
6598#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00006599 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00006600#endif
6601#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00006602 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00006603#endif
6604#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00006605 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00006606#endif
6607};
6608
6609static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006610conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006611{
6612 return conv_confname(arg, valuep, posix_constants_sysconf,
6613 sizeof(posix_constants_sysconf)
6614 / sizeof(struct constdef));
6615}
6616
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006617PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006618"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006619Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006620
6621static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006622posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006623{
6624 PyObject *result = NULL;
6625 int name;
6626
6627 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6628 int value;
6629
6630 errno = 0;
6631 value = sysconf(name);
6632 if (value == -1 && errno != 0)
6633 posix_error();
6634 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006635 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006636 }
6637 return result;
6638}
6639#endif
6640
6641
Fred Drakebec628d1999-12-15 18:31:10 +00006642/* This code is used to ensure that the tables of configuration value names
6643 * are in sorted order as required by conv_confname(), and also to build the
6644 * the exported dictionaries that are used to publish information about the
6645 * names available on the host platform.
6646 *
6647 * Sorting the table at runtime ensures that the table is properly ordered
6648 * when used, even for platforms we're not able to test on. It also makes
6649 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006650 */
Fred Drakebec628d1999-12-15 18:31:10 +00006651
6652static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006653cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006654{
6655 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00006656 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00006657 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00006658 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00006659
6660 return strcmp(c1->name, c2->name);
6661}
6662
6663static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006664setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00006665 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006666{
Fred Drakebec628d1999-12-15 18:31:10 +00006667 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006668 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006669
6670 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6671 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006672 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00006673 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006674
Barry Warsaw3155db32000-04-13 15:20:40 +00006675 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006676 PyObject *o = PyLong_FromLong(table[i].value);
6677 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6678 Py_XDECREF(o);
6679 Py_DECREF(d);
6680 return -1;
6681 }
6682 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006683 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006684 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006685}
6686
Fred Drakebec628d1999-12-15 18:31:10 +00006687/* Return -1 on failure, 0 on success. */
6688static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006689setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006690{
6691#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006692 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006693 sizeof(posix_constants_pathconf)
6694 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006695 "pathconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00006696 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006697#endif
6698#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006699 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006700 sizeof(posix_constants_confstr)
6701 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006702 "confstr_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00006703 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006704#endif
6705#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006706 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006707 sizeof(posix_constants_sysconf)
6708 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006709 "sysconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00006710 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006711#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006712 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006713}
Fred Draked86ed291999-12-15 15:34:33 +00006714
6715
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006716PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006717"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006718Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006719in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006720
6721static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006722posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006723{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006724 abort();
6725 /*NOTREACHED*/
6726 Py_FatalError("abort() called from Python code didn't abort!");
6727 return NULL;
6728}
Fred Drakebec628d1999-12-15 18:31:10 +00006729
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006730#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006731PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006732"startfile(filepath [, operation]) - Start a file with its associated\n\
6733application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006734\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006735When \"operation\" is not specified or \"open\", this acts like\n\
6736double-clicking the file in Explorer, or giving the file name as an\n\
6737argument to the DOS \"start\" command: the file is opened with whatever\n\
6738application (if any) its extension is associated.\n\
6739When another \"operation\" is given, it specifies what should be done with\n\
6740the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006741\n\
6742startfile returns as soon as the associated application is launched.\n\
6743There is no option to wait for the application to close, and no way\n\
6744to retrieve the application's exit status.\n\
6745\n\
6746The filepath is relative to the current directory. If you want to use\n\
6747an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006748the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006749
6750static PyObject *
6751win32_startfile(PyObject *self, PyObject *args)
6752{
Victor Stinner8c62be82010-05-06 00:08:46 +00006753 PyObject *ofilepath;
6754 char *filepath;
6755 char *operation = NULL;
6756 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006757
Victor Stinner8c62be82010-05-06 00:08:46 +00006758 PyObject *unipath, *woperation = NULL;
6759 if (!PyArg_ParseTuple(args, "U|s:startfile",
6760 &unipath, &operation)) {
6761 PyErr_Clear();
6762 goto normal;
6763 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006764
Victor Stinner8c62be82010-05-06 00:08:46 +00006765 if (operation) {
6766 woperation = PyUnicode_DecodeASCII(operation,
6767 strlen(operation), NULL);
6768 if (!woperation) {
6769 PyErr_Clear();
6770 operation = NULL;
6771 goto normal;
6772 }
6773 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006774
Victor Stinner8c62be82010-05-06 00:08:46 +00006775 Py_BEGIN_ALLOW_THREADS
6776 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6777 PyUnicode_AS_UNICODE(unipath),
6778 NULL, NULL, SW_SHOWNORMAL);
6779 Py_END_ALLOW_THREADS
6780
6781 Py_XDECREF(woperation);
6782 if (rc <= (HINSTANCE)32) {
6783 PyObject *errval = win32_error_unicode("startfile",
6784 PyUnicode_AS_UNICODE(unipath));
6785 return errval;
6786 }
6787 Py_INCREF(Py_None);
6788 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006789
6790normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00006791 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6792 PyUnicode_FSConverter, &ofilepath,
6793 &operation))
6794 return NULL;
6795 filepath = PyBytes_AsString(ofilepath);
6796 Py_BEGIN_ALLOW_THREADS
6797 rc = ShellExecute((HWND)0, operation, filepath,
6798 NULL, NULL, SW_SHOWNORMAL);
6799 Py_END_ALLOW_THREADS
6800 if (rc <= (HINSTANCE)32) {
6801 PyObject *errval = win32_error("startfile", filepath);
6802 Py_DECREF(ofilepath);
6803 return errval;
6804 }
6805 Py_DECREF(ofilepath);
6806 Py_INCREF(Py_None);
6807 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006808}
6809#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006810
Martin v. Löwis438b5342002-12-27 10:16:42 +00006811#ifdef HAVE_GETLOADAVG
6812PyDoc_STRVAR(posix_getloadavg__doc__,
6813"getloadavg() -> (float, float, float)\n\n\
6814Return the number of processes in the system run queue averaged over\n\
6815the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6816was unobtainable");
6817
6818static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006819posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006820{
6821 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006822 if (getloadavg(loadavg, 3)!=3) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006823 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6824 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00006825 } else
Victor Stinner8c62be82010-05-06 00:08:46 +00006826 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00006827}
6828#endif
6829
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006830#ifdef MS_WINDOWS
6831
6832PyDoc_STRVAR(win32_urandom__doc__,
6833"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006834Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006835
6836typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6837 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6838 DWORD dwFlags );
6839typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6840 BYTE *pbBuffer );
6841
6842static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006843/* This handle is never explicitly released. Instead, the operating
6844 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006845static HCRYPTPROV hCryptProv = 0;
6846
Tim Peters4ad82172004-08-30 17:02:04 +00006847static PyObject*
6848win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006849{
Victor Stinner8c62be82010-05-06 00:08:46 +00006850 int howMany;
6851 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006852
Victor Stinner8c62be82010-05-06 00:08:46 +00006853 /* Read arguments */
6854 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6855 return NULL;
6856 if (howMany < 0)
6857 return PyErr_Format(PyExc_ValueError,
6858 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006859
Victor Stinner8c62be82010-05-06 00:08:46 +00006860 if (hCryptProv == 0) {
6861 HINSTANCE hAdvAPI32 = NULL;
6862 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006863
Victor Stinner8c62be82010-05-06 00:08:46 +00006864 /* Obtain handle to the DLL containing CryptoAPI
6865 This should not fail */
6866 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6867 if(hAdvAPI32 == NULL)
6868 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006869
Victor Stinner8c62be82010-05-06 00:08:46 +00006870 /* Obtain pointers to the CryptoAPI functions
6871 This will fail on some early versions of Win95 */
6872 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6873 hAdvAPI32,
6874 "CryptAcquireContextA");
6875 if (pCryptAcquireContext == NULL)
6876 return PyErr_Format(PyExc_NotImplementedError,
6877 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006878
Victor Stinner8c62be82010-05-06 00:08:46 +00006879 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6880 hAdvAPI32, "CryptGenRandom");
6881 if (pCryptGenRandom == NULL)
6882 return PyErr_Format(PyExc_NotImplementedError,
6883 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006884
Victor Stinner8c62be82010-05-06 00:08:46 +00006885 /* Acquire context */
6886 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6887 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6888 return win32_error("CryptAcquireContext", NULL);
6889 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006890
Victor Stinner8c62be82010-05-06 00:08:46 +00006891 /* Allocate bytes */
6892 result = PyBytes_FromStringAndSize(NULL, howMany);
6893 if (result != NULL) {
6894 /* Get random data */
6895 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
6896 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
6897 PyBytes_AS_STRING(result))) {
6898 Py_DECREF(result);
6899 return win32_error("CryptGenRandom", NULL);
6900 }
6901 }
6902 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006903}
6904#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006905
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006906PyDoc_STRVAR(device_encoding__doc__,
6907"device_encoding(fd) -> str\n\n\
6908Return a string describing the encoding of the device\n\
6909if the output is a terminal; else return None.");
6910
6911static PyObject *
6912device_encoding(PyObject *self, PyObject *args)
6913{
Victor Stinner8c62be82010-05-06 00:08:46 +00006914 int fd;
6915 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6916 return NULL;
6917 if (!_PyVerify_fd(fd) || !isatty(fd)) {
6918 Py_INCREF(Py_None);
6919 return Py_None;
6920 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006921#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00006922 if (fd == 0) {
6923 char buf[100];
6924 sprintf(buf, "cp%d", GetConsoleCP());
6925 return PyUnicode_FromString(buf);
6926 }
6927 if (fd == 1 || fd == 2) {
6928 char buf[100];
6929 sprintf(buf, "cp%d", GetConsoleOutputCP());
6930 return PyUnicode_FromString(buf);
6931 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006932#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00006933 {
6934 char *codeset = nl_langinfo(CODESET);
6935 if (codeset != NULL && codeset[0] != 0)
6936 return PyUnicode_FromString(codeset);
6937 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006938#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006939 Py_INCREF(Py_None);
6940 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006941}
6942
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006943#ifdef __VMS
6944/* Use openssl random routine */
6945#include <openssl/rand.h>
6946PyDoc_STRVAR(vms_urandom__doc__,
6947"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006948Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006949
6950static PyObject*
6951vms_urandom(PyObject *self, PyObject *args)
6952{
Victor Stinner8c62be82010-05-06 00:08:46 +00006953 int howMany;
6954 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006955
Victor Stinner8c62be82010-05-06 00:08:46 +00006956 /* Read arguments */
6957 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6958 return NULL;
6959 if (howMany < 0)
6960 return PyErr_Format(PyExc_ValueError,
6961 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006962
Victor Stinner8c62be82010-05-06 00:08:46 +00006963 /* Allocate bytes */
6964 result = PyBytes_FromStringAndSize(NULL, howMany);
6965 if (result != NULL) {
6966 /* Get random data */
6967 if (RAND_pseudo_bytes((unsigned char*)
6968 PyBytes_AS_STRING(result),
6969 howMany) < 0) {
6970 Py_DECREF(result);
6971 return PyErr_Format(PyExc_ValueError,
6972 "RAND_pseudo_bytes");
6973 }
6974 }
6975 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006976}
6977#endif
6978
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00006979#ifdef HAVE_SETRESUID
6980PyDoc_STRVAR(posix_setresuid__doc__,
6981"setresuid(ruid, euid, suid)\n\n\
6982Set the current process's real, effective, and saved user ids.");
6983
6984static PyObject*
6985posix_setresuid (PyObject *self, PyObject *args)
6986{
Victor Stinner8c62be82010-05-06 00:08:46 +00006987 /* We assume uid_t is no larger than a long. */
6988 long ruid, euid, suid;
6989 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
6990 return NULL;
6991 if (setresuid(ruid, euid, suid) < 0)
6992 return posix_error();
6993 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00006994}
6995#endif
6996
6997#ifdef HAVE_SETRESGID
6998PyDoc_STRVAR(posix_setresgid__doc__,
6999"setresgid(rgid, egid, sgid)\n\n\
7000Set the current process's real, effective, and saved group ids.");
7001
7002static PyObject*
7003posix_setresgid (PyObject *self, PyObject *args)
7004{
Victor Stinner8c62be82010-05-06 00:08:46 +00007005 /* We assume uid_t is no larger than a long. */
7006 long rgid, egid, sgid;
7007 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7008 return NULL;
7009 if (setresgid(rgid, egid, sgid) < 0)
7010 return posix_error();
7011 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007012}
7013#endif
7014
7015#ifdef HAVE_GETRESUID
7016PyDoc_STRVAR(posix_getresuid__doc__,
7017"getresuid() -> (ruid, euid, suid)\n\n\
7018Get tuple of the current process's real, effective, and saved user ids.");
7019
7020static PyObject*
7021posix_getresuid (PyObject *self, PyObject *noargs)
7022{
Victor Stinner8c62be82010-05-06 00:08:46 +00007023 uid_t ruid, euid, suid;
7024 long l_ruid, l_euid, l_suid;
7025 if (getresuid(&ruid, &euid, &suid) < 0)
7026 return posix_error();
7027 /* Force the values into long's as we don't know the size of uid_t. */
7028 l_ruid = ruid;
7029 l_euid = euid;
7030 l_suid = suid;
7031 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007032}
7033#endif
7034
7035#ifdef HAVE_GETRESGID
7036PyDoc_STRVAR(posix_getresgid__doc__,
7037"getresgid() -> (rgid, egid, sgid)\n\n\
7038Get tuple of the current process's real, effective, and saved user ids.");
7039
7040static PyObject*
7041posix_getresgid (PyObject *self, PyObject *noargs)
7042{
Victor Stinner8c62be82010-05-06 00:08:46 +00007043 uid_t rgid, egid, sgid;
7044 long l_rgid, l_egid, l_sgid;
7045 if (getresgid(&rgid, &egid, &sgid) < 0)
7046 return posix_error();
7047 /* Force the values into long's as we don't know the size of uid_t. */
7048 l_rgid = rgid;
7049 l_egid = egid;
7050 l_sgid = sgid;
7051 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007052}
7053#endif
7054
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007055static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007056 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007057#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007058 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007059#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007060 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007061#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007063#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007064 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007065#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007066 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007067#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007068#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007069 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007070#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007071#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007072 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007073#endif /* HAVE_LCHMOD */
7074#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007075 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007076#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007077#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007078 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007079#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007080#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007081 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007082#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007083#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007085#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007086#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007087 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007088#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007089#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007090 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7091 METH_NOARGS, posix_getcwd__doc__},
7092 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7093 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007094#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007095#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007096 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007097#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007098 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7099 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7100 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007101#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007102 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007103#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007104#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007105 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007106#endif /* HAVE_READLINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007107 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7108 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7109 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
7110 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007111#ifdef HAVE_SYMLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007112 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007113#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007114#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007115 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007116#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007117 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007118#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007119 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007120#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007121 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7122 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7123 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007124#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007125 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007126#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007127 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007128#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7130 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007131#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007132#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007133 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7134 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007135#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007136 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7137 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007138#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007139#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007140#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007141 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007142#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007143#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007144 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007145#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007146#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007147 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007148#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007149#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007151#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007152#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007153 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007154#endif /* HAVE_GETEGID */
7155#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007156 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007157#endif /* HAVE_GETEUID */
7158#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007160#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007161#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007163#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007164 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007165#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007166 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007167#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007168#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007169 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007170#endif /* HAVE_GETPPID */
7171#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007172 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007173#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007174#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007175 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007176#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007177#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007178 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007179#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007180#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007181 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007182#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007183#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007184 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007185#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007186#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007187 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7188 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007189#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007190#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007191 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007192#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007193#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007194 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007195#endif /* HAVE_SETEUID */
7196#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007197 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007198#endif /* HAVE_SETEGID */
7199#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007200 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007201#endif /* HAVE_SETREUID */
7202#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007203 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007204#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007205#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007206 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007207#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007208#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007209 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007210#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007211#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007212 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007213#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007214#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007215 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007216#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007217#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007218 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007219#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007220#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007221 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007222#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007223#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007224 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007225#endif /* HAVE_WAIT3 */
7226#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007227 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007228#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007229#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007230 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007231#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007232#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007233 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007234#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007235#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007236 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007237#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007238#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007239 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007240#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007241#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007242 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007243#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007244#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007245 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007246#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007247 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7248 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7249 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7250 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7251 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7252 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7253 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7254 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7255 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7256 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7257 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007258#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007259 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007260#endif
7261#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007262 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007263#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007264#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007265 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007266#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007267#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007268 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7269 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7270 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007271#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007272#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007273 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007274#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007275#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007276 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007277#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007278#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007279 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007280#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007281 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007282#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007283 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007284#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007285#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007286 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007287#endif
7288#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007289 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007290#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007291#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007292#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00007293 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00007294#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007295#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007296 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007297#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007298#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00007299 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007300#endif /* WIFSTOPPED */
7301#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00007302 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007303#endif /* WIFSIGNALED */
7304#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00007305 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007306#endif /* WIFEXITED */
7307#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00007308 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007309#endif /* WEXITSTATUS */
7310#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007311 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007312#endif /* WTERMSIG */
7313#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007314 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007315#endif /* WSTOPSIG */
7316#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007317#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007318 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007319#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007320#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007321 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007322#endif
Fred Drakec9680921999-12-13 16:37:25 +00007323#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00007324 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007325#endif
7326#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007327 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007328#endif
7329#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007331#endif
7332#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007333 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007334#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007335 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007336#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007337 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00007338#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007339#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00007340 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007341#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007342 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007343 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007344 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007345 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007346 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007347 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007348#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007349 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007350#endif
7351#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007352 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007353#endif
7354#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007355 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007356#endif
7357#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007358 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007359#endif
7360
Victor Stinner8c62be82010-05-06 00:08:46 +00007361 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007362};
7363
7364
Barry Warsaw4a342091996-12-19 23:50:02 +00007365static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007366ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007367{
Victor Stinner8c62be82010-05-06 00:08:46 +00007368 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007369}
7370
Guido van Rossumd48f2521997-12-05 22:19:34 +00007371#if defined(PYOS_OS2)
7372/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007373static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007374{
7375 APIRET rc;
7376 ULONG values[QSV_MAX+1];
7377 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007378 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007379
7380 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007381 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007382 Py_END_ALLOW_THREADS
7383
7384 if (rc != NO_ERROR) {
7385 os2_error(rc);
7386 return -1;
7387 }
7388
Fred Drake4d1e64b2002-04-15 19:40:07 +00007389 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7390 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7391 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7392 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7393 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7394 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7395 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007396
7397 switch (values[QSV_VERSION_MINOR]) {
7398 case 0: ver = "2.00"; break;
7399 case 10: ver = "2.10"; break;
7400 case 11: ver = "2.11"; break;
7401 case 30: ver = "3.00"; break;
7402 case 40: ver = "4.00"; break;
7403 case 50: ver = "5.00"; break;
7404 default:
Tim Peters885d4572001-11-28 20:27:42 +00007405 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00007406 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00007407 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007408 ver = &tmp[0];
7409 }
7410
7411 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007412 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007413 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007414
7415 /* Add Indicator of Which Drive was Used to Boot the System */
7416 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7417 tmp[1] = ':';
7418 tmp[2] = '\0';
7419
Fred Drake4d1e64b2002-04-15 19:40:07 +00007420 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007421}
7422#endif
7423
Barry Warsaw4a342091996-12-19 23:50:02 +00007424static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007425all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007426{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007427#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007428 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007429#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007430#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007431 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007432#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007433#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007434 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007435#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007436#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007437 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007438#endif
Fred Drakec9680921999-12-13 16:37:25 +00007439#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007440 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00007441#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007442#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007443 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007444#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007445#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007446 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007447#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007448#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00007449 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007450#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007451#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00007452 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007453#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007454#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00007455 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007456#endif
7457#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00007458 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007459#endif
7460#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00007461 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007462#endif
7463#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00007464 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007465#endif
7466#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007467 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007468#endif
7469#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00007470 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007471#endif
7472#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007473 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007474#endif
7475#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007476 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007477#endif
7478#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007479 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007480#endif
7481#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007482 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007483#endif
7484#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00007485 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007486#endif
7487#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00007488 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007489#endif
7490#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007491 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007492#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007493#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00007494 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00007495#endif
7496#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00007497 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00007498#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007499#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00007500 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007501#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007502#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007503 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00007504#endif
7505#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007506 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00007507#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007508
Tim Peters5aa91602002-01-30 05:46:57 +00007509/* MS Windows */
7510#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007511 /* Don't inherit in child processes. */
7512 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007513#endif
7514#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00007515 /* Optimize for short life (keep in memory). */
7516 /* MS forgot to define this one with a non-underscore form too. */
7517 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007518#endif
7519#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00007520 /* Automatically delete when last handle is closed. */
7521 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007522#endif
7523#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00007524 /* Optimize for random access. */
7525 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007526#endif
7527#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007528 /* Optimize for sequential access. */
7529 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007530#endif
7531
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007532/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007533#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007534 /* Send a SIGIO signal whenever input or output
7535 becomes available on file descriptor */
7536 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007537#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007538#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007539 /* Direct disk access. */
7540 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007541#endif
7542#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00007543 /* Must be a directory. */
7544 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007545#endif
7546#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00007547 /* Do not follow links. */
7548 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007549#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007550#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007551 /* Do not update the access time. */
7552 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007553#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007554
Victor Stinner8c62be82010-05-06 00:08:46 +00007555 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007556#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007557 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007558#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007559#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007560 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007561#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007562#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00007563 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007564#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007565#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00007566 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007567#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007568#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00007569 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007570#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007571#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00007572 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007573#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007574#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00007575 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007576#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007577#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00007578 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007579#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007580#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00007581 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007582#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007583#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00007584 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007585#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007586#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00007587 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007588#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007589#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00007590 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007591#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007592#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00007593 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007594#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007595#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00007596 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007597#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007598#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00007599 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007600#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007601#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007602 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007603#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007604#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00007605 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007606#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007607
Guido van Rossum246bc171999-02-01 23:54:31 +00007608#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007609#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00007610 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7611 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7612 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7613 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7614 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7615 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7616 if (ins(d, "P_PM", (long)P_PM)) return -1;
7617 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7618 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7619 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7620 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7621 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7622 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7623 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7624 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7625 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7626 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7627 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7628 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7629 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007630#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007631 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7632 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7633 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7634 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7635 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007636#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007637#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007638
Guido van Rossumd48f2521997-12-05 22:19:34 +00007639#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007640 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007641#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007642 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00007643}
7644
7645
Tim Peters5aa91602002-01-30 05:46:57 +00007646#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007647#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007648#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007649
7650#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007651#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007652#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007653
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007654#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007655#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007656#define MODNAME "posix"
7657#endif
7658
Martin v. Löwis1a214512008-06-11 05:26:20 +00007659static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007660 PyModuleDef_HEAD_INIT,
7661 MODNAME,
7662 posix__doc__,
7663 -1,
7664 posix_methods,
7665 NULL,
7666 NULL,
7667 NULL,
7668 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00007669};
7670
7671
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007672PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007673INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007674{
Victor Stinner8c62be82010-05-06 00:08:46 +00007675 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007676
Victor Stinner8c62be82010-05-06 00:08:46 +00007677 m = PyModule_Create(&posixmodule);
7678 if (m == NULL)
7679 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007680
Victor Stinner8c62be82010-05-06 00:08:46 +00007681 /* Initialize environ dictionary */
7682 v = convertenviron();
7683 Py_XINCREF(v);
7684 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
7685 return NULL;
7686 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007687
Victor Stinner8c62be82010-05-06 00:08:46 +00007688 if (all_ins(m))
7689 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007690
Victor Stinner8c62be82010-05-06 00:08:46 +00007691 if (setup_confname_tables(m))
7692 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007693
Victor Stinner8c62be82010-05-06 00:08:46 +00007694 Py_INCREF(PyExc_OSError);
7695 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007696
Guido van Rossumb3d39562000-01-31 18:41:26 +00007697#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007698 if (posix_putenv_garbage == NULL)
7699 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007700#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007701
Victor Stinner8c62be82010-05-06 00:08:46 +00007702 if (!initialized) {
7703 stat_result_desc.name = MODNAME ".stat_result";
7704 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7705 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7706 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7707 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7708 structseq_new = StatResultType.tp_new;
7709 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007710
Victor Stinner8c62be82010-05-06 00:08:46 +00007711 statvfs_result_desc.name = MODNAME ".statvfs_result";
7712 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007713#ifdef NEED_TICKS_PER_SECOND
7714# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007716# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00007717 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007718# else
Victor Stinner8c62be82010-05-06 00:08:46 +00007719 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007720# endif
7721#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007722 }
7723 Py_INCREF((PyObject*) &StatResultType);
7724 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
7725 Py_INCREF((PyObject*) &StatVFSResultType);
7726 PyModule_AddObject(m, "statvfs_result",
7727 (PyObject*) &StatVFSResultType);
7728 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007729
7730#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00007731 /*
7732 * Step 2 of weak-linking support on Mac OS X.
7733 *
7734 * The code below removes functions that are not available on the
7735 * currently active platform.
7736 *
7737 * This block allow one to use a python binary that was build on
7738 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7739 * OSX 10.4.
7740 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007741#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00007742 if (fstatvfs == NULL) {
7743 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7744 return NULL;
7745 }
7746 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007747#endif /* HAVE_FSTATVFS */
7748
7749#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00007750 if (statvfs == NULL) {
7751 if (PyObject_DelAttrString(m, "statvfs") == -1) {
7752 return NULL;
7753 }
7754 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007755#endif /* HAVE_STATVFS */
7756
7757# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007758 if (lchown == NULL) {
7759 if (PyObject_DelAttrString(m, "lchown") == -1) {
7760 return NULL;
7761 }
7762 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007763#endif /* HAVE_LCHOWN */
7764
7765
7766#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00007767 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007768
Guido van Rossumb6775db1994-08-01 11:34:53 +00007769}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007770
7771#ifdef __cplusplus
7772}
7773#endif