blob: ed2447bb3975e8801c54a5271fa5bc99d786e3a4 [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 Stinner97b89882010-05-06 00:25:39 +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 Stinner97b89882010-05-06 00:25:39 +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 Stinner97b89882010-05-06 00:25:39 +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 Stinner97b89882010-05-06 00:25:39 +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 Stinner97b89882010-05-06 00:25:39 +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 Stinner97b89882010-05-06 00:25:39 +0000119#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000120#define HAVE_WAIT 1
121#else
Victor Stinner97b89882010-05-06 00:25:39 +0000122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Victor Stinner97b89882010-05-06 00:25:39 +0000124#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#define HAVE_EXECV 1
126#define HAVE_PIPE 1
Victor Stinner97b89882010-05-06 00:25:39 +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 Stinner97b89882010-05-06 00:25:39 +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 Stinner97b89882010-05-06 00:25:39 +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 Stinner97b89882010-05-06 00:25:39 +0000150#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000151#define HAVE_WAIT 1
Victor Stinner97b89882010-05-06 00:25:39 +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 Stinner97b89882010-05-06 00:25:39 +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
Antoine Pitrouc3ee1662009-05-23 16:02:33 +0000306/* Issue #1983: pid_t can be longer than a C long on some systems */
Antoine Pitrou7852c422009-05-24 11:58:35 +0000307#if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
Antoine Pitrouc3ee1662009-05-23 16:02:33 +0000308#define PARSE_PID "i"
309#define PyLong_FromPid PyLong_FromLong
310#define PyLong_AsPid PyLong_AsLong
311#elif SIZEOF_PID_T == SIZEOF_LONG
312#define PARSE_PID "l"
313#define PyLong_FromPid PyLong_FromLong
314#define PyLong_AsPid PyLong_AsLong
315#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
316#define PARSE_PID "L"
317#define PyLong_FromPid PyLong_FromLongLong
318#define PyLong_AsPid PyLong_AsLongLong
319#else
320#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
Antoine Pitrouc3ee1662009-05-23 16:02:33 +0000321#endif /* SIZEOF_PID_T */
322
Greg Wardb48bc172000-03-01 21:51:56 +0000323/* Don't use the "_r" form if we don't need it (also, won't have a
324 prototype for it, at least on Solaris -- maybe others as well?). */
325#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
326#define USE_CTERMID_R
327#endif
328
Fred Drake699f3522000-06-29 21:12:41 +0000329/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000330#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000331#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner97b89882010-05-06 00:25:39 +0000332# define STAT win32_stat
333# define FSTAT win32_fstat
334# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000335#else
Victor Stinner97b89882010-05-06 00:25:39 +0000336# define STAT stat
337# define FSTAT fstat
338# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000339#endif
340
Tim Peters11b23062003-04-23 02:39:17 +0000341#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000342#include <sys/mkdev.h>
343#else
344#if defined(MAJOR_IN_SYSMACROS)
345#include <sys/sysmacros.h>
346#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000347#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
348#include <sys/mkdev.h>
349#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000350#endif
Fred Drake699f3522000-06-29 21:12:41 +0000351
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000352#if defined _MSC_VER && _MSC_VER >= 1400
353/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
354 * valid and throw an assertion if it isn't.
355 * Normally, an invalid fd is likely to be a C program error and therefore
356 * an assertion can be useful, but it does contradict the POSIX standard
357 * which for write(2) states:
358 * "Otherwise, -1 shall be returned and errno set to indicate the error."
359 * "[EBADF] The fildes argument is not a valid file descriptor open for
360 * writing."
361 * Furthermore, python allows the user to enter any old integer
362 * as a fd and should merely raise a python exception on error.
363 * The Microsoft CRT doesn't provide an official way to check for the
364 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner97b89882010-05-06 00:25:39 +0000365 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000366 * internal structures involved.
367 * The structures below must be updated for each version of visual studio
368 * according to the file internal.h in the CRT source, until MS comes
369 * up with a less hacky way to do this.
370 * (all of this is to avoid globally modifying the CRT behaviour using
371 * _set_invalid_parameter_handler() and _CrtSetReportMode())
372 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000373/* The actual size of the structure is determined at runtime.
374 * Only the first items must be present.
375 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000376typedef struct {
Victor Stinner97b89882010-05-06 00:25:39 +0000377 intptr_t osfhnd;
378 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000379} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000380
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000381extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000382#define IOINFO_L2E 5
383#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
384#define IOINFO_ARRAYS 64
385#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
386#define FOPEN 0x01
387#define _NO_CONSOLE_FILENO (intptr_t)-2
388
389/* This function emulates what the windows CRT does to validate file handles */
390int
391_PyVerify_fd(int fd)
392{
Victor Stinner97b89882010-05-06 00:25:39 +0000393 const int i1 = fd >> IOINFO_L2E;
394 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000395
Antoine Pitrou835b4452010-08-15 18:32:16 +0000396 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000397
Victor Stinner97b89882010-05-06 00:25:39 +0000398 /* Determine the actual size of the ioinfo structure,
399 * as used by the CRT loaded in memory
400 */
401 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
402 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
403 }
404 if (sizeof_ioinfo == 0) {
405 /* This should not happen... */
406 goto fail;
407 }
408
409 /* See that it isn't a special CLEAR fileno */
410 if (fd != _NO_CONSOLE_FILENO) {
411 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
412 * we check pointer validity and other info
413 */
414 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
415 /* finally, check that the file is open */
416 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
417 if (info->osfile & FOPEN) {
418 return 1;
419 }
420 }
421 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000422 fail:
Victor Stinner97b89882010-05-06 00:25:39 +0000423 errno = EBADF;
424 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000425}
426
427/* the special case of checking dup2. The target fd must be in a sensible range */
428static int
429_PyVerify_fd_dup2(int fd1, int fd2)
430{
Victor Stinner97b89882010-05-06 00:25:39 +0000431 if (!_PyVerify_fd(fd1))
432 return 0;
433 if (fd2 == _NO_CONSOLE_FILENO)
434 return 0;
435 if ((unsigned)fd2 < _NHANDLE_)
436 return 1;
437 else
438 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000439}
440#else
441/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
442#define _PyVerify_fd_dup2(A, B) (1)
443#endif
444
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000446#ifdef WITH_NEXT_FRAMEWORK
447/* On Darwin/MacOSX a shared library or framework has no access to
448** environ directly, we must obtain it with _NSGetEnviron().
449*/
450#include <crt_externs.h>
451static char **environ;
452#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000454#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000455
Barry Warsaw53699e91996-12-10 23:23:01 +0000456static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000457convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458{
Victor Stinner97b89882010-05-06 00:25:39 +0000459 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000460#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +0000461 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000462#else
Victor Stinner97b89882010-05-06 00:25:39 +0000463 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000464#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000465#if defined(PYOS_OS2)
Victor Stinner97b89882010-05-06 00:25:39 +0000466 APIRET rc;
467 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
468#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000469
Victor Stinner97b89882010-05-06 00:25:39 +0000470 d = PyDict_New();
471 if (d == NULL)
472 return NULL;
473#ifdef WITH_NEXT_FRAMEWORK
474 if (environ == NULL)
475 environ = *_NSGetEnviron();
476#endif
477#ifdef MS_WINDOWS
478 /* _wenviron must be initialized in this way if the program is started
479 through main() instead of wmain(). */
480 _wgetenv(L"");
481 if (_wenviron == NULL)
482 return d;
483 /* This part ignores errors */
484 for (e = _wenviron; *e != NULL; e++) {
485 PyObject *k;
486 PyObject *v;
487 wchar_t *p = wcschr(*e, L'=');
488 if (p == NULL)
489 continue;
490 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
491 if (k == NULL) {
492 PyErr_Clear();
493 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000494 }
Victor Stinner97b89882010-05-06 00:25:39 +0000495 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
496 if (v == NULL) {
497 PyErr_Clear();
498 Py_DECREF(k);
499 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000500 }
Victor Stinner97b89882010-05-06 00:25:39 +0000501 if (PyDict_GetItem(d, k) == NULL) {
502 if (PyDict_SetItem(d, k, v) != 0)
503 PyErr_Clear();
504 }
505 Py_DECREF(k);
506 Py_DECREF(v);
507 }
508#else
509 if (environ == NULL)
510 return d;
511 /* This part ignores errors */
512 for (e = environ; *e != NULL; e++) {
513 PyObject *k;
514 PyObject *v;
515 char *p = strchr(*e, '=');
516 if (p == NULL)
517 continue;
518 k = PyUnicode_Decode(*e, (int)(p-*e),
519 Py_FileSystemDefaultEncoding, "surrogateescape");
520 if (k == NULL) {
521 PyErr_Clear();
522 continue;
523 }
524 v = PyUnicode_Decode(p+1, strlen(p+1),
525 Py_FileSystemDefaultEncoding, "surrogateescape");
526 if (v == NULL) {
527 PyErr_Clear();
528 Py_DECREF(k);
529 continue;
530 }
531 if (PyDict_GetItem(d, k) == NULL) {
532 if (PyDict_SetItem(d, k, v) != 0)
533 PyErr_Clear();
534 }
535 Py_DECREF(k);
536 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000537 }
538#endif
Victor Stinner97b89882010-05-06 00:25:39 +0000539#if defined(PYOS_OS2)
540 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
541 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
542 PyObject *v = PyBytes_FromString(buffer);
543 PyDict_SetItemString(d, "BEGINLIBPATH", v);
544 Py_DECREF(v);
545 }
546 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
547 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
548 PyObject *v = PyBytes_FromString(buffer);
549 PyDict_SetItemString(d, "ENDLIBPATH", v);
550 Py_DECREF(v);
551 }
552#endif
553 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554}
555
Martin v. Löwis011e8422009-05-05 04:43:17 +0000556/* Convert a bytes object to a char*. Optionally lock the buffer if it is a
557 bytes array. */
558
559static char*
560bytes2str(PyObject* o, int lock)
561{
Victor Stinner97b89882010-05-06 00:25:39 +0000562 if(PyBytes_Check(o))
563 return PyBytes_AsString(o);
564 else if(PyByteArray_Check(o)) {
565 if (lock && PyObject_GetBuffer(o, NULL, 0) < 0)
566 /* On a bytearray, this should not fail. */
567 PyErr_BadInternalCall();
568 return PyByteArray_AsString(o);
569 } else {
570 /* The FS converter should have verified that this
571 is either bytes or bytearray. */
572 Py_FatalError("bad object passed to bytes2str");
573 /* not reached. */
574 return "";
575 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000576}
577
578/* Release the lock, decref the object. */
579static void
580release_bytes(PyObject* o)
581{
Victor Stinner97b89882010-05-06 00:25:39 +0000582 if (PyByteArray_Check(o))
583 o->ob_type->tp_as_buffer->bf_releasebuffer(o, 0);
584 Py_DECREF(o);
Martin v. Löwis011e8422009-05-05 04:43:17 +0000585}
586
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588/* Set a POSIX-specific error from errno, and return NULL */
589
Barry Warsawd58d7641998-07-23 16:14:40 +0000590static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000591posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000592{
Victor Stinner97b89882010-05-06 00:25:39 +0000593 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594}
Barry Warsawd58d7641998-07-23 16:14:40 +0000595static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000596posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000597{
Victor Stinner97b89882010-05-06 00:25:39 +0000598 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000599}
600
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000601#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000602static PyObject *
603posix_error_with_unicode_filename(Py_UNICODE* name)
604{
Victor Stinner97b89882010-05-06 00:25:39 +0000605 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000606}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000607#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000608
609
Mark Hammondef8b6542001-05-13 08:04:26 +0000610static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000611posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000612{
Victor Stinner97b89882010-05-06 00:25:39 +0000613 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError,
614 bytes2str(name, 0));
615 release_bytes(name);
616 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000617}
618
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000619#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000620static PyObject *
621win32_error(char* function, char* filename)
622{
Victor Stinner97b89882010-05-06 00:25:39 +0000623 /* XXX We should pass the function name along in the future.
624 (winreg.c also wants to pass the function name.)
625 This would however require an additional param to the
626 Windows error object, which is non-trivial.
627 */
628 errno = GetLastError();
629 if (filename)
630 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
631 else
632 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000633}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000634
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000635static PyObject *
636win32_error_unicode(char* function, Py_UNICODE* filename)
637{
Victor Stinner97b89882010-05-06 00:25:39 +0000638 /* XXX - see win32_error for comments on 'function' */
639 errno = GetLastError();
640 if (filename)
641 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
642 else
643 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000644}
645
Thomas Wouters477c8d52006-05-27 19:21:47 +0000646static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000647convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000648{
Victor Stinner97b89882010-05-06 00:25:39 +0000649 if (PyUnicode_CheckExact(*param))
650 Py_INCREF(*param);
651 else if (PyUnicode_Check(*param))
652 /* For a Unicode subtype that's not a Unicode object,
653 return a true Unicode object with the same data. */
654 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
655 PyUnicode_GET_SIZE(*param));
656 else
657 *param = PyUnicode_FromEncodedObject(*param,
658 Py_FileSystemDefaultEncoding,
659 "strict");
660 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000661}
662
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000663#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000664
Guido van Rossumd48f2521997-12-05 22:19:34 +0000665#if defined(PYOS_OS2)
666/**********************************************************************
667 * Helper Function to Trim and Format OS/2 Messages
668 **********************************************************************/
Victor Stinner97b89882010-05-06 00:25:39 +0000669static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000670os2_formatmsg(char *msgbuf, int msglen, char *reason)
671{
672 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
673
674 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
675 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
676
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000677 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000678 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
679 }
680
681 /* Add Optional Reason Text */
682 if (reason) {
683 strcat(msgbuf, " : ");
684 strcat(msgbuf, reason);
685 }
686}
687
688/**********************************************************************
689 * Decode an OS/2 Operating System Error Code
690 *
691 * A convenience function to lookup an OS/2 error code and return a
692 * text message we can use to raise a Python exception.
693 *
694 * Notes:
695 * The messages for errors returned from the OS/2 kernel reside in
696 * the file OSO001.MSG in the \OS2 directory hierarchy.
697 *
698 **********************************************************************/
Victor Stinner97b89882010-05-06 00:25:39 +0000699static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000700os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
701{
702 APIRET rc;
703 ULONG msglen;
704
705 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
706 Py_BEGIN_ALLOW_THREADS
707 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
708 errorcode, "oso001.msg", &msglen);
709 Py_END_ALLOW_THREADS
710
711 if (rc == NO_ERROR)
712 os2_formatmsg(msgbuf, msglen, reason);
713 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000714 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner97b89882010-05-06 00:25:39 +0000715 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000716
717 return msgbuf;
718}
719
720/* Set an OS/2-specific error and return NULL. OS/2 kernel
721 errors are not in a global variable e.g. 'errno' nor are
722 they congruent with posix error numbers. */
723
Victor Stinner97b89882010-05-06 00:25:39 +0000724static PyObject *
725os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000726{
727 char text[1024];
728 PyObject *v;
729
730 os2_strerror(text, sizeof(text), code, "");
731
732 v = Py_BuildValue("(is)", code, text);
733 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000734 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000735 Py_DECREF(v);
736 }
737 return NULL; /* Signal to Python that an Exception is Pending */
738}
739
740#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000741
742/* POSIX generic methods */
743
Barry Warsaw53699e91996-12-10 23:23:01 +0000744static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000745posix_fildes(PyObject *fdobj, int (*func)(int))
746{
Victor Stinner97b89882010-05-06 00:25:39 +0000747 int fd;
748 int res;
749 fd = PyObject_AsFileDescriptor(fdobj);
750 if (fd < 0)
751 return NULL;
752 if (!_PyVerify_fd(fd))
753 return posix_error();
754 Py_BEGIN_ALLOW_THREADS
755 res = (*func)(fd);
756 Py_END_ALLOW_THREADS
757 if (res < 0)
758 return posix_error();
759 Py_INCREF(Py_None);
760 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000761}
Guido van Rossum21142a01999-01-08 21:05:37 +0000762
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000763#ifdef MS_WINDOWS
Tim Peters11b23062003-04-23 02:39:17 +0000764static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000765unicode_file_names(void)
766{
Victor Stinner97b89882010-05-06 00:25:39 +0000767 static int canusewide = -1;
768 if (canusewide == -1) {
769 /* As per doc for ::GetVersion(), this is the correct test for
770 the Windows NT family. */
771 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
772 }
773 return canusewide;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000774}
775#endif
Tim Peters11b23062003-04-23 02:39:17 +0000776
Guido van Rossum21142a01999-01-08 21:05:37 +0000777static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000778posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000779{
Victor Stinner97b89882010-05-06 00:25:39 +0000780 PyObject *opath1 = NULL;
781 char *path1;
782 int res;
783 if (!PyArg_ParseTuple(args, format,
784 PyUnicode_FSConverter, &opath1))
785 return NULL;
786 path1 = bytes2str(opath1, 1);
787 Py_BEGIN_ALLOW_THREADS
788 res = (*func)(path1);
789 Py_END_ALLOW_THREADS
790 if (res < 0)
791 return posix_error_with_allocated_filename(opath1);
792 release_bytes(opath1);
793 Py_INCREF(Py_None);
794 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000795}
796
Barry Warsaw53699e91996-12-10 23:23:01 +0000797static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000798posix_2str(PyObject *args,
Victor Stinner97b89882010-05-06 00:25:39 +0000799 char *format,
800 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000801{
Victor Stinner97b89882010-05-06 00:25:39 +0000802 PyObject *opath1 = NULL, *opath2 = NULL;
803 char *path1, *path2;
804 int res;
805 if (!PyArg_ParseTuple(args, format,
806 PyUnicode_FSConverter, &opath1,
807 PyUnicode_FSConverter, &opath2)) {
808 return NULL;
809 }
810 path1 = bytes2str(opath1, 1);
811 path2 = bytes2str(opath2, 1);
812 Py_BEGIN_ALLOW_THREADS
813 res = (*func)(path1, path2);
814 Py_END_ALLOW_THREADS
815 release_bytes(opath1);
816 release_bytes(opath2);
817 if (res != 0)
818 /* XXX how to report both path1 and path2??? */
819 return posix_error();
820 Py_INCREF(Py_None);
821 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822}
823
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000824#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000825static PyObject*
Victor Stinner97b89882010-05-06 00:25:39 +0000826win32_1str(PyObject* args, char* func,
827 char* format, BOOL (__stdcall *funcA)(LPCSTR),
828 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000829{
Victor Stinner97b89882010-05-06 00:25:39 +0000830 PyObject *uni;
831 char *ansi;
832 BOOL result;
833 if (unicode_file_names()) {
834 if (!PyArg_ParseTuple(args, wformat, &uni))
835 PyErr_Clear();
836 else {
837 Py_BEGIN_ALLOW_THREADS
838 result = funcW(PyUnicode_AsUnicode(uni));
839 Py_END_ALLOW_THREADS
840 if (!result)
841 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
842 Py_INCREF(Py_None);
843 return Py_None;
844 }
845 }
846 if (!PyArg_ParseTuple(args, format, &ansi))
847 return NULL;
848 Py_BEGIN_ALLOW_THREADS
849 result = funcA(ansi);
850 Py_END_ALLOW_THREADS
851 if (!result)
852 return win32_error(func, ansi);
853 Py_INCREF(Py_None);
854 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000855
856}
857
858/* This is a reimplementation of the C library's chdir function,
859 but one that produces Win32 errors instead of DOS error codes.
860 chdir is essentially a wrapper around SetCurrentDirectory; however,
861 it also needs to set "magic" environment variables indicating
862 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000863static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000864win32_chdir(LPCSTR path)
865{
Victor Stinner97b89882010-05-06 00:25:39 +0000866 char new_path[MAX_PATH+1];
867 int result;
868 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000869
Victor Stinner97b89882010-05-06 00:25:39 +0000870 if(!SetCurrentDirectoryA(path))
871 return FALSE;
872 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
873 if (!result)
874 return FALSE;
875 /* In the ANSI API, there should not be any paths longer
876 than MAX_PATH. */
877 assert(result <= MAX_PATH+1);
878 if (strncmp(new_path, "\\\\", 2) == 0 ||
879 strncmp(new_path, "//", 2) == 0)
880 /* UNC path, nothing to do. */
881 return TRUE;
882 env[1] = new_path[0];
883 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000884}
885
886/* The Unicode version differs from the ANSI version
887 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000888static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000889win32_wchdir(LPCWSTR path)
890{
Victor Stinner97b89882010-05-06 00:25:39 +0000891 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
892 int result;
893 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000894
Victor Stinner97b89882010-05-06 00:25:39 +0000895 if(!SetCurrentDirectoryW(path))
896 return FALSE;
897 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
898 if (!result)
899 return FALSE;
900 if (result > MAX_PATH+1) {
901 new_path = malloc(result * sizeof(wchar_t));
902 if (!new_path) {
903 SetLastError(ERROR_OUTOFMEMORY);
904 return FALSE;
905 }
906 result = GetCurrentDirectoryW(result, new_path);
907 if (!result) {
908 free(new_path);
909 return FALSE;
910 }
911 }
912 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
913 wcsncmp(new_path, L"//", 2) == 0)
914 /* UNC path, nothing to do. */
915 return TRUE;
916 env[1] = new_path[0];
917 result = SetEnvironmentVariableW(env, new_path);
918 if (new_path != _new_path)
919 free(new_path);
920 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000921}
922#endif
923
Martin v. Löwis14694662006-02-03 12:54:16 +0000924#ifdef MS_WINDOWS
925/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
926 - time stamps are restricted to second resolution
927 - file modification times suffer from forth-and-back conversions between
928 UTC and local time
929 Therefore, we implement our own stat, based on the Win32 API directly.
930*/
Victor Stinner97b89882010-05-06 00:25:39 +0000931#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000932
933struct win32_stat{
934 int st_dev;
935 __int64 st_ino;
936 unsigned short st_mode;
937 int st_nlink;
938 int st_uid;
939 int st_gid;
940 int st_rdev;
941 __int64 st_size;
942 int st_atime;
943 int st_atime_nsec;
944 int st_mtime;
945 int st_mtime_nsec;
946 int st_ctime;
947 int st_ctime_nsec;
948};
949
950static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
951
952static void
953FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
954{
Victor Stinner97b89882010-05-06 00:25:39 +0000955 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
956 /* Cannot simply cast and dereference in_ptr,
957 since it might not be aligned properly */
958 __int64 in;
959 memcpy(&in, in_ptr, sizeof(in));
960 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
961 /* XXX Win32 supports time stamps past 2038; we currently don't */
962 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000963}
964
Thomas Wouters477c8d52006-05-27 19:21:47 +0000965static void
966time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
967{
Victor Stinner97b89882010-05-06 00:25:39 +0000968 /* XXX endianness */
969 __int64 out;
970 out = time_in + secs_between_epochs;
971 out = out * 10000000 + nsec_in / 100;
972 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000973}
974
Martin v. Löwis14694662006-02-03 12:54:16 +0000975/* Below, we *know* that ugo+r is 0444 */
976#if _S_IREAD != 0400
977#error Unsupported C library
978#endif
979static int
980attributes_to_mode(DWORD attr)
981{
Victor Stinner97b89882010-05-06 00:25:39 +0000982 int m = 0;
983 if (attr & FILE_ATTRIBUTE_DIRECTORY)
984 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
985 else
986 m |= _S_IFREG;
987 if (attr & FILE_ATTRIBUTE_READONLY)
988 m |= 0444;
989 else
990 m |= 0666;
991 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +0000992}
993
994static int
995attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
996{
Victor Stinner97b89882010-05-06 00:25:39 +0000997 memset(result, 0, sizeof(*result));
998 result->st_mode = attributes_to_mode(info->dwFileAttributes);
999 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1000 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1001 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1002 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +00001003
Victor Stinner97b89882010-05-06 00:25:39 +00001004 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001005}
1006
Thomas Wouters89f507f2006-12-13 04:49:30 +00001007/* Emulate GetFileAttributesEx[AW] on Windows 95 */
1008static int checked = 0;
1009static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
1010static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
1011static void
1012check_gfax()
1013{
Victor Stinner97b89882010-05-06 00:25:39 +00001014 HINSTANCE hKernel32;
1015 if (checked)
1016 return;
1017 checked = 1;
1018 hKernel32 = GetModuleHandle("KERNEL32");
1019 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
1020 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
Thomas Wouters89f507f2006-12-13 04:49:30 +00001021}
1022
Guido van Rossumd8faa362007-04-27 19:54:29 +00001023static BOOL
1024attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1025{
Victor Stinner97b89882010-05-06 00:25:39 +00001026 HANDLE hFindFile;
1027 WIN32_FIND_DATAA FileData;
1028 hFindFile = FindFirstFileA(pszFile, &FileData);
1029 if (hFindFile == INVALID_HANDLE_VALUE)
1030 return FALSE;
1031 FindClose(hFindFile);
1032 pfad->dwFileAttributes = FileData.dwFileAttributes;
1033 pfad->ftCreationTime = FileData.ftCreationTime;
1034 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1035 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1036 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1037 pfad->nFileSizeLow = FileData.nFileSizeLow;
1038 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001039}
1040
1041static BOOL
1042attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1043{
Victor Stinner97b89882010-05-06 00:25:39 +00001044 HANDLE hFindFile;
1045 WIN32_FIND_DATAW FileData;
1046 hFindFile = FindFirstFileW(pszFile, &FileData);
1047 if (hFindFile == INVALID_HANDLE_VALUE)
1048 return FALSE;
1049 FindClose(hFindFile);
1050 pfad->dwFileAttributes = FileData.dwFileAttributes;
1051 pfad->ftCreationTime = FileData.ftCreationTime;
1052 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1053 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1054 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1055 pfad->nFileSizeLow = FileData.nFileSizeLow;
1056 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001057}
1058
Thomas Wouters89f507f2006-12-13 04:49:30 +00001059static BOOL WINAPI
Victor Stinner97b89882010-05-06 00:25:39 +00001060Py_GetFileAttributesExA(LPCSTR pszFile,
1061 GET_FILEEX_INFO_LEVELS level,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001062 LPVOID pv)
1063{
Victor Stinner97b89882010-05-06 00:25:39 +00001064 BOOL result;
1065 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1066 /* First try to use the system's implementation, if that is
1067 available and either succeeds to gives an error other than
1068 that it isn't implemented. */
1069 check_gfax();
1070 if (gfaxa) {
1071 result = gfaxa(pszFile, level, pv);
1072 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1073 return result;
1074 }
1075 /* It's either not present, or not implemented.
1076 Emulate using FindFirstFile. */
1077 if (level != GetFileExInfoStandard) {
1078 SetLastError(ERROR_INVALID_PARAMETER);
1079 return FALSE;
1080 }
1081 /* Use GetFileAttributes to validate that the file name
1082 does not contain wildcards (which FindFirstFile would
1083 accept). */
1084 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
1085 return FALSE;
1086 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001087}
1088
1089static BOOL WINAPI
Victor Stinner97b89882010-05-06 00:25:39 +00001090Py_GetFileAttributesExW(LPCWSTR pszFile,
1091 GET_FILEEX_INFO_LEVELS level,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001092 LPVOID pv)
1093{
Victor Stinner97b89882010-05-06 00:25:39 +00001094 BOOL result;
1095 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1096 /* First try to use the system's implementation, if that is
1097 available and either succeeds to gives an error other than
1098 that it isn't implemented. */
1099 check_gfax();
1100 if (gfaxa) {
1101 result = gfaxw(pszFile, level, pv);
1102 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1103 return result;
1104 }
1105 /* It's either not present, or not implemented.
1106 Emulate using FindFirstFile. */
1107 if (level != GetFileExInfoStandard) {
1108 SetLastError(ERROR_INVALID_PARAMETER);
1109 return FALSE;
1110 }
1111 /* Use GetFileAttributes to validate that the file name
1112 does not contain wildcards (which FindFirstFile would
1113 accept). */
1114 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
1115 return FALSE;
1116 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001117}
1118
Victor Stinner97b89882010-05-06 00:25:39 +00001119static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001120win32_stat(const char* path, struct win32_stat *result)
1121{
Victor Stinner97b89882010-05-06 00:25:39 +00001122 WIN32_FILE_ATTRIBUTE_DATA info;
1123 int code;
1124 char *dot;
1125 /* XXX not supported on Win95 and NT 3.x */
1126 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
1127 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1128 /* Protocol violation: we explicitly clear errno, instead of
1129 setting it to a POSIX error. Callers should use GetLastError. */
1130 errno = 0;
1131 return -1;
1132 } else {
1133 /* Could not get attributes on open file. Fall back to
1134 reading the directory. */
1135 if (!attributes_from_dir(path, &info)) {
1136 /* Very strange. This should not fail now */
1137 errno = 0;
1138 return -1;
1139 }
1140 }
1141 }
1142 code = attribute_data_to_stat(&info, result);
1143 if (code != 0)
1144 return code;
1145 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1146 dot = strrchr(path, '.');
1147 if (dot) {
1148 if (stricmp(dot, ".bat") == 0 ||
1149 stricmp(dot, ".cmd") == 0 ||
1150 stricmp(dot, ".exe") == 0 ||
1151 stricmp(dot, ".com") == 0)
1152 result->st_mode |= 0111;
1153 }
1154 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001155}
1156
Victor Stinner97b89882010-05-06 00:25:39 +00001157static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001158win32_wstat(const wchar_t* path, struct win32_stat *result)
1159{
Victor Stinner97b89882010-05-06 00:25:39 +00001160 int code;
1161 const wchar_t *dot;
1162 WIN32_FILE_ATTRIBUTE_DATA info;
1163 /* XXX not supported on Win95 and NT 3.x */
1164 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1165 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1166 /* Protocol violation: we explicitly clear errno, instead of
1167 setting it to a POSIX error. Callers should use GetLastError. */
1168 errno = 0;
1169 return -1;
1170 } else {
1171 /* Could not get attributes on open file. Fall back to
1172 reading the directory. */
1173 if (!attributes_from_dir_w(path, &info)) {
1174 /* Very strange. This should not fail now */
1175 errno = 0;
1176 return -1;
1177 }
1178 }
1179 }
1180 code = attribute_data_to_stat(&info, result);
1181 if (code < 0)
1182 return code;
1183 /* Set IFEXEC if it is an .exe, .bat, ... */
1184 dot = wcsrchr(path, '.');
1185 if (dot) {
1186 if (_wcsicmp(dot, L".bat") == 0 ||
1187 _wcsicmp(dot, L".cmd") == 0 ||
1188 _wcsicmp(dot, L".exe") == 0 ||
1189 _wcsicmp(dot, L".com") == 0)
1190 result->st_mode |= 0111;
1191 }
1192 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001193}
1194
1195static int
1196win32_fstat(int file_number, struct win32_stat *result)
1197{
Victor Stinner97b89882010-05-06 00:25:39 +00001198 BY_HANDLE_FILE_INFORMATION info;
1199 HANDLE h;
1200 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001201
Victor Stinner97b89882010-05-06 00:25:39 +00001202 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001203
Victor Stinner97b89882010-05-06 00:25:39 +00001204 /* Protocol violation: we explicitly clear errno, instead of
1205 setting it to a POSIX error. Callers should use GetLastError. */
1206 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001207
Victor Stinner97b89882010-05-06 00:25:39 +00001208 if (h == INVALID_HANDLE_VALUE) {
1209 /* This is really a C library error (invalid file handle).
1210 We set the Win32 error to the closes one matching. */
1211 SetLastError(ERROR_INVALID_HANDLE);
1212 return -1;
1213 }
1214 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001215
Victor Stinner97b89882010-05-06 00:25:39 +00001216 type = GetFileType(h);
1217 if (type == FILE_TYPE_UNKNOWN) {
1218 DWORD error = GetLastError();
1219 if (error != 0) {
1220 return -1;
1221 }
1222 /* else: valid but unknown file */
1223 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001224
Victor Stinner97b89882010-05-06 00:25:39 +00001225 if (type != FILE_TYPE_DISK) {
1226 if (type == FILE_TYPE_CHAR)
1227 result->st_mode = _S_IFCHR;
1228 else if (type == FILE_TYPE_PIPE)
1229 result->st_mode = _S_IFIFO;
1230 return 0;
1231 }
1232
1233 if (!GetFileInformationByHandle(h, &info)) {
1234 return -1;
1235 }
1236
1237 /* similar to stat() */
1238 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1239 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1240 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1241 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1242 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1243 /* specific to fstat() */
1244 result->st_nlink = info.nNumberOfLinks;
1245 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1246 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001247}
1248
1249#endif /* MS_WINDOWS */
1250
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001251PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001252"stat_result: Result from stat or lstat.\n\n\
1253This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001254 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001255or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1256\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001257Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1258or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001259\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001260See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001261
1262static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner97b89882010-05-06 00:25:39 +00001263 {"st_mode", "protection bits"},
1264 {"st_ino", "inode"},
1265 {"st_dev", "device"},
1266 {"st_nlink", "number of hard links"},
1267 {"st_uid", "user ID of owner"},
1268 {"st_gid", "group ID of owner"},
1269 {"st_size", "total size, in bytes"},
1270 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1271 {NULL, "integer time of last access"},
1272 {NULL, "integer time of last modification"},
1273 {NULL, "integer time of last change"},
1274 {"st_atime", "time of last access"},
1275 {"st_mtime", "time of last modification"},
1276 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001277#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner97b89882010-05-06 00:25:39 +00001278 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001279#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001280#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner97b89882010-05-06 00:25:39 +00001281 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001282#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001283#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner97b89882010-05-06 00:25:39 +00001284 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001285#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001286#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00001287 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001288#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001289#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner97b89882010-05-06 00:25:39 +00001290 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001291#endif
1292#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner97b89882010-05-06 00:25:39 +00001293 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001294#endif
Victor Stinner97b89882010-05-06 00:25:39 +00001295 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001296};
1297
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001298#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001299#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001300#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001301#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001302#endif
1303
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001304#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001305#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1306#else
1307#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1308#endif
1309
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001310#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001311#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1312#else
1313#define ST_RDEV_IDX ST_BLOCKS_IDX
1314#endif
1315
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001316#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1317#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1318#else
1319#define ST_FLAGS_IDX ST_RDEV_IDX
1320#endif
1321
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001322#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001323#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001324#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001325#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001326#endif
1327
1328#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1329#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1330#else
1331#define ST_BIRTHTIME_IDX ST_GEN_IDX
1332#endif
1333
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001334static PyStructSequence_Desc stat_result_desc = {
Victor Stinner97b89882010-05-06 00:25:39 +00001335 "stat_result", /* name */
1336 stat_result__doc__, /* doc */
1337 stat_result_fields,
1338 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001339};
1340
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001341PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001342"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1343This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001344 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001345or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001346\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001347See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001348
1349static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner97b89882010-05-06 00:25:39 +00001350 {"f_bsize", },
1351 {"f_frsize", },
1352 {"f_blocks", },
1353 {"f_bfree", },
1354 {"f_bavail", },
1355 {"f_files", },
1356 {"f_ffree", },
1357 {"f_favail", },
1358 {"f_flag", },
1359 {"f_namemax",},
1360 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001361};
1362
1363static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner97b89882010-05-06 00:25:39 +00001364 "statvfs_result", /* name */
1365 statvfs_result__doc__, /* doc */
1366 statvfs_result_fields,
1367 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001368};
1369
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001370static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001371static PyTypeObject StatResultType;
1372static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001373static newfunc structseq_new;
1374
1375static PyObject *
1376statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1377{
Victor Stinner97b89882010-05-06 00:25:39 +00001378 PyStructSequence *result;
1379 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001380
Victor Stinner97b89882010-05-06 00:25:39 +00001381 result = (PyStructSequence*)structseq_new(type, args, kwds);
1382 if (!result)
1383 return NULL;
1384 /* If we have been initialized from a tuple,
1385 st_?time might be set to None. Initialize it
1386 from the int slots. */
1387 for (i = 7; i <= 9; i++) {
1388 if (result->ob_item[i+3] == Py_None) {
1389 Py_DECREF(Py_None);
1390 Py_INCREF(result->ob_item[i]);
1391 result->ob_item[i+3] = result->ob_item[i];
1392 }
1393 }
1394 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001395}
1396
1397
1398
1399/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001400static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001401
1402PyDoc_STRVAR(stat_float_times__doc__,
1403"stat_float_times([newval]) -> oldval\n\n\
1404Determine whether os.[lf]stat represents time stamps as float objects.\n\
1405If newval is True, future calls to stat() return floats, if it is False,\n\
1406future calls return ints. \n\
1407If newval is omitted, return the current setting.\n");
1408
1409static PyObject*
1410stat_float_times(PyObject* self, PyObject *args)
1411{
Victor Stinner97b89882010-05-06 00:25:39 +00001412 int newval = -1;
1413 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1414 return NULL;
1415 if (newval == -1)
1416 /* Return old value */
1417 return PyBool_FromLong(_stat_float_times);
1418 _stat_float_times = newval;
1419 Py_INCREF(Py_None);
1420 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001421}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001422
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001423static void
1424fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1425{
Victor Stinner97b89882010-05-06 00:25:39 +00001426 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001427#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner97b89882010-05-06 00:25:39 +00001428 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001429#else
Victor Stinner97b89882010-05-06 00:25:39 +00001430 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001431#endif
Victor Stinner97b89882010-05-06 00:25:39 +00001432 if (!ival)
1433 return;
1434 if (_stat_float_times) {
1435 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1436 } else {
1437 fval = ival;
1438 Py_INCREF(fval);
1439 }
1440 PyStructSequence_SET_ITEM(v, index, ival);
1441 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001442}
1443
Tim Peters5aa91602002-01-30 05:46:57 +00001444/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001445 (used by posix_stat() and posix_fstat()) */
1446static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001447_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001448{
Victor Stinner97b89882010-05-06 00:25:39 +00001449 unsigned long ansec, mnsec, cnsec;
1450 PyObject *v = PyStructSequence_New(&StatResultType);
1451 if (v == NULL)
1452 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001453
Victor Stinner97b89882010-05-06 00:25:39 +00001454 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001455#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner97b89882010-05-06 00:25:39 +00001456 PyStructSequence_SET_ITEM(v, 1,
1457 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001458#else
Victor Stinner97b89882010-05-06 00:25:39 +00001459 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001460#endif
1461#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner97b89882010-05-06 00:25:39 +00001462 PyStructSequence_SET_ITEM(v, 2,
1463 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001464#else
Victor Stinner97b89882010-05-06 00:25:39 +00001465 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001466#endif
Victor Stinner97b89882010-05-06 00:25:39 +00001467 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1468 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1469 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001470#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner97b89882010-05-06 00:25:39 +00001471 PyStructSequence_SET_ITEM(v, 6,
1472 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001473#else
Victor Stinner97b89882010-05-06 00:25:39 +00001474 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001475#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001476
Martin v. Löwis14694662006-02-03 12:54:16 +00001477#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner97b89882010-05-06 00:25:39 +00001478 ansec = st->st_atim.tv_nsec;
1479 mnsec = st->st_mtim.tv_nsec;
1480 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001481#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner97b89882010-05-06 00:25:39 +00001482 ansec = st->st_atimespec.tv_nsec;
1483 mnsec = st->st_mtimespec.tv_nsec;
1484 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001485#elif defined(HAVE_STAT_NSEC)
Victor Stinner97b89882010-05-06 00:25:39 +00001486 ansec = st->st_atime_nsec;
1487 mnsec = st->st_mtime_nsec;
1488 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001489#else
Victor Stinner97b89882010-05-06 00:25:39 +00001490 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001491#endif
Victor Stinner97b89882010-05-06 00:25:39 +00001492 fill_time(v, 7, st->st_atime, ansec);
1493 fill_time(v, 8, st->st_mtime, mnsec);
1494 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001495
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001496#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner97b89882010-05-06 00:25:39 +00001497 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1498 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001499#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001500#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner97b89882010-05-06 00:25:39 +00001501 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1502 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001503#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001504#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner97b89882010-05-06 00:25:39 +00001505 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1506 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001507#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001508#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner97b89882010-05-06 00:25:39 +00001509 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1510 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001511#endif
1512#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner97b89882010-05-06 00:25:39 +00001513 {
1514 PyObject *val;
1515 unsigned long bsec,bnsec;
1516 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001517#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner97b89882010-05-06 00:25:39 +00001518 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001519#else
Victor Stinner97b89882010-05-06 00:25:39 +00001520 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001521#endif
Victor Stinner97b89882010-05-06 00:25:39 +00001522 if (_stat_float_times) {
1523 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1524 } else {
1525 val = PyLong_FromLong((long)bsec);
1526 }
1527 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1528 val);
1529 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001530#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001531#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00001532 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1533 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001534#endif
Fred Drake699f3522000-06-29 21:12:41 +00001535
Victor Stinner97b89882010-05-06 00:25:39 +00001536 if (PyErr_Occurred()) {
1537 Py_DECREF(v);
1538 return NULL;
1539 }
Fred Drake699f3522000-06-29 21:12:41 +00001540
Victor Stinner97b89882010-05-06 00:25:39 +00001541 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001542}
1543
Martin v. Löwisd8948722004-06-02 09:57:56 +00001544#ifdef MS_WINDOWS
1545
1546/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1547 where / can be used in place of \ and the trailing slash is optional.
1548 Both SERVER and SHARE must have at least one character.
1549*/
1550
1551#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1552#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001553#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001554#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001555#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001556
Tim Peters4ad82172004-08-30 17:02:04 +00001557static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001558IsUNCRootA(char *path, int pathlen)
1559{
Victor Stinner97b89882010-05-06 00:25:39 +00001560 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001561
Victor Stinner97b89882010-05-06 00:25:39 +00001562 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001563
Victor Stinner97b89882010-05-06 00:25:39 +00001564 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1565 /* minimum UNCRoot is \\x\y */
1566 return FALSE;
1567 for (i = 2; i < pathlen ; i++)
1568 if (ISSLASH(path[i])) break;
1569 if (i == 2 || i == pathlen)
1570 /* do not allow \\\SHARE or \\SERVER */
1571 return FALSE;
1572 share = i+1;
1573 for (i = share; i < pathlen; i++)
1574 if (ISSLASH(path[i])) break;
1575 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001576
Victor Stinner97b89882010-05-06 00:25:39 +00001577 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001578}
1579
Tim Peters4ad82172004-08-30 17:02:04 +00001580static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001581IsUNCRootW(Py_UNICODE *path, int pathlen)
1582{
Victor Stinner97b89882010-05-06 00:25:39 +00001583 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001584
Victor Stinner97b89882010-05-06 00:25:39 +00001585 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001586
Victor Stinner97b89882010-05-06 00:25:39 +00001587 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1588 /* minimum UNCRoot is \\x\y */
1589 return FALSE;
1590 for (i = 2; i < pathlen ; i++)
1591 if (ISSLASH(path[i])) break;
1592 if (i == 2 || i == pathlen)
1593 /* do not allow \\\SHARE or \\SERVER */
1594 return FALSE;
1595 share = i+1;
1596 for (i = share; i < pathlen; i++)
1597 if (ISSLASH(path[i])) break;
1598 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001599
Victor Stinner97b89882010-05-06 00:25:39 +00001600 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001601}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001602#endif /* MS_WINDOWS */
1603
Barry Warsaw53699e91996-12-10 23:23:01 +00001604static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001605posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner97b89882010-05-06 00:25:39 +00001606 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001607#ifdef __VMS
Victor Stinner97b89882010-05-06 00:25:39 +00001608 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001609#else
Victor Stinner97b89882010-05-06 00:25:39 +00001610 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001611#endif
Victor Stinner97b89882010-05-06 00:25:39 +00001612 char *wformat,
1613 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001614{
Victor Stinner97b89882010-05-06 00:25:39 +00001615 STRUCT_STAT st;
1616 PyObject *opath;
1617 char *path;
1618 int res;
1619 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001620
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001621#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00001622 /* If on wide-character-capable OS see if argument
1623 is Unicode and if so use wide API. */
1624 if (unicode_file_names()) {
1625 PyUnicodeObject *po;
1626 if (PyArg_ParseTuple(args, wformat, &po)) {
1627 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001628
Victor Stinner97b89882010-05-06 00:25:39 +00001629 Py_BEGIN_ALLOW_THREADS
1630 /* PyUnicode_AS_UNICODE result OK without
1631 thread lock as it is a simple dereference. */
1632 res = wstatfunc(wpath, &st);
1633 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001634
Victor Stinner97b89882010-05-06 00:25:39 +00001635 if (res != 0)
1636 return win32_error_unicode("stat", wpath);
1637 return _pystat_fromstructstat(&st);
1638 }
1639 /* Drop the argument parsing error as narrow strings
1640 are also valid. */
1641 PyErr_Clear();
1642 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001643#endif
1644
Victor Stinner97b89882010-05-06 00:25:39 +00001645 if (!PyArg_ParseTuple(args, format,
1646 PyUnicode_FSConverter, &opath))
1647 return NULL;
1648 path = bytes2str(opath, 1);
1649 Py_BEGIN_ALLOW_THREADS
1650 res = (*statfunc)(path, &st);
1651 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001652
Victor Stinner97b89882010-05-06 00:25:39 +00001653 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001654#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00001655 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001656#else
Victor Stinner97b89882010-05-06 00:25:39 +00001657 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001658#endif
Victor Stinner97b89882010-05-06 00:25:39 +00001659 }
1660 else
1661 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001662
Victor Stinner97b89882010-05-06 00:25:39 +00001663 release_bytes(opath);
1664 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001665}
1666
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001667/* POSIX methods */
1668
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001669PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001670"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001671Use the real uid/gid to test for access to a path. Note that most\n\
1672operations will use the effective uid/gid, therefore this routine can\n\
1673be used in a suid/sgid environment to test if the invoking user has the\n\
1674specified access to the path. The mode argument can be F_OK to test\n\
1675existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001676
1677static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001678posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001679{
Victor Stinner97b89882010-05-06 00:25:39 +00001680 PyObject *opath;
1681 char *path;
1682 int mode;
1683
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001684#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00001685 DWORD attr;
1686 if (unicode_file_names()) {
1687 PyUnicodeObject *po;
1688 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1689 Py_BEGIN_ALLOW_THREADS
1690 /* PyUnicode_AS_UNICODE OK without thread lock as
1691 it is a simple dereference. */
1692 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1693 Py_END_ALLOW_THREADS
1694 goto finish;
1695 }
1696 /* Drop the argument parsing error as narrow strings
1697 are also valid. */
1698 PyErr_Clear();
1699 }
1700 if (!PyArg_ParseTuple(args, "O&i:access",
1701 PyUnicode_FSConverter, &opath, &mode))
1702 return 0;
1703 path = bytes2str(opath, 1);
1704 Py_BEGIN_ALLOW_THREADS
1705 attr = GetFileAttributesA(path);
1706 Py_END_ALLOW_THREADS
1707 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001708finish:
Victor Stinner97b89882010-05-06 00:25:39 +00001709 if (attr == 0xFFFFFFFF)
1710 /* File does not exist, or cannot read attributes */
1711 return PyBool_FromLong(0);
1712 /* Access is possible if either write access wasn't requested, or
1713 the file isn't read-only, or if it's a directory, as there are
1714 no read-only directories on Windows. */
1715 return PyBool_FromLong(!(mode & 2)
1716 || !(attr & FILE_ATTRIBUTE_READONLY)
1717 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001718#else
Victor Stinner97b89882010-05-06 00:25:39 +00001719 int res;
1720 if (!PyArg_ParseTuple(args, "O&i:access",
1721 PyUnicode_FSConverter, &opath, &mode))
1722 return NULL;
1723 path = bytes2str(opath, 1);
1724 Py_BEGIN_ALLOW_THREADS
1725 res = access(path, mode);
1726 Py_END_ALLOW_THREADS
1727 release_bytes(opath);
1728 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001729#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001730}
1731
Guido van Rossumd371ff11999-01-25 16:12:23 +00001732#ifndef F_OK
1733#define F_OK 0
1734#endif
1735#ifndef R_OK
1736#define R_OK 4
1737#endif
1738#ifndef W_OK
1739#define W_OK 2
1740#endif
1741#ifndef X_OK
1742#define X_OK 1
1743#endif
1744
1745#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001746PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001747"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001748Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001749
1750static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001751posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001752{
Victor Stinner97b89882010-05-06 00:25:39 +00001753 int id;
1754 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001755
Victor Stinner97b89882010-05-06 00:25:39 +00001756 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1757 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001758
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001759#if defined(__VMS)
Victor Stinner97b89882010-05-06 00:25:39 +00001760 /* file descriptor 0 only, the default input device (stdin) */
1761 if (id == 0) {
1762 ret = ttyname();
1763 }
1764 else {
1765 ret = NULL;
1766 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001767#else
Victor Stinner97b89882010-05-06 00:25:39 +00001768 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001769#endif
Victor Stinner97b89882010-05-06 00:25:39 +00001770 if (ret == NULL)
1771 return posix_error();
Victor Stinnerbae0e622010-08-15 09:15:02 +00001772 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001773}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001774#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001775
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001776#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001777PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001778"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001779Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001780
1781static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001782posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001783{
Victor Stinner97b89882010-05-06 00:25:39 +00001784 char *ret;
1785 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001786
Greg Wardb48bc172000-03-01 21:51:56 +00001787#ifdef USE_CTERMID_R
Victor Stinner97b89882010-05-06 00:25:39 +00001788 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001789#else
Victor Stinner97b89882010-05-06 00:25:39 +00001790 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001791#endif
Victor Stinner97b89882010-05-06 00:25:39 +00001792 if (ret == NULL)
1793 return posix_error();
Victor Stinnerbae0e622010-08-15 09:15:02 +00001794 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001795}
1796#endif
1797
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001798PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001799"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001800Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001801
Barry Warsaw53699e91996-12-10 23:23:01 +00001802static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001803posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001804{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001805#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00001806 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001807#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner97b89882010-05-06 00:25:39 +00001808 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001809#elif defined(__VMS)
Victor Stinner97b89882010-05-06 00:25:39 +00001810 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001811#else
Victor Stinner97b89882010-05-06 00:25:39 +00001812 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001813#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001814}
1815
Fred Drake4d1e64b2002-04-15 19:40:07 +00001816#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001817PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001818"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001819Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001820opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001821
1822static PyObject *
1823posix_fchdir(PyObject *self, PyObject *fdobj)
1824{
Victor Stinner97b89882010-05-06 00:25:39 +00001825 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001826}
1827#endif /* HAVE_FCHDIR */
1828
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001829
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001830PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001831"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001832Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001833
Barry Warsaw53699e91996-12-10 23:23:01 +00001834static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001835posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001836{
Victor Stinner97b89882010-05-06 00:25:39 +00001837 PyObject *opath = NULL;
1838 char *path = NULL;
1839 int i;
1840 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001841#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00001842 DWORD attr;
1843 if (unicode_file_names()) {
1844 PyUnicodeObject *po;
1845 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1846 Py_BEGIN_ALLOW_THREADS
1847 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1848 if (attr != 0xFFFFFFFF) {
1849 if (i & _S_IWRITE)
1850 attr &= ~FILE_ATTRIBUTE_READONLY;
1851 else
1852 attr |= FILE_ATTRIBUTE_READONLY;
1853 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1854 }
1855 else
1856 res = 0;
1857 Py_END_ALLOW_THREADS
1858 if (!res)
1859 return win32_error_unicode("chmod",
1860 PyUnicode_AS_UNICODE(po));
1861 Py_INCREF(Py_None);
1862 return Py_None;
1863 }
1864 /* Drop the argument parsing error as narrow strings
1865 are also valid. */
1866 PyErr_Clear();
1867 }
1868 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1869 &opath, &i))
1870 return NULL;
1871 path = bytes2str(opath, 1);
1872 Py_BEGIN_ALLOW_THREADS
1873 attr = GetFileAttributesA(path);
1874 if (attr != 0xFFFFFFFF) {
1875 if (i & _S_IWRITE)
1876 attr &= ~FILE_ATTRIBUTE_READONLY;
1877 else
1878 attr |= FILE_ATTRIBUTE_READONLY;
1879 res = SetFileAttributesA(path, attr);
1880 }
1881 else
1882 res = 0;
1883 Py_END_ALLOW_THREADS
1884 if (!res) {
1885 win32_error("chmod", path);
1886 release_bytes(opath);
1887 return NULL;
1888 }
1889 release_bytes(opath);
1890 Py_INCREF(Py_None);
1891 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001892#else /* MS_WINDOWS */
Victor Stinner97b89882010-05-06 00:25:39 +00001893 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1894 &opath, &i))
1895 return NULL;
1896 path = bytes2str(opath, 1);
1897 Py_BEGIN_ALLOW_THREADS
1898 res = chmod(path, i);
1899 Py_END_ALLOW_THREADS
1900 if (res < 0)
1901 return posix_error_with_allocated_filename(opath);
1902 release_bytes(opath);
1903 Py_INCREF(Py_None);
1904 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001905#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001906}
1907
Christian Heimes4e30a842007-11-30 22:12:06 +00001908#ifdef HAVE_FCHMOD
1909PyDoc_STRVAR(posix_fchmod__doc__,
1910"fchmod(fd, mode)\n\n\
1911Change the access permissions of the file given by file\n\
1912descriptor fd.");
1913
1914static PyObject *
1915posix_fchmod(PyObject *self, PyObject *args)
1916{
Victor Stinner97b89882010-05-06 00:25:39 +00001917 int fd, mode, res;
1918 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1919 return NULL;
1920 Py_BEGIN_ALLOW_THREADS
1921 res = fchmod(fd, mode);
1922 Py_END_ALLOW_THREADS
1923 if (res < 0)
1924 return posix_error();
1925 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001926}
1927#endif /* HAVE_FCHMOD */
1928
1929#ifdef HAVE_LCHMOD
1930PyDoc_STRVAR(posix_lchmod__doc__,
1931"lchmod(path, mode)\n\n\
1932Change the access permissions of a file. If path is a symlink, this\n\
1933affects the link itself rather than the target.");
1934
1935static PyObject *
1936posix_lchmod(PyObject *self, PyObject *args)
1937{
Victor Stinner97b89882010-05-06 00:25:39 +00001938 PyObject *opath;
1939 char *path;
1940 int i;
1941 int res;
1942 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1943 &opath, &i))
1944 return NULL;
1945 path = bytes2str(opath, 1);
1946 Py_BEGIN_ALLOW_THREADS
1947 res = lchmod(path, i);
1948 Py_END_ALLOW_THREADS
1949 if (res < 0)
1950 return posix_error_with_allocated_filename(opath);
1951 release_bytes(opath);
1952 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001953}
1954#endif /* HAVE_LCHMOD */
1955
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001956
Thomas Wouterscf297e42007-02-23 15:07:44 +00001957#ifdef HAVE_CHFLAGS
1958PyDoc_STRVAR(posix_chflags__doc__,
1959"chflags(path, flags)\n\n\
1960Set file flags.");
1961
1962static PyObject *
1963posix_chflags(PyObject *self, PyObject *args)
1964{
Victor Stinner97b89882010-05-06 00:25:39 +00001965 PyObject *opath;
1966 char *path;
1967 unsigned long flags;
1968 int res;
1969 if (!PyArg_ParseTuple(args, "O&k:chflags",
1970 PyUnicode_FSConverter, &opath, &flags))
1971 return NULL;
1972 path = bytes2str(opath, 1);
1973 Py_BEGIN_ALLOW_THREADS
1974 res = chflags(path, flags);
1975 Py_END_ALLOW_THREADS
1976 if (res < 0)
1977 return posix_error_with_allocated_filename(opath);
1978 release_bytes(opath);
1979 Py_INCREF(Py_None);
1980 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001981}
1982#endif /* HAVE_CHFLAGS */
1983
1984#ifdef HAVE_LCHFLAGS
1985PyDoc_STRVAR(posix_lchflags__doc__,
1986"lchflags(path, flags)\n\n\
1987Set file flags.\n\
1988This function will not follow symbolic links.");
1989
1990static PyObject *
1991posix_lchflags(PyObject *self, PyObject *args)
1992{
Victor Stinner97b89882010-05-06 00:25:39 +00001993 PyObject *opath;
1994 char *path;
1995 unsigned long flags;
1996 int res;
1997 if (!PyArg_ParseTuple(args, "O&k:lchflags",
1998 PyUnicode_FSConverter, &opath, &flags))
1999 return NULL;
2000 path = bytes2str(opath, 1);
2001 Py_BEGIN_ALLOW_THREADS
2002 res = lchflags(path, flags);
2003 Py_END_ALLOW_THREADS
2004 if (res < 0)
2005 return posix_error_with_allocated_filename(opath);
2006 release_bytes(opath);
2007 Py_INCREF(Py_None);
2008 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002009}
2010#endif /* HAVE_LCHFLAGS */
2011
Martin v. Löwis244edc82001-10-04 22:44:26 +00002012#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002013PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002014"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002015Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002016
2017static PyObject *
2018posix_chroot(PyObject *self, PyObject *args)
2019{
Victor Stinner97b89882010-05-06 00:25:39 +00002020 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002021}
2022#endif
2023
Guido van Rossum21142a01999-01-08 21:05:37 +00002024#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002025PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002026"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002027force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002028
2029static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002030posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002031{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002032 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002033}
2034#endif /* HAVE_FSYNC */
2035
2036#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002037
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002038#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002039extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2040#endif
2041
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002042PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002043"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002044force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002045 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002046
2047static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002048posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002049{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002050 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002051}
2052#endif /* HAVE_FDATASYNC */
2053
2054
Fredrik Lundh10723342000-07-10 16:38:09 +00002055#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002056PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002057"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002058Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002059
Barry Warsaw53699e91996-12-10 23:23:01 +00002060static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002061posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002062{
Victor Stinner97b89882010-05-06 00:25:39 +00002063 PyObject *opath;
2064 char *path;
2065 long uid, gid;
2066 int res;
2067 if (!PyArg_ParseTuple(args, "O&ll:chown",
2068 PyUnicode_FSConverter, &opath,
2069 &uid, &gid))
2070 return NULL;
2071 path = bytes2str(opath, 1);
2072 Py_BEGIN_ALLOW_THREADS
2073 res = chown(path, (uid_t) uid, (gid_t) gid);
2074 Py_END_ALLOW_THREADS
2075 if (res < 0)
2076 return posix_error_with_allocated_filename(opath);
2077 release_bytes(opath);
2078 Py_INCREF(Py_None);
2079 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002080}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002081#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002082
Christian Heimes4e30a842007-11-30 22:12:06 +00002083#ifdef HAVE_FCHOWN
2084PyDoc_STRVAR(posix_fchown__doc__,
2085"fchown(fd, uid, gid)\n\n\
2086Change the owner and group id of the file given by file descriptor\n\
2087fd to the numeric uid and gid.");
2088
2089static PyObject *
2090posix_fchown(PyObject *self, PyObject *args)
2091{
Victor Stinner97b89882010-05-06 00:25:39 +00002092 int fd;
2093 long uid, gid;
2094 int res;
2095 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2096 return NULL;
2097 Py_BEGIN_ALLOW_THREADS
2098 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2099 Py_END_ALLOW_THREADS
2100 if (res < 0)
2101 return posix_error();
2102 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002103}
2104#endif /* HAVE_FCHOWN */
2105
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002106#ifdef HAVE_LCHOWN
2107PyDoc_STRVAR(posix_lchown__doc__,
2108"lchown(path, uid, gid)\n\n\
2109Change the owner and group id of path to the numeric uid and gid.\n\
2110This function will not follow symbolic links.");
2111
2112static PyObject *
2113posix_lchown(PyObject *self, PyObject *args)
2114{
Victor Stinner97b89882010-05-06 00:25:39 +00002115 PyObject *opath;
2116 char *path;
2117 long uid, gid;
2118 int res;
2119 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2120 PyUnicode_FSConverter, &opath,
2121 &uid, &gid))
2122 return NULL;
2123 path = bytes2str(opath, 1);
2124 Py_BEGIN_ALLOW_THREADS
2125 res = lchown(path, (uid_t) uid, (gid_t) gid);
2126 Py_END_ALLOW_THREADS
2127 if (res < 0)
2128 return posix_error_with_allocated_filename(opath);
2129 release_bytes(opath);
2130 Py_INCREF(Py_None);
2131 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002132}
2133#endif /* HAVE_LCHOWN */
2134
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002135
Guido van Rossum36bc6801995-06-14 22:54:23 +00002136#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002137static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002138posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002139{
Victor Stinner97b89882010-05-06 00:25:39 +00002140 char buf[1026];
2141 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002142
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002143#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00002144 if (!use_bytes && unicode_file_names()) {
2145 wchar_t wbuf[1026];
2146 wchar_t *wbuf2 = wbuf;
2147 PyObject *resobj;
2148 DWORD len;
2149 Py_BEGIN_ALLOW_THREADS
2150 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2151 /* If the buffer is large enough, len does not include the
2152 terminating \0. If the buffer is too small, len includes
2153 the space needed for the terminator. */
2154 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2155 wbuf2 = malloc(len * sizeof(wchar_t));
2156 if (wbuf2)
2157 len = GetCurrentDirectoryW(len, wbuf2);
2158 }
2159 Py_END_ALLOW_THREADS
2160 if (!wbuf2) {
2161 PyErr_NoMemory();
2162 return NULL;
2163 }
2164 if (!len) {
2165 if (wbuf2 != wbuf) free(wbuf2);
2166 return win32_error("getcwdu", NULL);
2167 }
2168 resobj = PyUnicode_FromWideChar(wbuf2, len);
2169 if (wbuf2 != wbuf) free(wbuf2);
2170 return resobj;
2171 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002172#endif
2173
Victor Stinner97b89882010-05-06 00:25:39 +00002174 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002175#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner97b89882010-05-06 00:25:39 +00002176 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002177#else
Victor Stinner97b89882010-05-06 00:25:39 +00002178 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002179#endif
Victor Stinner97b89882010-05-06 00:25:39 +00002180 Py_END_ALLOW_THREADS
2181 if (res == NULL)
2182 return posix_error();
2183 if (use_bytes)
2184 return PyBytes_FromStringAndSize(buf, strlen(buf));
2185 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002186}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002187
2188PyDoc_STRVAR(posix_getcwd__doc__,
2189"getcwd() -> path\n\n\
2190Return a unicode string representing the current working directory.");
2191
2192static PyObject *
2193posix_getcwd_unicode(PyObject *self)
2194{
2195 return posix_getcwd(0);
2196}
2197
2198PyDoc_STRVAR(posix_getcwdb__doc__,
2199"getcwdb() -> path\n\n\
2200Return a bytes string representing the current working directory.");
2201
2202static PyObject *
2203posix_getcwd_bytes(PyObject *self)
2204{
2205 return posix_getcwd(1);
2206}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002207#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002208
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002209
Guido van Rossumb6775db1994-08-01 11:34:53 +00002210#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002211PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002212"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002213Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002214
Barry Warsaw53699e91996-12-10 23:23:01 +00002215static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002216posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002217{
Victor Stinner97b89882010-05-06 00:25:39 +00002218 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002219}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002220#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002221
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002223PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002224"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002225Return a list containing the names of the entries in the directory.\n\
2226\n\
Victor Stinner97b89882010-05-06 00:25:39 +00002227 path: path of directory to list\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002228\n\
2229The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002230entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002231
Barry Warsaw53699e91996-12-10 23:23:01 +00002232static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002233posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002234{
Victor Stinner97b89882010-05-06 00:25:39 +00002235 /* XXX Should redo this putting the (now four) versions of opendir
2236 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002237#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002238
Victor Stinner97b89882010-05-06 00:25:39 +00002239 PyObject *d, *v;
2240 HANDLE hFindFile;
2241 BOOL result;
2242 WIN32_FIND_DATA FileData;
2243 PyObject *opath;
2244 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2245 char *bufptr = namebuf;
2246 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002247
Victor Stinner97b89882010-05-06 00:25:39 +00002248 /* If on wide-character-capable OS see if argument
2249 is Unicode and if so use wide API. */
2250 if (unicode_file_names()) {
2251 PyObject *po;
2252 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2253 WIN32_FIND_DATAW wFileData;
2254 Py_UNICODE *wnamebuf;
2255 /* Overallocate for \\*.*\0 */
2256 len = PyUnicode_GET_SIZE(po);
2257 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2258 if (!wnamebuf) {
2259 PyErr_NoMemory();
2260 return NULL;
2261 }
2262 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2263 if (len > 0) {
2264 Py_UNICODE wch = wnamebuf[len-1];
2265 if (wch != L'/' && wch != L'\\' && wch != L':')
2266 wnamebuf[len++] = L'\\';
2267 wcscpy(wnamebuf + len, L"*.*");
2268 }
2269 if ((d = PyList_New(0)) == NULL) {
2270 free(wnamebuf);
2271 return NULL;
2272 }
Antoine Pitroubd25d592010-08-09 23:47:57 +00002273 Py_BEGIN_ALLOW_THREADS
Victor Stinner97b89882010-05-06 00:25:39 +00002274 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroubd25d592010-08-09 23:47:57 +00002275 Py_END_ALLOW_THREADS
Victor Stinner97b89882010-05-06 00:25:39 +00002276 if (hFindFile == INVALID_HANDLE_VALUE) {
2277 int error = GetLastError();
2278 if (error == ERROR_FILE_NOT_FOUND) {
2279 free(wnamebuf);
2280 return d;
2281 }
2282 Py_DECREF(d);
2283 win32_error_unicode("FindFirstFileW", wnamebuf);
2284 free(wnamebuf);
2285 return NULL;
2286 }
2287 do {
2288 /* Skip over . and .. */
2289 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2290 wcscmp(wFileData.cFileName, L"..") != 0) {
2291 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2292 if (v == NULL) {
2293 Py_DECREF(d);
2294 d = NULL;
2295 break;
2296 }
2297 if (PyList_Append(d, v) != 0) {
2298 Py_DECREF(v);
2299 Py_DECREF(d);
2300 d = NULL;
2301 break;
2302 }
2303 Py_DECREF(v);
2304 }
2305 Py_BEGIN_ALLOW_THREADS
2306 result = FindNextFileW(hFindFile, &wFileData);
2307 Py_END_ALLOW_THREADS
2308 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2309 it got to the end of the directory. */
2310 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2311 Py_DECREF(d);
2312 win32_error_unicode("FindNextFileW", wnamebuf);
2313 FindClose(hFindFile);
2314 free(wnamebuf);
2315 return NULL;
2316 }
2317 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002318
Victor Stinner97b89882010-05-06 00:25:39 +00002319 if (FindClose(hFindFile) == FALSE) {
2320 Py_DECREF(d);
2321 win32_error_unicode("FindClose", wnamebuf);
2322 free(wnamebuf);
2323 return NULL;
2324 }
2325 free(wnamebuf);
2326 return d;
2327 }
2328 /* Drop the argument parsing error as narrow strings
2329 are also valid. */
2330 PyErr_Clear();
2331 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002332
Victor Stinner97b89882010-05-06 00:25:39 +00002333 if (!PyArg_ParseTuple(args, "O&:listdir",
2334 PyUnicode_FSConverter, &opath))
2335 return NULL;
2336 if (PyObject_Size(opath)+1 > MAX_PATH) {
2337 PyErr_SetString(PyExc_ValueError, "path too long");
2338 Py_DECREF(opath);
2339 return NULL;
2340 }
2341 strcpy(namebuf, bytes2str(opath, 0));
2342 len = PyObject_Size(opath);
2343 if (len > 0) {
2344 char ch = namebuf[len-1];
2345 if (ch != SEP && ch != ALTSEP && ch != ':')
2346 namebuf[len++] = '/';
2347 strcpy(namebuf + len, "*.*");
2348 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002349
Victor Stinner97b89882010-05-06 00:25:39 +00002350 if ((d = PyList_New(0)) == NULL)
2351 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002352
Antoine Pitroubd25d592010-08-09 23:47:57 +00002353 Py_BEGIN_ALLOW_THREADS
Victor Stinner97b89882010-05-06 00:25:39 +00002354 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroubd25d592010-08-09 23:47:57 +00002355 Py_END_ALLOW_THREADS
Victor Stinner97b89882010-05-06 00:25:39 +00002356 if (hFindFile == INVALID_HANDLE_VALUE) {
2357 int error = GetLastError();
2358 if (error == ERROR_FILE_NOT_FOUND)
2359 return d;
2360 Py_DECREF(d);
2361 return win32_error("FindFirstFile", namebuf);
2362 }
2363 do {
2364 /* Skip over . and .. */
2365 if (strcmp(FileData.cFileName, ".") != 0 &&
2366 strcmp(FileData.cFileName, "..") != 0) {
2367 v = PyBytes_FromString(FileData.cFileName);
2368 if (v == NULL) {
2369 Py_DECREF(d);
2370 d = NULL;
2371 break;
2372 }
2373 if (PyList_Append(d, v) != 0) {
2374 Py_DECREF(v);
2375 Py_DECREF(d);
2376 d = NULL;
2377 break;
2378 }
2379 Py_DECREF(v);
2380 }
2381 Py_BEGIN_ALLOW_THREADS
2382 result = FindNextFile(hFindFile, &FileData);
2383 Py_END_ALLOW_THREADS
2384 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2385 it got to the end of the directory. */
2386 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2387 Py_DECREF(d);
2388 win32_error("FindNextFile", namebuf);
2389 FindClose(hFindFile);
2390 return NULL;
2391 }
2392 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002393
Victor Stinner97b89882010-05-06 00:25:39 +00002394 if (FindClose(hFindFile) == FALSE) {
2395 Py_DECREF(d);
2396 return win32_error("FindClose", namebuf);
2397 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002398
Victor Stinner97b89882010-05-06 00:25:39 +00002399 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002400
Tim Peters0bb44a42000-09-15 07:44:49 +00002401#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002402
2403#ifndef MAX_PATH
2404#define MAX_PATH CCHMAXPATH
2405#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002406 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002407 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002408 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002409 PyObject *d, *v;
2410 char namebuf[MAX_PATH+5];
2411 HDIR hdir = 1;
2412 ULONG srchcnt = 1;
2413 FILEFINDBUF3 ep;
2414 APIRET rc;
2415
Victor Stinner97b89882010-05-06 00:25:39 +00002416 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002417 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002418 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002419 name = bytes2str(oname);
2420 len = PyObject_Size(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002421 if (len >= MAX_PATH) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002422 release_bytes(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002423 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002424 return NULL;
2425 }
2426 strcpy(namebuf, name);
2427 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002428 if (*pt == ALTSEP)
2429 *pt = SEP;
2430 if (namebuf[len-1] != SEP)
2431 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002432 strcpy(namebuf + len, "*.*");
2433
Neal Norwitz6c913782007-10-14 03:23:09 +00002434 if ((d = PyList_New(0)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002435 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002436 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002437 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002438
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002439 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2440 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002441 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002442 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2443 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2444 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002445
2446 if (rc != NO_ERROR) {
2447 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002448 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002449 }
2450
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002451 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002452 do {
2453 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002454 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002455 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002456
2457 strcpy(namebuf, ep.achName);
2458
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002459 /* Leave Case of Name Alone -- In Native Form */
2460 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002461
Christian Heimes72b710a2008-05-26 13:28:38 +00002462 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002463 if (v == NULL) {
2464 Py_DECREF(d);
2465 d = NULL;
2466 break;
2467 }
2468 if (PyList_Append(d, v) != 0) {
2469 Py_DECREF(v);
2470 Py_DECREF(d);
2471 d = NULL;
2472 break;
2473 }
2474 Py_DECREF(v);
2475 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2476 }
2477
Martin v. Löwis011e8422009-05-05 04:43:17 +00002478 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002479 return d;
2480#else
Victor Stinner97b89882010-05-06 00:25:39 +00002481 PyObject *oname;
2482 char *name;
2483 PyObject *d, *v;
2484 DIR *dirp;
2485 struct dirent *ep;
2486 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002487
Victor Stinner97b89882010-05-06 00:25:39 +00002488 errno = 0;
2489 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2490 arg_is_unicode = 0;
2491 PyErr_Clear();
2492 }
2493 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
2494 return NULL;
2495 name = bytes2str(oname, 1);
Antoine Pitrou037077f2010-09-04 17:26:01 +00002496 Py_BEGIN_ALLOW_THREADS
2497 dirp = opendir(name);
2498 Py_END_ALLOW_THREADS
2499 if (dirp == NULL) {
Victor Stinner97b89882010-05-06 00:25:39 +00002500 return posix_error_with_allocated_filename(oname);
2501 }
2502 if ((d = PyList_New(0)) == NULL) {
Antoine Pitrou037077f2010-09-04 17:26:01 +00002503 Py_BEGIN_ALLOW_THREADS
Victor Stinner97b89882010-05-06 00:25:39 +00002504 closedir(dirp);
Antoine Pitrou037077f2010-09-04 17:26:01 +00002505 Py_END_ALLOW_THREADS
Victor Stinner97b89882010-05-06 00:25:39 +00002506 release_bytes(oname);
2507 return NULL;
2508 }
2509 for (;;) {
2510 errno = 0;
2511 Py_BEGIN_ALLOW_THREADS
2512 ep = readdir(dirp);
2513 Py_END_ALLOW_THREADS
2514 if (ep == NULL) {
2515 if (errno == 0) {
2516 break;
2517 } else {
Antoine Pitrou037077f2010-09-04 17:26:01 +00002518 Py_BEGIN_ALLOW_THREADS
Victor Stinner97b89882010-05-06 00:25:39 +00002519 closedir(dirp);
Antoine Pitrou037077f2010-09-04 17:26:01 +00002520 Py_END_ALLOW_THREADS
Victor Stinner97b89882010-05-06 00:25:39 +00002521 Py_DECREF(d);
2522 return posix_error_with_allocated_filename(oname);
2523 }
2524 }
2525 if (ep->d_name[0] == '.' &&
2526 (NAMLEN(ep) == 1 ||
2527 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2528 continue;
Victor Stinner203406c2010-05-14 18:07:39 +00002529 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner97b89882010-05-06 00:25:39 +00002530 if (v == NULL) {
Victor Stinner203406c2010-05-14 18:07:39 +00002531 Py_DECREF(d);
2532 d = NULL;
Victor Stinner97b89882010-05-06 00:25:39 +00002533 break;
2534 }
Victor Stinner203406c2010-05-14 18:07:39 +00002535 if (arg_is_unicode) {
2536 PyObject *w;
2537
2538 w = PyUnicode_FromEncodedObject(v,
2539 Py_FileSystemDefaultEncoding,
2540 "surrogateescape");
2541 Py_DECREF(v);
2542 if (w != NULL)
2543 v = w;
2544 else {
2545 /* Encoding failed to decode ASCII bytes.
2546 Raise exception. */
2547 Py_DECREF(d);
2548 d = NULL;
2549 break;
2550 }
2551 }
Victor Stinner97b89882010-05-06 00:25:39 +00002552 if (PyList_Append(d, v) != 0) {
2553 Py_DECREF(v);
Victor Stinner203406c2010-05-14 18:07:39 +00002554 Py_DECREF(d);
2555 d = NULL;
Victor Stinner97b89882010-05-06 00:25:39 +00002556 break;
2557 }
2558 Py_DECREF(v);
2559 }
Antoine Pitrou037077f2010-09-04 17:26:01 +00002560 Py_BEGIN_ALLOW_THREADS
Victor Stinner97b89882010-05-06 00:25:39 +00002561 closedir(dirp);
Antoine Pitrou037077f2010-09-04 17:26:01 +00002562 Py_END_ALLOW_THREADS
Victor Stinner97b89882010-05-06 00:25:39 +00002563 release_bytes(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002564
Victor Stinner97b89882010-05-06 00:25:39 +00002565 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002566
Tim Peters0bb44a42000-09-15 07:44:49 +00002567#endif /* which OS */
2568} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002569
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002570#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002571/* A helper function for abspath on win32 */
2572static PyObject *
2573posix__getfullpathname(PyObject *self, PyObject *args)
2574{
Victor Stinner97b89882010-05-06 00:25:39 +00002575 PyObject *opath;
2576 char *path;
2577 char outbuf[MAX_PATH*2];
2578 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002579#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00002580 if (unicode_file_names()) {
2581 PyUnicodeObject *po;
2582 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2583 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2584 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2585 Py_UNICODE *wtemp;
2586 DWORD result;
2587 PyObject *v;
2588 result = GetFullPathNameW(wpath,
2589 sizeof(woutbuf)/sizeof(woutbuf[0]),
2590 woutbuf, &wtemp);
2591 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2592 woutbufp = malloc(result * sizeof(Py_UNICODE));
2593 if (!woutbufp)
2594 return PyErr_NoMemory();
2595 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2596 }
2597 if (result)
2598 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2599 else
2600 v = win32_error_unicode("GetFullPathNameW", wpath);
2601 if (woutbufp != woutbuf)
2602 free(woutbufp);
2603 return v;
2604 }
2605 /* Drop the argument parsing error as narrow strings
2606 are also valid. */
2607 PyErr_Clear();
2608 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002609#endif
Victor Stinner97b89882010-05-06 00:25:39 +00002610 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2611 PyUnicode_FSConverter, &opath))
2612 return NULL;
2613 path = bytes2str(opath, 1);
2614 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2615 outbuf, &temp)) {
2616 win32_error("GetFullPathName", path);
2617 release_bytes(opath);
2618 return NULL;
2619 }
2620 release_bytes(opath);
2621 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2622 return PyUnicode_Decode(outbuf, strlen(outbuf),
2623 Py_FileSystemDefaultEncoding, NULL);
2624 }
2625 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002626} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002627#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002628
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002629PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002630"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002631Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002632
Barry Warsaw53699e91996-12-10 23:23:01 +00002633static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002634posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002635{
Victor Stinner97b89882010-05-06 00:25:39 +00002636 int res;
2637 PyObject *opath;
2638 char *path;
2639 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002640
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002641#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00002642 if (unicode_file_names()) {
2643 PyUnicodeObject *po;
2644 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2645 Py_BEGIN_ALLOW_THREADS
2646 /* PyUnicode_AS_UNICODE OK without thread lock as
2647 it is a simple dereference. */
2648 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2649 Py_END_ALLOW_THREADS
2650 if (!res)
2651 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2652 Py_INCREF(Py_None);
2653 return Py_None;
2654 }
2655 /* Drop the argument parsing error as narrow strings
2656 are also valid. */
2657 PyErr_Clear();
2658 }
2659 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2660 PyUnicode_FSConverter, &opath, &mode))
2661 return NULL;
2662 path = bytes2str(opath, 1);
2663 Py_BEGIN_ALLOW_THREADS
2664 /* PyUnicode_AS_UNICODE OK without thread lock as
2665 it is a simple dereference. */
2666 res = CreateDirectoryA(path, NULL);
2667 Py_END_ALLOW_THREADS
2668 if (!res) {
2669 win32_error("mkdir", path);
2670 release_bytes(opath);
2671 return NULL;
2672 }
2673 release_bytes(opath);
2674 Py_INCREF(Py_None);
2675 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002676#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002677
Victor Stinner97b89882010-05-06 00:25:39 +00002678 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2679 PyUnicode_FSConverter, &opath, &mode))
2680 return NULL;
2681 path = bytes2str(opath, 1);
2682 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002683#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner97b89882010-05-06 00:25:39 +00002684 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002685#else
Victor Stinner97b89882010-05-06 00:25:39 +00002686 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002687#endif
Victor Stinner97b89882010-05-06 00:25:39 +00002688 Py_END_ALLOW_THREADS
2689 if (res < 0)
2690 return posix_error_with_allocated_filename(opath);
2691 release_bytes(opath);
2692 Py_INCREF(Py_None);
2693 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002694#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002695}
2696
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002697
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002698/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2699#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002700#include <sys/resource.h>
2701#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002702
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002703
2704#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002705PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002706"nice(inc) -> new_priority\n\n\
2707Decrease the priority of process by inc and return the new priority.");
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_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002711{
Victor Stinner97b89882010-05-06 00:25:39 +00002712 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002713
Victor Stinner97b89882010-05-06 00:25:39 +00002714 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2715 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002716
Victor Stinner97b89882010-05-06 00:25:39 +00002717 /* There are two flavours of 'nice': one that returns the new
2718 priority (as required by almost all standards out there) and the
2719 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2720 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002721
Victor Stinner97b89882010-05-06 00:25:39 +00002722 If we are of the nice family that returns the new priority, we
2723 need to clear errno before the call, and check if errno is filled
2724 before calling posix_error() on a returnvalue of -1, because the
2725 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002726
Victor Stinner97b89882010-05-06 00:25:39 +00002727 errno = 0;
2728 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002729#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner97b89882010-05-06 00:25:39 +00002730 if (value == 0)
2731 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002732#endif
Victor Stinner97b89882010-05-06 00:25:39 +00002733 if (value == -1 && errno != 0)
2734 /* either nice() or getpriority() returned an error */
2735 return posix_error();
2736 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002737}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002738#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002739
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002740PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002741"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002742Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002743
Barry Warsaw53699e91996-12-10 23:23:01 +00002744static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002745posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002746{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002747#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00002748 PyObject *o1, *o2;
2749 char *p1, *p2;
2750 BOOL result;
2751 if (unicode_file_names()) {
2752 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2753 goto error;
2754 if (!convert_to_unicode(&o1))
2755 goto error;
2756 if (!convert_to_unicode(&o2)) {
2757 Py_DECREF(o1);
2758 goto error;
2759 }
2760 Py_BEGIN_ALLOW_THREADS
2761 result = MoveFileW(PyUnicode_AsUnicode(o1),
2762 PyUnicode_AsUnicode(o2));
2763 Py_END_ALLOW_THREADS
2764 Py_DECREF(o1);
2765 Py_DECREF(o2);
2766 if (!result)
2767 return win32_error("rename", NULL);
2768 Py_INCREF(Py_None);
2769 return Py_None;
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002770error:
Victor Stinner97b89882010-05-06 00:25:39 +00002771 PyErr_Clear();
2772 }
2773 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2774 return NULL;
2775 Py_BEGIN_ALLOW_THREADS
2776 result = MoveFileA(p1, p2);
2777 Py_END_ALLOW_THREADS
2778 if (!result)
2779 return win32_error("rename", NULL);
2780 Py_INCREF(Py_None);
2781 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002782#else
Victor Stinner97b89882010-05-06 00:25:39 +00002783 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002784#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002785}
2786
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002787
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002788PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002789"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002790Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002791
Barry Warsaw53699e91996-12-10 23:23:01 +00002792static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002793posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002794{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002795#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00002796 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002797#else
Victor Stinner97b89882010-05-06 00:25:39 +00002798 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002799#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002800}
2801
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002803PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002804"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002805Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002806
Barry Warsaw53699e91996-12-10 23:23:01 +00002807static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002808posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002809{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002810#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00002811 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002812#else
Victor Stinner97b89882010-05-06 00:25:39 +00002813 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002814#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002815}
2816
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002817
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002818#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002819PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002820"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002821Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002822
Barry Warsaw53699e91996-12-10 23:23:01 +00002823static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002824posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002825{
Victor Stinner97b89882010-05-06 00:25:39 +00002826 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002827#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00002828 wchar_t *command;
2829 if (!PyArg_ParseTuple(args, "u:system", &command))
2830 return NULL;
Victor Stinnera5ab4fc2010-04-16 12:47:52 +00002831
Victor Stinner97b89882010-05-06 00:25:39 +00002832 Py_BEGIN_ALLOW_THREADS
2833 sts = _wsystem(command);
2834 Py_END_ALLOW_THREADS
Victor Stinnera5ab4fc2010-04-16 12:47:52 +00002835#else
Victor Stinner97b89882010-05-06 00:25:39 +00002836 PyObject *command_obj;
2837 char *command;
2838 if (!PyArg_ParseTuple(args, "O&:system",
2839 PyUnicode_FSConverter, &command_obj))
2840 return NULL;
Victor Stinnera5ab4fc2010-04-16 12:47:52 +00002841
Victor Stinner97b89882010-05-06 00:25:39 +00002842 command = bytes2str(command_obj, 1);
2843 Py_BEGIN_ALLOW_THREADS
2844 sts = system(command);
2845 Py_END_ALLOW_THREADS
2846 release_bytes(command_obj);
Victor Stinnera5ab4fc2010-04-16 12:47:52 +00002847#endif
Victor Stinner97b89882010-05-06 00:25:39 +00002848 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002849}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002850#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002851
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002852
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002853PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002854"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002855Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002856
Barry Warsaw53699e91996-12-10 23:23:01 +00002857static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002858posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002859{
Victor Stinner97b89882010-05-06 00:25:39 +00002860 int i;
2861 if (!PyArg_ParseTuple(args, "i:umask", &i))
2862 return NULL;
2863 i = (int)umask(i);
2864 if (i < 0)
2865 return posix_error();
2866 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002867}
2868
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002869
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002870PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002871"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002872Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002873
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002874PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002875"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002876Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002877
Barry Warsaw53699e91996-12-10 23:23:01 +00002878static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002879posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002880{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002881#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00002882 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002883#else
Victor Stinner97b89882010-05-06 00:25:39 +00002884 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002885#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002886}
2887
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002888
Guido van Rossumb6775db1994-08-01 11:34:53 +00002889#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002890PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002891"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002892Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002893
Barry Warsaw53699e91996-12-10 23:23:01 +00002894static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002895posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002896{
Victor Stinner97b89882010-05-06 00:25:39 +00002897 struct utsname u;
2898 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002899
Victor Stinner97b89882010-05-06 00:25:39 +00002900 Py_BEGIN_ALLOW_THREADS
2901 res = uname(&u);
2902 Py_END_ALLOW_THREADS
2903 if (res < 0)
2904 return posix_error();
2905 return Py_BuildValue("(sssss)",
2906 u.sysname,
2907 u.nodename,
2908 u.release,
2909 u.version,
2910 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002911}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002912#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002913
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002914static int
2915extract_time(PyObject *t, long* sec, long* usec)
2916{
Victor Stinner97b89882010-05-06 00:25:39 +00002917 long intval;
2918 if (PyFloat_Check(t)) {
2919 double tval = PyFloat_AsDouble(t);
2920 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
2921 if (!intobj)
2922 return -1;
2923 intval = PyLong_AsLong(intobj);
2924 Py_DECREF(intobj);
2925 if (intval == -1 && PyErr_Occurred())
2926 return -1;
2927 *sec = intval;
2928 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2929 if (*usec < 0)
2930 /* If rounding gave us a negative number,
2931 truncate. */
2932 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002933 return 0;
Victor Stinner97b89882010-05-06 00:25:39 +00002934 }
2935 intval = PyLong_AsLong(t);
2936 if (intval == -1 && PyErr_Occurred())
2937 return -1;
2938 *sec = intval;
2939 *usec = 0;
2940 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002941}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002942
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002943PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002944"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002945utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002946Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002947second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002948
Barry Warsaw53699e91996-12-10 23:23:01 +00002949static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002950posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002951{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002952#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00002953 PyObject *arg;
2954 PyUnicodeObject *obwpath;
2955 wchar_t *wpath = NULL;
2956 PyObject *oapath;
2957 char *apath;
2958 HANDLE hFile;
2959 long atimesec, mtimesec, ausec, musec;
2960 FILETIME atime, mtime;
2961 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002962
Victor Stinner97b89882010-05-06 00:25:39 +00002963 if (unicode_file_names()) {
2964 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2965 wpath = PyUnicode_AS_UNICODE(obwpath);
2966 Py_BEGIN_ALLOW_THREADS
2967 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2968 NULL, OPEN_EXISTING,
2969 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2970 Py_END_ALLOW_THREADS
2971 if (hFile == INVALID_HANDLE_VALUE)
2972 return win32_error_unicode("utime", wpath);
2973 } else
2974 /* Drop the argument parsing error as narrow strings
2975 are also valid. */
2976 PyErr_Clear();
2977 }
2978 if (!wpath) {
2979 if (!PyArg_ParseTuple(args, "O&O:utime",
2980 PyUnicode_FSConverter, &oapath, &arg))
2981 return NULL;
2982 apath = bytes2str(oapath, 1);
2983 Py_BEGIN_ALLOW_THREADS
2984 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2985 NULL, OPEN_EXISTING,
2986 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2987 Py_END_ALLOW_THREADS
2988 if (hFile == INVALID_HANDLE_VALUE) {
2989 win32_error("utime", apath);
2990 release_bytes(oapath);
2991 return NULL;
2992 }
2993 release_bytes(oapath);
2994 }
2995
2996 if (arg == Py_None) {
2997 SYSTEMTIME now;
2998 GetSystemTime(&now);
2999 if (!SystemTimeToFileTime(&now, &mtime) ||
3000 !SystemTimeToFileTime(&now, &atime)) {
3001 win32_error("utime", NULL);
3002 goto done;
3003 }
3004 }
3005 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3006 PyErr_SetString(PyExc_TypeError,
3007 "utime() arg 2 must be a tuple (atime, mtime)");
3008 goto done;
3009 }
3010 else {
3011 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3012 &atimesec, &ausec) == -1)
3013 goto done;
3014 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3015 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3016 &mtimesec, &musec) == -1)
3017 goto done;
3018 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3019 }
3020 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3021 /* Avoid putting the file name into the error here,
3022 as that may confuse the user into believing that
3023 something is wrong with the file, when it also
3024 could be the time stamp that gives a problem. */
3025 win32_error("utime", NULL);
3026 }
3027 Py_INCREF(Py_None);
3028 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003029done:
Victor Stinner97b89882010-05-06 00:25:39 +00003030 CloseHandle(hFile);
3031 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003032#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003033
Victor Stinner97b89882010-05-06 00:25:39 +00003034 PyObject *opath;
3035 char *path;
3036 long atime, mtime, ausec, musec;
3037 int res;
3038 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003039
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003040#if defined(HAVE_UTIMES)
Victor Stinner97b89882010-05-06 00:25:39 +00003041 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003042#define ATIME buf[0].tv_sec
3043#define MTIME buf[1].tv_sec
3044#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003045/* XXX should define struct utimbuf instead, above */
Victor Stinner97b89882010-05-06 00:25:39 +00003046 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003047#define ATIME buf.actime
3048#define MTIME buf.modtime
3049#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003050#else /* HAVE_UTIMES */
Victor Stinner97b89882010-05-06 00:25:39 +00003051 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003052#define ATIME buf[0]
3053#define MTIME buf[1]
3054#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003055#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003056
Mark Hammond817c9292003-12-03 01:22:38 +00003057
Victor Stinner97b89882010-05-06 00:25:39 +00003058 if (!PyArg_ParseTuple(args, "O&O:utime",
3059 PyUnicode_FSConverter, &opath, &arg))
3060 return NULL;
3061 path = bytes2str(opath, 1);
3062 if (arg == Py_None) {
3063 /* optional time values not given */
3064 Py_BEGIN_ALLOW_THREADS
3065 res = utime(path, NULL);
3066 Py_END_ALLOW_THREADS
3067 }
3068 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3069 PyErr_SetString(PyExc_TypeError,
3070 "utime() arg 2 must be a tuple (atime, mtime)");
3071 release_bytes(opath);
3072 return NULL;
3073 }
3074 else {
3075 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3076 &atime, &ausec) == -1) {
3077 release_bytes(opath);
3078 return NULL;
3079 }
3080 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3081 &mtime, &musec) == -1) {
3082 release_bytes(opath);
3083 return NULL;
3084 }
3085 ATIME = atime;
3086 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003087#ifdef HAVE_UTIMES
Victor Stinner97b89882010-05-06 00:25:39 +00003088 buf[0].tv_usec = ausec;
3089 buf[1].tv_usec = musec;
3090 Py_BEGIN_ALLOW_THREADS
3091 res = utimes(path, buf);
3092 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003093#else
Victor Stinner97b89882010-05-06 00:25:39 +00003094 Py_BEGIN_ALLOW_THREADS
3095 res = utime(path, UTIME_ARG);
3096 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003097#endif /* HAVE_UTIMES */
Victor Stinner97b89882010-05-06 00:25:39 +00003098 }
3099 if (res < 0) {
3100 return posix_error_with_allocated_filename(opath);
3101 }
3102 release_bytes(opath);
3103 Py_INCREF(Py_None);
3104 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003105#undef UTIME_ARG
3106#undef ATIME
3107#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003108#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003109}
3110
Guido van Rossum85e3b011991-06-03 12:42:10 +00003111
Guido van Rossum3b066191991-06-04 19:40:25 +00003112/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003113
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003114PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003115"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003116Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003117
Barry Warsaw53699e91996-12-10 23:23:01 +00003118static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003119posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003120{
Victor Stinner97b89882010-05-06 00:25:39 +00003121 int sts;
3122 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3123 return NULL;
3124 _exit(sts);
3125 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003126}
3127
Martin v. Löwis114619e2002-10-07 06:44:21 +00003128#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3129static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003130free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003131{
Victor Stinner97b89882010-05-06 00:25:39 +00003132 Py_ssize_t i;
3133 for (i = 0; i < count; i++)
3134 PyMem_Free(array[i]);
3135 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003136}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003137
Antoine Pitrou69f71142009-05-24 21:25:49 +00003138static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003139int fsconvert_strdup(PyObject *o, char**out)
3140{
Victor Stinner97b89882010-05-06 00:25:39 +00003141 PyObject *bytes;
3142 Py_ssize_t size;
3143 if (!PyUnicode_FSConverter(o, &bytes))
3144 return 0;
3145 size = PyObject_Size(bytes);
3146 *out = PyMem_Malloc(size+1);
3147 if (!*out)
3148 return 0;
3149 /* Don't lock bytes, as we hold the GIL */
3150 memcpy(*out, bytes2str(bytes, 0), size+1);
3151 Py_DECREF(bytes);
3152 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003153}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003154#endif
3155
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003156
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003157#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003158PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003159"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003160Execute an executable path with arguments, replacing current process.\n\
3161\n\
Victor Stinner97b89882010-05-06 00:25:39 +00003162 path: path of executable file\n\
3163 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003164
Barry Warsaw53699e91996-12-10 23:23:01 +00003165static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003166posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003167{
Victor Stinner97b89882010-05-06 00:25:39 +00003168 PyObject *opath;
3169 char *path;
3170 PyObject *argv;
3171 char **argvlist;
3172 Py_ssize_t i, argc;
3173 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003174
Victor Stinner97b89882010-05-06 00:25:39 +00003175 /* execv has two arguments: (path, argv), where
3176 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003177
Victor Stinner97b89882010-05-06 00:25:39 +00003178 if (!PyArg_ParseTuple(args, "O&O:execv",
3179 PyUnicode_FSConverter,
3180 &opath, &argv))
3181 return NULL;
3182 path = bytes2str(opath, 1);
3183 if (PyList_Check(argv)) {
3184 argc = PyList_Size(argv);
3185 getitem = PyList_GetItem;
3186 }
3187 else if (PyTuple_Check(argv)) {
3188 argc = PyTuple_Size(argv);
3189 getitem = PyTuple_GetItem;
3190 }
3191 else {
3192 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3193 release_bytes(opath);
3194 return NULL;
3195 }
3196 if (argc < 1) {
3197 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3198 release_bytes(opath);
3199 return NULL;
3200 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003201
Victor Stinner97b89882010-05-06 00:25:39 +00003202 argvlist = PyMem_NEW(char *, argc+1);
3203 if (argvlist == NULL) {
3204 release_bytes(opath);
3205 return PyErr_NoMemory();
3206 }
3207 for (i = 0; i < argc; i++) {
3208 if (!fsconvert_strdup((*getitem)(argv, i),
3209 &argvlist[i])) {
3210 free_string_array(argvlist, i);
3211 PyErr_SetString(PyExc_TypeError,
3212 "execv() arg 2 must contain only strings");
3213 release_bytes(opath);
3214 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003215
Victor Stinner97b89882010-05-06 00:25:39 +00003216 }
3217 }
3218 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003219
Victor Stinner97b89882010-05-06 00:25:39 +00003220 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003221
Victor Stinner97b89882010-05-06 00:25:39 +00003222 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003223
Victor Stinner97b89882010-05-06 00:25:39 +00003224 free_string_array(argvlist, argc);
3225 release_bytes(opath);
3226 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003227}
3228
Victor Stinnera27dcb72010-04-25 22:39:07 +00003229static char**
3230parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3231{
Victor Stinner97b89882010-05-06 00:25:39 +00003232 char **envlist;
3233 Py_ssize_t i, pos, envc;
3234 PyObject *keys=NULL, *vals=NULL;
3235 PyObject *key, *val, *key2, *val2;
3236 char *p, *k, *v;
3237 size_t len;
Victor Stinnera27dcb72010-04-25 22:39:07 +00003238
Victor Stinner97b89882010-05-06 00:25:39 +00003239 i = PyMapping_Size(env);
3240 if (i < 0)
3241 return NULL;
3242 envlist = PyMem_NEW(char *, i + 1);
3243 if (envlist == NULL) {
3244 PyErr_NoMemory();
3245 return NULL;
3246 }
3247 envc = 0;
3248 keys = PyMapping_Keys(env);
3249 vals = PyMapping_Values(env);
3250 if (!keys || !vals)
3251 goto error;
3252 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3253 PyErr_Format(PyExc_TypeError,
3254 "env.keys() or env.values() is not a list");
3255 goto error;
3256 }
Victor Stinnera27dcb72010-04-25 22:39:07 +00003257
Victor Stinner97b89882010-05-06 00:25:39 +00003258 for (pos = 0; pos < i; pos++) {
3259 key = PyList_GetItem(keys, pos);
3260 val = PyList_GetItem(vals, pos);
3261 if (!key || !val)
3262 goto error;
Victor Stinnera27dcb72010-04-25 22:39:07 +00003263
Victor Stinner97b89882010-05-06 00:25:39 +00003264 if (PyUnicode_FSConverter(key, &key2) == 0)
3265 goto error;
3266 if (PyUnicode_FSConverter(val, &val2) == 0) {
3267 Py_DECREF(key2);
3268 goto error;
3269 }
Victor Stinnera27dcb72010-04-25 22:39:07 +00003270
3271#if defined(PYOS_OS2)
Victor Stinner97b89882010-05-06 00:25:39 +00003272 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3273 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinnera27dcb72010-04-25 22:39:07 +00003274#endif
Victor Stinner97b89882010-05-06 00:25:39 +00003275 k = PyBytes_AsString(key2);
3276 v = PyBytes_AsString(val2);
3277 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinnera27dcb72010-04-25 22:39:07 +00003278
Victor Stinner97b89882010-05-06 00:25:39 +00003279 p = PyMem_NEW(char, len);
3280 if (p == NULL) {
3281 PyErr_NoMemory();
3282 Py_DECREF(key2);
3283 Py_DECREF(val2);
3284 goto error;
3285 }
3286 PyOS_snprintf(p, len, "%s=%s", k, v);
3287 envlist[envc++] = p;
3288 Py_DECREF(key2);
3289 Py_DECREF(val2);
Victor Stinnera27dcb72010-04-25 22:39:07 +00003290#if defined(PYOS_OS2)
Victor Stinner97b89882010-05-06 00:25:39 +00003291 }
Victor Stinnera27dcb72010-04-25 22:39:07 +00003292#endif
Victor Stinner97b89882010-05-06 00:25:39 +00003293 }
3294 Py_DECREF(vals);
3295 Py_DECREF(keys);
Victor Stinnera27dcb72010-04-25 22:39:07 +00003296
Victor Stinner97b89882010-05-06 00:25:39 +00003297 envlist[envc] = 0;
3298 *envc_ptr = envc;
3299 return envlist;
Victor Stinnera27dcb72010-04-25 22:39:07 +00003300
3301error:
Victor Stinner97b89882010-05-06 00:25:39 +00003302 Py_XDECREF(keys);
3303 Py_XDECREF(vals);
3304 while (--envc >= 0)
3305 PyMem_DEL(envlist[envc]);
3306 PyMem_DEL(envlist);
3307 return NULL;
Victor Stinnera27dcb72010-04-25 22:39:07 +00003308}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003310PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003311"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003312Execute a path with arguments and environment, replacing current process.\n\
3313\n\
Victor Stinner97b89882010-05-06 00:25:39 +00003314 path: path of executable file\n\
3315 args: tuple or list of arguments\n\
3316 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003317
Barry Warsaw53699e91996-12-10 23:23:01 +00003318static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003319posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003320{
Victor Stinner97b89882010-05-06 00:25:39 +00003321 PyObject *opath;
3322 char *path;
3323 PyObject *argv, *env;
3324 char **argvlist;
3325 char **envlist;
3326 Py_ssize_t i, argc, envc;
3327 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3328 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003329
Victor Stinner97b89882010-05-06 00:25:39 +00003330 /* execve has three arguments: (path, argv, env), where
3331 argv is a list or tuple of strings and env is a dictionary
3332 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003333
Victor Stinner97b89882010-05-06 00:25:39 +00003334 if (!PyArg_ParseTuple(args, "O&OO:execve",
3335 PyUnicode_FSConverter,
3336 &opath, &argv, &env))
3337 return NULL;
3338 path = bytes2str(opath, 1);
3339 if (PyList_Check(argv)) {
3340 argc = PyList_Size(argv);
3341 getitem = PyList_GetItem;
3342 }
3343 else if (PyTuple_Check(argv)) {
3344 argc = PyTuple_Size(argv);
3345 getitem = PyTuple_GetItem;
3346 }
3347 else {
3348 PyErr_SetString(PyExc_TypeError,
3349 "execve() arg 2 must be a tuple or list");
3350 goto fail_0;
3351 }
3352 if (!PyMapping_Check(env)) {
3353 PyErr_SetString(PyExc_TypeError,
3354 "execve() arg 3 must be a mapping object");
3355 goto fail_0;
3356 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003357
Victor Stinner97b89882010-05-06 00:25:39 +00003358 argvlist = PyMem_NEW(char *, argc+1);
3359 if (argvlist == NULL) {
3360 PyErr_NoMemory();
3361 goto fail_0;
3362 }
3363 for (i = 0; i < argc; i++) {
3364 if (!fsconvert_strdup((*getitem)(argv, i),
3365 &argvlist[i]))
3366 {
3367 lastarg = i;
3368 goto fail_1;
3369 }
3370 }
3371 lastarg = argc;
3372 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003373
Victor Stinner97b89882010-05-06 00:25:39 +00003374 envlist = parse_envlist(env, &envc);
3375 if (envlist == NULL)
3376 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003377
Victor Stinner97b89882010-05-06 00:25:39 +00003378 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003379
Victor Stinner97b89882010-05-06 00:25:39 +00003380 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003381
Victor Stinner97b89882010-05-06 00:25:39 +00003382 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003383
Victor Stinner97b89882010-05-06 00:25:39 +00003384 while (--envc >= 0)
3385 PyMem_DEL(envlist[envc]);
3386 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003387 fail_1:
Victor Stinner97b89882010-05-06 00:25:39 +00003388 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003389 fail_0:
Victor Stinner97b89882010-05-06 00:25:39 +00003390 release_bytes(opath);
3391 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003392}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003393#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003394
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003395
Guido van Rossuma1065681999-01-25 23:20:23 +00003396#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003397PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003398"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003399Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003400\n\
Victor Stinner97b89882010-05-06 00:25:39 +00003401 mode: mode of process creation\n\
3402 path: path of executable file\n\
3403 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003404
3405static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003406posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003407{
Victor Stinner97b89882010-05-06 00:25:39 +00003408 PyObject *opath;
3409 char *path;
3410 PyObject *argv;
3411 char **argvlist;
3412 int mode, i;
3413 Py_ssize_t argc;
3414 Py_intptr_t spawnval;
3415 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003416
Victor Stinner97b89882010-05-06 00:25:39 +00003417 /* spawnv has three arguments: (mode, path, argv), where
3418 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003419
Victor Stinner97b89882010-05-06 00:25:39 +00003420 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3421 PyUnicode_FSConverter,
3422 &opath, &argv))
3423 return NULL;
3424 path = bytes2str(opath, 1);
3425 if (PyList_Check(argv)) {
3426 argc = PyList_Size(argv);
3427 getitem = PyList_GetItem;
3428 }
3429 else if (PyTuple_Check(argv)) {
3430 argc = PyTuple_Size(argv);
3431 getitem = PyTuple_GetItem;
3432 }
3433 else {
3434 PyErr_SetString(PyExc_TypeError,
3435 "spawnv() arg 2 must be a tuple or list");
3436 release_bytes(opath);
3437 return NULL;
3438 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003439
Victor Stinner97b89882010-05-06 00:25:39 +00003440 argvlist = PyMem_NEW(char *, argc+1);
3441 if (argvlist == NULL) {
3442 release_bytes(opath);
3443 return PyErr_NoMemory();
3444 }
3445 for (i = 0; i < argc; i++) {
3446 if (!fsconvert_strdup((*getitem)(argv, i),
3447 &argvlist[i])) {
3448 free_string_array(argvlist, i);
3449 PyErr_SetString(
3450 PyExc_TypeError,
3451 "spawnv() arg 2 must contain only strings");
3452 release_bytes(opath);
3453 return NULL;
3454 }
3455 }
3456 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003457
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003458#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner97b89882010-05-06 00:25:39 +00003459 Py_BEGIN_ALLOW_THREADS
3460 spawnval = spawnv(mode, path, argvlist);
3461 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003462#else
Victor Stinner97b89882010-05-06 00:25:39 +00003463 if (mode == _OLD_P_OVERLAY)
3464 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003465
Victor Stinner97b89882010-05-06 00:25:39 +00003466 Py_BEGIN_ALLOW_THREADS
3467 spawnval = _spawnv(mode, path, argvlist);
3468 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003469#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003470
Victor Stinner97b89882010-05-06 00:25:39 +00003471 free_string_array(argvlist, argc);
3472 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003473
Victor Stinner97b89882010-05-06 00:25:39 +00003474 if (spawnval == -1)
3475 return posix_error();
3476 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003477#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner97b89882010-05-06 00:25:39 +00003478 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003479#else
Victor Stinner97b89882010-05-06 00:25:39 +00003480 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003481#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003482}
3483
3484
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003485PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003486"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003487Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003488\n\
Victor Stinner97b89882010-05-06 00:25:39 +00003489 mode: mode of process creation\n\
3490 path: path of executable file\n\
3491 args: tuple or list of arguments\n\
3492 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003493
3494static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003495posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003496{
Victor Stinner97b89882010-05-06 00:25:39 +00003497 PyObject *opath;
3498 char *path;
3499 PyObject *argv, *env;
3500 char **argvlist;
3501 char **envlist;
3502 PyObject *res = NULL;
Antoine Pitrou835b4452010-08-15 18:32:16 +00003503 int mode;
3504 Py_ssize_t argc, i, envc;
Victor Stinner97b89882010-05-06 00:25:39 +00003505 Py_intptr_t spawnval;
3506 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3507 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003508
Victor Stinner97b89882010-05-06 00:25:39 +00003509 /* spawnve has four arguments: (mode, path, argv, env), where
3510 argv is a list or tuple of strings and env is a dictionary
3511 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003512
Victor Stinner97b89882010-05-06 00:25:39 +00003513 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3514 PyUnicode_FSConverter,
3515 &opath, &argv, &env))
3516 return NULL;
3517 path = bytes2str(opath, 1);
3518 if (PyList_Check(argv)) {
3519 argc = PyList_Size(argv);
3520 getitem = PyList_GetItem;
3521 }
3522 else if (PyTuple_Check(argv)) {
3523 argc = PyTuple_Size(argv);
3524 getitem = PyTuple_GetItem;
3525 }
3526 else {
3527 PyErr_SetString(PyExc_TypeError,
3528 "spawnve() arg 2 must be a tuple or list");
3529 goto fail_0;
3530 }
3531 if (!PyMapping_Check(env)) {
3532 PyErr_SetString(PyExc_TypeError,
3533 "spawnve() arg 3 must be a mapping object");
3534 goto fail_0;
3535 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003536
Victor Stinner97b89882010-05-06 00:25:39 +00003537 argvlist = PyMem_NEW(char *, argc+1);
3538 if (argvlist == NULL) {
3539 PyErr_NoMemory();
3540 goto fail_0;
3541 }
3542 for (i = 0; i < argc; i++) {
3543 if (!fsconvert_strdup((*getitem)(argv, i),
3544 &argvlist[i]))
3545 {
3546 lastarg = i;
3547 goto fail_1;
3548 }
3549 }
3550 lastarg = argc;
3551 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003552
Victor Stinner97b89882010-05-06 00:25:39 +00003553 envlist = parse_envlist(env, &envc);
3554 if (envlist == NULL)
3555 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003556
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003557#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner97b89882010-05-06 00:25:39 +00003558 Py_BEGIN_ALLOW_THREADS
3559 spawnval = spawnve(mode, path, argvlist, envlist);
3560 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003561#else
Victor Stinner97b89882010-05-06 00:25:39 +00003562 if (mode == _OLD_P_OVERLAY)
3563 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003564
Victor Stinner97b89882010-05-06 00:25:39 +00003565 Py_BEGIN_ALLOW_THREADS
3566 spawnval = _spawnve(mode, path, argvlist, envlist);
3567 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003568#endif
Tim Peters25059d32001-12-07 20:35:43 +00003569
Victor Stinner97b89882010-05-06 00:25:39 +00003570 if (spawnval == -1)
3571 (void) posix_error();
3572 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003573#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner97b89882010-05-06 00:25:39 +00003574 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003575#else
Victor Stinner97b89882010-05-06 00:25:39 +00003576 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003577#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003578
Victor Stinner97b89882010-05-06 00:25:39 +00003579 while (--envc >= 0)
3580 PyMem_DEL(envlist[envc]);
3581 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003582 fail_1:
Victor Stinner97b89882010-05-06 00:25:39 +00003583 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003584 fail_0:
Victor Stinner97b89882010-05-06 00:25:39 +00003585 release_bytes(opath);
3586 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003587}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003588
3589/* OS/2 supports spawnvp & spawnvpe natively */
3590#if defined(PYOS_OS2)
3591PyDoc_STRVAR(posix_spawnvp__doc__,
3592"spawnvp(mode, file, args)\n\n\
3593Execute the program 'file' in a new process, using the environment\n\
3594search path to find the file.\n\
3595\n\
Victor Stinner97b89882010-05-06 00:25:39 +00003596 mode: mode of process creation\n\
3597 file: executable file name\n\
3598 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003599
3600static PyObject *
3601posix_spawnvp(PyObject *self, PyObject *args)
3602{
Victor Stinner97b89882010-05-06 00:25:39 +00003603 PyObject *opath;
3604 char *path;
3605 PyObject *argv;
3606 char **argvlist;
3607 int mode, i, argc;
3608 Py_intptr_t spawnval;
3609 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003610
Victor Stinner97b89882010-05-06 00:25:39 +00003611 /* spawnvp has three arguments: (mode, path, argv), where
3612 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003613
Victor Stinner97b89882010-05-06 00:25:39 +00003614 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3615 PyUnicode_FSConverter,
3616 &opath, &argv))
3617 return NULL;
3618 path = bytes2str(opath);
3619 if (PyList_Check(argv)) {
3620 argc = PyList_Size(argv);
3621 getitem = PyList_GetItem;
3622 }
3623 else if (PyTuple_Check(argv)) {
3624 argc = PyTuple_Size(argv);
3625 getitem = PyTuple_GetItem;
3626 }
3627 else {
3628 PyErr_SetString(PyExc_TypeError,
3629 "spawnvp() arg 2 must be a tuple or list");
3630 release_bytes(opath);
3631 return NULL;
3632 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003633
Victor Stinner97b89882010-05-06 00:25:39 +00003634 argvlist = PyMem_NEW(char *, argc+1);
3635 if (argvlist == NULL) {
3636 release_bytes(opath);
3637 return PyErr_NoMemory();
3638 }
3639 for (i = 0; i < argc; i++) {
3640 if (!fsconvert_strdup((*getitem)(argv, i),
3641 &argvlist[i])) {
3642 free_string_array(argvlist, i);
3643 PyErr_SetString(
3644 PyExc_TypeError,
3645 "spawnvp() arg 2 must contain only strings");
3646 release_bytes(opath);
3647 return NULL;
3648 }
3649 }
3650 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003651
Victor Stinner97b89882010-05-06 00:25:39 +00003652 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003653#if defined(PYCC_GCC)
Victor Stinner97b89882010-05-06 00:25:39 +00003654 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003655#else
Victor Stinner97b89882010-05-06 00:25:39 +00003656 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003657#endif
Victor Stinner97b89882010-05-06 00:25:39 +00003658 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003659
Victor Stinner97b89882010-05-06 00:25:39 +00003660 free_string_array(argvlist, argc);
3661 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003662
Victor Stinner97b89882010-05-06 00:25:39 +00003663 if (spawnval == -1)
3664 return posix_error();
3665 else
3666 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003667}
3668
3669
3670PyDoc_STRVAR(posix_spawnvpe__doc__,
3671"spawnvpe(mode, file, args, env)\n\n\
3672Execute the program 'file' in a new process, using the environment\n\
3673search path to find the file.\n\
3674\n\
Victor Stinner97b89882010-05-06 00:25:39 +00003675 mode: mode of process creation\n\
3676 file: executable file name\n\
3677 args: tuple or list of arguments\n\
3678 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003679
3680static PyObject *
3681posix_spawnvpe(PyObject *self, PyObject *args)
3682{
Victor Stinner97b89882010-05-06 00:25:39 +00003683 PyObject *opath
3684 char *path;
3685 PyObject *argv, *env;
3686 char **argvlist;
3687 char **envlist;
3688 PyObject *res=NULL;
Antoine Pitrou835b4452010-08-15 18:32:16 +00003689 int mode;
3690 Py_ssize_t argc, i, envc;
Victor Stinner97b89882010-05-06 00:25:39 +00003691 Py_intptr_t spawnval;
3692 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3693 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003694
Victor Stinner97b89882010-05-06 00:25:39 +00003695 /* spawnvpe has four arguments: (mode, path, argv, env), where
3696 argv is a list or tuple of strings and env is a dictionary
3697 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003698
Victor Stinner97b89882010-05-06 00:25:39 +00003699 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3700 PyUnicode_FSConverter,
3701 &opath, &argv, &env))
3702 return NULL;
3703 path = bytes2str(opath);
3704 if (PyList_Check(argv)) {
3705 argc = PyList_Size(argv);
3706 getitem = PyList_GetItem;
3707 }
3708 else if (PyTuple_Check(argv)) {
3709 argc = PyTuple_Size(argv);
3710 getitem = PyTuple_GetItem;
3711 }
3712 else {
3713 PyErr_SetString(PyExc_TypeError,
3714 "spawnvpe() arg 2 must be a tuple or list");
3715 goto fail_0;
3716 }
3717 if (!PyMapping_Check(env)) {
3718 PyErr_SetString(PyExc_TypeError,
3719 "spawnvpe() arg 3 must be a mapping object");
3720 goto fail_0;
3721 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003722
Victor Stinner97b89882010-05-06 00:25:39 +00003723 argvlist = PyMem_NEW(char *, argc+1);
3724 if (argvlist == NULL) {
3725 PyErr_NoMemory();
3726 goto fail_0;
3727 }
3728 for (i = 0; i < argc; i++) {
3729 if (!fsconvert_strdup((*getitem)(argv, i),
3730 &argvlist[i]))
3731 {
3732 lastarg = i;
3733 goto fail_1;
3734 }
3735 }
3736 lastarg = argc;
3737 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003738
Victor Stinner97b89882010-05-06 00:25:39 +00003739 envlist = parse_envlist(env, &envc);
3740 if (envlist == NULL)
3741 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003742
Victor Stinner97b89882010-05-06 00:25:39 +00003743 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003744#if defined(PYCC_GCC)
Victor Stinner97b89882010-05-06 00:25:39 +00003745 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003746#else
Victor Stinner97b89882010-05-06 00:25:39 +00003747 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003748#endif
Victor Stinner97b89882010-05-06 00:25:39 +00003749 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003750
Victor Stinner97b89882010-05-06 00:25:39 +00003751 if (spawnval == -1)
3752 (void) posix_error();
3753 else
3754 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003755
Victor Stinner97b89882010-05-06 00:25:39 +00003756 while (--envc >= 0)
3757 PyMem_DEL(envlist[envc]);
3758 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003759 fail_1:
Victor Stinner97b89882010-05-06 00:25:39 +00003760 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003761 fail_0:
Victor Stinner97b89882010-05-06 00:25:39 +00003762 release_bytes(opath);
3763 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003764}
3765#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003766#endif /* HAVE_SPAWNV */
3767
3768
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003769#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003770PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003771"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003772Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3773\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003774Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003775
3776static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003777posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003778{
Victor Stinner97b89882010-05-06 00:25:39 +00003779 pid_t pid;
3780 int result;
3781 _PyImport_AcquireLock();
3782 pid = fork1();
3783 result = _PyImport_ReleaseLock();
3784 if (pid == -1)
3785 return posix_error();
3786 if (pid == 0)
3787 PyOS_AfterFork();
3788 if (result < 0) {
3789 /* Don't clobber the OSError if the fork failed. */
3790 PyErr_SetString(PyExc_RuntimeError,
3791 "not holding the import lock");
3792 return NULL;
3793 }
3794 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003795}
3796#endif
3797
3798
Guido van Rossumad0ee831995-03-01 10:34:45 +00003799#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003800PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003801"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003802Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003803Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003804
Barry Warsaw53699e91996-12-10 23:23:01 +00003805static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003806posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003807{
Victor Stinner97b89882010-05-06 00:25:39 +00003808 pid_t pid;
3809 int result;
3810 _PyImport_AcquireLock();
3811 pid = fork();
3812 result = _PyImport_ReleaseLock();
3813 if (pid == -1)
3814 return posix_error();
3815 if (pid == 0)
3816 PyOS_AfterFork();
3817 if (result < 0) {
3818 /* Don't clobber the OSError if the fork failed. */
3819 PyErr_SetString(PyExc_RuntimeError,
3820 "not holding the import lock");
3821 return NULL;
3822 }
3823 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003824}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003825#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003826
Neal Norwitzb59798b2003-03-21 01:43:31 +00003827/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003828/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3829#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003830#define DEV_PTY_FILE "/dev/ptc"
3831#define HAVE_DEV_PTMX
3832#else
3833#define DEV_PTY_FILE "/dev/ptmx"
3834#endif
3835
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003836#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003837#ifdef HAVE_PTY_H
3838#include <pty.h>
3839#else
3840#ifdef HAVE_LIBUTIL_H
3841#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003842#endif /* HAVE_LIBUTIL_H */
3843#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003844#ifdef HAVE_STROPTS_H
3845#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003846#endif
3847#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003848
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003849#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003850PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003851"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003852Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003853
3854static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003855posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003856{
Victor Stinner97b89882010-05-06 00:25:39 +00003857 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003858#ifndef HAVE_OPENPTY
Victor Stinner97b89882010-05-06 00:25:39 +00003859 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003860#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003861#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner97b89882010-05-06 00:25:39 +00003862 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003863#ifdef sun
Victor Stinner97b89882010-05-06 00:25:39 +00003864 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003865#endif
3866#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003867
Thomas Wouters70c21a12000-07-14 14:28:33 +00003868#ifdef HAVE_OPENPTY
Victor Stinner97b89882010-05-06 00:25:39 +00003869 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3870 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003871#elif defined(HAVE__GETPTY)
Victor Stinner97b89882010-05-06 00:25:39 +00003872 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3873 if (slave_name == NULL)
3874 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00003875
Victor Stinner97b89882010-05-06 00:25:39 +00003876 slave_fd = open(slave_name, O_RDWR);
3877 if (slave_fd < 0)
3878 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003879#else
Victor Stinner97b89882010-05-06 00:25:39 +00003880 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3881 if (master_fd < 0)
3882 return posix_error();
3883 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3884 /* change permission of slave */
3885 if (grantpt(master_fd) < 0) {
3886 PyOS_setsig(SIGCHLD, sig_saved);
3887 return posix_error();
3888 }
3889 /* unlock slave */
3890 if (unlockpt(master_fd) < 0) {
3891 PyOS_setsig(SIGCHLD, sig_saved);
3892 return posix_error();
3893 }
3894 PyOS_setsig(SIGCHLD, sig_saved);
3895 slave_name = ptsname(master_fd); /* get name of slave */
3896 if (slave_name == NULL)
3897 return posix_error();
3898 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3899 if (slave_fd < 0)
3900 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003901#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner97b89882010-05-06 00:25:39 +00003902 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3903 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003904#ifndef __hpux
Victor Stinner97b89882010-05-06 00:25:39 +00003905 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003906#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003907#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003908#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003909
Victor Stinner97b89882010-05-06 00:25:39 +00003910 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003911
Fred Drake8cef4cf2000-06-28 16:40:38 +00003912}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003913#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003914
3915#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003916PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003917"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003918Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3919Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003920To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003921
3922static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003923posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003924{
Victor Stinner97b89882010-05-06 00:25:39 +00003925 int master_fd = -1, result;
3926 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003927
Victor Stinner97b89882010-05-06 00:25:39 +00003928 _PyImport_AcquireLock();
3929 pid = forkpty(&master_fd, NULL, NULL, NULL);
3930 result = _PyImport_ReleaseLock();
3931 if (pid == -1)
3932 return posix_error();
3933 if (pid == 0)
3934 PyOS_AfterFork();
3935 if (result < 0) {
3936 /* Don't clobber the OSError if the fork failed. */
3937 PyErr_SetString(PyExc_RuntimeError,
3938 "not holding the import lock");
3939 return NULL;
3940 }
3941 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003942}
3943#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003944
Guido van Rossumad0ee831995-03-01 10:34:45 +00003945#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003946PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003947"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003948Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003949
Barry Warsaw53699e91996-12-10 23:23:01 +00003950static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003951posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003952{
Victor Stinner97b89882010-05-06 00:25:39 +00003953 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003954}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003955#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003956
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003957
Guido van Rossumad0ee831995-03-01 10:34:45 +00003958#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003959PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003960"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003961Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003962
Barry Warsaw53699e91996-12-10 23:23:01 +00003963static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003964posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003965{
Victor Stinner97b89882010-05-06 00:25:39 +00003966 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003967}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003968#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003969
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003970
Guido van Rossumad0ee831995-03-01 10:34:45 +00003971#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003972PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003973"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003974Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003975
Barry Warsaw53699e91996-12-10 23:23:01 +00003976static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003977posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003978{
Victor Stinner97b89882010-05-06 00:25:39 +00003979 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003980}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003981#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003982
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003983
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003984PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003985"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003986Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003987
Barry Warsaw53699e91996-12-10 23:23:01 +00003988static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003989posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003990{
Victor Stinner97b89882010-05-06 00:25:39 +00003991 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003992}
3993
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003994
Fred Drakec9680921999-12-13 16:37:25 +00003995#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003996PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003997"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003998Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003999
4000static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004001posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004002{
4003 PyObject *result = NULL;
4004
Fred Drakec9680921999-12-13 16:37:25 +00004005#ifdef NGROUPS_MAX
4006#define MAX_GROUPS NGROUPS_MAX
4007#else
Victor Stinner97b89882010-05-06 00:25:39 +00004008 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004009#define MAX_GROUPS 64
4010#endif
Victor Stinner97b89882010-05-06 00:25:39 +00004011 gid_t grouplist[MAX_GROUPS];
Ronald Oussoren47076f72010-07-23 15:46:03 +00004012
4013 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4014 * This is a helper variable to store the intermediate result when
4015 * that happens.
4016 *
4017 * To keep the code readable the OSX behaviour is unconditional,
4018 * according to the POSIX spec this should be safe on all unix-y
4019 * systems.
4020 */
4021 gid_t* alt_grouplist = grouplist;
Victor Stinner97b89882010-05-06 00:25:39 +00004022 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004023
Victor Stinner97b89882010-05-06 00:25:39 +00004024 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussoren47076f72010-07-23 15:46:03 +00004025 if (n < 0) {
4026 if (errno == EINVAL) {
4027 n = getgroups(0, NULL);
4028 if (n == -1) {
4029 return posix_error();
4030 }
4031 if (n == 0) {
4032 /* Avoid malloc(0) */
4033 alt_grouplist = grouplist;
4034 } else {
4035 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4036 if (alt_grouplist == NULL) {
4037 errno = EINVAL;
4038 return posix_error();
4039 }
4040 n = getgroups(n, alt_grouplist);
4041 if (n == -1) {
4042 PyMem_Free(alt_grouplist);
4043 return posix_error();
4044 }
4045 }
4046 } else {
4047 return posix_error();
4048 }
4049 }
4050 result = PyList_New(n);
4051 if (result != NULL) {
Victor Stinner97b89882010-05-06 00:25:39 +00004052 int i;
4053 for (i = 0; i < n; ++i) {
Ronald Oussoren47076f72010-07-23 15:46:03 +00004054 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner97b89882010-05-06 00:25:39 +00004055 if (o == NULL) {
4056 Py_DECREF(result);
4057 result = NULL;
4058 break;
Fred Drakec9680921999-12-13 16:37:25 +00004059 }
Victor Stinner97b89882010-05-06 00:25:39 +00004060 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004061 }
Ronald Oussoren47076f72010-07-23 15:46:03 +00004062 }
4063
4064 if (alt_grouplist != grouplist) {
4065 PyMem_Free(alt_grouplist);
Victor Stinner97b89882010-05-06 00:25:39 +00004066 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004067
Fred Drakec9680921999-12-13 16:37:25 +00004068 return result;
4069}
4070#endif
4071
Martin v. Löwis606edc12002-06-13 21:09:11 +00004072#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004073PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004074"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004075Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004076
4077static PyObject *
4078posix_getpgid(PyObject *self, PyObject *args)
4079{
Victor Stinner97b89882010-05-06 00:25:39 +00004080 pid_t pid, pgid;
4081 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
4082 return NULL;
4083 pgid = getpgid(pid);
4084 if (pgid < 0)
4085 return posix_error();
4086 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004087}
4088#endif /* HAVE_GETPGID */
4089
4090
Guido van Rossumb6775db1994-08-01 11:34:53 +00004091#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004092PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004093"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004094Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004095
Barry Warsaw53699e91996-12-10 23:23:01 +00004096static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004097posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004098{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004099#ifdef GETPGRP_HAVE_ARG
Victor Stinner97b89882010-05-06 00:25:39 +00004100 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004101#else /* GETPGRP_HAVE_ARG */
Victor Stinner97b89882010-05-06 00:25:39 +00004102 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004103#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004104}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004105#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004106
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004107
Guido van Rossumb6775db1994-08-01 11:34:53 +00004108#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004109PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004110"setpgrp()\n\n\
Senthil Kumaran28fdadb2010-06-17 16:51:08 +00004111Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004112
Barry Warsaw53699e91996-12-10 23:23:01 +00004113static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004114posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004115{
Guido van Rossum64933891994-10-20 21:56:42 +00004116#ifdef SETPGRP_HAVE_ARG
Victor Stinner97b89882010-05-06 00:25:39 +00004117 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004118#else /* SETPGRP_HAVE_ARG */
Victor Stinner97b89882010-05-06 00:25:39 +00004119 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004120#endif /* SETPGRP_HAVE_ARG */
Victor Stinner97b89882010-05-06 00:25:39 +00004121 return posix_error();
4122 Py_INCREF(Py_None);
4123 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004124}
4125
Guido van Rossumb6775db1994-08-01 11:34:53 +00004126#endif /* HAVE_SETPGRP */
4127
Guido van Rossumad0ee831995-03-01 10:34:45 +00004128#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004129PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004130"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004131Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004132
Barry Warsaw53699e91996-12-10 23:23:01 +00004133static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004134posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004135{
Victor Stinner97b89882010-05-06 00:25:39 +00004136 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004137}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004138#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004139
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004140
Fred Drake12c6e2d1999-12-14 21:25:03 +00004141#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004142PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004143"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004144Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004145
4146static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004147posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004148{
Victor Stinner97b89882010-05-06 00:25:39 +00004149 PyObject *result = NULL;
4150 char *name;
4151 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004152
Victor Stinner97b89882010-05-06 00:25:39 +00004153 errno = 0;
4154 name = getlogin();
4155 if (name == NULL) {
4156 if (errno)
Victor Stinner85675992010-08-15 09:35:13 +00004157 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004158 else
Victor Stinner85675992010-08-15 09:35:13 +00004159 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner97b89882010-05-06 00:25:39 +00004160 }
4161 else
Victor Stinner85675992010-08-15 09:35:13 +00004162 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner97b89882010-05-06 00:25:39 +00004163 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004164
Fred Drake12c6e2d1999-12-14 21:25:03 +00004165 return result;
4166}
4167#endif
4168
Guido van Rossumad0ee831995-03-01 10:34:45 +00004169#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004170PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004171"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004172Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004173
Barry Warsaw53699e91996-12-10 23:23:01 +00004174static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004175posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004176{
Victor Stinner97b89882010-05-06 00:25:39 +00004177 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004178}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004179#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004180
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004181
Guido van Rossumad0ee831995-03-01 10:34:45 +00004182#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004183PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004184"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004185Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004186
Barry Warsaw53699e91996-12-10 23:23:01 +00004187static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004188posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004189{
Victor Stinner97b89882010-05-06 00:25:39 +00004190 pid_t pid;
4191 int sig;
4192 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
4193 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004194#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004195 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4196 APIRET rc;
4197 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004198 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004199
4200 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4201 APIRET rc;
4202 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004203 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004204
4205 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004206 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004207#else
Victor Stinner97b89882010-05-06 00:25:39 +00004208 if (kill(pid, sig) == -1)
4209 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004210#endif
Victor Stinner97b89882010-05-06 00:25:39 +00004211 Py_INCREF(Py_None);
4212 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004213}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004214#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004215
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004216#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004217PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004218"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004219Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004220
4221static PyObject *
4222posix_killpg(PyObject *self, PyObject *args)
4223{
Victor Stinner97b89882010-05-06 00:25:39 +00004224 int sig;
4225 pid_t pgid;
4226 /* XXX some man pages make the `pgid` parameter an int, others
4227 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4228 take the same type. Moreover, pid_t is always at least as wide as
4229 int (else compilation of this module fails), which is safe. */
4230 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
4231 return NULL;
4232 if (killpg(pgid, sig) == -1)
4233 return posix_error();
4234 Py_INCREF(Py_None);
4235 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004236}
4237#endif
4238
Guido van Rossumc0125471996-06-28 18:55:32 +00004239#ifdef HAVE_PLOCK
4240
4241#ifdef HAVE_SYS_LOCK_H
4242#include <sys/lock.h>
4243#endif
4244
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004245PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004246"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004247Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004248
Barry Warsaw53699e91996-12-10 23:23:01 +00004249static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004250posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004251{
Victor Stinner97b89882010-05-06 00:25:39 +00004252 int op;
4253 if (!PyArg_ParseTuple(args, "i:plock", &op))
4254 return NULL;
4255 if (plock(op) == -1)
4256 return posix_error();
4257 Py_INCREF(Py_None);
4258 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004259}
4260#endif
4261
Guido van Rossumb6775db1994-08-01 11:34:53 +00004262#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004263PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004264"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004265Set the current process's user id.");
4266
Barry Warsaw53699e91996-12-10 23:23:01 +00004267static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004268posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004269{
Victor Stinner97b89882010-05-06 00:25:39 +00004270 long uid_arg;
4271 uid_t uid;
4272 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4273 return NULL;
4274 uid = uid_arg;
4275 if (uid != uid_arg) {
4276 PyErr_SetString(PyExc_OverflowError, "user id too big");
4277 return NULL;
4278 }
4279 if (setuid(uid) < 0)
4280 return posix_error();
4281 Py_INCREF(Py_None);
4282 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004283}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004284#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004285
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004286
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004287#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004288PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004289"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004290Set the current process's effective user id.");
4291
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004292static PyObject *
4293posix_seteuid (PyObject *self, PyObject *args)
4294{
Victor Stinner97b89882010-05-06 00:25:39 +00004295 long euid_arg;
4296 uid_t euid;
4297 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4298 return NULL;
4299 euid = euid_arg;
4300 if (euid != euid_arg) {
4301 PyErr_SetString(PyExc_OverflowError, "user id too big");
4302 return NULL;
4303 }
4304 if (seteuid(euid) < 0) {
4305 return posix_error();
4306 } else {
4307 Py_INCREF(Py_None);
4308 return Py_None;
4309 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004310}
4311#endif /* HAVE_SETEUID */
4312
4313#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004314PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004315"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004316Set the current process's effective group id.");
4317
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004318static PyObject *
4319posix_setegid (PyObject *self, PyObject *args)
4320{
Victor Stinner97b89882010-05-06 00:25:39 +00004321 long egid_arg;
4322 gid_t egid;
4323 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4324 return NULL;
4325 egid = egid_arg;
4326 if (egid != egid_arg) {
4327 PyErr_SetString(PyExc_OverflowError, "group id too big");
4328 return NULL;
4329 }
4330 if (setegid(egid) < 0) {
4331 return posix_error();
4332 } else {
4333 Py_INCREF(Py_None);
4334 return Py_None;
4335 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004336}
4337#endif /* HAVE_SETEGID */
4338
4339#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004340PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004341"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004342Set the current process's real and effective user ids.");
4343
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004344static PyObject *
4345posix_setreuid (PyObject *self, PyObject *args)
4346{
Victor Stinner97b89882010-05-06 00:25:39 +00004347 long ruid_arg, euid_arg;
4348 uid_t ruid, euid;
4349 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4350 return NULL;
4351 if (ruid_arg == -1)
4352 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4353 else
4354 ruid = ruid_arg; /* otherwise, assign from our long */
4355 if (euid_arg == -1)
4356 euid = (uid_t)-1;
4357 else
4358 euid = euid_arg;
4359 if ((euid_arg != -1 && euid != euid_arg) ||
4360 (ruid_arg != -1 && ruid != ruid_arg)) {
4361 PyErr_SetString(PyExc_OverflowError, "user id too big");
4362 return NULL;
4363 }
4364 if (setreuid(ruid, euid) < 0) {
4365 return posix_error();
4366 } else {
4367 Py_INCREF(Py_None);
4368 return Py_None;
4369 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004370}
4371#endif /* HAVE_SETREUID */
4372
4373#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004374PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004375"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004376Set the current process's real and effective group ids.");
4377
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004378static PyObject *
4379posix_setregid (PyObject *self, PyObject *args)
4380{
Victor Stinner97b89882010-05-06 00:25:39 +00004381 long rgid_arg, egid_arg;
4382 gid_t rgid, egid;
4383 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4384 return NULL;
4385 if (rgid_arg == -1)
4386 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4387 else
4388 rgid = rgid_arg; /* otherwise, assign from our long */
4389 if (egid_arg == -1)
4390 egid = (gid_t)-1;
4391 else
4392 egid = egid_arg;
4393 if ((egid_arg != -1 && egid != egid_arg) ||
4394 (rgid_arg != -1 && rgid != rgid_arg)) {
4395 PyErr_SetString(PyExc_OverflowError, "group id too big");
4396 return NULL;
4397 }
4398 if (setregid(rgid, egid) < 0) {
4399 return posix_error();
4400 } else {
4401 Py_INCREF(Py_None);
4402 return Py_None;
4403 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004404}
4405#endif /* HAVE_SETREGID */
4406
Guido van Rossumb6775db1994-08-01 11:34:53 +00004407#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004408PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004409"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004410Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004411
Barry Warsaw53699e91996-12-10 23:23:01 +00004412static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004413posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004414{
Victor Stinner97b89882010-05-06 00:25:39 +00004415 long gid_arg;
4416 gid_t gid;
4417 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4418 return NULL;
4419 gid = gid_arg;
4420 if (gid != gid_arg) {
4421 PyErr_SetString(PyExc_OverflowError, "group id too big");
4422 return NULL;
4423 }
4424 if (setgid(gid) < 0)
4425 return posix_error();
4426 Py_INCREF(Py_None);
4427 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004428}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004429#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004430
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004431#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004432PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004433"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004434Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004435
4436static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004437posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004438{
Victor Stinner97b89882010-05-06 00:25:39 +00004439 int i, len;
4440 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004441
Victor Stinner97b89882010-05-06 00:25:39 +00004442 if (!PySequence_Check(groups)) {
4443 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4444 return NULL;
4445 }
4446 len = PySequence_Size(groups);
4447 if (len > MAX_GROUPS) {
4448 PyErr_SetString(PyExc_ValueError, "too many groups");
4449 return NULL;
4450 }
4451 for(i = 0; i < len; i++) {
4452 PyObject *elem;
4453 elem = PySequence_GetItem(groups, i);
4454 if (!elem)
4455 return NULL;
4456 if (!PyLong_Check(elem)) {
4457 PyErr_SetString(PyExc_TypeError,
4458 "groups must be integers");
4459 Py_DECREF(elem);
4460 return NULL;
4461 } else {
4462 unsigned long x = PyLong_AsUnsignedLong(elem);
4463 if (PyErr_Occurred()) {
4464 PyErr_SetString(PyExc_TypeError,
4465 "group id too big");
4466 Py_DECREF(elem);
4467 return NULL;
4468 }
4469 grouplist[i] = x;
4470 /* read back the value to see if it fitted in gid_t */
4471 if (grouplist[i] != x) {
4472 PyErr_SetString(PyExc_TypeError,
4473 "group id too big");
4474 Py_DECREF(elem);
4475 return NULL;
4476 }
4477 }
4478 Py_DECREF(elem);
4479 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004480
Victor Stinner97b89882010-05-06 00:25:39 +00004481 if (setgroups(len, grouplist) < 0)
4482 return posix_error();
4483 Py_INCREF(Py_None);
4484 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004485}
4486#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004487
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004488#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4489static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004490wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004491{
Victor Stinner97b89882010-05-06 00:25:39 +00004492 PyObject *result;
4493 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004494
Victor Stinner97b89882010-05-06 00:25:39 +00004495 if (pid == -1)
4496 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004497
Victor Stinner97b89882010-05-06 00:25:39 +00004498 if (struct_rusage == NULL) {
4499 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4500 if (m == NULL)
4501 return NULL;
4502 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4503 Py_DECREF(m);
4504 if (struct_rusage == NULL)
4505 return NULL;
4506 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004507
Victor Stinner97b89882010-05-06 00:25:39 +00004508 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4509 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4510 if (!result)
4511 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004512
4513#ifndef doubletime
4514#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4515#endif
4516
Victor Stinner97b89882010-05-06 00:25:39 +00004517 PyStructSequence_SET_ITEM(result, 0,
4518 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4519 PyStructSequence_SET_ITEM(result, 1,
4520 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004521#define SET_INT(result, index, value)\
Victor Stinner97b89882010-05-06 00:25:39 +00004522 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4523 SET_INT(result, 2, ru->ru_maxrss);
4524 SET_INT(result, 3, ru->ru_ixrss);
4525 SET_INT(result, 4, ru->ru_idrss);
4526 SET_INT(result, 5, ru->ru_isrss);
4527 SET_INT(result, 6, ru->ru_minflt);
4528 SET_INT(result, 7, ru->ru_majflt);
4529 SET_INT(result, 8, ru->ru_nswap);
4530 SET_INT(result, 9, ru->ru_inblock);
4531 SET_INT(result, 10, ru->ru_oublock);
4532 SET_INT(result, 11, ru->ru_msgsnd);
4533 SET_INT(result, 12, ru->ru_msgrcv);
4534 SET_INT(result, 13, ru->ru_nsignals);
4535 SET_INT(result, 14, ru->ru_nvcsw);
4536 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004537#undef SET_INT
4538
Victor Stinner97b89882010-05-06 00:25:39 +00004539 if (PyErr_Occurred()) {
4540 Py_DECREF(result);
4541 return NULL;
4542 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004543
Victor Stinner97b89882010-05-06 00:25:39 +00004544 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004545}
4546#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4547
4548#ifdef HAVE_WAIT3
4549PyDoc_STRVAR(posix_wait3__doc__,
4550"wait3(options) -> (pid, status, rusage)\n\n\
4551Wait for completion of a child process.");
4552
4553static PyObject *
4554posix_wait3(PyObject *self, PyObject *args)
4555{
Victor Stinner97b89882010-05-06 00:25:39 +00004556 pid_t pid;
4557 int options;
4558 struct rusage ru;
4559 WAIT_TYPE status;
4560 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004561
Victor Stinner97b89882010-05-06 00:25:39 +00004562 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4563 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004564
Victor Stinner97b89882010-05-06 00:25:39 +00004565 Py_BEGIN_ALLOW_THREADS
4566 pid = wait3(&status, options, &ru);
4567 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004568
Victor Stinner97b89882010-05-06 00:25:39 +00004569 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004570}
4571#endif /* HAVE_WAIT3 */
4572
4573#ifdef HAVE_WAIT4
4574PyDoc_STRVAR(posix_wait4__doc__,
4575"wait4(pid, options) -> (pid, status, rusage)\n\n\
4576Wait for completion of a given child process.");
4577
4578static PyObject *
4579posix_wait4(PyObject *self, PyObject *args)
4580{
Victor Stinner97b89882010-05-06 00:25:39 +00004581 pid_t pid;
4582 int options;
4583 struct rusage ru;
4584 WAIT_TYPE status;
4585 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004586
Victor Stinner97b89882010-05-06 00:25:39 +00004587 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
4588 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004589
Victor Stinner97b89882010-05-06 00:25:39 +00004590 Py_BEGIN_ALLOW_THREADS
4591 pid = wait4(pid, &status, options, &ru);
4592 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004593
Victor Stinner97b89882010-05-06 00:25:39 +00004594 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004595}
4596#endif /* HAVE_WAIT4 */
4597
Guido van Rossumb6775db1994-08-01 11:34:53 +00004598#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004599PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004600"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004601Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004602
Barry Warsaw53699e91996-12-10 23:23:01 +00004603static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004604posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004605{
Victor Stinner97b89882010-05-06 00:25:39 +00004606 pid_t pid;
4607 int options;
4608 WAIT_TYPE status;
4609 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004610
Victor Stinner97b89882010-05-06 00:25:39 +00004611 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
4612 return NULL;
4613 Py_BEGIN_ALLOW_THREADS
4614 pid = waitpid(pid, &status, options);
4615 Py_END_ALLOW_THREADS
4616 if (pid == -1)
4617 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004618
Victor Stinner97b89882010-05-06 00:25:39 +00004619 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004620}
4621
Tim Petersab034fa2002-02-01 11:27:43 +00004622#elif defined(HAVE_CWAIT)
4623
4624/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004625PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004626"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004627"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004628
4629static PyObject *
4630posix_waitpid(PyObject *self, PyObject *args)
4631{
Victor Stinner97b89882010-05-06 00:25:39 +00004632 Py_intptr_t pid;
4633 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004634
Victor Stinner97b89882010-05-06 00:25:39 +00004635 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
4636 return NULL;
4637 Py_BEGIN_ALLOW_THREADS
4638 pid = _cwait(&status, pid, options);
4639 Py_END_ALLOW_THREADS
4640 if (pid == -1)
4641 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004642
Victor Stinner97b89882010-05-06 00:25:39 +00004643 /* shift the status left a byte so this is more like the POSIX waitpid */
4644 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004645}
4646#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004647
Guido van Rossumad0ee831995-03-01 10:34:45 +00004648#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004649PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004650"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004651Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004652
Barry Warsaw53699e91996-12-10 23:23:01 +00004653static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004654posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004655{
Victor Stinner97b89882010-05-06 00:25:39 +00004656 pid_t pid;
4657 WAIT_TYPE status;
4658 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004659
Victor Stinner97b89882010-05-06 00:25:39 +00004660 Py_BEGIN_ALLOW_THREADS
4661 pid = wait(&status);
4662 Py_END_ALLOW_THREADS
4663 if (pid == -1)
4664 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004665
Victor Stinner97b89882010-05-06 00:25:39 +00004666 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004667}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004668#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004669
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004670
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004671PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004672"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004673Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004674
Barry Warsaw53699e91996-12-10 23:23:01 +00004675static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004676posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004677{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004678#ifdef HAVE_LSTAT
Victor Stinner97b89882010-05-06 00:25:39 +00004679 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004680#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004681#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00004682 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004683#else
Victor Stinner97b89882010-05-06 00:25:39 +00004684 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004685#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004686#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004687}
4688
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004689
Guido van Rossumb6775db1994-08-01 11:34:53 +00004690#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004691PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004692"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004693Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004694
Barry Warsaw53699e91996-12-10 23:23:01 +00004695static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004696posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004697{
Victor Stinner97b89882010-05-06 00:25:39 +00004698 PyObject* v;
4699 char buf[MAXPATHLEN];
4700 PyObject *opath;
4701 char *path;
4702 int n;
4703 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004704
Victor Stinner97b89882010-05-06 00:25:39 +00004705 if (!PyArg_ParseTuple(args, "O&:readlink",
4706 PyUnicode_FSConverter, &opath))
4707 return NULL;
4708 path = bytes2str(opath, 1);
4709 v = PySequence_GetItem(args, 0);
4710 if (v == NULL) {
4711 release_bytes(opath);
4712 return NULL;
4713 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004714
Victor Stinner97b89882010-05-06 00:25:39 +00004715 if (PyUnicode_Check(v)) {
4716 arg_is_unicode = 1;
4717 }
4718 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004719
Victor Stinner97b89882010-05-06 00:25:39 +00004720 Py_BEGIN_ALLOW_THREADS
4721 n = readlink(path, buf, (int) sizeof buf);
4722 Py_END_ALLOW_THREADS
4723 if (n < 0)
4724 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004725
Victor Stinner97b89882010-05-06 00:25:39 +00004726 release_bytes(opath);
Victor Stinner203406c2010-05-14 18:07:39 +00004727 v = PyBytes_FromStringAndSize(buf, n);
4728 if (arg_is_unicode) {
4729 PyObject *w;
4730
4731 w = PyUnicode_FromEncodedObject(v,
4732 Py_FileSystemDefaultEncoding,
4733 "surrogateescape");
4734 if (w != NULL) {
4735 Py_DECREF(v);
4736 v = w;
4737 }
4738 else {
4739 v = NULL;
4740 }
4741 }
4742 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004743}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004744#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004745
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004746
Guido van Rossumb6775db1994-08-01 11:34:53 +00004747#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004748PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004749"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004750Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004751
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004752static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004753posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004754{
Victor Stinner97b89882010-05-06 00:25:39 +00004755 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004756}
4757#endif /* HAVE_SYMLINK */
4758
4759
4760#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004761#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4762static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004763system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004764{
4765 ULONG value = 0;
4766
4767 Py_BEGIN_ALLOW_THREADS
4768 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4769 Py_END_ALLOW_THREADS
4770
4771 return value;
4772}
4773
4774static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004775posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004776{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004777 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner97b89882010-05-06 00:25:39 +00004778 return Py_BuildValue("ddddd",
4779 (double)0 /* t.tms_utime / HZ */,
4780 (double)0 /* t.tms_stime / HZ */,
4781 (double)0 /* t.tms_cutime / HZ */,
4782 (double)0 /* t.tms_cstime / HZ */,
4783 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00004784}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004785#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004786#define NEED_TICKS_PER_SECOND
4787static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004788static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004789posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004790{
Victor Stinner97b89882010-05-06 00:25:39 +00004791 struct tms t;
4792 clock_t c;
4793 errno = 0;
4794 c = times(&t);
4795 if (c == (clock_t) -1)
4796 return posix_error();
4797 return Py_BuildValue("ddddd",
4798 (double)t.tms_utime / ticks_per_second,
4799 (double)t.tms_stime / ticks_per_second,
4800 (double)t.tms_cutime / ticks_per_second,
4801 (double)t.tms_cstime / ticks_per_second,
4802 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004803}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004804#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004805#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004806
4807
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004808#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00004809#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004810static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004811posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004812{
Victor Stinner97b89882010-05-06 00:25:39 +00004813 FILETIME create, exit, kernel, user;
4814 HANDLE hProc;
4815 hProc = GetCurrentProcess();
4816 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4817 /* The fields of a FILETIME structure are the hi and lo part
4818 of a 64-bit value expressed in 100 nanosecond units.
4819 1e7 is one second in such units; 1e-7 the inverse.
4820 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4821 */
4822 return Py_BuildValue(
4823 "ddddd",
4824 (double)(user.dwHighDateTime*429.4967296 +
4825 user.dwLowDateTime*1e-7),
4826 (double)(kernel.dwHighDateTime*429.4967296 +
4827 kernel.dwLowDateTime*1e-7),
4828 (double)0,
4829 (double)0,
4830 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004831}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004832#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004833
4834#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004835PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004836"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004837Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004838#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004839
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004840
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004841#ifdef HAVE_GETSID
4842PyDoc_STRVAR(posix_getsid__doc__,
4843"getsid(pid) -> sid\n\n\
4844Call the system call getsid().");
4845
4846static PyObject *
4847posix_getsid(PyObject *self, PyObject *args)
4848{
Victor Stinner97b89882010-05-06 00:25:39 +00004849 pid_t pid;
4850 int sid;
4851 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
4852 return NULL;
4853 sid = getsid(pid);
4854 if (sid < 0)
4855 return posix_error();
4856 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004857}
4858#endif /* HAVE_GETSID */
4859
4860
Guido van Rossumb6775db1994-08-01 11:34:53 +00004861#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004862PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004863"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004864Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004865
Barry Warsaw53699e91996-12-10 23:23:01 +00004866static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004867posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004868{
Victor Stinner97b89882010-05-06 00:25:39 +00004869 if (setsid() < 0)
4870 return posix_error();
4871 Py_INCREF(Py_None);
4872 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004873}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004874#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004875
Guido van Rossumb6775db1994-08-01 11:34:53 +00004876#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004877PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004878"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004879Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004880
Barry Warsaw53699e91996-12-10 23:23:01 +00004881static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004882posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004883{
Victor Stinner97b89882010-05-06 00:25:39 +00004884 pid_t pid;
4885 int pgrp;
4886 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
4887 return NULL;
4888 if (setpgid(pid, pgrp) < 0)
4889 return posix_error();
4890 Py_INCREF(Py_None);
4891 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004892}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004893#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004894
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004895
Guido van Rossumb6775db1994-08-01 11:34:53 +00004896#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004897PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004898"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004899Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004900
Barry Warsaw53699e91996-12-10 23:23:01 +00004901static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004902posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004903{
Victor Stinner97b89882010-05-06 00:25:39 +00004904 int fd;
4905 pid_t pgid;
4906 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
4907 return NULL;
4908 pgid = tcgetpgrp(fd);
4909 if (pgid < 0)
4910 return posix_error();
4911 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004912}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004913#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004914
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004915
Guido van Rossumb6775db1994-08-01 11:34:53 +00004916#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004917PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004918"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004919Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004920
Barry Warsaw53699e91996-12-10 23:23:01 +00004921static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004922posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004923{
Victor Stinner97b89882010-05-06 00:25:39 +00004924 int fd;
4925 pid_t pgid;
4926 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
4927 return NULL;
4928 if (tcsetpgrp(fd, pgid) < 0)
4929 return posix_error();
4930 Py_INCREF(Py_None);
4931 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004932}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004933#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004934
Guido van Rossum687dd131993-05-17 08:34:16 +00004935/* Functions acting on file descriptors */
4936
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004937PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004938"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004939Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004940
Barry Warsaw53699e91996-12-10 23:23:01 +00004941static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004942posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004943{
Victor Stinner97b89882010-05-06 00:25:39 +00004944 PyObject *ofile;
4945 char *file;
4946 int flag;
4947 int mode = 0777;
4948 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004949
4950#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00004951 if (unicode_file_names()) {
4952 PyUnicodeObject *po;
4953 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4954 Py_BEGIN_ALLOW_THREADS
4955 /* PyUnicode_AS_UNICODE OK without thread
4956 lock as it is a simple dereference. */
4957 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4958 Py_END_ALLOW_THREADS
4959 if (fd < 0)
4960 return posix_error();
4961 return PyLong_FromLong((long)fd);
4962 }
4963 /* Drop the argument parsing error as narrow strings
4964 are also valid. */
4965 PyErr_Clear();
4966 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004967#endif
4968
Victor Stinner97b89882010-05-06 00:25:39 +00004969 if (!PyArg_ParseTuple(args, "O&i|i",
4970 PyUnicode_FSConverter, &ofile,
4971 &flag, &mode))
4972 return NULL;
4973 file = bytes2str(ofile, 1);
4974 Py_BEGIN_ALLOW_THREADS
4975 fd = open(file, flag, mode);
4976 Py_END_ALLOW_THREADS
4977 if (fd < 0)
4978 return posix_error_with_allocated_filename(ofile);
4979 release_bytes(ofile);
4980 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004981}
4982
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004983
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004984PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004985"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004986Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004987
Barry Warsaw53699e91996-12-10 23:23:01 +00004988static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004989posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004990{
Victor Stinner97b89882010-05-06 00:25:39 +00004991 int fd, res;
4992 if (!PyArg_ParseTuple(args, "i:close", &fd))
4993 return NULL;
4994 if (!_PyVerify_fd(fd))
4995 return posix_error();
4996 Py_BEGIN_ALLOW_THREADS
4997 res = close(fd);
4998 Py_END_ALLOW_THREADS
4999 if (res < 0)
5000 return posix_error();
5001 Py_INCREF(Py_None);
5002 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005003}
5004
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005005
Victor Stinner97b89882010-05-06 00:25:39 +00005006PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005007"closerange(fd_low, fd_high)\n\n\
5008Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5009
5010static PyObject *
5011posix_closerange(PyObject *self, PyObject *args)
5012{
Victor Stinner97b89882010-05-06 00:25:39 +00005013 int fd_from, fd_to, i;
5014 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5015 return NULL;
5016 Py_BEGIN_ALLOW_THREADS
5017 for (i = fd_from; i < fd_to; i++)
5018 if (_PyVerify_fd(i))
5019 close(i);
5020 Py_END_ALLOW_THREADS
5021 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005022}
5023
5024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005025PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005026"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005027Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005028
Barry Warsaw53699e91996-12-10 23:23:01 +00005029static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005030posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005031{
Victor Stinner97b89882010-05-06 00:25:39 +00005032 int fd;
5033 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5034 return NULL;
5035 if (!_PyVerify_fd(fd))
5036 return posix_error();
5037 Py_BEGIN_ALLOW_THREADS
5038 fd = dup(fd);
5039 Py_END_ALLOW_THREADS
5040 if (fd < 0)
5041 return posix_error();
5042 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005043}
5044
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005045
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005046PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005047"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005048Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005049
Barry Warsaw53699e91996-12-10 23:23:01 +00005050static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005051posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005052{
Victor Stinner97b89882010-05-06 00:25:39 +00005053 int fd, fd2, res;
5054 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5055 return NULL;
5056 if (!_PyVerify_fd_dup2(fd, fd2))
5057 return posix_error();
5058 Py_BEGIN_ALLOW_THREADS
5059 res = dup2(fd, fd2);
5060 Py_END_ALLOW_THREADS
5061 if (res < 0)
5062 return posix_error();
5063 Py_INCREF(Py_None);
5064 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005065}
5066
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005068PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005069"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005070Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005071
Barry Warsaw53699e91996-12-10 23:23:01 +00005072static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005073posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005074{
Victor Stinner97b89882010-05-06 00:25:39 +00005075 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005076#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner97b89882010-05-06 00:25:39 +00005077 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005078#else
Victor Stinner97b89882010-05-06 00:25:39 +00005079 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005080#endif
Victor Stinner97b89882010-05-06 00:25:39 +00005081 PyObject *posobj;
5082 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5083 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005084#ifdef SEEK_SET
Victor Stinner97b89882010-05-06 00:25:39 +00005085 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5086 switch (how) {
5087 case 0: how = SEEK_SET; break;
5088 case 1: how = SEEK_CUR; break;
5089 case 2: how = SEEK_END; break;
5090 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005091#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005092
5093#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner97b89882010-05-06 00:25:39 +00005094 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005095#else
Victor Stinner97b89882010-05-06 00:25:39 +00005096 pos = PyLong_Check(posobj) ?
5097 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005098#endif
Victor Stinner97b89882010-05-06 00:25:39 +00005099 if (PyErr_Occurred())
5100 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005101
Victor Stinner97b89882010-05-06 00:25:39 +00005102 if (!_PyVerify_fd(fd))
5103 return posix_error();
5104 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005105#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner97b89882010-05-06 00:25:39 +00005106 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005107#else
Victor Stinner97b89882010-05-06 00:25:39 +00005108 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005109#endif
Victor Stinner97b89882010-05-06 00:25:39 +00005110 Py_END_ALLOW_THREADS
5111 if (res < 0)
5112 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005113
5114#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner97b89882010-05-06 00:25:39 +00005115 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005116#else
Victor Stinner97b89882010-05-06 00:25:39 +00005117 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005118#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005119}
5120
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005122PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005123"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005124Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005125
Barry Warsaw53699e91996-12-10 23:23:01 +00005126static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005127posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005128{
Victor Stinner97b89882010-05-06 00:25:39 +00005129 int fd, size;
5130 Py_ssize_t n;
5131 PyObject *buffer;
5132 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5133 return NULL;
5134 if (size < 0) {
5135 errno = EINVAL;
5136 return posix_error();
5137 }
5138 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5139 if (buffer == NULL)
5140 return NULL;
5141 if (!_PyVerify_fd(fd))
5142 return posix_error();
5143 Py_BEGIN_ALLOW_THREADS
5144 n = read(fd, PyBytes_AS_STRING(buffer), size);
5145 Py_END_ALLOW_THREADS
5146 if (n < 0) {
5147 Py_DECREF(buffer);
5148 return posix_error();
5149 }
5150 if (n != size)
5151 _PyBytes_Resize(&buffer, n);
5152 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005153}
5154
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005156PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005157"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005158Write a string to a file descriptor.");
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_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005162{
Victor Stinner97b89882010-05-06 00:25:39 +00005163 Py_buffer pbuf;
5164 int fd;
5165 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005166
Victor Stinner97b89882010-05-06 00:25:39 +00005167 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5168 return NULL;
5169 if (!_PyVerify_fd(fd))
5170 return posix_error();
5171 Py_BEGIN_ALLOW_THREADS
5172 size = write(fd, pbuf.buf, (size_t)pbuf.len);
5173 Py_END_ALLOW_THREADS
5174 PyBuffer_Release(&pbuf);
5175 if (size < 0)
5176 return posix_error();
5177 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005178}
5179
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005180
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005181PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005182"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005183Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005184
Barry Warsaw53699e91996-12-10 23:23:01 +00005185static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005186posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005187{
Victor Stinner97b89882010-05-06 00:25:39 +00005188 int fd;
5189 STRUCT_STAT st;
5190 int res;
5191 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5192 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005193#ifdef __VMS
Victor Stinner97b89882010-05-06 00:25:39 +00005194 /* on OpenVMS we must ensure that all bytes are written to the file */
5195 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005196#endif
Victor Stinner97b89882010-05-06 00:25:39 +00005197 if (!_PyVerify_fd(fd))
5198 return posix_error();
5199 Py_BEGIN_ALLOW_THREADS
5200 res = FSTAT(fd, &st);
5201 Py_END_ALLOW_THREADS
5202 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005203#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00005204 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005205#else
Victor Stinner97b89882010-05-06 00:25:39 +00005206 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005207#endif
Victor Stinner97b89882010-05-06 00:25:39 +00005208 }
Tim Peters5aa91602002-01-30 05:46:57 +00005209
Victor Stinner97b89882010-05-06 00:25:39 +00005210 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005211}
5212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005213PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005214"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005215Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005216connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005217
5218static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005219posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005220{
Victor Stinner97b89882010-05-06 00:25:39 +00005221 int fd;
5222 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5223 return NULL;
5224 if (!_PyVerify_fd(fd))
5225 return PyBool_FromLong(0);
5226 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005227}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005228
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005229#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005230PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005231"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005232Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005233
Barry Warsaw53699e91996-12-10 23:23:01 +00005234static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005235posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005236{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005237#if defined(PYOS_OS2)
5238 HFILE read, write;
5239 APIRET rc;
5240
Victor Stinner97b89882010-05-06 00:25:39 +00005241 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005242 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner97b89882010-05-06 00:25:39 +00005243 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005244 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005245 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005246
5247 return Py_BuildValue("(ii)", read, write);
5248#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005249#if !defined(MS_WINDOWS)
Victor Stinner97b89882010-05-06 00:25:39 +00005250 int fds[2];
5251 int res;
5252 Py_BEGIN_ALLOW_THREADS
5253 res = pipe(fds);
5254 Py_END_ALLOW_THREADS
5255 if (res != 0)
5256 return posix_error();
5257 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005258#else /* MS_WINDOWS */
Victor Stinner97b89882010-05-06 00:25:39 +00005259 HANDLE read, write;
5260 int read_fd, write_fd;
5261 BOOL ok;
5262 Py_BEGIN_ALLOW_THREADS
5263 ok = CreatePipe(&read, &write, NULL, 0);
5264 Py_END_ALLOW_THREADS
5265 if (!ok)
5266 return win32_error("CreatePipe", NULL);
5267 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5268 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5269 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005270#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005271#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005272}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005273#endif /* HAVE_PIPE */
5274
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005275
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005276#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005277PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005278"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005279Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005280
Barry Warsaw53699e91996-12-10 23:23:01 +00005281static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005282posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005283{
Benjamin Peterson9ecbc072010-08-11 19:24:27 +00005284 PyObject *opath;
Victor Stinner97b89882010-05-06 00:25:39 +00005285 char *filename;
5286 int mode = 0666;
5287 int res;
Benjamin Peterson9ecbc072010-08-11 19:24:27 +00005288 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5289 &mode))
Victor Stinner97b89882010-05-06 00:25:39 +00005290 return NULL;
Benjamin Peterson9ecbc072010-08-11 19:24:27 +00005291 filename = PyBytes_AS_STRING(opath);
Victor Stinner97b89882010-05-06 00:25:39 +00005292 Py_BEGIN_ALLOW_THREADS
5293 res = mkfifo(filename, mode);
5294 Py_END_ALLOW_THREADS
Benjamin Peterson9ecbc072010-08-11 19:24:27 +00005295 Py_DECREF(opath);
Victor Stinner97b89882010-05-06 00:25:39 +00005296 if (res < 0)
5297 return posix_error();
5298 Py_INCREF(Py_None);
5299 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005300}
5301#endif
5302
5303
Neal Norwitz11690112002-07-30 01:08:28 +00005304#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005305PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005306"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005307Create a filesystem node (file, device special file or named pipe)\n\
5308named filename. mode specifies both the permissions to use and the\n\
5309type of node to be created, being combined (bitwise OR) with one of\n\
5310S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005311device defines the newly created device special file (probably using\n\
5312os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005313
5314
5315static PyObject *
5316posix_mknod(PyObject *self, PyObject *args)
5317{
Benjamin Peterson9ecbc072010-08-11 19:24:27 +00005318 PyObject *opath;
Victor Stinner97b89882010-05-06 00:25:39 +00005319 char *filename;
5320 int mode = 0600;
5321 int device = 0;
5322 int res;
Benjamin Peterson9ecbc072010-08-11 19:24:27 +00005323 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5324 &mode, &device))
Victor Stinner97b89882010-05-06 00:25:39 +00005325 return NULL;
Benjamin Peterson9ecbc072010-08-11 19:24:27 +00005326 filename = PyBytes_AS_STRING(opath);
Victor Stinner97b89882010-05-06 00:25:39 +00005327 Py_BEGIN_ALLOW_THREADS
5328 res = mknod(filename, mode, device);
5329 Py_END_ALLOW_THREADS
Benjamin Peterson9ecbc072010-08-11 19:24:27 +00005330 Py_DECREF(opath);
Victor Stinner97b89882010-05-06 00:25:39 +00005331 if (res < 0)
5332 return posix_error();
5333 Py_INCREF(Py_None);
5334 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005335}
5336#endif
5337
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005338#ifdef HAVE_DEVICE_MACROS
5339PyDoc_STRVAR(posix_major__doc__,
5340"major(device) -> major number\n\
5341Extracts a device major number from a raw device number.");
5342
5343static PyObject *
5344posix_major(PyObject *self, PyObject *args)
5345{
Victor Stinner97b89882010-05-06 00:25:39 +00005346 int device;
5347 if (!PyArg_ParseTuple(args, "i:major", &device))
5348 return NULL;
5349 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005350}
5351
5352PyDoc_STRVAR(posix_minor__doc__,
5353"minor(device) -> minor number\n\
5354Extracts a device minor number from a raw device number.");
5355
5356static PyObject *
5357posix_minor(PyObject *self, PyObject *args)
5358{
Victor Stinner97b89882010-05-06 00:25:39 +00005359 int device;
5360 if (!PyArg_ParseTuple(args, "i:minor", &device))
5361 return NULL;
5362 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005363}
5364
5365PyDoc_STRVAR(posix_makedev__doc__,
5366"makedev(major, minor) -> device number\n\
5367Composes a raw device number from the major and minor device numbers.");
5368
5369static PyObject *
5370posix_makedev(PyObject *self, PyObject *args)
5371{
Victor Stinner97b89882010-05-06 00:25:39 +00005372 int major, minor;
5373 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5374 return NULL;
5375 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005376}
5377#endif /* device macros */
5378
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005379
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005380#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005381PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005382"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005383Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005384
Barry Warsaw53699e91996-12-10 23:23:01 +00005385static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005386posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005387{
Victor Stinner97b89882010-05-06 00:25:39 +00005388 int fd;
5389 off_t length;
5390 int res;
5391 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005392
Victor Stinner97b89882010-05-06 00:25:39 +00005393 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5394 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005395
5396#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner97b89882010-05-06 00:25:39 +00005397 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005398#else
Victor Stinner97b89882010-05-06 00:25:39 +00005399 length = PyLong_Check(lenobj) ?
5400 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005401#endif
Victor Stinner97b89882010-05-06 00:25:39 +00005402 if (PyErr_Occurred())
5403 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005404
Victor Stinner97b89882010-05-06 00:25:39 +00005405 Py_BEGIN_ALLOW_THREADS
5406 res = ftruncate(fd, length);
5407 Py_END_ALLOW_THREADS
5408 if (res < 0)
5409 return posix_error();
5410 Py_INCREF(Py_None);
5411 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005412}
5413#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005414
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005415#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005416PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005417"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005418Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005419
Fred Drake762e2061999-08-26 17:23:54 +00005420/* Save putenv() parameters as values here, so we can collect them when they
5421 * get re-set with another call for the same key. */
5422static PyObject *posix_putenv_garbage;
5423
Tim Peters5aa91602002-01-30 05:46:57 +00005424static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005425posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005426{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005427#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00005428 wchar_t *s1, *s2;
5429 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005430#else
Victor Stinner97b89882010-05-06 00:25:39 +00005431 PyObject *os1, *os2;
5432 char *s1, *s2;
5433 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005434#endif
Victor Stinner97b89882010-05-06 00:25:39 +00005435 PyObject *newstr;
5436 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005437
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005438#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00005439 if (!PyArg_ParseTuple(args,
5440 "uu:putenv",
5441 &s1, &s2))
5442 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005443#else
Victor Stinner97b89882010-05-06 00:25:39 +00005444 if (!PyArg_ParseTuple(args,
5445 "O&O&:putenv",
5446 PyUnicode_FSConverter, &os1,
5447 PyUnicode_FSConverter, &os2))
5448 return NULL;
5449 s1 = bytes2str(os1, 1);
5450 s2 = bytes2str(os2, 1);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005451#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005452
5453#if defined(PYOS_OS2)
5454 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5455 APIRET rc;
5456
Guido van Rossumd48f2521997-12-05 22:19:34 +00005457 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5458 if (rc != NO_ERROR)
5459 return os2_error(rc);
5460
5461 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5462 APIRET rc;
5463
Guido van Rossumd48f2521997-12-05 22:19:34 +00005464 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5465 if (rc != NO_ERROR)
5466 return os2_error(rc);
5467 } else {
5468#endif
Victor Stinner97b89882010-05-06 00:25:39 +00005469 /* XXX This can leak memory -- not easy to fix :-( */
5470 /* len includes space for a trailing \0; the size arg to
5471 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005472#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00005473 len = wcslen(s1) + wcslen(s2) + 2;
5474 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005475#else
Victor Stinner97b89882010-05-06 00:25:39 +00005476 len = strlen(s1) + strlen(s2) + 2;
5477 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005478#endif
Victor Stinner97b89882010-05-06 00:25:39 +00005479 if (newstr == NULL)
5480 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005481#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00005482 newenv = PyUnicode_AsUnicode(newstr);
5483 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5484 if (_wputenv(newenv)) {
5485 Py_DECREF(newstr);
5486 posix_error();
5487 return NULL;
5488 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005489#else
Victor Stinner97b89882010-05-06 00:25:39 +00005490 newenv = PyBytes_AS_STRING(newstr);
5491 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5492 if (putenv(newenv)) {
5493 Py_DECREF(newstr);
5494 release_bytes(os1);
5495 release_bytes(os2);
5496 posix_error();
5497 return NULL;
5498 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005499#endif
Victor Stinner97b89882010-05-06 00:25:39 +00005500 /* Install the first arg and newstr in posix_putenv_garbage;
5501 * this will cause previous value to be collected. This has to
5502 * happen after the real putenv() call because the old value
5503 * was still accessible until then. */
5504 if (PyDict_SetItem(posix_putenv_garbage,
5505 PyTuple_GET_ITEM(args, 0), newstr)) {
5506 /* really not much we can do; just leak */
5507 PyErr_Clear();
5508 }
5509 else {
5510 Py_DECREF(newstr);
5511 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005512
5513#if defined(PYOS_OS2)
5514 }
5515#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00005516#ifndef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00005517 release_bytes(os1);
5518 release_bytes(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005519#endif
Victor Stinner97b89882010-05-06 00:25:39 +00005520 Py_INCREF(Py_None);
5521 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005522}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005523#endif /* putenv */
5524
Guido van Rossumc524d952001-10-19 01:31:59 +00005525#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005526PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005527"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005528Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005529
5530static PyObject *
5531posix_unsetenv(PyObject *self, PyObject *args)
5532{
Victor Stinner97b89882010-05-06 00:25:39 +00005533 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00005534
Victor Stinner97b89882010-05-06 00:25:39 +00005535 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5536 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00005537
Victor Stinner97b89882010-05-06 00:25:39 +00005538 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00005539
Victor Stinner97b89882010-05-06 00:25:39 +00005540 /* Remove the key from posix_putenv_garbage;
5541 * this will cause it to be collected. This has to
5542 * happen after the real unsetenv() call because the
5543 * old value was still accessible until then.
5544 */
5545 if (PyDict_DelItem(posix_putenv_garbage,
5546 PyTuple_GET_ITEM(args, 0))) {
5547 /* really not much we can do; just leak */
5548 PyErr_Clear();
5549 }
Guido van Rossumc524d952001-10-19 01:31:59 +00005550
Victor Stinner97b89882010-05-06 00:25:39 +00005551 Py_INCREF(Py_None);
5552 return Py_None;
Guido van Rossumc524d952001-10-19 01:31:59 +00005553}
5554#endif /* unsetenv */
5555
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005556PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005557"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005558Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005559
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005560static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005561posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005562{
Victor Stinner97b89882010-05-06 00:25:39 +00005563 int code;
5564 char *message;
5565 if (!PyArg_ParseTuple(args, "i:strerror", &code))
5566 return NULL;
5567 message = strerror(code);
5568 if (message == NULL) {
5569 PyErr_SetString(PyExc_ValueError,
5570 "strerror() argument out of range");
5571 return NULL;
5572 }
5573 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005574}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005575
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005576
Guido van Rossumc9641791998-08-04 15:26:23 +00005577#ifdef HAVE_SYS_WAIT_H
5578
Fred Drake106c1a02002-04-23 15:58:02 +00005579#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005580PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005581"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005582Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005583
5584static PyObject *
5585posix_WCOREDUMP(PyObject *self, PyObject *args)
5586{
Victor Stinner97b89882010-05-06 00:25:39 +00005587 WAIT_TYPE status;
5588 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005589
Victor Stinner97b89882010-05-06 00:25:39 +00005590 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
5591 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005592
Victor Stinner97b89882010-05-06 00:25:39 +00005593 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005594}
5595#endif /* WCOREDUMP */
5596
5597#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005598PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005599"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005600Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005601job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005602
5603static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005604posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005605{
Victor Stinner97b89882010-05-06 00:25:39 +00005606 WAIT_TYPE status;
5607 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005608
Victor Stinner97b89882010-05-06 00:25:39 +00005609 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
5610 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005611
Victor Stinner97b89882010-05-06 00:25:39 +00005612 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005613}
5614#endif /* WIFCONTINUED */
5615
Guido van Rossumc9641791998-08-04 15:26:23 +00005616#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005617PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005618"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005619Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005620
5621static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005622posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005623{
Victor Stinner97b89882010-05-06 00:25:39 +00005624 WAIT_TYPE status;
5625 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005626
Victor Stinner97b89882010-05-06 00:25:39 +00005627 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
5628 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005629
Victor Stinner97b89882010-05-06 00:25:39 +00005630 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005631}
5632#endif /* WIFSTOPPED */
5633
5634#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005635PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005636"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005637Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005638
5639static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005640posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005641{
Victor Stinner97b89882010-05-06 00:25:39 +00005642 WAIT_TYPE status;
5643 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005644
Victor Stinner97b89882010-05-06 00:25:39 +00005645 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
5646 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005647
Victor Stinner97b89882010-05-06 00:25:39 +00005648 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005649}
5650#endif /* WIFSIGNALED */
5651
5652#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005653PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005654"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005655Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005656system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005657
5658static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005659posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005660{
Victor Stinner97b89882010-05-06 00:25:39 +00005661 WAIT_TYPE status;
5662 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005663
Victor Stinner97b89882010-05-06 00:25:39 +00005664 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
5665 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005666
Victor Stinner97b89882010-05-06 00:25:39 +00005667 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005668}
5669#endif /* WIFEXITED */
5670
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005671#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005672PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005673"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005674Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005675
5676static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005677posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005678{
Victor Stinner97b89882010-05-06 00:25:39 +00005679 WAIT_TYPE status;
5680 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005681
Victor Stinner97b89882010-05-06 00:25:39 +00005682 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
5683 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005684
Victor Stinner97b89882010-05-06 00:25:39 +00005685 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005686}
5687#endif /* WEXITSTATUS */
5688
5689#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005690PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005691"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005692Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005693value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005694
5695static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005696posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005697{
Victor Stinner97b89882010-05-06 00:25:39 +00005698 WAIT_TYPE status;
5699 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005700
Victor Stinner97b89882010-05-06 00:25:39 +00005701 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
5702 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005703
Victor Stinner97b89882010-05-06 00:25:39 +00005704 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005705}
5706#endif /* WTERMSIG */
5707
5708#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005709PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005710"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005711Return the signal that stopped the process that provided\n\
5712the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005713
5714static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005715posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005716{
Victor Stinner97b89882010-05-06 00:25:39 +00005717 WAIT_TYPE status;
5718 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005719
Victor Stinner97b89882010-05-06 00:25:39 +00005720 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
5721 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005722
Victor Stinner97b89882010-05-06 00:25:39 +00005723 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005724}
5725#endif /* WSTOPSIG */
5726
5727#endif /* HAVE_SYS_WAIT_H */
5728
5729
Thomas Wouters477c8d52006-05-27 19:21:47 +00005730#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005731#ifdef _SCO_DS
5732/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5733 needed definitions in sys/statvfs.h */
5734#define _SVID3
5735#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005736#include <sys/statvfs.h>
5737
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005738static PyObject*
5739_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner97b89882010-05-06 00:25:39 +00005740 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5741 if (v == NULL)
5742 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005743
5744#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner97b89882010-05-06 00:25:39 +00005745 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5746 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5747 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5748 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5749 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5750 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5751 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5752 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5753 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5754 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005755#else
Victor Stinner97b89882010-05-06 00:25:39 +00005756 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5757 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5758 PyStructSequence_SET_ITEM(v, 2,
5759 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
5760 PyStructSequence_SET_ITEM(v, 3,
5761 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
5762 PyStructSequence_SET_ITEM(v, 4,
5763 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
5764 PyStructSequence_SET_ITEM(v, 5,
5765 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
5766 PyStructSequence_SET_ITEM(v, 6,
5767 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
5768 PyStructSequence_SET_ITEM(v, 7,
5769 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
5770 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5771 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005772#endif
5773
Victor Stinner97b89882010-05-06 00:25:39 +00005774 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005775}
5776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005777PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005778"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005779Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005780
5781static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005782posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005783{
Victor Stinner97b89882010-05-06 00:25:39 +00005784 int fd, res;
5785 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005786
Victor Stinner97b89882010-05-06 00:25:39 +00005787 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
5788 return NULL;
5789 Py_BEGIN_ALLOW_THREADS
5790 res = fstatvfs(fd, &st);
5791 Py_END_ALLOW_THREADS
5792 if (res != 0)
5793 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005794
Victor Stinner97b89882010-05-06 00:25:39 +00005795 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005796}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005797#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005798
5799
Thomas Wouters477c8d52006-05-27 19:21:47 +00005800#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005801#include <sys/statvfs.h>
5802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005803PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005804"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005805Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005806
5807static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005808posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005809{
Victor Stinner97b89882010-05-06 00:25:39 +00005810 char *path;
5811 int res;
5812 struct statvfs st;
5813 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
5814 return NULL;
5815 Py_BEGIN_ALLOW_THREADS
5816 res = statvfs(path, &st);
5817 Py_END_ALLOW_THREADS
5818 if (res != 0)
5819 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005820
Victor Stinner97b89882010-05-06 00:25:39 +00005821 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005822}
5823#endif /* HAVE_STATVFS */
5824
Fred Drakec9680921999-12-13 16:37:25 +00005825/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5826 * It maps strings representing configuration variable names to
5827 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005828 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005829 * rarely-used constants. There are three separate tables that use
5830 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005831 *
5832 * This code is always included, even if none of the interfaces that
5833 * need it are included. The #if hackery needed to avoid it would be
5834 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005835 */
5836struct constdef {
5837 char *name;
5838 long value;
5839};
5840
Fred Drake12c6e2d1999-12-14 21:25:03 +00005841static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005842conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005843 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005844{
Christian Heimes217cfd12007-12-02 14:31:20 +00005845 if (PyLong_Check(arg)) {
Victor Stinner97b89882010-05-06 00:25:39 +00005846 *valuep = PyLong_AS_LONG(arg);
5847 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005848 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005849 else {
Victor Stinner97b89882010-05-06 00:25:39 +00005850 /* look up the value in the table using a binary search */
5851 size_t lo = 0;
5852 size_t mid;
5853 size_t hi = tablesize;
5854 int cmp;
5855 const char *confname;
5856 if (!PyUnicode_Check(arg)) {
5857 PyErr_SetString(PyExc_TypeError,
5858 "configuration names must be strings or integers");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005859 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005860 }
Victor Stinner97b89882010-05-06 00:25:39 +00005861 confname = _PyUnicode_AsString(arg);
5862 if (confname == NULL)
5863 return 0;
5864 while (lo < hi) {
5865 mid = (lo + hi) / 2;
5866 cmp = strcmp(confname, table[mid].name);
5867 if (cmp < 0)
5868 hi = mid;
5869 else if (cmp > 0)
5870 lo = mid + 1;
5871 else {
5872 *valuep = table[mid].value;
5873 return 1;
5874 }
5875 }
5876 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
5877 return 0;
5878 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005879}
5880
5881
5882#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5883static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005884#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00005885 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00005886#endif
5887#ifdef _PC_ABI_ASYNC_IO
Victor Stinner97b89882010-05-06 00:25:39 +00005888 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00005889#endif
Fred Drakec9680921999-12-13 16:37:25 +00005890#ifdef _PC_ASYNC_IO
Victor Stinner97b89882010-05-06 00:25:39 +00005891 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005892#endif
5893#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner97b89882010-05-06 00:25:39 +00005894 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00005895#endif
5896#ifdef _PC_FILESIZEBITS
Victor Stinner97b89882010-05-06 00:25:39 +00005897 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00005898#endif
5899#ifdef _PC_LAST
Victor Stinner97b89882010-05-06 00:25:39 +00005900 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00005901#endif
5902#ifdef _PC_LINK_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00005903 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005904#endif
5905#ifdef _PC_MAX_CANON
Victor Stinner97b89882010-05-06 00:25:39 +00005906 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00005907#endif
5908#ifdef _PC_MAX_INPUT
Victor Stinner97b89882010-05-06 00:25:39 +00005909 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00005910#endif
5911#ifdef _PC_NAME_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00005912 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005913#endif
5914#ifdef _PC_NO_TRUNC
Victor Stinner97b89882010-05-06 00:25:39 +00005915 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00005916#endif
5917#ifdef _PC_PATH_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00005918 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005919#endif
5920#ifdef _PC_PIPE_BUF
Victor Stinner97b89882010-05-06 00:25:39 +00005921 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00005922#endif
5923#ifdef _PC_PRIO_IO
Victor Stinner97b89882010-05-06 00:25:39 +00005924 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005925#endif
5926#ifdef _PC_SOCK_MAXBUF
Victor Stinner97b89882010-05-06 00:25:39 +00005927 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00005928#endif
5929#ifdef _PC_SYNC_IO
Victor Stinner97b89882010-05-06 00:25:39 +00005930 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005931#endif
5932#ifdef _PC_VDISABLE
Victor Stinner97b89882010-05-06 00:25:39 +00005933 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00005934#endif
5935};
5936
Fred Drakec9680921999-12-13 16:37:25 +00005937static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005938conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005939{
5940 return conv_confname(arg, valuep, posix_constants_pathconf,
5941 sizeof(posix_constants_pathconf)
5942 / sizeof(struct constdef));
5943}
5944#endif
5945
5946#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005947PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005948"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005949Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005950If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005951
5952static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005953posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005954{
5955 PyObject *result = NULL;
5956 int name, fd;
5957
Fred Drake12c6e2d1999-12-14 21:25:03 +00005958 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5959 conv_path_confname, &name)) {
Victor Stinner97b89882010-05-06 00:25:39 +00005960 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00005961
Victor Stinner97b89882010-05-06 00:25:39 +00005962 errno = 0;
5963 limit = fpathconf(fd, name);
5964 if (limit == -1 && errno != 0)
5965 posix_error();
5966 else
5967 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005968 }
5969 return result;
5970}
5971#endif
5972
5973
5974#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005975PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005976"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005977Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005978If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005979
5980static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005981posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005982{
5983 PyObject *result = NULL;
5984 int name;
5985 char *path;
5986
5987 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5988 conv_path_confname, &name)) {
Victor Stinner97b89882010-05-06 00:25:39 +00005989 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00005990
Victor Stinner97b89882010-05-06 00:25:39 +00005991 errno = 0;
5992 limit = pathconf(path, name);
5993 if (limit == -1 && errno != 0) {
5994 if (errno == EINVAL)
5995 /* could be a path or name problem */
5996 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00005997 else
Victor Stinner97b89882010-05-06 00:25:39 +00005998 posix_error_with_filename(path);
5999 }
6000 else
6001 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006002 }
6003 return result;
6004}
6005#endif
6006
6007#ifdef HAVE_CONFSTR
6008static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006009#ifdef _CS_ARCHITECTURE
Victor Stinner97b89882010-05-06 00:25:39 +00006010 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006011#endif
Mark Dickinson466e9262010-04-16 16:32:49 +00006012#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner97b89882010-05-06 00:25:39 +00006013 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson466e9262010-04-16 16:32:49 +00006014#endif
6015#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner97b89882010-05-06 00:25:39 +00006016 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson466e9262010-04-16 16:32:49 +00006017#endif
Fred Draked86ed291999-12-15 15:34:33 +00006018#ifdef _CS_HOSTNAME
Victor Stinner97b89882010-05-06 00:25:39 +00006019 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006020#endif
6021#ifdef _CS_HW_PROVIDER
Victor Stinner97b89882010-05-06 00:25:39 +00006022 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006023#endif
6024#ifdef _CS_HW_SERIAL
Victor Stinner97b89882010-05-06 00:25:39 +00006025 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006026#endif
6027#ifdef _CS_INITTAB_NAME
Victor Stinner97b89882010-05-06 00:25:39 +00006028 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006029#endif
Fred Drakec9680921999-12-13 16:37:25 +00006030#ifdef _CS_LFS64_CFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006031 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006032#endif
6033#ifdef _CS_LFS64_LDFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006034 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006035#endif
6036#ifdef _CS_LFS64_LIBS
Victor Stinner97b89882010-05-06 00:25:39 +00006037 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006038#endif
6039#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006040 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006041#endif
6042#ifdef _CS_LFS_CFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006043 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006044#endif
6045#ifdef _CS_LFS_LDFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006046 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006047#endif
6048#ifdef _CS_LFS_LIBS
Victor Stinner97b89882010-05-06 00:25:39 +00006049 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006050#endif
6051#ifdef _CS_LFS_LINTFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006052 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006053#endif
Fred Draked86ed291999-12-15 15:34:33 +00006054#ifdef _CS_MACHINE
Victor Stinner97b89882010-05-06 00:25:39 +00006055 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006056#endif
Fred Drakec9680921999-12-13 16:37:25 +00006057#ifdef _CS_PATH
Victor Stinner97b89882010-05-06 00:25:39 +00006058 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006059#endif
Fred Draked86ed291999-12-15 15:34:33 +00006060#ifdef _CS_RELEASE
Victor Stinner97b89882010-05-06 00:25:39 +00006061 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006062#endif
6063#ifdef _CS_SRPC_DOMAIN
Victor Stinner97b89882010-05-06 00:25:39 +00006064 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006065#endif
6066#ifdef _CS_SYSNAME
Victor Stinner97b89882010-05-06 00:25:39 +00006067 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006068#endif
6069#ifdef _CS_VERSION
Victor Stinner97b89882010-05-06 00:25:39 +00006070 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006071#endif
Fred Drakec9680921999-12-13 16:37:25 +00006072#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006073 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006074#endif
6075#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006076 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006077#endif
6078#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner97b89882010-05-06 00:25:39 +00006079 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006080#endif
6081#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006082 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006083#endif
6084#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006085 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006086#endif
6087#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006088 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006089#endif
6090#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner97b89882010-05-06 00:25:39 +00006091 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006092#endif
6093#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006094 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006095#endif
6096#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006097 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006098#endif
6099#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006100 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006101#endif
6102#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner97b89882010-05-06 00:25:39 +00006103 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006104#endif
6105#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006106 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006107#endif
6108#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006109 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006110#endif
6111#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006112 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006113#endif
6114#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner97b89882010-05-06 00:25:39 +00006115 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006116#endif
6117#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00006118 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006119#endif
Fred Draked86ed291999-12-15 15:34:33 +00006120#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner97b89882010-05-06 00:25:39 +00006121 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006122#endif
6123#ifdef _MIPS_CS_BASE
Victor Stinner97b89882010-05-06 00:25:39 +00006124 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006125#endif
6126#ifdef _MIPS_CS_HOSTID
Victor Stinner97b89882010-05-06 00:25:39 +00006127 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006128#endif
6129#ifdef _MIPS_CS_HW_NAME
Victor Stinner97b89882010-05-06 00:25:39 +00006130 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006131#endif
6132#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner97b89882010-05-06 00:25:39 +00006133 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006134#endif
6135#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner97b89882010-05-06 00:25:39 +00006136 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006137#endif
6138#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner97b89882010-05-06 00:25:39 +00006139 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006140#endif
6141#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner97b89882010-05-06 00:25:39 +00006142 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006143#endif
6144#ifdef _MIPS_CS_OS_NAME
Victor Stinner97b89882010-05-06 00:25:39 +00006145 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006146#endif
6147#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner97b89882010-05-06 00:25:39 +00006148 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006149#endif
6150#ifdef _MIPS_CS_PROCESSORS
Victor Stinner97b89882010-05-06 00:25:39 +00006151 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006152#endif
6153#ifdef _MIPS_CS_SERIAL
Victor Stinner97b89882010-05-06 00:25:39 +00006154 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006155#endif
6156#ifdef _MIPS_CS_VENDOR
Victor Stinner97b89882010-05-06 00:25:39 +00006157 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006158#endif
Fred Drakec9680921999-12-13 16:37:25 +00006159};
6160
6161static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006162conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006163{
6164 return conv_confname(arg, valuep, posix_constants_confstr,
6165 sizeof(posix_constants_confstr)
6166 / sizeof(struct constdef));
6167}
6168
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006169PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006170"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006171Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006172
6173static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006174posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006175{
6176 PyObject *result = NULL;
6177 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006178 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006179
6180 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinner97b89882010-05-06 00:25:39 +00006181 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006182
Fred Drakec9680921999-12-13 16:37:25 +00006183 errno = 0;
Victor Stinner97b89882010-05-06 00:25:39 +00006184 len = confstr(name, buffer, sizeof(buffer));
6185 if (len == 0) {
6186 if (errno) {
6187 posix_error();
6188 }
6189 else {
6190 result = Py_None;
6191 Py_INCREF(Py_None);
6192 }
Fred Drakec9680921999-12-13 16:37:25 +00006193 }
6194 else {
Victor Stinner97b89882010-05-06 00:25:39 +00006195 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006196 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006197 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006198 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006199 }
6200 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006201 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006202 }
6203 }
6204 return result;
6205}
6206#endif
6207
6208
6209#ifdef HAVE_SYSCONF
6210static struct constdef posix_constants_sysconf[] = {
6211#ifdef _SC_2_CHAR_TERM
Victor Stinner97b89882010-05-06 00:25:39 +00006212 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006213#endif
6214#ifdef _SC_2_C_BIND
Victor Stinner97b89882010-05-06 00:25:39 +00006215 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006216#endif
6217#ifdef _SC_2_C_DEV
Victor Stinner97b89882010-05-06 00:25:39 +00006218 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006219#endif
6220#ifdef _SC_2_C_VERSION
Victor Stinner97b89882010-05-06 00:25:39 +00006221 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006222#endif
6223#ifdef _SC_2_FORT_DEV
Victor Stinner97b89882010-05-06 00:25:39 +00006224 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006225#endif
6226#ifdef _SC_2_FORT_RUN
Victor Stinner97b89882010-05-06 00:25:39 +00006227 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006228#endif
6229#ifdef _SC_2_LOCALEDEF
Victor Stinner97b89882010-05-06 00:25:39 +00006230 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006231#endif
6232#ifdef _SC_2_SW_DEV
Victor Stinner97b89882010-05-06 00:25:39 +00006233 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006234#endif
6235#ifdef _SC_2_UPE
Victor Stinner97b89882010-05-06 00:25:39 +00006236 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006237#endif
6238#ifdef _SC_2_VERSION
Victor Stinner97b89882010-05-06 00:25:39 +00006239 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006240#endif
Fred Draked86ed291999-12-15 15:34:33 +00006241#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner97b89882010-05-06 00:25:39 +00006242 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006243#endif
6244#ifdef _SC_ACL
Victor Stinner97b89882010-05-06 00:25:39 +00006245 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006246#endif
Fred Drakec9680921999-12-13 16:37:25 +00006247#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006248 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006249#endif
Fred Drakec9680921999-12-13 16:37:25 +00006250#ifdef _SC_AIO_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006251 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006252#endif
6253#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006254 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006255#endif
6256#ifdef _SC_ARG_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006257 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006258#endif
6259#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner97b89882010-05-06 00:25:39 +00006260 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006261#endif
6262#ifdef _SC_ATEXIT_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006263 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006264#endif
Fred Draked86ed291999-12-15 15:34:33 +00006265#ifdef _SC_AUDIT
Victor Stinner97b89882010-05-06 00:25:39 +00006266 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006267#endif
Fred Drakec9680921999-12-13 16:37:25 +00006268#ifdef _SC_AVPHYS_PAGES
Victor Stinner97b89882010-05-06 00:25:39 +00006269 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006270#endif
6271#ifdef _SC_BC_BASE_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006272 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006273#endif
6274#ifdef _SC_BC_DIM_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006275 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006276#endif
6277#ifdef _SC_BC_SCALE_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006278 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006279#endif
6280#ifdef _SC_BC_STRING_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006281 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006282#endif
Fred Draked86ed291999-12-15 15:34:33 +00006283#ifdef _SC_CAP
Victor Stinner97b89882010-05-06 00:25:39 +00006284 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006285#endif
Fred Drakec9680921999-12-13 16:37:25 +00006286#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006287 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006288#endif
6289#ifdef _SC_CHAR_BIT
Victor Stinner97b89882010-05-06 00:25:39 +00006290 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006291#endif
6292#ifdef _SC_CHAR_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006293 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006294#endif
6295#ifdef _SC_CHAR_MIN
Victor Stinner97b89882010-05-06 00:25:39 +00006296 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006297#endif
6298#ifdef _SC_CHILD_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006299 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006300#endif
6301#ifdef _SC_CLK_TCK
Victor Stinner97b89882010-05-06 00:25:39 +00006302 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006303#endif
6304#ifdef _SC_COHER_BLKSZ
Victor Stinner97b89882010-05-06 00:25:39 +00006305 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006306#endif
6307#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006308 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006309#endif
6310#ifdef _SC_DCACHE_ASSOC
Victor Stinner97b89882010-05-06 00:25:39 +00006311 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006312#endif
6313#ifdef _SC_DCACHE_BLKSZ
Victor Stinner97b89882010-05-06 00:25:39 +00006314 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006315#endif
6316#ifdef _SC_DCACHE_LINESZ
Victor Stinner97b89882010-05-06 00:25:39 +00006317 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006318#endif
6319#ifdef _SC_DCACHE_SZ
Victor Stinner97b89882010-05-06 00:25:39 +00006320 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006321#endif
6322#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner97b89882010-05-06 00:25:39 +00006323 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006324#endif
6325#ifdef _SC_DELAYTIMER_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006326 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006327#endif
6328#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006329 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006330#endif
6331#ifdef _SC_EXPR_NEST_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006332 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006333#endif
6334#ifdef _SC_FSYNC
Victor Stinner97b89882010-05-06 00:25:39 +00006335 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006336#endif
6337#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006338 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006339#endif
6340#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006341 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006342#endif
6343#ifdef _SC_ICACHE_ASSOC
Victor Stinner97b89882010-05-06 00:25:39 +00006344 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006345#endif
6346#ifdef _SC_ICACHE_BLKSZ
Victor Stinner97b89882010-05-06 00:25:39 +00006347 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006348#endif
6349#ifdef _SC_ICACHE_LINESZ
Victor Stinner97b89882010-05-06 00:25:39 +00006350 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006351#endif
6352#ifdef _SC_ICACHE_SZ
Victor Stinner97b89882010-05-06 00:25:39 +00006353 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006354#endif
Fred Draked86ed291999-12-15 15:34:33 +00006355#ifdef _SC_INF
Victor Stinner97b89882010-05-06 00:25:39 +00006356 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006357#endif
Fred Drakec9680921999-12-13 16:37:25 +00006358#ifdef _SC_INT_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006359 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006360#endif
6361#ifdef _SC_INT_MIN
Victor Stinner97b89882010-05-06 00:25:39 +00006362 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006363#endif
6364#ifdef _SC_IOV_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006365 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006366#endif
Fred Draked86ed291999-12-15 15:34:33 +00006367#ifdef _SC_IP_SECOPTS
Victor Stinner97b89882010-05-06 00:25:39 +00006368 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006369#endif
Fred Drakec9680921999-12-13 16:37:25 +00006370#ifdef _SC_JOB_CONTROL
Victor Stinner97b89882010-05-06 00:25:39 +00006371 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006372#endif
Fred Draked86ed291999-12-15 15:34:33 +00006373#ifdef _SC_KERN_POINTERS
Victor Stinner97b89882010-05-06 00:25:39 +00006374 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006375#endif
6376#ifdef _SC_KERN_SIM
Victor Stinner97b89882010-05-06 00:25:39 +00006377 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006378#endif
Fred Drakec9680921999-12-13 16:37:25 +00006379#ifdef _SC_LINE_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006380 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006381#endif
6382#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006383 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006384#endif
6385#ifdef _SC_LOGNAME_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006386 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006387#endif
6388#ifdef _SC_LONG_BIT
Victor Stinner97b89882010-05-06 00:25:39 +00006389 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006390#endif
Fred Draked86ed291999-12-15 15:34:33 +00006391#ifdef _SC_MAC
Victor Stinner97b89882010-05-06 00:25:39 +00006392 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00006393#endif
Fred Drakec9680921999-12-13 16:37:25 +00006394#ifdef _SC_MAPPED_FILES
Victor Stinner97b89882010-05-06 00:25:39 +00006395 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00006396#endif
6397#ifdef _SC_MAXPID
Victor Stinner97b89882010-05-06 00:25:39 +00006398 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00006399#endif
6400#ifdef _SC_MB_LEN_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006401 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006402#endif
6403#ifdef _SC_MEMLOCK
Victor Stinner97b89882010-05-06 00:25:39 +00006404 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00006405#endif
6406#ifdef _SC_MEMLOCK_RANGE
Victor Stinner97b89882010-05-06 00:25:39 +00006407 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00006408#endif
6409#ifdef _SC_MEMORY_PROTECTION
Victor Stinner97b89882010-05-06 00:25:39 +00006410 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00006411#endif
6412#ifdef _SC_MESSAGE_PASSING
Victor Stinner97b89882010-05-06 00:25:39 +00006413 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00006414#endif
Fred Draked86ed291999-12-15 15:34:33 +00006415#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner97b89882010-05-06 00:25:39 +00006416 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00006417#endif
Fred Drakec9680921999-12-13 16:37:25 +00006418#ifdef _SC_MQ_OPEN_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006419 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006420#endif
6421#ifdef _SC_MQ_PRIO_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006422 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006423#endif
Fred Draked86ed291999-12-15 15:34:33 +00006424#ifdef _SC_NACLS_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006425 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006426#endif
Fred Drakec9680921999-12-13 16:37:25 +00006427#ifdef _SC_NGROUPS_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006428 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006429#endif
6430#ifdef _SC_NL_ARGMAX
Victor Stinner97b89882010-05-06 00:25:39 +00006431 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006432#endif
6433#ifdef _SC_NL_LANGMAX
Victor Stinner97b89882010-05-06 00:25:39 +00006434 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006435#endif
6436#ifdef _SC_NL_MSGMAX
Victor Stinner97b89882010-05-06 00:25:39 +00006437 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006438#endif
6439#ifdef _SC_NL_NMAX
Victor Stinner97b89882010-05-06 00:25:39 +00006440 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006441#endif
6442#ifdef _SC_NL_SETMAX
Victor Stinner97b89882010-05-06 00:25:39 +00006443 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006444#endif
6445#ifdef _SC_NL_TEXTMAX
Victor Stinner97b89882010-05-06 00:25:39 +00006446 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006447#endif
6448#ifdef _SC_NPROCESSORS_CONF
Victor Stinner97b89882010-05-06 00:25:39 +00006449 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00006450#endif
6451#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner97b89882010-05-06 00:25:39 +00006452 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00006453#endif
Fred Draked86ed291999-12-15 15:34:33 +00006454#ifdef _SC_NPROC_CONF
Victor Stinner97b89882010-05-06 00:25:39 +00006455 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00006456#endif
6457#ifdef _SC_NPROC_ONLN
Victor Stinner97b89882010-05-06 00:25:39 +00006458 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00006459#endif
Fred Drakec9680921999-12-13 16:37:25 +00006460#ifdef _SC_NZERO
Victor Stinner97b89882010-05-06 00:25:39 +00006461 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00006462#endif
6463#ifdef _SC_OPEN_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006464 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006465#endif
6466#ifdef _SC_PAGESIZE
Victor Stinner97b89882010-05-06 00:25:39 +00006467 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006468#endif
6469#ifdef _SC_PAGE_SIZE
Victor Stinner97b89882010-05-06 00:25:39 +00006470 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006471#endif
6472#ifdef _SC_PASS_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006473 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006474#endif
6475#ifdef _SC_PHYS_PAGES
Victor Stinner97b89882010-05-06 00:25:39 +00006476 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006477#endif
6478#ifdef _SC_PII
Victor Stinner97b89882010-05-06 00:25:39 +00006479 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00006480#endif
6481#ifdef _SC_PII_INTERNET
Victor Stinner97b89882010-05-06 00:25:39 +00006482 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00006483#endif
6484#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner97b89882010-05-06 00:25:39 +00006485 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00006486#endif
6487#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner97b89882010-05-06 00:25:39 +00006488 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00006489#endif
6490#ifdef _SC_PII_OSI
Victor Stinner97b89882010-05-06 00:25:39 +00006491 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00006492#endif
6493#ifdef _SC_PII_OSI_CLTS
Victor Stinner97b89882010-05-06 00:25:39 +00006494 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00006495#endif
6496#ifdef _SC_PII_OSI_COTS
Victor Stinner97b89882010-05-06 00:25:39 +00006497 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00006498#endif
6499#ifdef _SC_PII_OSI_M
Victor Stinner97b89882010-05-06 00:25:39 +00006500 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00006501#endif
6502#ifdef _SC_PII_SOCKET
Victor Stinner97b89882010-05-06 00:25:39 +00006503 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00006504#endif
6505#ifdef _SC_PII_XTI
Victor Stinner97b89882010-05-06 00:25:39 +00006506 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00006507#endif
6508#ifdef _SC_POLL
Victor Stinner97b89882010-05-06 00:25:39 +00006509 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00006510#endif
6511#ifdef _SC_PRIORITIZED_IO
Victor Stinner97b89882010-05-06 00:25:39 +00006512 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006513#endif
6514#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner97b89882010-05-06 00:25:39 +00006515 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006516#endif
6517#ifdef _SC_REALTIME_SIGNALS
Victor Stinner97b89882010-05-06 00:25:39 +00006518 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00006519#endif
6520#ifdef _SC_RE_DUP_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006521 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006522#endif
6523#ifdef _SC_RTSIG_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006524 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006525#endif
6526#ifdef _SC_SAVED_IDS
Victor Stinner97b89882010-05-06 00:25:39 +00006527 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00006528#endif
6529#ifdef _SC_SCHAR_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006530 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006531#endif
6532#ifdef _SC_SCHAR_MIN
Victor Stinner97b89882010-05-06 00:25:39 +00006533 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006534#endif
6535#ifdef _SC_SELECT
Victor Stinner97b89882010-05-06 00:25:39 +00006536 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00006537#endif
6538#ifdef _SC_SEMAPHORES
Victor Stinner97b89882010-05-06 00:25:39 +00006539 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00006540#endif
6541#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006542 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006543#endif
6544#ifdef _SC_SEM_VALUE_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006545 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006546#endif
6547#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner97b89882010-05-06 00:25:39 +00006548 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00006549#endif
6550#ifdef _SC_SHRT_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006551 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006552#endif
6553#ifdef _SC_SHRT_MIN
Victor Stinner97b89882010-05-06 00:25:39 +00006554 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006555#endif
6556#ifdef _SC_SIGQUEUE_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006557 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006558#endif
6559#ifdef _SC_SIGRT_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006560 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006561#endif
6562#ifdef _SC_SIGRT_MIN
Victor Stinner97b89882010-05-06 00:25:39 +00006563 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006564#endif
Fred Draked86ed291999-12-15 15:34:33 +00006565#ifdef _SC_SOFTPOWER
Victor Stinner97b89882010-05-06 00:25:39 +00006566 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00006567#endif
Fred Drakec9680921999-12-13 16:37:25 +00006568#ifdef _SC_SPLIT_CACHE
Victor Stinner97b89882010-05-06 00:25:39 +00006569 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00006570#endif
6571#ifdef _SC_SSIZE_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006572 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006573#endif
6574#ifdef _SC_STACK_PROT
Victor Stinner97b89882010-05-06 00:25:39 +00006575 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00006576#endif
6577#ifdef _SC_STREAM_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006578 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006579#endif
6580#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner97b89882010-05-06 00:25:39 +00006581 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006582#endif
6583#ifdef _SC_THREADS
Victor Stinner97b89882010-05-06 00:25:39 +00006584 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00006585#endif
6586#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner97b89882010-05-06 00:25:39 +00006587 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00006588#endif
6589#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner97b89882010-05-06 00:25:39 +00006590 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006591#endif
6592#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner97b89882010-05-06 00:25:39 +00006593 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00006594#endif
6595#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006596 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006597#endif
6598#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner97b89882010-05-06 00:25:39 +00006599 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006600#endif
6601#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner97b89882010-05-06 00:25:39 +00006602 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00006603#endif
6604#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner97b89882010-05-06 00:25:39 +00006605 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00006606#endif
6607#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner97b89882010-05-06 00:25:39 +00006608 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00006609#endif
6610#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner97b89882010-05-06 00:25:39 +00006611 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00006612#endif
6613#ifdef _SC_THREAD_STACK_MIN
Victor Stinner97b89882010-05-06 00:25:39 +00006614 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006615#endif
6616#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006617 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006618#endif
6619#ifdef _SC_TIMERS
Victor Stinner97b89882010-05-06 00:25:39 +00006620 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00006621#endif
6622#ifdef _SC_TIMER_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006623 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006624#endif
6625#ifdef _SC_TTY_NAME_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006626 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006627#endif
6628#ifdef _SC_TZNAME_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006629 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006630#endif
6631#ifdef _SC_T_IOV_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006632 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006633#endif
6634#ifdef _SC_UCHAR_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006635 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006636#endif
6637#ifdef _SC_UINT_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006638 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006639#endif
6640#ifdef _SC_UIO_MAXIOV
Victor Stinner97b89882010-05-06 00:25:39 +00006641 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00006642#endif
6643#ifdef _SC_ULONG_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006644 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006645#endif
6646#ifdef _SC_USHRT_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00006647 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006648#endif
6649#ifdef _SC_VERSION
Victor Stinner97b89882010-05-06 00:25:39 +00006650 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006651#endif
6652#ifdef _SC_WORD_BIT
Victor Stinner97b89882010-05-06 00:25:39 +00006653 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006654#endif
6655#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner97b89882010-05-06 00:25:39 +00006656 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00006657#endif
6658#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner97b89882010-05-06 00:25:39 +00006659 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00006660#endif
6661#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner97b89882010-05-06 00:25:39 +00006662 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00006663#endif
6664#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner97b89882010-05-06 00:25:39 +00006665 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00006666#endif
6667#ifdef _SC_XOPEN_CRYPT
Victor Stinner97b89882010-05-06 00:25:39 +00006668 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00006669#endif
6670#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner97b89882010-05-06 00:25:39 +00006671 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00006672#endif
6673#ifdef _SC_XOPEN_LEGACY
Victor Stinner97b89882010-05-06 00:25:39 +00006674 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00006675#endif
6676#ifdef _SC_XOPEN_REALTIME
Victor Stinner97b89882010-05-06 00:25:39 +00006677 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00006678#endif
6679#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner97b89882010-05-06 00:25:39 +00006680 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00006681#endif
6682#ifdef _SC_XOPEN_SHM
Victor Stinner97b89882010-05-06 00:25:39 +00006683 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00006684#endif
6685#ifdef _SC_XOPEN_UNIX
Victor Stinner97b89882010-05-06 00:25:39 +00006686 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00006687#endif
6688#ifdef _SC_XOPEN_VERSION
Victor Stinner97b89882010-05-06 00:25:39 +00006689 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006690#endif
6691#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner97b89882010-05-06 00:25:39 +00006692 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006693#endif
6694#ifdef _SC_XOPEN_XPG2
Victor Stinner97b89882010-05-06 00:25:39 +00006695 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00006696#endif
6697#ifdef _SC_XOPEN_XPG3
Victor Stinner97b89882010-05-06 00:25:39 +00006698 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00006699#endif
6700#ifdef _SC_XOPEN_XPG4
Victor Stinner97b89882010-05-06 00:25:39 +00006701 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00006702#endif
6703};
6704
6705static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006706conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006707{
6708 return conv_confname(arg, valuep, posix_constants_sysconf,
6709 sizeof(posix_constants_sysconf)
6710 / sizeof(struct constdef));
6711}
6712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006713PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006714"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006715Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006716
6717static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006718posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006719{
6720 PyObject *result = NULL;
6721 int name;
6722
6723 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6724 int value;
6725
6726 errno = 0;
6727 value = sysconf(name);
6728 if (value == -1 && errno != 0)
6729 posix_error();
6730 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006731 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006732 }
6733 return result;
6734}
6735#endif
6736
6737
Fred Drakebec628d1999-12-15 18:31:10 +00006738/* This code is used to ensure that the tables of configuration value names
6739 * are in sorted order as required by conv_confname(), and also to build the
6740 * the exported dictionaries that are used to publish information about the
6741 * names available on the host platform.
6742 *
6743 * Sorting the table at runtime ensures that the table is properly ordered
6744 * when used, even for platforms we're not able to test on. It also makes
6745 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006746 */
Fred Drakebec628d1999-12-15 18:31:10 +00006747
6748static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006749cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006750{
6751 const struct constdef *c1 =
Victor Stinner97b89882010-05-06 00:25:39 +00006752 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00006753 const struct constdef *c2 =
Victor Stinner97b89882010-05-06 00:25:39 +00006754 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00006755
6756 return strcmp(c1->name, c2->name);
6757}
6758
6759static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006760setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner97b89882010-05-06 00:25:39 +00006761 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006762{
Fred Drakebec628d1999-12-15 18:31:10 +00006763 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006764 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006765
6766 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6767 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006768 if (d == NULL)
Victor Stinner97b89882010-05-06 00:25:39 +00006769 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006770
Barry Warsaw3155db32000-04-13 15:20:40 +00006771 for (i=0; i < tablesize; ++i) {
Victor Stinner97b89882010-05-06 00:25:39 +00006772 PyObject *o = PyLong_FromLong(table[i].value);
6773 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6774 Py_XDECREF(o);
6775 Py_DECREF(d);
6776 return -1;
6777 }
6778 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006779 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006780 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006781}
6782
Fred Drakebec628d1999-12-15 18:31:10 +00006783/* Return -1 on failure, 0 on success. */
6784static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006785setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006786{
6787#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006788 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006789 sizeof(posix_constants_pathconf)
6790 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006791 "pathconf_names", module))
Victor Stinner97b89882010-05-06 00:25:39 +00006792 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006793#endif
6794#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006795 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006796 sizeof(posix_constants_confstr)
6797 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006798 "confstr_names", module))
Victor Stinner97b89882010-05-06 00:25:39 +00006799 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006800#endif
6801#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006802 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006803 sizeof(posix_constants_sysconf)
6804 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006805 "sysconf_names", module))
Victor Stinner97b89882010-05-06 00:25:39 +00006806 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006807#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006808 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006809}
Fred Draked86ed291999-12-15 15:34:33 +00006810
6811
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006812PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006813"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006814Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006815in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006816
6817static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006818posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006819{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006820 abort();
6821 /*NOTREACHED*/
6822 Py_FatalError("abort() called from Python code didn't abort!");
6823 return NULL;
6824}
Fred Drakebec628d1999-12-15 18:31:10 +00006825
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006826#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006827PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006828"startfile(filepath [, operation]) - Start a file with its associated\n\
6829application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006830\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006831When \"operation\" is not specified or \"open\", this acts like\n\
6832double-clicking the file in Explorer, or giving the file name as an\n\
6833argument to the DOS \"start\" command: the file is opened with whatever\n\
6834application (if any) its extension is associated.\n\
6835When another \"operation\" is given, it specifies what should be done with\n\
6836the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006837\n\
6838startfile returns as soon as the associated application is launched.\n\
6839There is no option to wait for the application to close, and no way\n\
6840to retrieve the application's exit status.\n\
6841\n\
6842The filepath is relative to the current directory. If you want to use\n\
6843an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006844the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006845
6846static PyObject *
6847win32_startfile(PyObject *self, PyObject *args)
6848{
Victor Stinner97b89882010-05-06 00:25:39 +00006849 PyObject *ofilepath;
6850 char *filepath;
6851 char *operation = NULL;
6852 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006853
Victor Stinner97b89882010-05-06 00:25:39 +00006854 if (unicode_file_names()) {
6855 PyObject *unipath, *woperation = NULL;
6856 if (!PyArg_ParseTuple(args, "U|s:startfile",
6857 &unipath, &operation)) {
6858 PyErr_Clear();
6859 goto normal;
6860 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006861
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006862
Victor Stinner97b89882010-05-06 00:25:39 +00006863 if (operation) {
6864 woperation = PyUnicode_DecodeASCII(operation,
6865 strlen(operation), NULL);
6866 if (!woperation) {
6867 PyErr_Clear();
6868 operation = NULL;
6869 goto normal;
6870 }
6871 }
6872
6873 Py_BEGIN_ALLOW_THREADS
6874 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6875 PyUnicode_AS_UNICODE(unipath),
6876 NULL, NULL, SW_SHOWNORMAL);
6877 Py_END_ALLOW_THREADS
6878
6879 Py_XDECREF(woperation);
6880 if (rc <= (HINSTANCE)32) {
6881 PyObject *errval = win32_error_unicode("startfile",
6882 PyUnicode_AS_UNICODE(unipath));
6883 return errval;
6884 }
6885 Py_INCREF(Py_None);
6886 return Py_None;
6887 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006888
6889normal:
Victor Stinner97b89882010-05-06 00:25:39 +00006890 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6891 PyUnicode_FSConverter, &ofilepath,
6892 &operation))
6893 return NULL;
6894 filepath = bytes2str(ofilepath, 1);
6895 Py_BEGIN_ALLOW_THREADS
6896 rc = ShellExecute((HWND)0, operation, filepath,
6897 NULL, NULL, SW_SHOWNORMAL);
6898 Py_END_ALLOW_THREADS
6899 if (rc <= (HINSTANCE)32) {
6900 PyObject *errval = win32_error("startfile", filepath);
6901 release_bytes(ofilepath);
6902 return errval;
6903 }
6904 release_bytes(ofilepath);
6905 Py_INCREF(Py_None);
6906 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006907}
6908#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006909
Martin v. Löwis438b5342002-12-27 10:16:42 +00006910#ifdef HAVE_GETLOADAVG
6911PyDoc_STRVAR(posix_getloadavg__doc__,
6912"getloadavg() -> (float, float, float)\n\n\
6913Return the number of processes in the system run queue averaged over\n\
6914the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6915was unobtainable");
6916
6917static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006918posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006919{
6920 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006921 if (getloadavg(loadavg, 3)!=3) {
Victor Stinner97b89882010-05-06 00:25:39 +00006922 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6923 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00006924 } else
Victor Stinner97b89882010-05-06 00:25:39 +00006925 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00006926}
6927#endif
6928
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006929#ifdef MS_WINDOWS
6930
6931PyDoc_STRVAR(win32_urandom__doc__,
6932"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006933Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006934
6935typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6936 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6937 DWORD dwFlags );
6938typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6939 BYTE *pbBuffer );
6940
6941static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006942/* This handle is never explicitly released. Instead, the operating
6943 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006944static HCRYPTPROV hCryptProv = 0;
6945
Tim Peters4ad82172004-08-30 17:02:04 +00006946static PyObject*
6947win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006948{
Victor Stinner97b89882010-05-06 00:25:39 +00006949 int howMany;
6950 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006951
Victor Stinner97b89882010-05-06 00:25:39 +00006952 /* Read arguments */
6953 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6954 return NULL;
6955 if (howMany < 0)
6956 return PyErr_Format(PyExc_ValueError,
6957 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006958
Victor Stinner97b89882010-05-06 00:25:39 +00006959 if (hCryptProv == 0) {
6960 HINSTANCE hAdvAPI32 = NULL;
6961 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006962
Victor Stinner97b89882010-05-06 00:25:39 +00006963 /* Obtain handle to the DLL containing CryptoAPI
6964 This should not fail */
6965 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6966 if(hAdvAPI32 == NULL)
6967 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006968
Victor Stinner97b89882010-05-06 00:25:39 +00006969 /* Obtain pointers to the CryptoAPI functions
6970 This will fail on some early versions of Win95 */
6971 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6972 hAdvAPI32,
6973 "CryptAcquireContextA");
6974 if (pCryptAcquireContext == NULL)
6975 return PyErr_Format(PyExc_NotImplementedError,
6976 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006977
Victor Stinner97b89882010-05-06 00:25:39 +00006978 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6979 hAdvAPI32, "CryptGenRandom");
6980 if (pCryptGenRandom == NULL)
6981 return PyErr_Format(PyExc_NotImplementedError,
6982 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006983
Victor Stinner97b89882010-05-06 00:25:39 +00006984 /* Acquire context */
6985 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6986 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6987 return win32_error("CryptAcquireContext", NULL);
6988 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006989
Victor Stinner97b89882010-05-06 00:25:39 +00006990 /* Allocate bytes */
6991 result = PyBytes_FromStringAndSize(NULL, howMany);
6992 if (result != NULL) {
6993 /* Get random data */
6994 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
6995 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
6996 PyBytes_AS_STRING(result))) {
6997 Py_DECREF(result);
6998 return win32_error("CryptGenRandom", NULL);
6999 }
7000 }
7001 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007002}
7003#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007004
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007005PyDoc_STRVAR(device_encoding__doc__,
7006"device_encoding(fd) -> str\n\n\
7007Return a string describing the encoding of the device\n\
7008if the output is a terminal; else return None.");
7009
7010static PyObject *
7011device_encoding(PyObject *self, PyObject *args)
7012{
Victor Stinner97b89882010-05-06 00:25:39 +00007013 int fd;
7014 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7015 return NULL;
7016 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7017 Py_INCREF(Py_None);
7018 return Py_None;
7019 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007020#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner97b89882010-05-06 00:25:39 +00007021 if (fd == 0) {
7022 char buf[100];
7023 sprintf(buf, "cp%d", GetConsoleCP());
7024 return PyUnicode_FromString(buf);
7025 }
7026 if (fd == 1 || fd == 2) {
7027 char buf[100];
7028 sprintf(buf, "cp%d", GetConsoleOutputCP());
7029 return PyUnicode_FromString(buf);
7030 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007031#elif defined(CODESET)
Victor Stinner97b89882010-05-06 00:25:39 +00007032 {
7033 char *codeset = nl_langinfo(CODESET);
7034 if (codeset != NULL && codeset[0] != 0)
7035 return PyUnicode_FromString(codeset);
7036 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007037#endif
Victor Stinner97b89882010-05-06 00:25:39 +00007038 Py_INCREF(Py_None);
7039 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007040}
7041
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007042#ifdef __VMS
7043/* Use openssl random routine */
7044#include <openssl/rand.h>
7045PyDoc_STRVAR(vms_urandom__doc__,
7046"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007047Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007048
7049static PyObject*
7050vms_urandom(PyObject *self, PyObject *args)
7051{
Victor Stinner97b89882010-05-06 00:25:39 +00007052 int howMany;
7053 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007054
Victor Stinner97b89882010-05-06 00:25:39 +00007055 /* Read arguments */
7056 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7057 return NULL;
7058 if (howMany < 0)
7059 return PyErr_Format(PyExc_ValueError,
7060 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007061
Victor Stinner97b89882010-05-06 00:25:39 +00007062 /* Allocate bytes */
7063 result = PyBytes_FromStringAndSize(NULL, howMany);
7064 if (result != NULL) {
7065 /* Get random data */
7066 if (RAND_pseudo_bytes((unsigned char*)
7067 PyBytes_AS_STRING(result),
7068 howMany) < 0) {
7069 Py_DECREF(result);
7070 return PyErr_Format(PyExc_ValueError,
7071 "RAND_pseudo_bytes");
7072 }
7073 }
7074 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007075}
7076#endif
7077
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007078static PyMethodDef posix_methods[] = {
Victor Stinner97b89882010-05-06 00:25:39 +00007079 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007080#ifdef HAVE_TTYNAME
Victor Stinner97b89882010-05-06 00:25:39 +00007081 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007082#endif
Victor Stinner97b89882010-05-06 00:25:39 +00007083 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007084#ifdef HAVE_CHFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00007085 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007086#endif /* HAVE_CHFLAGS */
Victor Stinner97b89882010-05-06 00:25:39 +00007087 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007088#ifdef HAVE_FCHMOD
Victor Stinner97b89882010-05-06 00:25:39 +00007089 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007090#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007091#ifdef HAVE_CHOWN
Victor Stinner97b89882010-05-06 00:25:39 +00007092 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007093#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007094#ifdef HAVE_LCHMOD
Victor Stinner97b89882010-05-06 00:25:39 +00007095 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007096#endif /* HAVE_LCHMOD */
7097#ifdef HAVE_FCHOWN
Victor Stinner97b89882010-05-06 00:25:39 +00007098 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007099#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007100#ifdef HAVE_LCHFLAGS
Victor Stinner97b89882010-05-06 00:25:39 +00007101 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007102#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007103#ifdef HAVE_LCHOWN
Victor Stinner97b89882010-05-06 00:25:39 +00007104 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007105#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007106#ifdef HAVE_CHROOT
Victor Stinner97b89882010-05-06 00:25:39 +00007107 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007108#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007109#ifdef HAVE_CTERMID
Victor Stinner97b89882010-05-06 00:25:39 +00007110 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007111#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007112#ifdef HAVE_GETCWD
Victor Stinner97b89882010-05-06 00:25:39 +00007113 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7114 METH_NOARGS, posix_getcwd__doc__},
7115 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7116 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007117#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007118#ifdef HAVE_LINK
Victor Stinner97b89882010-05-06 00:25:39 +00007119 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007120#endif /* HAVE_LINK */
Victor Stinner97b89882010-05-06 00:25:39 +00007121 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7122 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7123 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007124#ifdef HAVE_NICE
Victor Stinner97b89882010-05-06 00:25:39 +00007125 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007126#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007127#ifdef HAVE_READLINK
Victor Stinner97b89882010-05-06 00:25:39 +00007128 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007129#endif /* HAVE_READLINK */
Victor Stinner97b89882010-05-06 00:25:39 +00007130 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7131 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7132 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
7133 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007134#ifdef HAVE_SYMLINK
Victor Stinner97b89882010-05-06 00:25:39 +00007135 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007136#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007137#ifdef HAVE_SYSTEM
Victor Stinner97b89882010-05-06 00:25:39 +00007138 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007139#endif
Victor Stinner97b89882010-05-06 00:25:39 +00007140 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007141#ifdef HAVE_UNAME
Victor Stinner97b89882010-05-06 00:25:39 +00007142 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007143#endif /* HAVE_UNAME */
Victor Stinner97b89882010-05-06 00:25:39 +00007144 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7145 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7146 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007147#ifdef HAVE_TIMES
Victor Stinner97b89882010-05-06 00:25:39 +00007148 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007149#endif /* HAVE_TIMES */
Victor Stinner97b89882010-05-06 00:25:39 +00007150 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007151#ifdef HAVE_EXECV
Victor Stinner97b89882010-05-06 00:25:39 +00007152 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7153 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007154#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007155#ifdef HAVE_SPAWNV
Victor Stinner97b89882010-05-06 00:25:39 +00007156 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7157 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007158#if defined(PYOS_OS2)
Victor Stinner97b89882010-05-06 00:25:39 +00007159 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7160 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007161#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007162#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007163#ifdef HAVE_FORK1
Victor Stinner97b89882010-05-06 00:25:39 +00007164 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007165#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007166#ifdef HAVE_FORK
Victor Stinner97b89882010-05-06 00:25:39 +00007167 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007168#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007169#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner97b89882010-05-06 00:25:39 +00007170 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007171#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007172#ifdef HAVE_FORKPTY
Victor Stinner97b89882010-05-06 00:25:39 +00007173 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007174#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007175#ifdef HAVE_GETEGID
Victor Stinner97b89882010-05-06 00:25:39 +00007176 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007177#endif /* HAVE_GETEGID */
7178#ifdef HAVE_GETEUID
Victor Stinner97b89882010-05-06 00:25:39 +00007179 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007180#endif /* HAVE_GETEUID */
7181#ifdef HAVE_GETGID
Victor Stinner97b89882010-05-06 00:25:39 +00007182 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007183#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007184#ifdef HAVE_GETGROUPS
Victor Stinner97b89882010-05-06 00:25:39 +00007185 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007186#endif
Victor Stinner97b89882010-05-06 00:25:39 +00007187 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007188#ifdef HAVE_GETPGRP
Victor Stinner97b89882010-05-06 00:25:39 +00007189 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007190#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007191#ifdef HAVE_GETPPID
Victor Stinner97b89882010-05-06 00:25:39 +00007192 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007193#endif /* HAVE_GETPPID */
7194#ifdef HAVE_GETUID
Victor Stinner97b89882010-05-06 00:25:39 +00007195 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007196#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007197#ifdef HAVE_GETLOGIN
Victor Stinner97b89882010-05-06 00:25:39 +00007198 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007199#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007200#ifdef HAVE_KILL
Victor Stinner97b89882010-05-06 00:25:39 +00007201 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007202#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007203#ifdef HAVE_KILLPG
Victor Stinner97b89882010-05-06 00:25:39 +00007204 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007205#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007206#ifdef HAVE_PLOCK
Victor Stinner97b89882010-05-06 00:25:39 +00007207 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007208#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007209#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00007210 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007211#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007212#ifdef HAVE_SETUID
Victor Stinner97b89882010-05-06 00:25:39 +00007213 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007214#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007215#ifdef HAVE_SETEUID
Victor Stinner97b89882010-05-06 00:25:39 +00007216 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007217#endif /* HAVE_SETEUID */
7218#ifdef HAVE_SETEGID
Victor Stinner97b89882010-05-06 00:25:39 +00007219 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007220#endif /* HAVE_SETEGID */
7221#ifdef HAVE_SETREUID
Victor Stinner97b89882010-05-06 00:25:39 +00007222 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007223#endif /* HAVE_SETREUID */
7224#ifdef HAVE_SETREGID
Victor Stinner97b89882010-05-06 00:25:39 +00007225 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007226#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007227#ifdef HAVE_SETGID
Victor Stinner97b89882010-05-06 00:25:39 +00007228 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007229#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007230#ifdef HAVE_SETGROUPS
Victor Stinner97b89882010-05-06 00:25:39 +00007231 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007232#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007233#ifdef HAVE_GETPGID
Victor Stinner97b89882010-05-06 00:25:39 +00007234 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007235#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007236#ifdef HAVE_SETPGRP
Victor Stinner97b89882010-05-06 00:25:39 +00007237 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007238#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007239#ifdef HAVE_WAIT
Victor Stinner97b89882010-05-06 00:25:39 +00007240 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007241#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007242#ifdef HAVE_WAIT3
Victor Stinner97b89882010-05-06 00:25:39 +00007243 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007244#endif /* HAVE_WAIT3 */
7245#ifdef HAVE_WAIT4
Victor Stinner97b89882010-05-06 00:25:39 +00007246 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007247#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007248#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner97b89882010-05-06 00:25:39 +00007249 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007250#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007251#ifdef HAVE_GETSID
Victor Stinner97b89882010-05-06 00:25:39 +00007252 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007253#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007254#ifdef HAVE_SETSID
Victor Stinner97b89882010-05-06 00:25:39 +00007255 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007256#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007257#ifdef HAVE_SETPGID
Victor Stinner97b89882010-05-06 00:25:39 +00007258 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007259#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007260#ifdef HAVE_TCGETPGRP
Victor Stinner97b89882010-05-06 00:25:39 +00007261 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007262#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007263#ifdef HAVE_TCSETPGRP
Victor Stinner97b89882010-05-06 00:25:39 +00007264 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007265#endif /* HAVE_TCSETPGRP */
Victor Stinner97b89882010-05-06 00:25:39 +00007266 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7267 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7268 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7269 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7270 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7271 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7272 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7273 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7274 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7275 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7276 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007277#ifdef HAVE_PIPE
Victor Stinner97b89882010-05-06 00:25:39 +00007278 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007279#endif
7280#ifdef HAVE_MKFIFO
Victor Stinner97b89882010-05-06 00:25:39 +00007281 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007282#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007283#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner97b89882010-05-06 00:25:39 +00007284 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007285#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007286#ifdef HAVE_DEVICE_MACROS
Victor Stinner97b89882010-05-06 00:25:39 +00007287 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7288 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7289 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007290#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007291#ifdef HAVE_FTRUNCATE
Victor Stinner97b89882010-05-06 00:25:39 +00007292 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007293#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007294#ifdef HAVE_PUTENV
Victor Stinner97b89882010-05-06 00:25:39 +00007295 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007296#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007297#ifdef HAVE_UNSETENV
Victor Stinner97b89882010-05-06 00:25:39 +00007298 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007299#endif
Victor Stinner97b89882010-05-06 00:25:39 +00007300 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007301#ifdef HAVE_FCHDIR
Victor Stinner97b89882010-05-06 00:25:39 +00007302 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007303#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007304#ifdef HAVE_FSYNC
Victor Stinner97b89882010-05-06 00:25:39 +00007305 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007306#endif
7307#ifdef HAVE_FDATASYNC
Victor Stinner97b89882010-05-06 00:25:39 +00007308 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007309#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007310#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007311#ifdef WCOREDUMP
Victor Stinner97b89882010-05-06 00:25:39 +00007312 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00007313#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007314#ifdef WIFCONTINUED
Victor Stinner97b89882010-05-06 00:25:39 +00007315 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007316#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007317#ifdef WIFSTOPPED
Victor Stinner97b89882010-05-06 00:25:39 +00007318 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007319#endif /* WIFSTOPPED */
7320#ifdef WIFSIGNALED
Victor Stinner97b89882010-05-06 00:25:39 +00007321 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007322#endif /* WIFSIGNALED */
7323#ifdef WIFEXITED
Victor Stinner97b89882010-05-06 00:25:39 +00007324 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007325#endif /* WIFEXITED */
7326#ifdef WEXITSTATUS
Victor Stinner97b89882010-05-06 00:25:39 +00007327 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007328#endif /* WEXITSTATUS */
7329#ifdef WTERMSIG
Victor Stinner97b89882010-05-06 00:25:39 +00007330 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007331#endif /* WTERMSIG */
7332#ifdef WSTOPSIG
Victor Stinner97b89882010-05-06 00:25:39 +00007333 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007334#endif /* WSTOPSIG */
7335#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007336#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner97b89882010-05-06 00:25:39 +00007337 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007338#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007339#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner97b89882010-05-06 00:25:39 +00007340 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007341#endif
Fred Drakec9680921999-12-13 16:37:25 +00007342#ifdef HAVE_CONFSTR
Victor Stinner97b89882010-05-06 00:25:39 +00007343 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007344#endif
7345#ifdef HAVE_SYSCONF
Victor Stinner97b89882010-05-06 00:25:39 +00007346 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007347#endif
7348#ifdef HAVE_FPATHCONF
Victor Stinner97b89882010-05-06 00:25:39 +00007349 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007350#endif
7351#ifdef HAVE_PATHCONF
Victor Stinner97b89882010-05-06 00:25:39 +00007352 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007353#endif
Victor Stinner97b89882010-05-06 00:25:39 +00007354 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007355#ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00007356 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00007357#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007358#ifdef HAVE_GETLOADAVG
Victor Stinner97b89882010-05-06 00:25:39 +00007359 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007360#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007361 #ifdef MS_WINDOWS
Victor Stinner97b89882010-05-06 00:25:39 +00007362 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007363 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007364 #ifdef __VMS
Victor Stinner97b89882010-05-06 00:25:39 +00007365 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007366 #endif
Victor Stinner97b89882010-05-06 00:25:39 +00007367 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007368};
7369
7370
Barry Warsaw4a342091996-12-19 23:50:02 +00007371static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007372ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007373{
Victor Stinner97b89882010-05-06 00:25:39 +00007374 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007375}
7376
Guido van Rossumd48f2521997-12-05 22:19:34 +00007377#if defined(PYOS_OS2)
7378/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007379static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007380{
7381 APIRET rc;
7382 ULONG values[QSV_MAX+1];
7383 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007384 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007385
7386 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007387 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007388 Py_END_ALLOW_THREADS
7389
7390 if (rc != NO_ERROR) {
7391 os2_error(rc);
7392 return -1;
7393 }
7394
Fred Drake4d1e64b2002-04-15 19:40:07 +00007395 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7396 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7397 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7398 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7399 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7400 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7401 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007402
7403 switch (values[QSV_VERSION_MINOR]) {
7404 case 0: ver = "2.00"; break;
7405 case 10: ver = "2.10"; break;
7406 case 11: ver = "2.11"; break;
7407 case 30: ver = "3.00"; break;
7408 case 40: ver = "4.00"; break;
7409 case 50: ver = "5.00"; break;
7410 default:
Tim Peters885d4572001-11-28 20:27:42 +00007411 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner97b89882010-05-06 00:25:39 +00007412 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00007413 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007414 ver = &tmp[0];
7415 }
7416
7417 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007418 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007419 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007420
7421 /* Add Indicator of Which Drive was Used to Boot the System */
7422 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7423 tmp[1] = ':';
7424 tmp[2] = '\0';
7425
Fred Drake4d1e64b2002-04-15 19:40:07 +00007426 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007427}
7428#endif
7429
Barry Warsaw4a342091996-12-19 23:50:02 +00007430static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007431all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007432{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007433#ifdef F_OK
Victor Stinner97b89882010-05-06 00:25:39 +00007434 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007435#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007436#ifdef R_OK
Victor Stinner97b89882010-05-06 00:25:39 +00007437 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007438#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007439#ifdef W_OK
Victor Stinner97b89882010-05-06 00:25:39 +00007440 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007441#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007442#ifdef X_OK
Victor Stinner97b89882010-05-06 00:25:39 +00007443 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007444#endif
Fred Drakec9680921999-12-13 16:37:25 +00007445#ifdef NGROUPS_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00007446 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00007447#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007448#ifdef TMP_MAX
Victor Stinner97b89882010-05-06 00:25:39 +00007449 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007450#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007451#ifdef WCONTINUED
Victor Stinner97b89882010-05-06 00:25:39 +00007452 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007453#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007454#ifdef WNOHANG
Victor Stinner97b89882010-05-06 00:25:39 +00007455 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007456#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007457#ifdef WUNTRACED
Victor Stinner97b89882010-05-06 00:25:39 +00007458 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007459#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007460#ifdef O_RDONLY
Victor Stinner97b89882010-05-06 00:25:39 +00007461 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007462#endif
7463#ifdef O_WRONLY
Victor Stinner97b89882010-05-06 00:25:39 +00007464 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007465#endif
7466#ifdef O_RDWR
Victor Stinner97b89882010-05-06 00:25:39 +00007467 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007468#endif
7469#ifdef O_NDELAY
Victor Stinner97b89882010-05-06 00:25:39 +00007470 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007471#endif
7472#ifdef O_NONBLOCK
Victor Stinner97b89882010-05-06 00:25:39 +00007473 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007474#endif
7475#ifdef O_APPEND
Victor Stinner97b89882010-05-06 00:25:39 +00007476 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007477#endif
7478#ifdef O_DSYNC
Victor Stinner97b89882010-05-06 00:25:39 +00007479 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007480#endif
7481#ifdef O_RSYNC
Victor Stinner97b89882010-05-06 00:25:39 +00007482 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007483#endif
7484#ifdef O_SYNC
Victor Stinner97b89882010-05-06 00:25:39 +00007485 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007486#endif
7487#ifdef O_NOCTTY
Victor Stinner97b89882010-05-06 00:25:39 +00007488 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007489#endif
7490#ifdef O_CREAT
Victor Stinner97b89882010-05-06 00:25:39 +00007491 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007492#endif
7493#ifdef O_EXCL
Victor Stinner97b89882010-05-06 00:25:39 +00007494 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007495#endif
7496#ifdef O_TRUNC
Victor Stinner97b89882010-05-06 00:25:39 +00007497 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007498#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007499#ifdef O_BINARY
Victor Stinner97b89882010-05-06 00:25:39 +00007500 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00007501#endif
7502#ifdef O_TEXT
Victor Stinner97b89882010-05-06 00:25:39 +00007503 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00007504#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007505#ifdef O_LARGEFILE
Victor Stinner97b89882010-05-06 00:25:39 +00007506 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007507#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007508#ifdef O_SHLOCK
Victor Stinner97b89882010-05-06 00:25:39 +00007509 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00007510#endif
7511#ifdef O_EXLOCK
Victor Stinner97b89882010-05-06 00:25:39 +00007512 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00007513#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007514
Tim Peters5aa91602002-01-30 05:46:57 +00007515/* MS Windows */
7516#ifdef O_NOINHERIT
Victor Stinner97b89882010-05-06 00:25:39 +00007517 /* Don't inherit in child processes. */
7518 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007519#endif
7520#ifdef _O_SHORT_LIVED
Victor Stinner97b89882010-05-06 00:25:39 +00007521 /* Optimize for short life (keep in memory). */
7522 /* MS forgot to define this one with a non-underscore form too. */
7523 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007524#endif
7525#ifdef O_TEMPORARY
Victor Stinner97b89882010-05-06 00:25:39 +00007526 /* Automatically delete when last handle is closed. */
7527 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007528#endif
7529#ifdef O_RANDOM
Victor Stinner97b89882010-05-06 00:25:39 +00007530 /* Optimize for random access. */
7531 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007532#endif
7533#ifdef O_SEQUENTIAL
Victor Stinner97b89882010-05-06 00:25:39 +00007534 /* Optimize for sequential access. */
7535 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007536#endif
7537
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007538/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007539#ifdef O_ASYNC
Victor Stinner97b89882010-05-06 00:25:39 +00007540 /* Send a SIGIO signal whenever input or output
7541 becomes available on file descriptor */
7542 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007543#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007544#ifdef O_DIRECT
Victor Stinner97b89882010-05-06 00:25:39 +00007545 /* Direct disk access. */
7546 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007547#endif
7548#ifdef O_DIRECTORY
Victor Stinner97b89882010-05-06 00:25:39 +00007549 /* Must be a directory. */
7550 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007551#endif
7552#ifdef O_NOFOLLOW
Victor Stinner97b89882010-05-06 00:25:39 +00007553 /* Do not follow links. */
7554 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007555#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007556#ifdef O_NOATIME
Victor Stinner97b89882010-05-06 00:25:39 +00007557 /* Do not update the access time. */
7558 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007559#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007560
Victor Stinner97b89882010-05-06 00:25:39 +00007561 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007562#ifdef EX_OK
Victor Stinner97b89882010-05-06 00:25:39 +00007563 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007564#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007565#ifdef EX_USAGE
Victor Stinner97b89882010-05-06 00:25:39 +00007566 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007567#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007568#ifdef EX_DATAERR
Victor Stinner97b89882010-05-06 00:25:39 +00007569 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007570#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007571#ifdef EX_NOINPUT
Victor Stinner97b89882010-05-06 00:25:39 +00007572 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007573#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007574#ifdef EX_NOUSER
Victor Stinner97b89882010-05-06 00:25:39 +00007575 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007576#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007577#ifdef EX_NOHOST
Victor Stinner97b89882010-05-06 00:25:39 +00007578 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007579#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007580#ifdef EX_UNAVAILABLE
Victor Stinner97b89882010-05-06 00:25:39 +00007581 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007582#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007583#ifdef EX_SOFTWARE
Victor Stinner97b89882010-05-06 00:25:39 +00007584 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007585#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007586#ifdef EX_OSERR
Victor Stinner97b89882010-05-06 00:25:39 +00007587 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007588#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007589#ifdef EX_OSFILE
Victor Stinner97b89882010-05-06 00:25:39 +00007590 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007591#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007592#ifdef EX_CANTCREAT
Victor Stinner97b89882010-05-06 00:25:39 +00007593 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007594#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007595#ifdef EX_IOERR
Victor Stinner97b89882010-05-06 00:25:39 +00007596 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007597#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007598#ifdef EX_TEMPFAIL
Victor Stinner97b89882010-05-06 00:25:39 +00007599 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007600#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007601#ifdef EX_PROTOCOL
Victor Stinner97b89882010-05-06 00:25:39 +00007602 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007603#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007604#ifdef EX_NOPERM
Victor Stinner97b89882010-05-06 00:25:39 +00007605 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007606#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007607#ifdef EX_CONFIG
Victor Stinner97b89882010-05-06 00:25:39 +00007608 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007609#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007610#ifdef EX_NOTFOUND
Victor Stinner97b89882010-05-06 00:25:39 +00007611 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007612#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007613
Guido van Rossum246bc171999-02-01 23:54:31 +00007614#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007615#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner97b89882010-05-06 00:25:39 +00007616 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7617 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7618 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7619 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7620 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7621 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7622 if (ins(d, "P_PM", (long)P_PM)) return -1;
7623 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7624 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7625 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7626 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7627 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7628 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7629 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7630 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7631 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7632 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7633 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7634 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7635 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007636#else
Victor Stinner97b89882010-05-06 00:25:39 +00007637 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7638 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7639 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7640 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7641 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007642#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007643#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007644
Guido van Rossumd48f2521997-12-05 22:19:34 +00007645#if defined(PYOS_OS2)
Victor Stinner97b89882010-05-06 00:25:39 +00007646 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007647#endif
Victor Stinner97b89882010-05-06 00:25:39 +00007648 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00007649}
7650
7651
Tim Peters5aa91602002-01-30 05:46:57 +00007652#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007653#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007654#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007655
7656#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007657#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007658#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007659
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007660#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007661#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007662#define MODNAME "posix"
7663#endif
7664
Martin v. Löwis1a214512008-06-11 05:26:20 +00007665static struct PyModuleDef posixmodule = {
Victor Stinner97b89882010-05-06 00:25:39 +00007666 PyModuleDef_HEAD_INIT,
7667 MODNAME,
7668 posix__doc__,
7669 -1,
7670 posix_methods,
7671 NULL,
7672 NULL,
7673 NULL,
7674 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00007675};
7676
7677
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007678PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007679INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007680{
Victor Stinner97b89882010-05-06 00:25:39 +00007681 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007682
Victor Stinner97b89882010-05-06 00:25:39 +00007683 m = PyModule_Create(&posixmodule);
7684 if (m == NULL)
7685 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007686
Victor Stinner97b89882010-05-06 00:25:39 +00007687 /* Initialize environ dictionary */
7688 v = convertenviron();
7689 Py_XINCREF(v);
7690 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
7691 return NULL;
7692 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007693
Victor Stinner97b89882010-05-06 00:25:39 +00007694 if (all_ins(m))
7695 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007696
Victor Stinner97b89882010-05-06 00:25:39 +00007697 if (setup_confname_tables(m))
7698 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007699
Victor Stinner97b89882010-05-06 00:25:39 +00007700 Py_INCREF(PyExc_OSError);
7701 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007702
Guido van Rossumb3d39562000-01-31 18:41:26 +00007703#ifdef HAVE_PUTENV
Victor Stinner97b89882010-05-06 00:25:39 +00007704 if (posix_putenv_garbage == NULL)
7705 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007706#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007707
Victor Stinner97b89882010-05-06 00:25:39 +00007708 if (!initialized) {
7709 stat_result_desc.name = MODNAME ".stat_result";
7710 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7711 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7712 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7713 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7714 structseq_new = StatResultType.tp_new;
7715 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007716
Victor Stinner97b89882010-05-06 00:25:39 +00007717 statvfs_result_desc.name = MODNAME ".statvfs_result";
7718 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007719#ifdef NEED_TICKS_PER_SECOND
7720# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner97b89882010-05-06 00:25:39 +00007721 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007722# elif defined(HZ)
Victor Stinner97b89882010-05-06 00:25:39 +00007723 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007724# else
Victor Stinner97b89882010-05-06 00:25:39 +00007725 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007726# endif
7727#endif
Victor Stinner97b89882010-05-06 00:25:39 +00007728 }
7729 Py_INCREF((PyObject*) &StatResultType);
7730 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
7731 Py_INCREF((PyObject*) &StatVFSResultType);
7732 PyModule_AddObject(m, "statvfs_result",
7733 (PyObject*) &StatVFSResultType);
7734 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007735
7736#ifdef __APPLE__
Victor Stinner97b89882010-05-06 00:25:39 +00007737 /*
7738 * Step 2 of weak-linking support on Mac OS X.
7739 *
7740 * The code below removes functions that are not available on the
7741 * currently active platform.
7742 *
7743 * This block allow one to use a python binary that was build on
7744 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7745 * OSX 10.4.
7746 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007747#ifdef HAVE_FSTATVFS
Victor Stinner97b89882010-05-06 00:25:39 +00007748 if (fstatvfs == NULL) {
7749 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7750 return NULL;
7751 }
7752 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007753#endif /* HAVE_FSTATVFS */
7754
7755#ifdef HAVE_STATVFS
Victor Stinner97b89882010-05-06 00:25:39 +00007756 if (statvfs == NULL) {
7757 if (PyObject_DelAttrString(m, "statvfs") == -1) {
7758 return NULL;
7759 }
7760 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007761#endif /* HAVE_STATVFS */
7762
7763# ifdef HAVE_LCHOWN
Victor Stinner97b89882010-05-06 00:25:39 +00007764 if (lchown == NULL) {
7765 if (PyObject_DelAttrString(m, "lchown") == -1) {
7766 return NULL;
7767 }
7768 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007769#endif /* HAVE_LCHOWN */
7770
7771
7772#endif /* __APPLE__ */
Victor Stinner97b89882010-05-06 00:25:39 +00007773 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007774
Guido van Rossumb6775db1994-08-01 11:34:53 +00007775}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007776
7777#ifdef __cplusplus
7778}
7779#endif