blob: 7f6568c82ceed72f1b5b8a83c269420f268a5287 [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 /*
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * 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
72#include <sys/wait.h> /* For WNOHANG */
73#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
Guido van Rossumc5a0f531997-12-02 20:36:02 +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
107#define HAVE_SYSTEM 1
108#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
114#ifdef __BORLANDC__ /* Borland compiler */
115#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
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_SYSTEM 1
120#define HAVE_WAIT 1
121#else
122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000124#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#define HAVE_EXECV 1
126#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000128#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000129#define HAVE_FSYNC 1
130#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 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#else /* all other compilers */
135/* Unix functions that the configure script doesn't check for */
136#define HAVE_EXECV 1
137#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000138#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
139#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
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150#define HAVE_SYSTEM 1
151#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +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>
Tim Petersee66d0c2002-07-15 16:10:55 +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)
Martin v. Löwis14694662006-02-03 12:54:16 +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
336# define STAT stat
337# define FSTAT fstat
338# define STRUCT_STAT struct stat
339#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
365 * by using the exported __pinfo data member and knowledge of the
366 * 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 {
377 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{
393 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
396 static int sizeof_ioinfo = 0;
397
398 /* 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 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000408
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 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000416 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
417 if (info->osfile & FOPEN) {
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000418 return 1;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000419 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000420 }
421 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000422 fail:
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000423 errno = EBADF;
424 return 0;
425}
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{
431 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;
439}
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{
Barry Warsaw53699e91996-12-10 23:23:01 +0000459 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000460#ifdef MS_WINDOWS
461 wchar_t **e;
462#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000464#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000465 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466 if (d == NULL)
467 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000468#ifdef WITH_NEXT_FRAMEWORK
469 if (environ == NULL)
470 environ = *_NSGetEnviron();
471#endif
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000472#ifdef MS_WINDOWS
473 /* _wenviron must be initialized in this way if the program is started
474 through main() instead of wmain(). */
475 _wgetenv(L"");
476 if (_wenviron == NULL)
477 return d;
478 /* This part ignores errors */
479 for (e = _wenviron; *e != NULL; e++) {
480 PyObject *k;
481 PyObject *v;
482 wchar_t *p = wcschr(*e, L'=');
483 if (p == NULL)
484 continue;
485 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
486 if (k == NULL) {
487 PyErr_Clear();
488 continue;
489 }
490 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
491 if (v == NULL) {
492 PyErr_Clear();
493 Py_DECREF(k);
494 continue;
495 }
496 if (PyDict_GetItem(d, k) == NULL) {
497 if (PyDict_SetItem(d, k, v) != 0)
498 PyErr_Clear();
499 }
500 Py_DECREF(k);
501 Py_DECREF(v);
502 }
503#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000504 if (environ == NULL)
505 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000506 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000507 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000508 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000509 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000510 char *p = strchr(*e, '=');
511 if (p == NULL)
512 continue;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000513 k = PyUnicode_Decode(*e, (int)(p-*e),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000514 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000515 if (k == NULL) {
516 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000518 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000519 v = PyUnicode_Decode(p+1, strlen(p+1),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000520 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000521 if (v == NULL) {
522 PyErr_Clear();
523 Py_DECREF(k);
524 continue;
525 }
526 if (PyDict_GetItem(d, k) == NULL) {
527 if (PyDict_SetItem(d, k, v) != 0)
528 PyErr_Clear();
529 }
530 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000531 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000533#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000534#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000535 {
536 APIRET rc;
537 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
538
539 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000540 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000541 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000542 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000543 Py_DECREF(v);
544 }
545 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
546 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000547 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000548 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000549 Py_DECREF(v);
550 }
551 }
552#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553 return d;
554}
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{
562 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 }
576}
577
578/* Release the lock, decref the object. */
579static void
580release_bytes(PyObject* o)
581{
582 if (PyByteArray_Check(o))
583 o->ob_type->tp_as_buffer->bf_releasebuffer(NULL, 0);
584 Py_DECREF(o);
585}
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{
Barry Warsawca74da41999-02-09 19:31:45 +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{
Barry Warsawca74da41999-02-09 19:31:45 +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{
605 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
606}
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{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000613 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError,
614 bytes2str(name, 0));
615 release_bytes(name);
Mark Hammondef8b6542001-05-13 08:04:26 +0000616 return rc;
617}
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{
Mark Hammond33a6da92000-08-15 00:46:38 +0000623 /* XXX We should pass the function name along in the future.
Georg Brandl38feaf02008-05-25 07:45:51 +0000624 (winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000625 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000626 Windows error object, which is non-trivial.
627 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000628 errno = GetLastError();
629 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000630 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000631 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000632 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{
638 /* 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);
644}
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{
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000649 if (PyUnicode_CheckExact(*param))
650 Py_INCREF(*param);
651 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000652 /* For a Unicode subtype that's not a Unicode object,
653 return a true Unicode object with the same data. */
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000654 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
655 PyUnicode_GET_SIZE(*param));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000656 else
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000657 *param = PyUnicode_FromEncodedObject(*param,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658 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 **********************************************************************/
669 static void
670os2_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 **********************************************************************/
699 static char *
700os2_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,
Tim Peters885d4572001-11-28 20:27:42 +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
724static PyObject * os2_error(int code)
725{
726 char text[1024];
727 PyObject *v;
728
729 os2_strerror(text, sizeof(text), code, "");
730
731 v = Py_BuildValue("(is)", code, text);
732 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000733 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000734 Py_DECREF(v);
735 }
736 return NULL; /* Signal to Python that an Exception is Pending */
737}
738
739#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000740
741/* POSIX generic methods */
742
Barry Warsaw53699e91996-12-10 23:23:01 +0000743static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000744posix_fildes(PyObject *fdobj, int (*func)(int))
745{
746 int fd;
747 int res;
748 fd = PyObject_AsFileDescriptor(fdobj);
749 if (fd < 0)
750 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000751 if (!_PyVerify_fd(fd))
752 return posix_error();
Fred Drake4d1e64b2002-04-15 19:40:07 +0000753 Py_BEGIN_ALLOW_THREADS
754 res = (*func)(fd);
755 Py_END_ALLOW_THREADS
756 if (res < 0)
757 return posix_error();
758 Py_INCREF(Py_None);
759 return Py_None;
760}
Guido van Rossum21142a01999-01-08 21:05:37 +0000761
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000762#ifdef MS_WINDOWS
Tim Peters11b23062003-04-23 02:39:17 +0000763static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000764unicode_file_names(void)
765{
766 static int canusewide = -1;
767 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000768 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000769 the Windows NT family. */
770 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
771 }
772 return canusewide;
773}
774#endif
Tim Peters11b23062003-04-23 02:39:17 +0000775
Guido van Rossum21142a01999-01-08 21:05:37 +0000776static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000778{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000779 PyObject *opath1 = NULL;
780 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000781 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000782 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000783 PyUnicode_FSConverter, &opath1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000784 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000785 path1 = bytes2str(opath1, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000786 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000787 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000788 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000789 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +0000790 return posix_error_with_allocated_filename(opath1);
791 release_bytes(opath1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000792 Py_INCREF(Py_None);
793 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000794}
795
Barry Warsaw53699e91996-12-10 23:23:01 +0000796static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000797posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000798 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000799 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000800{
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000801 PyObject *opath1 = NULL, *opath2 = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000802 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000803 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000804 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000805 PyUnicode_FSConverter, &opath1,
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000806 PyUnicode_FSConverter, &opath2)) {
807 Py_XDECREF(opath1);
808 Py_XDECREF(opath2);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000809 return NULL;
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000810 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000811 path1 = bytes2str(opath1, 1);
812 path2 = bytes2str(opath2, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000813 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000814 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000815 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +0000816 release_bytes(opath1);
817 release_bytes(opath2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000818 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000819 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000820 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000821 Py_INCREF(Py_None);
822 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000823}
824
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000825#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000826static PyObject*
827win32_1str(PyObject* args, char* func,
828 char* format, BOOL (__stdcall *funcA)(LPCSTR),
829 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
830{
831 PyObject *uni;
832 char *ansi;
833 BOOL result;
834 if (unicode_file_names()) {
835 if (!PyArg_ParseTuple(args, wformat, &uni))
836 PyErr_Clear();
837 else {
838 Py_BEGIN_ALLOW_THREADS
839 result = funcW(PyUnicode_AsUnicode(uni));
840 Py_END_ALLOW_THREADS
841 if (!result)
842 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
843 Py_INCREF(Py_None);
844 return Py_None;
845 }
846 }
847 if (!PyArg_ParseTuple(args, format, &ansi))
848 return NULL;
849 Py_BEGIN_ALLOW_THREADS
850 result = funcA(ansi);
851 Py_END_ALLOW_THREADS
852 if (!result)
853 return win32_error(func, ansi);
854 Py_INCREF(Py_None);
855 return Py_None;
856
857}
858
859/* This is a reimplementation of the C library's chdir function,
860 but one that produces Win32 errors instead of DOS error codes.
861 chdir is essentially a wrapper around SetCurrentDirectory; however,
862 it also needs to set "magic" environment variables indicating
863 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000864static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000865win32_chdir(LPCSTR path)
866{
867 char new_path[MAX_PATH+1];
868 int result;
869 char env[4] = "=x:";
870
871 if(!SetCurrentDirectoryA(path))
872 return FALSE;
873 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
874 if (!result)
875 return FALSE;
876 /* In the ANSI API, there should not be any paths longer
877 than MAX_PATH. */
878 assert(result <= MAX_PATH+1);
879 if (strncmp(new_path, "\\\\", 2) == 0 ||
880 strncmp(new_path, "//", 2) == 0)
881 /* UNC path, nothing to do. */
882 return TRUE;
883 env[1] = new_path[0];
884 return SetEnvironmentVariableA(env, new_path);
885}
886
887/* The Unicode version differs from the ANSI version
888 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000889static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000890win32_wchdir(LPCWSTR path)
891{
892 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
893 int result;
894 wchar_t env[4] = L"=x:";
895
896 if(!SetCurrentDirectoryW(path))
897 return FALSE;
898 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
899 if (!result)
900 return FALSE;
901 if (result > MAX_PATH+1) {
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000902 new_path = malloc(result * sizeof(wchar_t));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000903 if (!new_path) {
904 SetLastError(ERROR_OUTOFMEMORY);
905 return FALSE;
906 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000907 result = GetCurrentDirectoryW(result, new_path);
908 if (!result) {
909 free(new_path);
910 return FALSE;
911 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000912 }
913 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
914 wcsncmp(new_path, L"//", 2) == 0)
915 /* UNC path, nothing to do. */
916 return TRUE;
917 env[1] = new_path[0];
918 result = SetEnvironmentVariableW(env, new_path);
919 if (new_path != _new_path)
920 free(new_path);
921 return result;
922}
923#endif
924
Martin v. Löwis14694662006-02-03 12:54:16 +0000925#ifdef MS_WINDOWS
926/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
927 - time stamps are restricted to second resolution
928 - file modification times suffer from forth-and-back conversions between
929 UTC and local time
930 Therefore, we implement our own stat, based on the Win32 API directly.
931*/
932#define HAVE_STAT_NSEC 1
933
934struct win32_stat{
935 int st_dev;
936 __int64 st_ino;
937 unsigned short st_mode;
938 int st_nlink;
939 int st_uid;
940 int st_gid;
941 int st_rdev;
942 __int64 st_size;
943 int st_atime;
944 int st_atime_nsec;
945 int st_mtime;
946 int st_mtime_nsec;
947 int st_ctime;
948 int st_ctime_nsec;
949};
950
951static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
952
953static void
954FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
955{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
957 /* Cannot simply cast and dereference in_ptr,
958 since it might not be aligned properly */
959 __int64 in;
960 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000961 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
962 /* XXX Win32 supports time stamps past 2038; we currently don't */
963 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
964}
965
Thomas Wouters477c8d52006-05-27 19:21:47 +0000966static void
967time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
968{
969 /* XXX endianness */
970 __int64 out;
971 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000972 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000973 memcpy(out_ptr, &out, sizeof(out));
974}
975
Martin v. Löwis14694662006-02-03 12:54:16 +0000976/* Below, we *know* that ugo+r is 0444 */
977#if _S_IREAD != 0400
978#error Unsupported C library
979#endif
980static int
981attributes_to_mode(DWORD attr)
982{
983 int m = 0;
984 if (attr & FILE_ATTRIBUTE_DIRECTORY)
985 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
986 else
987 m |= _S_IFREG;
988 if (attr & FILE_ATTRIBUTE_READONLY)
989 m |= 0444;
990 else
991 m |= 0666;
992 return m;
993}
994
995static int
996attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
997{
998 memset(result, 0, sizeof(*result));
999 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1000 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1001 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1002 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1003 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1004
1005 return 0;
1006}
1007
Thomas Wouters89f507f2006-12-13 04:49:30 +00001008/* Emulate GetFileAttributesEx[AW] on Windows 95 */
1009static int checked = 0;
1010static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
1011static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
1012static void
1013check_gfax()
1014{
1015 HINSTANCE hKernel32;
1016 if (checked)
1017 return;
1018 checked = 1;
1019 hKernel32 = GetModuleHandle("KERNEL32");
1020 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
1021 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
1022}
1023
Guido van Rossumd8faa362007-04-27 19:54:29 +00001024static BOOL
1025attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1026{
1027 HANDLE hFindFile;
1028 WIN32_FIND_DATAA FileData;
1029 hFindFile = FindFirstFileA(pszFile, &FileData);
1030 if (hFindFile == INVALID_HANDLE_VALUE)
1031 return FALSE;
1032 FindClose(hFindFile);
1033 pfad->dwFileAttributes = FileData.dwFileAttributes;
1034 pfad->ftCreationTime = FileData.ftCreationTime;
1035 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1036 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1037 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1038 pfad->nFileSizeLow = FileData.nFileSizeLow;
1039 return TRUE;
1040}
1041
1042static BOOL
1043attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1044{
1045 HANDLE hFindFile;
1046 WIN32_FIND_DATAW FileData;
1047 hFindFile = FindFirstFileW(pszFile, &FileData);
1048 if (hFindFile == INVALID_HANDLE_VALUE)
1049 return FALSE;
1050 FindClose(hFindFile);
1051 pfad->dwFileAttributes = FileData.dwFileAttributes;
1052 pfad->ftCreationTime = FileData.ftCreationTime;
1053 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1054 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1055 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1056 pfad->nFileSizeLow = FileData.nFileSizeLow;
1057 return TRUE;
1058}
1059
Thomas Wouters89f507f2006-12-13 04:49:30 +00001060static BOOL WINAPI
1061Py_GetFileAttributesExA(LPCSTR pszFile,
1062 GET_FILEEX_INFO_LEVELS level,
1063 LPVOID pv)
1064{
1065 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001066 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1067 /* First try to use the system's implementation, if that is
1068 available and either succeeds to gives an error other than
1069 that it isn't implemented. */
1070 check_gfax();
1071 if (gfaxa) {
1072 result = gfaxa(pszFile, level, pv);
1073 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1074 return result;
1075 }
1076 /* It's either not present, or not implemented.
1077 Emulate using FindFirstFile. */
1078 if (level != GetFileExInfoStandard) {
1079 SetLastError(ERROR_INVALID_PARAMETER);
1080 return FALSE;
1081 }
1082 /* Use GetFileAttributes to validate that the file name
1083 does not contain wildcards (which FindFirstFile would
1084 accept). */
1085 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
1086 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001087 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001088}
1089
1090static BOOL WINAPI
1091Py_GetFileAttributesExW(LPCWSTR pszFile,
1092 GET_FILEEX_INFO_LEVELS level,
1093 LPVOID pv)
1094{
1095 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001096 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1097 /* First try to use the system's implementation, if that is
1098 available and either succeeds to gives an error other than
1099 that it isn't implemented. */
1100 check_gfax();
1101 if (gfaxa) {
1102 result = gfaxw(pszFile, level, pv);
1103 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1104 return result;
1105 }
1106 /* It's either not present, or not implemented.
1107 Emulate using FindFirstFile. */
1108 if (level != GetFileExInfoStandard) {
1109 SetLastError(ERROR_INVALID_PARAMETER);
1110 return FALSE;
1111 }
1112 /* Use GetFileAttributes to validate that the file name
1113 does not contain wildcards (which FindFirstFile would
1114 accept). */
1115 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
1116 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001117 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001118}
1119
Martin v. Löwis14694662006-02-03 12:54:16 +00001120static int
1121win32_stat(const char* path, struct win32_stat *result)
1122{
1123 WIN32_FILE_ATTRIBUTE_DATA info;
1124 int code;
1125 char *dot;
1126 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001127 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001128 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1129 /* Protocol violation: we explicitly clear errno, instead of
1130 setting it to a POSIX error. Callers should use GetLastError. */
1131 errno = 0;
1132 return -1;
1133 } else {
1134 /* Could not get attributes on open file. Fall back to
1135 reading the directory. */
1136 if (!attributes_from_dir(path, &info)) {
1137 /* Very strange. This should not fail now */
1138 errno = 0;
1139 return -1;
1140 }
1141 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001142 }
1143 code = attribute_data_to_stat(&info, result);
1144 if (code != 0)
1145 return code;
1146 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1147 dot = strrchr(path, '.');
1148 if (dot) {
1149 if (stricmp(dot, ".bat") == 0 ||
1150 stricmp(dot, ".cmd") == 0 ||
1151 stricmp(dot, ".exe") == 0 ||
1152 stricmp(dot, ".com") == 0)
1153 result->st_mode |= 0111;
1154 }
1155 return code;
1156}
1157
1158static int
1159win32_wstat(const wchar_t* path, struct win32_stat *result)
1160{
1161 int code;
1162 const wchar_t *dot;
1163 WIN32_FILE_ATTRIBUTE_DATA info;
1164 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001165 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001166 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1167 /* Protocol violation: we explicitly clear errno, instead of
1168 setting it to a POSIX error. Callers should use GetLastError. */
1169 errno = 0;
1170 return -1;
1171 } else {
1172 /* Could not get attributes on open file. Fall back to
1173 reading the directory. */
1174 if (!attributes_from_dir_w(path, &info)) {
1175 /* Very strange. This should not fail now */
1176 errno = 0;
1177 return -1;
1178 }
1179 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001180 }
1181 code = attribute_data_to_stat(&info, result);
1182 if (code < 0)
1183 return code;
1184 /* Set IFEXEC if it is an .exe, .bat, ... */
1185 dot = wcsrchr(path, '.');
1186 if (dot) {
1187 if (_wcsicmp(dot, L".bat") == 0 ||
1188 _wcsicmp(dot, L".cmd") == 0 ||
1189 _wcsicmp(dot, L".exe") == 0 ||
1190 _wcsicmp(dot, L".com") == 0)
1191 result->st_mode |= 0111;
1192 }
1193 return code;
1194}
1195
1196static int
1197win32_fstat(int file_number, struct win32_stat *result)
1198{
1199 BY_HANDLE_FILE_INFORMATION info;
1200 HANDLE h;
1201 int type;
1202
1203 h = (HANDLE)_get_osfhandle(file_number);
1204
1205 /* Protocol violation: we explicitly clear errno, instead of
1206 setting it to a POSIX error. Callers should use GetLastError. */
1207 errno = 0;
1208
1209 if (h == INVALID_HANDLE_VALUE) {
1210 /* This is really a C library error (invalid file handle).
1211 We set the Win32 error to the closes one matching. */
1212 SetLastError(ERROR_INVALID_HANDLE);
1213 return -1;
1214 }
1215 memset(result, 0, sizeof(*result));
1216
1217 type = GetFileType(h);
1218 if (type == FILE_TYPE_UNKNOWN) {
1219 DWORD error = GetLastError();
1220 if (error != 0) {
1221 return -1;
1222 }
1223 /* else: valid but unknown file */
1224 }
1225
1226 if (type != FILE_TYPE_DISK) {
1227 if (type == FILE_TYPE_CHAR)
1228 result->st_mode = _S_IFCHR;
1229 else if (type == FILE_TYPE_PIPE)
1230 result->st_mode = _S_IFIFO;
1231 return 0;
1232 }
1233
1234 if (!GetFileInformationByHandle(h, &info)) {
1235 return -1;
1236 }
1237
1238 /* similar to stat() */
1239 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1240 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1241 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1242 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1243 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1244 /* specific to fstat() */
1245 result->st_nlink = info.nNumberOfLinks;
1246 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1247 return 0;
1248}
1249
1250#endif /* MS_WINDOWS */
1251
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001252PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001253"stat_result: Result from stat or lstat.\n\n\
1254This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001255 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001256or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1257\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001258Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1259or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001260\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001261See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001262
1263static PyStructSequence_Field stat_result_fields[] = {
1264 {"st_mode", "protection bits"},
1265 {"st_ino", "inode"},
1266 {"st_dev", "device"},
1267 {"st_nlink", "number of hard links"},
1268 {"st_uid", "user ID of owner"},
1269 {"st_gid", "group ID of owner"},
1270 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001271 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1272 {NULL, "integer time of last access"},
1273 {NULL, "integer time of last modification"},
1274 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001275 {"st_atime", "time of last access"},
1276 {"st_mtime", "time of last modification"},
1277 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001278#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001279 {"st_blksize", "blocksize for filesystem I/O"},
1280#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001281#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001282 {"st_blocks", "number of blocks allocated"},
1283#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001284#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001285 {"st_rdev", "device type (if inode device)"},
1286#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001287#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1288 {"st_flags", "user defined flags for file"},
1289#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001290#ifdef HAVE_STRUCT_STAT_ST_GEN
1291 {"st_gen", "generation number"},
1292#endif
1293#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1294 {"st_birthtime", "time of creation"},
1295#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001296 {0}
1297};
1298
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001299#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001300#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001301#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001302#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001303#endif
1304
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001305#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001306#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1307#else
1308#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1309#endif
1310
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001311#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001312#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1313#else
1314#define ST_RDEV_IDX ST_BLOCKS_IDX
1315#endif
1316
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001317#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1318#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1319#else
1320#define ST_FLAGS_IDX ST_RDEV_IDX
1321#endif
1322
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001323#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001324#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001325#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001326#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001327#endif
1328
1329#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1330#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1331#else
1332#define ST_BIRTHTIME_IDX ST_GEN_IDX
1333#endif
1334
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001335static PyStructSequence_Desc stat_result_desc = {
1336 "stat_result", /* name */
1337 stat_result__doc__, /* doc */
1338 stat_result_fields,
1339 10
1340};
1341
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001342PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001343"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1344This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001345 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001346or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001347\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001348See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001349
1350static PyStructSequence_Field statvfs_result_fields[] = {
1351 {"f_bsize", },
1352 {"f_frsize", },
1353 {"f_blocks", },
1354 {"f_bfree", },
1355 {"f_bavail", },
1356 {"f_files", },
1357 {"f_ffree", },
1358 {"f_favail", },
1359 {"f_flag", },
1360 {"f_namemax",},
1361 {0}
1362};
1363
1364static PyStructSequence_Desc statvfs_result_desc = {
1365 "statvfs_result", /* name */
1366 statvfs_result__doc__, /* doc */
1367 statvfs_result_fields,
1368 10
1369};
1370
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001371static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001372static PyTypeObject StatResultType;
1373static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001374static newfunc structseq_new;
1375
1376static PyObject *
1377statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1378{
1379 PyStructSequence *result;
1380 int i;
1381
1382 result = (PyStructSequence*)structseq_new(type, args, kwds);
1383 if (!result)
1384 return NULL;
1385 /* If we have been initialized from a tuple,
1386 st_?time might be set to None. Initialize it
1387 from the int slots. */
1388 for (i = 7; i <= 9; i++) {
1389 if (result->ob_item[i+3] == Py_None) {
1390 Py_DECREF(Py_None);
1391 Py_INCREF(result->ob_item[i]);
1392 result->ob_item[i+3] = result->ob_item[i];
1393 }
1394 }
1395 return (PyObject*)result;
1396}
1397
1398
1399
1400/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001401static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001402
1403PyDoc_STRVAR(stat_float_times__doc__,
1404"stat_float_times([newval]) -> oldval\n\n\
1405Determine whether os.[lf]stat represents time stamps as float objects.\n\
1406If newval is True, future calls to stat() return floats, if it is False,\n\
1407future calls return ints. \n\
1408If newval is omitted, return the current setting.\n");
1409
1410static PyObject*
1411stat_float_times(PyObject* self, PyObject *args)
1412{
1413 int newval = -1;
1414 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1415 return NULL;
1416 if (newval == -1)
1417 /* Return old value */
1418 return PyBool_FromLong(_stat_float_times);
1419 _stat_float_times = newval;
1420 Py_INCREF(Py_None);
1421 return Py_None;
1422}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001423
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001424static void
1425fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1426{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001427 PyObject *fval,*ival;
1428#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001429 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001430#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001431 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001432#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001433 if (!ival)
1434 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001435 if (_stat_float_times) {
1436 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1437 } else {
1438 fval = ival;
1439 Py_INCREF(fval);
1440 }
1441 PyStructSequence_SET_ITEM(v, index, ival);
1442 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001443}
1444
Tim Peters5aa91602002-01-30 05:46:57 +00001445/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001446 (used by posix_stat() and posix_fstat()) */
1447static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001448_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001449{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001450 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001451 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001452 if (v == NULL)
1453 return NULL;
1454
Christian Heimes217cfd12007-12-02 14:31:20 +00001455 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001456#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001457 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001458 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001459#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001460 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001461#endif
1462#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001463 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001464 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001465#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001466 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001467#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00001468 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1469 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1470 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001471#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001472 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001473 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001474#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001475 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001476#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001477
Martin v. Löwis14694662006-02-03 12:54:16 +00001478#if defined(HAVE_STAT_TV_NSEC)
1479 ansec = st->st_atim.tv_nsec;
1480 mnsec = st->st_mtim.tv_nsec;
1481 cnsec = st->st_ctim.tv_nsec;
1482#elif defined(HAVE_STAT_TV_NSEC2)
1483 ansec = st->st_atimespec.tv_nsec;
1484 mnsec = st->st_mtimespec.tv_nsec;
1485 cnsec = st->st_ctimespec.tv_nsec;
1486#elif defined(HAVE_STAT_NSEC)
1487 ansec = st->st_atime_nsec;
1488 mnsec = st->st_mtime_nsec;
1489 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001490#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001491 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001492#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001493 fill_time(v, 7, st->st_atime, ansec);
1494 fill_time(v, 8, st->st_mtime, mnsec);
1495 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001496
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001497#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001498 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001499 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001500#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001501#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001502 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001503 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001504#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001505#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001506 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001507 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001508#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001509#ifdef HAVE_STRUCT_STAT_ST_GEN
1510 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001511 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001512#endif
1513#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1514 {
1515 PyObject *val;
1516 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001517 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001518#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001519 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001520#else
1521 bnsec = 0;
1522#endif
1523 if (_stat_float_times) {
1524 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1525 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001526 val = PyLong_FromLong((long)bsec);
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001527 }
1528 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1529 val);
1530 }
1531#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001532#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1533 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001534 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001535#endif
Fred Drake699f3522000-06-29 21:12:41 +00001536
1537 if (PyErr_Occurred()) {
1538 Py_DECREF(v);
1539 return NULL;
1540 }
1541
1542 return v;
1543}
1544
Martin v. Löwisd8948722004-06-02 09:57:56 +00001545#ifdef MS_WINDOWS
1546
1547/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1548 where / can be used in place of \ and the trailing slash is optional.
1549 Both SERVER and SHARE must have at least one character.
1550*/
1551
1552#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1553#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001554#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001555#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001556#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001557
Tim Peters4ad82172004-08-30 17:02:04 +00001558static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001559IsUNCRootA(char *path, int pathlen)
1560{
1561 #define ISSLASH ISSLASHA
1562
1563 int i, share;
1564
1565 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1566 /* minimum UNCRoot is \\x\y */
1567 return FALSE;
1568 for (i = 2; i < pathlen ; i++)
1569 if (ISSLASH(path[i])) break;
1570 if (i == 2 || i == pathlen)
1571 /* do not allow \\\SHARE or \\SERVER */
1572 return FALSE;
1573 share = i+1;
1574 for (i = share; i < pathlen; i++)
1575 if (ISSLASH(path[i])) break;
1576 return (i != share && (i == pathlen || i == pathlen-1));
1577
1578 #undef ISSLASH
1579}
1580
Tim Peters4ad82172004-08-30 17:02:04 +00001581static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001582IsUNCRootW(Py_UNICODE *path, int pathlen)
1583{
1584 #define ISSLASH ISSLASHW
1585
1586 int i, share;
1587
1588 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1589 /* minimum UNCRoot is \\x\y */
1590 return FALSE;
1591 for (i = 2; i < pathlen ; i++)
1592 if (ISSLASH(path[i])) break;
1593 if (i == 2 || i == pathlen)
1594 /* do not allow \\\SHARE or \\SERVER */
1595 return FALSE;
1596 share = i+1;
1597 for (i = share; i < pathlen; i++)
1598 if (ISSLASH(path[i])) break;
1599 return (i != share && (i == pathlen || i == pathlen-1));
1600
1601 #undef ISSLASH
1602}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001603#endif /* MS_WINDOWS */
1604
Barry Warsaw53699e91996-12-10 23:23:01 +00001605static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001606posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001607 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001608#ifdef __VMS
1609 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1610#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001611 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001612#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001613 char *wformat,
1614 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001615{
Fred Drake699f3522000-06-29 21:12:41 +00001616 STRUCT_STAT st;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001617 PyObject *opath;
1618 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001619 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001620 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001621
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001622#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001623 /* If on wide-character-capable OS see if argument
1624 is Unicode and if so use wide API. */
1625 if (unicode_file_names()) {
1626 PyUnicodeObject *po;
1627 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001628 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1629
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001630 Py_BEGIN_ALLOW_THREADS
1631 /* PyUnicode_AS_UNICODE result OK without
1632 thread lock as it is a simple dereference. */
1633 res = wstatfunc(wpath, &st);
1634 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001635
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001636 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001637 return win32_error_unicode("stat", wpath);
1638 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001639 }
1640 /* Drop the argument parsing error as narrow strings
1641 are also valid. */
1642 PyErr_Clear();
1643 }
1644#endif
1645
Tim Peters5aa91602002-01-30 05:46:57 +00001646 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +00001647 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001648 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001649 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00001650 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001651 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001652 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001653
1654 if (res != 0) {
1655#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001656 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001657#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001658 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001659#endif
1660 }
1661 else
1662 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001663
Martin v. Löwis011e8422009-05-05 04:43:17 +00001664 release_bytes(opath);
Martin v. Löwis14694662006-02-03 12:54:16 +00001665 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001666}
1667
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001668/* POSIX methods */
1669
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001670PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001671"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001672Use the real uid/gid to test for access to a path. Note that most\n\
1673operations will use the effective uid/gid, therefore this routine can\n\
1674be used in a suid/sgid environment to test if the invoking user has the\n\
1675specified access to the path. The mode argument can be F_OK to test\n\
1676existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001677
1678static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001679posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001680{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001681 PyObject *opath;
Guido van Rossum015f22a1999-01-06 22:52:38 +00001682 char *path;
1683 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001684
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001685#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001686 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001687 if (unicode_file_names()) {
1688 PyUnicodeObject *po;
1689 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1690 Py_BEGIN_ALLOW_THREADS
1691 /* PyUnicode_AS_UNICODE OK without thread lock as
1692 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001693 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001694 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001695 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001696 }
1697 /* Drop the argument parsing error as narrow strings
1698 are also valid. */
1699 PyErr_Clear();
1700 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001701 if (!PyArg_ParseTuple(args, "O&i:access",
1702 PyUnicode_FSConverter, &opath, &mode))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001703 return 0;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001704 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001705 Py_BEGIN_ALLOW_THREADS
1706 attr = GetFileAttributesA(path);
1707 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001708 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001709finish:
1710 if (attr == 0xFFFFFFFF)
1711 /* File does not exist, or cannot read attributes */
1712 return PyBool_FromLong(0);
1713 /* Access is possible if either write access wasn't requested, or
Guido van Rossumb00324f2007-12-04 01:13:14 +00001714 the file isn't read-only, or if it's a directory, as there are
1715 no read-only directories on Windows. */
1716 return PyBool_FromLong(!(mode & 2)
1717 || !(attr & FILE_ATTRIBUTE_READONLY)
1718 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001719#else
1720 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001721 if (!PyArg_ParseTuple(args, "O&i:access",
1722 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001723 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001724 path = bytes2str(opath, 1);
Guido van Rossum015f22a1999-01-06 22:52:38 +00001725 Py_BEGIN_ALLOW_THREADS
1726 res = access(path, mode);
1727 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001728 release_bytes(opath);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001729 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001730#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001731}
1732
Guido van Rossumd371ff11999-01-25 16:12:23 +00001733#ifndef F_OK
1734#define F_OK 0
1735#endif
1736#ifndef R_OK
1737#define R_OK 4
1738#endif
1739#ifndef W_OK
1740#define W_OK 2
1741#endif
1742#ifndef X_OK
1743#define X_OK 1
1744#endif
1745
1746#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001747PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001748"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001749Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001750
1751static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001752posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001753{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001754 int id;
1755 char *ret;
1756
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001757 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001758 return NULL;
1759
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001760#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001761 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001762 if (id == 0) {
1763 ret = ttyname();
1764 }
1765 else {
1766 ret = NULL;
1767 }
1768#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001769 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001770#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001771 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001772 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001773 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001774}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001775#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001776
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001777#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001778PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001779"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001780Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001781
1782static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001783posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001784{
1785 char *ret;
1786 char buffer[L_ctermid];
1787
Greg Wardb48bc172000-03-01 21:51:56 +00001788#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001789 ret = ctermid_r(buffer);
1790#else
1791 ret = ctermid(buffer);
1792#endif
1793 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001794 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001795 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001796}
1797#endif
1798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001800"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001801Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001802
Barry Warsaw53699e91996-12-10 23:23:01 +00001803static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001804posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001805{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001806#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00001807 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001808#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001809 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001810#elif defined(__VMS)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001811 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001812#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001813 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001814#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001815}
1816
Fred Drake4d1e64b2002-04-15 19:40:07 +00001817#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001818PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001819"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001820Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001821opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001822
1823static PyObject *
1824posix_fchdir(PyObject *self, PyObject *fdobj)
1825{
1826 return posix_fildes(fdobj, fchdir);
1827}
1828#endif /* HAVE_FCHDIR */
1829
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001830
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001831PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001832"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001833Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001834
Barry Warsaw53699e91996-12-10 23:23:01 +00001835static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001836posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001837{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001838 PyObject *opath = NULL;
Mark Hammondef8b6542001-05-13 08:04:26 +00001839 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001840 int i;
1841 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001842#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001843 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001844 if (unicode_file_names()) {
1845 PyUnicodeObject *po;
1846 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1847 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001848 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1849 if (attr != 0xFFFFFFFF) {
1850 if (i & _S_IWRITE)
1851 attr &= ~FILE_ATTRIBUTE_READONLY;
1852 else
1853 attr |= FILE_ATTRIBUTE_READONLY;
1854 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1855 }
1856 else
1857 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001858 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001859 if (!res)
1860 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001861 PyUnicode_AS_UNICODE(po));
1862 Py_INCREF(Py_None);
1863 return Py_None;
1864 }
1865 /* Drop the argument parsing error as narrow strings
1866 are also valid. */
1867 PyErr_Clear();
1868 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001869 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1870 &opath, &i))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001871 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001872 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001873 Py_BEGIN_ALLOW_THREADS
1874 attr = GetFileAttributesA(path);
1875 if (attr != 0xFFFFFFFF) {
1876 if (i & _S_IWRITE)
1877 attr &= ~FILE_ATTRIBUTE_READONLY;
1878 else
1879 attr |= FILE_ATTRIBUTE_READONLY;
1880 res = SetFileAttributesA(path, attr);
1881 }
1882 else
1883 res = 0;
1884 Py_END_ALLOW_THREADS
1885 if (!res) {
1886 win32_error("chmod", path);
Martin v. Löwis011e8422009-05-05 04:43:17 +00001887 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001888 return NULL;
1889 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001890 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001891 Py_INCREF(Py_None);
1892 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001893#else /* MS_WINDOWS */
Martin v. Löwis011e8422009-05-05 04:43:17 +00001894 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1895 &opath, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001896 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001897 path = bytes2str(opath, 1);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001898 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001899 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001900 Py_END_ALLOW_THREADS
1901 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001902 return posix_error_with_allocated_filename(opath);
1903 release_bytes(opath);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001904 Py_INCREF(Py_None);
1905 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001906#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001907}
1908
Christian Heimes4e30a842007-11-30 22:12:06 +00001909#ifdef HAVE_FCHMOD
1910PyDoc_STRVAR(posix_fchmod__doc__,
1911"fchmod(fd, mode)\n\n\
1912Change the access permissions of the file given by file\n\
1913descriptor fd.");
1914
1915static PyObject *
1916posix_fchmod(PyObject *self, PyObject *args)
1917{
1918 int fd, mode, res;
1919 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1920 return NULL;
1921 Py_BEGIN_ALLOW_THREADS
1922 res = fchmod(fd, mode);
1923 Py_END_ALLOW_THREADS
1924 if (res < 0)
1925 return posix_error();
1926 Py_RETURN_NONE;
1927}
1928#endif /* HAVE_FCHMOD */
1929
1930#ifdef HAVE_LCHMOD
1931PyDoc_STRVAR(posix_lchmod__doc__,
1932"lchmod(path, mode)\n\n\
1933Change the access permissions of a file. If path is a symlink, this\n\
1934affects the link itself rather than the target.");
1935
1936static PyObject *
1937posix_lchmod(PyObject *self, PyObject *args)
1938{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001939 PyObject *opath;
1940 char *path;
Christian Heimes4e30a842007-11-30 22:12:06 +00001941 int i;
1942 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001943 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1944 &opath, &i))
Christian Heimes4e30a842007-11-30 22:12:06 +00001945 return NULL;
Eric Smith86a05ec2009-05-05 13:07:30 +00001946 path = bytes2str(opath, 1);
Christian Heimes4e30a842007-11-30 22:12:06 +00001947 Py_BEGIN_ALLOW_THREADS
1948 res = lchmod(path, i);
1949 Py_END_ALLOW_THREADS
1950 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001951 return posix_error_with_allocated_filename(opath);
1952 release_bytes(opath);
Christian Heimes4e30a842007-11-30 22:12:06 +00001953 Py_RETURN_NONE;
1954}
1955#endif /* HAVE_LCHMOD */
1956
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001957
Thomas Wouterscf297e42007-02-23 15:07:44 +00001958#ifdef HAVE_CHFLAGS
1959PyDoc_STRVAR(posix_chflags__doc__,
1960"chflags(path, flags)\n\n\
1961Set file flags.");
1962
1963static PyObject *
1964posix_chflags(PyObject *self, PyObject *args)
1965{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001966 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001967 char *path;
1968 unsigned long flags;
1969 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001970 if (!PyArg_ParseTuple(args, "O&k:chflags",
1971 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00001972 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001973 path = bytes2str(opath, 1);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001974 Py_BEGIN_ALLOW_THREADS
1975 res = chflags(path, flags);
1976 Py_END_ALLOW_THREADS
1977 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001978 return posix_error_with_allocated_filename(opath);
1979 release_bytes(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001980 Py_INCREF(Py_None);
1981 return Py_None;
1982}
1983#endif /* HAVE_CHFLAGS */
1984
1985#ifdef HAVE_LCHFLAGS
1986PyDoc_STRVAR(posix_lchflags__doc__,
1987"lchflags(path, flags)\n\n\
1988Set file flags.\n\
1989This function will not follow symbolic links.");
1990
1991static PyObject *
1992posix_lchflags(PyObject *self, PyObject *args)
1993{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001994 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001995 char *path;
1996 unsigned long flags;
1997 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001998 if (!PyArg_ParseTuple(args, "O&k:lchflags",
Martin v. Löwis4adbc342009-05-05 17:17:55 +00001999 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00002000 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002001 path = bytes2str(opath, 1);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002002 Py_BEGIN_ALLOW_THREADS
2003 res = lchflags(path, flags);
2004 Py_END_ALLOW_THREADS
2005 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002006 return posix_error_with_allocated_filename(opath);
2007 release_bytes(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002008 Py_INCREF(Py_None);
2009 return Py_None;
2010}
2011#endif /* HAVE_LCHFLAGS */
2012
Martin v. Löwis244edc82001-10-04 22:44:26 +00002013#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002014PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002015"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002016Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002017
2018static PyObject *
2019posix_chroot(PyObject *self, PyObject *args)
2020{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002021 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002022}
2023#endif
2024
Guido van Rossum21142a01999-01-08 21:05:37 +00002025#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002026PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002027"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002028force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002029
2030static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002031posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002032{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002033 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002034}
2035#endif /* HAVE_FSYNC */
2036
2037#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002038
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002039#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002040extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2041#endif
2042
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002043PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002044"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002045force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002046 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002047
2048static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002049posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002050{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002051 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002052}
2053#endif /* HAVE_FDATASYNC */
2054
2055
Fredrik Lundh10723342000-07-10 16:38:09 +00002056#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002057PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002058"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002060
Barry Warsaw53699e91996-12-10 23:23:01 +00002061static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002062posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002063{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002064 PyObject *opath;
2065 char *path;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002066 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00002067 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002068 if (!PyArg_ParseTuple(args, "O&ll:chown",
2069 PyUnicode_FSConverter, &opath,
Mark Hammondef8b6542001-05-13 08:04:26 +00002070 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00002071 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002072 path = bytes2str(opath, 1);
Fredrik Lundh44328e62000-07-10 15:59:30 +00002073 Py_BEGIN_ALLOW_THREADS
2074 res = chown(path, (uid_t) uid, (gid_t) gid);
2075 Py_END_ALLOW_THREADS
2076 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002077 return posix_error_with_allocated_filename(opath);
2078 release_bytes(opath);
Fredrik Lundh44328e62000-07-10 15:59:30 +00002079 Py_INCREF(Py_None);
2080 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002081}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002082#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002083
Christian Heimes4e30a842007-11-30 22:12:06 +00002084#ifdef HAVE_FCHOWN
2085PyDoc_STRVAR(posix_fchown__doc__,
2086"fchown(fd, uid, gid)\n\n\
2087Change the owner and group id of the file given by file descriptor\n\
2088fd to the numeric uid and gid.");
2089
2090static PyObject *
2091posix_fchown(PyObject *self, PyObject *args)
2092{
2093 int fd, uid, gid;
2094 int res;
2095 if (!PyArg_ParseTuple(args, "iii: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;
2103}
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{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002115 PyObject *opath;
2116 char *path;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002117 int uid, gid;
2118 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002119 if (!PyArg_ParseTuple(args, "O&ii:lchown",
2120 PyUnicode_FSConverter, &opath,
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002121 &uid, &gid))
2122 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002123 path = bytes2str(opath, 1);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002124 Py_BEGIN_ALLOW_THREADS
2125 res = lchown(path, (uid_t) uid, (gid_t) gid);
2126 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00002127 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002128 return posix_error_with_allocated_filename(opath);
2129 release_bytes(opath);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002130 Py_INCREF(Py_None);
2131 return Py_None;
2132}
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{
2140 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
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002144 if (!use_bytes && unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002145 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00002146 wchar_t *wbuf2 = wbuf;
2147 PyObject *resobj;
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002148 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002149 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002150 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 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002159 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002160 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;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002171 }
2172#endif
2173
2174 Py_BEGIN_ALLOW_THREADS
2175#if defined(PYOS_OS2) && defined(PYCC_GCC)
2176 res = _getcwd2(buf, sizeof buf);
2177#else
2178 res = getcwd(buf, sizeof buf);
2179#endif
2180 Py_END_ALLOW_THREADS
2181 if (res == NULL)
2182 return posix_error();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002183 if (use_bytes)
2184 return PyBytes_FromStringAndSize(buf, strlen(buf));
Martin v. Löwis43c57782009-05-10 08:15:24 +00002185 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{
Martin v. Löwis011e8422009-05-05 04:43:17 +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\
2227 path: path of directory to list\n\
2228\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{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002235 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002236 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
Barry Warsaw53699e91996-12-10 23:23:01 +00002239 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002240 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002241 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002242 WIN32_FIND_DATA FileData;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002243 PyObject *opath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002244 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002245 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002246 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002247
Mark Hammondc2e85bd2002-10-03 05:10: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()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002251 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002252 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2253 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002254 Py_UNICODE *wnamebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002255 /* Overallocate for \\*.*\0 */
2256 len = PyUnicode_GET_SIZE(po);
2257 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2258 if (!wnamebuf) {
2259 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002260 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002261 }
2262 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002263 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 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002269 if ((d = PyList_New(0)) == NULL) {
2270 free(wnamebuf);
2271 return NULL;
2272 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002273 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2274 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002275 int error = GetLastError();
2276 if (error == ERROR_FILE_NOT_FOUND) {
2277 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002278 return d;
2279 }
2280 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002281 win32_error_unicode("FindFirstFileW", wnamebuf);
2282 free(wnamebuf);
2283 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002284 }
2285 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002286 /* Skip over . and .. */
2287 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2288 wcscmp(wFileData.cFileName, L"..") != 0) {
2289 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2290 if (v == NULL) {
2291 Py_DECREF(d);
2292 d = NULL;
2293 break;
2294 }
2295 if (PyList_Append(d, v) != 0) {
2296 Py_DECREF(v);
2297 Py_DECREF(d);
2298 d = NULL;
2299 break;
2300 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002301 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002302 }
Georg Brandl622927b2006-03-07 12:48:03 +00002303 Py_BEGIN_ALLOW_THREADS
2304 result = FindNextFileW(hFindFile, &wFileData);
2305 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002306 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2307 it got to the end of the directory. */
2308 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2309 Py_DECREF(d);
2310 win32_error_unicode("FindNextFileW", wnamebuf);
2311 FindClose(hFindFile);
2312 free(wnamebuf);
2313 return NULL;
2314 }
Georg Brandl622927b2006-03-07 12:48:03 +00002315 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002316
2317 if (FindClose(hFindFile) == FALSE) {
2318 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002319 win32_error_unicode("FindClose", wnamebuf);
2320 free(wnamebuf);
2321 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002322 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002323 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002324 return d;
2325 }
2326 /* Drop the argument parsing error as narrow strings
2327 are also valid. */
2328 PyErr_Clear();
2329 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002330
Martin v. Löwis011e8422009-05-05 04:43:17 +00002331 if (!PyArg_ParseTuple(args, "O&:listdir",
2332 PyUnicode_FSConverter, &opath))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002333 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002334 if (PyObject_Size(opath)+1 > MAX_PATH) {
2335 PyErr_SetString(PyExc_ValueError, "path too long");
2336 Py_DECREF(opath);
2337 return NULL;
2338 }
2339 strcpy(namebuf, bytes2str(opath, 0));
2340 len = PyObject_Size(opath);
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002341 if (len > 0) {
2342 char ch = namebuf[len-1];
2343 if (ch != SEP && ch != ALTSEP && ch != ':')
2344 namebuf[len++] = '/';
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002345 strcpy(namebuf + len, "*.*");
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002346 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002347
Barry Warsaw53699e91996-12-10 23:23:01 +00002348 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002349 return NULL;
2350
2351 hFindFile = FindFirstFile(namebuf, &FileData);
2352 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002353 int error = GetLastError();
2354 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002355 return d;
2356 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002357 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002358 }
2359 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002360 /* Skip over . and .. */
2361 if (strcmp(FileData.cFileName, ".") != 0 &&
2362 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002363 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002364 if (v == NULL) {
2365 Py_DECREF(d);
2366 d = NULL;
2367 break;
2368 }
2369 if (PyList_Append(d, v) != 0) {
2370 Py_DECREF(v);
2371 Py_DECREF(d);
2372 d = NULL;
2373 break;
2374 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002375 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002376 }
Georg Brandl622927b2006-03-07 12:48:03 +00002377 Py_BEGIN_ALLOW_THREADS
2378 result = FindNextFile(hFindFile, &FileData);
2379 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002380 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2381 it got to the end of the directory. */
2382 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2383 Py_DECREF(d);
2384 win32_error("FindNextFile", namebuf);
2385 FindClose(hFindFile);
2386 return NULL;
2387 }
Georg Brandl622927b2006-03-07 12:48:03 +00002388 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002389
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002390 if (FindClose(hFindFile) == FALSE) {
2391 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002392 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002393 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002394
2395 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002396
Tim Peters0bb44a42000-09-15 07:44:49 +00002397#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002398
2399#ifndef MAX_PATH
2400#define MAX_PATH CCHMAXPATH
2401#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002402 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002403 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002404 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002405 PyObject *d, *v;
2406 char namebuf[MAX_PATH+5];
2407 HDIR hdir = 1;
2408 ULONG srchcnt = 1;
2409 FILEFINDBUF3 ep;
2410 APIRET rc;
2411
Martin v. Löwis011e8422009-05-05 04:43:17 +00002412 if (!PyArg_ParseTuple(args, "O&:listdir",
2413 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002414 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002415 name = bytes2str(oname);
2416 len = PyObject_Size(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002417 if (len >= MAX_PATH) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002418 release_bytes(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002419 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002420 return NULL;
2421 }
2422 strcpy(namebuf, name);
2423 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002424 if (*pt == ALTSEP)
2425 *pt = SEP;
2426 if (namebuf[len-1] != SEP)
2427 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002428 strcpy(namebuf + len, "*.*");
2429
Neal Norwitz6c913782007-10-14 03:23:09 +00002430 if ((d = PyList_New(0)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002431 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002432 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002433 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002434
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002435 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2436 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002437 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002438 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2439 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2440 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002441
2442 if (rc != NO_ERROR) {
2443 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002444 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002445 }
2446
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002447 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002448 do {
2449 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002450 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002451 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002452
2453 strcpy(namebuf, ep.achName);
2454
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002455 /* Leave Case of Name Alone -- In Native Form */
2456 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002457
Christian Heimes72b710a2008-05-26 13:28:38 +00002458 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002459 if (v == NULL) {
2460 Py_DECREF(d);
2461 d = NULL;
2462 break;
2463 }
2464 if (PyList_Append(d, v) != 0) {
2465 Py_DECREF(v);
2466 Py_DECREF(d);
2467 d = NULL;
2468 break;
2469 }
2470 Py_DECREF(v);
2471 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2472 }
2473
Martin v. Löwis011e8422009-05-05 04:43:17 +00002474 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002475 return d;
2476#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002477 PyObject *oname;
2478 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +00002479 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002480 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002481 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002482 int arg_is_unicode = 1;
2483
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002484 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002485 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2486 arg_is_unicode = 0;
2487 PyErr_Clear();
2488 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002489 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002490 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002491 name = bytes2str(oname, 1);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002492 if ((dirp = opendir(name)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002493 return posix_error_with_allocated_filename(oname);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002494 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002495 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002496 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002497 release_bytes(oname);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002498 return NULL;
2499 }
Georg Brandl622927b2006-03-07 12:48:03 +00002500 for (;;) {
Georg Brandl3dbca812008-07-23 16:10:53 +00002501 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002502 Py_BEGIN_ALLOW_THREADS
2503 ep = readdir(dirp);
2504 Py_END_ALLOW_THREADS
Georg Brandl3dbca812008-07-23 16:10:53 +00002505 if (ep == NULL) {
2506 if (errno == 0) {
2507 break;
2508 } else {
2509 closedir(dirp);
2510 Py_DECREF(d);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002511 return posix_error_with_allocated_filename(oname);
Georg Brandl3dbca812008-07-23 16:10:53 +00002512 }
2513 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002514 if (ep->d_name[0] == '.' &&
2515 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002516 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002517 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002518 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002519 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002520 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002521 d = NULL;
2522 break;
2523 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002524 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002525 PyObject *w;
2526
2527 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002528 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00002529 "surrogateescape");
Martin v. Löwis011e8422009-05-05 04:43:17 +00002530 Py_DECREF(v);
2531 if (w != NULL)
Just van Rossum6a421832003-03-04 19:30:44 +00002532 v = w;
Just van Rossum6a421832003-03-04 19:30:44 +00002533 else {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002534 /* Encoding failed to decode ASCII bytes.
2535 Raise exception. */
2536 Py_DECREF(d);
2537 d = NULL;
2538 break;
Just van Rossum46c97842003-02-25 21:42:15 +00002539 }
Just van Rossum46c97842003-02-25 21:42:15 +00002540 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002541 if (PyList_Append(d, v) != 0) {
2542 Py_DECREF(v);
2543 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002544 d = NULL;
2545 break;
2546 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002547 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002548 }
2549 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002550 release_bytes(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002551
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002552 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002553
Tim Peters0bb44a42000-09-15 07:44:49 +00002554#endif /* which OS */
2555} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002556
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002557#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002558/* A helper function for abspath on win32 */
2559static PyObject *
2560posix__getfullpathname(PyObject *self, PyObject *args)
2561{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002562 PyObject *opath;
2563 char *path;
Mark Hammondef8b6542001-05-13 08:04:26 +00002564 char outbuf[MAX_PATH*2];
2565 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002566#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002567 if (unicode_file_names()) {
2568 PyUnicodeObject *po;
2569 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
Benjamin Petersonf608c612008-11-16 18:33:53 +00002570 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2571 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002572 Py_UNICODE *wtemp;
Benjamin Petersonf608c612008-11-16 18:33:53 +00002573 DWORD result;
2574 PyObject *v;
2575 result = GetFullPathNameW(wpath,
2576 sizeof(woutbuf)/sizeof(woutbuf[0]),
2577 woutbuf, &wtemp);
2578 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2579 woutbufp = malloc(result * sizeof(Py_UNICODE));
2580 if (!woutbufp)
2581 return PyErr_NoMemory();
2582 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2583 }
2584 if (result)
2585 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2586 else
2587 v = win32_error_unicode("GetFullPathNameW", wpath);
2588 if (woutbufp != woutbuf)
2589 free(woutbufp);
2590 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002591 }
2592 /* Drop the argument parsing error as narrow strings
2593 are also valid. */
2594 PyErr_Clear();
2595 }
2596#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002597 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2598 PyUnicode_FSConverter, &opath))
Mark Hammondef8b6542001-05-13 08:04:26 +00002599 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002600 path = bytes2str(opath, 1);
2601 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2602 outbuf, &temp)) {
2603 win32_error("GetFullPathName", path);
2604 release_bytes(opath);
2605 return NULL;
2606 }
2607 release_bytes(opath);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002608 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2609 return PyUnicode_Decode(outbuf, strlen(outbuf),
2610 Py_FileSystemDefaultEncoding, NULL);
2611 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002612 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002613} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002614#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002616PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002617"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002618Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002619
Barry Warsaw53699e91996-12-10 23:23:01 +00002620static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002621posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002622{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002623 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002624 PyObject *opath;
2625 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002626 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002627
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002628#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002629 if (unicode_file_names()) {
2630 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002631 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002632 Py_BEGIN_ALLOW_THREADS
2633 /* PyUnicode_AS_UNICODE OK without thread lock as
2634 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002635 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002636 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002637 if (!res)
2638 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002639 Py_INCREF(Py_None);
2640 return Py_None;
2641 }
2642 /* Drop the argument parsing error as narrow strings
2643 are also valid. */
2644 PyErr_Clear();
2645 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002646 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2647 PyUnicode_FSConverter, &opath, &mode))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002648 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002649 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002650 Py_BEGIN_ALLOW_THREADS
2651 /* PyUnicode_AS_UNICODE OK without thread lock as
2652 it is a simple dereference. */
2653 res = CreateDirectoryA(path, NULL);
2654 Py_END_ALLOW_THREADS
2655 if (!res) {
2656 win32_error("mkdir", path);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002657 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002658 return NULL;
2659 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002660 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002661 Py_INCREF(Py_None);
2662 return Py_None;
2663#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002664
Martin v. Löwis011e8422009-05-05 04:43:17 +00002665 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2666 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002667 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002668 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00002669 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002670#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002671 res = mkdir(path);
2672#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002673 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002674#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002675 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002676 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002677 return posix_error_with_allocated_filename(opath);
2678 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002679 Py_INCREF(Py_None);
2680 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002681#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002682}
2683
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002684
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002685/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2686#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002687#include <sys/resource.h>
2688#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002689
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002690
2691#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002692PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002693"nice(inc) -> new_priority\n\n\
2694Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002695
Barry Warsaw53699e91996-12-10 23:23:01 +00002696static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002697posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002698{
2699 int increment, value;
2700
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002701 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002702 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002703
2704 /* There are two flavours of 'nice': one that returns the new
2705 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002706 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2707 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002708
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002709 If we are of the nice family that returns the new priority, we
2710 need to clear errno before the call, and check if errno is filled
2711 before calling posix_error() on a returnvalue of -1, because the
2712 -1 may be the actual new priority! */
2713
2714 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002715 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002716#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002717 if (value == 0)
2718 value = getpriority(PRIO_PROCESS, 0);
2719#endif
2720 if (value == -1 && errno != 0)
2721 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002722 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002723 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002724}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002725#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002726
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002727PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002728"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002729Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002730
Barry Warsaw53699e91996-12-10 23:23:01 +00002731static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002732posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002733{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002734#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002735 PyObject *o1, *o2;
2736 char *p1, *p2;
2737 BOOL result;
2738 if (unicode_file_names()) {
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002739 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2740 goto error;
2741 if (!convert_to_unicode(&o1))
2742 goto error;
2743 if (!convert_to_unicode(&o2)) {
2744 Py_DECREF(o1);
2745 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002746 }
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002747 Py_BEGIN_ALLOW_THREADS
2748 result = MoveFileW(PyUnicode_AsUnicode(o1),
2749 PyUnicode_AsUnicode(o2));
2750 Py_END_ALLOW_THREADS
2751 Py_DECREF(o1);
2752 Py_DECREF(o2);
2753 if (!result)
2754 return win32_error("rename", NULL);
2755 Py_INCREF(Py_None);
2756 return Py_None;
2757error:
2758 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002759 }
2760 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2761 return NULL;
2762 Py_BEGIN_ALLOW_THREADS
2763 result = MoveFileA(p1, p2);
2764 Py_END_ALLOW_THREADS
2765 if (!result)
2766 return win32_error("rename", NULL);
2767 Py_INCREF(Py_None);
2768 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002769#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002770 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002771#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002772}
2773
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002774
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002775PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002776"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002777Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002778
Barry Warsaw53699e91996-12-10 23:23:01 +00002779static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002780posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002781{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002782#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002783 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002784#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002785 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002786#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002787}
2788
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002789
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002790PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002791"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002792Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002793
Barry Warsaw53699e91996-12-10 23:23:01 +00002794static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002795posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002796{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002797#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00002798 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002799#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002800 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002801#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002802}
2803
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002804
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002805#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002806PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002807"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002808Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002809
Barry Warsaw53699e91996-12-10 23:23:01 +00002810static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002811posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002812{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002813 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002814#ifdef MS_WINDOWS
2815 wchar_t *command;
2816 if (!PyArg_ParseTuple(args, "u:system", &command))
2817 return NULL;
2818#else
2819 char *command;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002820 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002821 return NULL;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002822#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002823 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002824#ifdef MS_WINDOWS
2825 sts = _wsystem(command);
2826#else
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002827 sts = system(command);
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002828#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002829 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +00002830 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002831}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002832#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002833
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002834
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002835PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002836"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002837Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002838
Barry Warsaw53699e91996-12-10 23:23:01 +00002839static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002840posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002841{
2842 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002843 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002844 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002845 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002846 if (i < 0)
2847 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002848 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002849}
2850
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002851
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002852PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002853"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002854Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002855
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002856PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002857"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002858Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002859
Barry Warsaw53699e91996-12-10 23:23:01 +00002860static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002861posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002862{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002863#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002864 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002865#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002866 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002867#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002868}
2869
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002870
Guido van Rossumb6775db1994-08-01 11:34:53 +00002871#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002872PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002873"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002874Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002875
Barry Warsaw53699e91996-12-10 23:23:01 +00002876static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002877posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002878{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002879 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002880 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002881
Barry Warsaw53699e91996-12-10 23:23:01 +00002882 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002883 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002884 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002885 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002886 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002887 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002888 u.sysname,
2889 u.nodename,
2890 u.release,
2891 u.version,
2892 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002893}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002894#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002895
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002896static int
2897extract_time(PyObject *t, long* sec, long* usec)
2898{
2899 long intval;
2900 if (PyFloat_Check(t)) {
2901 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002902 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002903 if (!intobj)
2904 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002905 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002906 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002907 if (intval == -1 && PyErr_Occurred())
2908 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002909 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002910 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002911 if (*usec < 0)
2912 /* If rounding gave us a negative number,
2913 truncate. */
2914 *usec = 0;
2915 return 0;
2916 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002917 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002918 if (intval == -1 && PyErr_Occurred())
2919 return -1;
2920 *sec = intval;
2921 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002922 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002923}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002924
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002925PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002926"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002927utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002928Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002929second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002930
Barry Warsaw53699e91996-12-10 23:23:01 +00002931static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002932posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002933{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002934#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002935 PyObject *arg;
2936 PyUnicodeObject *obwpath;
2937 wchar_t *wpath = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002938 PyObject *oapath;
2939 char *apath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002940 HANDLE hFile;
2941 long atimesec, mtimesec, ausec, musec;
2942 FILETIME atime, mtime;
2943 PyObject *result = NULL;
2944
2945 if (unicode_file_names()) {
2946 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2947 wpath = PyUnicode_AS_UNICODE(obwpath);
2948 Py_BEGIN_ALLOW_THREADS
2949 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002950 NULL, OPEN_EXISTING,
2951 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002952 Py_END_ALLOW_THREADS
2953 if (hFile == INVALID_HANDLE_VALUE)
2954 return win32_error_unicode("utime", wpath);
2955 } else
2956 /* Drop the argument parsing error as narrow strings
2957 are also valid. */
2958 PyErr_Clear();
2959 }
2960 if (!wpath) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002961 if (!PyArg_ParseTuple(args, "O&O:utime",
2962 PyUnicode_FSConverter, &oapath, &arg))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002963 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002964 apath = bytes2str(oapath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002965 Py_BEGIN_ALLOW_THREADS
2966 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002967 NULL, OPEN_EXISTING,
2968 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002969 Py_END_ALLOW_THREADS
2970 if (hFile == INVALID_HANDLE_VALUE) {
2971 win32_error("utime", apath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002972 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002973 return NULL;
2974 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002975 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002976 }
2977
2978 if (arg == Py_None) {
2979 SYSTEMTIME now;
2980 GetSystemTime(&now);
2981 if (!SystemTimeToFileTime(&now, &mtime) ||
2982 !SystemTimeToFileTime(&now, &atime)) {
2983 win32_error("utime", NULL);
2984 goto done;
2985 }
2986 }
2987 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2988 PyErr_SetString(PyExc_TypeError,
2989 "utime() arg 2 must be a tuple (atime, mtime)");
2990 goto done;
2991 }
2992 else {
2993 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2994 &atimesec, &ausec) == -1)
2995 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002996 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002997 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2998 &mtimesec, &musec) == -1)
2999 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003000 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003001 }
3002 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3003 /* Avoid putting the file name into the error here,
3004 as that may confuse the user into believing that
3005 something is wrong with the file, when it also
3006 could be the time stamp that gives a problem. */
3007 win32_error("utime", NULL);
3008 }
3009 Py_INCREF(Py_None);
3010 result = Py_None;
3011done:
3012 CloseHandle(hFile);
3013 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003014#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003015
Martin v. Löwis011e8422009-05-05 04:43:17 +00003016 PyObject *opath;
3017 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003018 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00003019 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00003020 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003021
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003022#if defined(HAVE_UTIMES)
3023 struct timeval buf[2];
3024#define ATIME buf[0].tv_sec
3025#define MTIME buf[1].tv_sec
3026#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003027/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003028 struct utimbuf buf;
3029#define ATIME buf.actime
3030#define MTIME buf.modtime
3031#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003032#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003033 time_t buf[2];
3034#define ATIME buf[0]
3035#define MTIME buf[1]
3036#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003037#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003038
Mark Hammond817c9292003-12-03 01:22:38 +00003039
Martin v. Löwis011e8422009-05-05 04:43:17 +00003040 if (!PyArg_ParseTuple(args, "O&O:utime",
3041 PyUnicode_FSConverter, &opath, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003042 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003043 path = bytes2str(opath, 1);
Barry Warsaw3cef8562000-05-01 16:17:24 +00003044 if (arg == Py_None) {
3045 /* optional time values not given */
3046 Py_BEGIN_ALLOW_THREADS
3047 res = utime(path, NULL);
3048 Py_END_ALLOW_THREADS
3049 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003050 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00003051 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003052 "utime() arg 2 must be a tuple (atime, mtime)");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003053 release_bytes(opath);
Barry Warsaw3cef8562000-05-01 16:17:24 +00003054 return NULL;
3055 }
3056 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003057 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00003058 &atime, &ausec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003059 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003060 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00003061 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003062 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00003063 &mtime, &musec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003064 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003065 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00003066 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00003067 ATIME = atime;
3068 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003069#ifdef HAVE_UTIMES
3070 buf[0].tv_usec = ausec;
3071 buf[1].tv_usec = musec;
3072 Py_BEGIN_ALLOW_THREADS
3073 res = utimes(path, buf);
3074 Py_END_ALLOW_THREADS
3075#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00003076 Py_BEGIN_ALLOW_THREADS
3077 res = utime(path, UTIME_ARG);
3078 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003079#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00003080 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00003081 if (res < 0) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003082 return posix_error_with_allocated_filename(opath);
Mark Hammond2d5914b2004-05-04 08:10:37 +00003083 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00003084 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00003085 Py_INCREF(Py_None);
3086 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003087#undef UTIME_ARG
3088#undef ATIME
3089#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003090#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003091}
3092
Guido van Rossum85e3b011991-06-03 12:42:10 +00003093
Guido van Rossum3b066191991-06-04 19:40:25 +00003094/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003096PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003097"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003098Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003099
Barry Warsaw53699e91996-12-10 23:23:01 +00003100static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003101posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003102{
3103 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003104 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003105 return NULL;
3106 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00003107 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003108}
3109
Martin v. Löwis114619e2002-10-07 06:44:21 +00003110#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3111static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003112free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003113{
Martin v. Löwis725507b2006-03-07 12:08:51 +00003114 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00003115 for (i = 0; i < count; i++)
3116 PyMem_Free(array[i]);
3117 PyMem_DEL(array);
3118}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003119
Antoine Pitrou69f71142009-05-24 21:25:49 +00003120static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003121int fsconvert_strdup(PyObject *o, char**out)
3122{
3123 PyObject *bytes;
3124 Py_ssize_t size;
3125 if (!PyUnicode_FSConverter(o, &bytes))
3126 return 0;
3127 size = PyObject_Size(bytes);
3128 *out = PyMem_Malloc(size+1);
3129 if (!*out)
3130 return 0;
3131 /* Don't lock bytes, as we hold the GIL */
3132 memcpy(*out, bytes2str(bytes, 0), size+1);
3133 Py_DECREF(bytes);
3134 return 1;
3135}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003136#endif
3137
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003138
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003139#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003140PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003141"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003142Execute an executable path with arguments, replacing current process.\n\
3143\n\
3144 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003145 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003146
Barry Warsaw53699e91996-12-10 23:23:01 +00003147static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003148posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003149{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003150 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003151 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003152 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003153 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003154 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003155 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003156
Guido van Rossum89b33251993-10-22 14:26:06 +00003157 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00003158 argv is a list or tuple of strings. */
3159
Martin v. Löwis011e8422009-05-05 04:43:17 +00003160 if (!PyArg_ParseTuple(args, "O&O:execv",
3161 PyUnicode_FSConverter,
3162 &opath, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003163 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003164 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003165 if (PyList_Check(argv)) {
3166 argc = PyList_Size(argv);
3167 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003168 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003169 else if (PyTuple_Check(argv)) {
3170 argc = PyTuple_Size(argv);
3171 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003172 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003173 else {
Fred Drake661ea262000-10-24 19:57:45 +00003174 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003175 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003176 return NULL;
3177 }
Thomas Heller6790d602007-08-30 17:15:14 +00003178 if (argc < 1) {
3179 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003180 release_bytes(opath);
Thomas Heller6790d602007-08-30 17:15:14 +00003181 return NULL;
3182 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003183
Barry Warsaw53699e91996-12-10 23:23:01 +00003184 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003185 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003186 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003187 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003188 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003189 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003190 if (!fsconvert_strdup((*getitem)(argv, i),
3191 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003192 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00003193 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003194 "execv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003195 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003196 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003197
Guido van Rossum85e3b011991-06-03 12:42:10 +00003198 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003199 }
3200 argvlist[argc] = NULL;
3201
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003202 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003203
Guido van Rossum85e3b011991-06-03 12:42:10 +00003204 /* If we get here it's definitely an error */
3205
Martin v. Löwis114619e2002-10-07 06:44:21 +00003206 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003207 release_bytes(opath);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003208 return posix_error();
3209}
3210
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003211
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003212PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003213"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003214Execute a path with arguments and environment, replacing current process.\n\
3215\n\
3216 path: path of executable file\n\
3217 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003218 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003219
Barry Warsaw53699e91996-12-10 23:23:01 +00003220static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003221posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003222{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003223 PyObject *opath;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003224 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003225 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003226 char **argvlist;
3227 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003228 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003229 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003230 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003231 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003232
3233 /* execve has three arguments: (path, argv, env), where
3234 argv is a list or tuple of strings and env is a dictionary
3235 like posix.environ. */
3236
Martin v. Löwis011e8422009-05-05 04:43:17 +00003237 if (!PyArg_ParseTuple(args, "O&OO:execve",
3238 PyUnicode_FSConverter,
3239 &opath, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003240 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003241 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003242 if (PyList_Check(argv)) {
3243 argc = PyList_Size(argv);
3244 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003245 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003246 else if (PyTuple_Check(argv)) {
3247 argc = PyTuple_Size(argv);
3248 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003249 }
3250 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003251 PyErr_SetString(PyExc_TypeError,
3252 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003253 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003254 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003255 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003256 PyErr_SetString(PyExc_TypeError,
3257 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003258 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003259 }
3260
Barry Warsaw53699e91996-12-10 23:23:01 +00003261 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003262 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003263 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003264 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003265 }
3266 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003267 if (!fsconvert_strdup((*getitem)(argv, i),
3268 &argvlist[i]))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003269 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003270 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003271 goto fail_1;
3272 }
3273 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003274 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003275 argvlist[argc] = NULL;
3276
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003277 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003278 if (i < 0)
3279 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003280 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003281 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003282 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003283 goto fail_1;
3284 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003285 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003286 keys = PyMapping_Keys(env);
3287 vals = PyMapping_Values(env);
3288 if (!keys || !vals)
3289 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003290 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3291 PyErr_SetString(PyExc_TypeError,
3292 "execve(): env.keys() or env.values() is not a list");
3293 goto fail_2;
3294 }
Tim Peters5aa91602002-01-30 05:46:57 +00003295
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003296 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003297 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003298 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003299
3300 key = PyList_GetItem(keys, pos);
3301 val = PyList_GetItem(vals, pos);
3302 if (!key || !val)
3303 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003304
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003305 if (!PyArg_Parse(
3306 key,
3307 "s;execve() arg 3 contains a non-string key",
3308 &k) ||
3309 !PyArg_Parse(
3310 val,
3311 "s;execve() arg 3 contains a non-string value",
3312 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003313 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003314 goto fail_2;
3315 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003316
3317#if defined(PYOS_OS2)
3318 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3319 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3320#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003321 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003322 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003323 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003324 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003325 goto fail_2;
3326 }
Tim Petersc8996f52001-12-03 20:41:00 +00003327 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003328 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003329#if defined(PYOS_OS2)
3330 }
3331#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003332 }
3333 envlist[envc] = 0;
3334
3335 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003336
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003337 /* If we get here it's definitely an error */
3338
3339 (void) posix_error();
3340
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003341 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003342 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003343 PyMem_DEL(envlist[envc]);
3344 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003345 fail_1:
3346 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003347 Py_XDECREF(vals);
3348 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003349 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003350 release_bytes(opath);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003351 return NULL;
3352}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003353#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003354
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003355
Guido van Rossuma1065681999-01-25 23:20:23 +00003356#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003357PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003358"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003359Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003360\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003361 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003362 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003363 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003364
3365static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003366posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003367{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003368 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003369 char *path;
3370 PyObject *argv;
3371 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003372 int mode, i;
3373 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003374 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003375 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003376
3377 /* spawnv has three arguments: (mode, path, argv), where
3378 argv is a list or tuple of strings. */
3379
Martin v. Löwis011e8422009-05-05 04:43:17 +00003380 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3381 PyUnicode_FSConverter,
3382 &opath, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003383 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003384 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003385 if (PyList_Check(argv)) {
3386 argc = PyList_Size(argv);
3387 getitem = PyList_GetItem;
3388 }
3389 else if (PyTuple_Check(argv)) {
3390 argc = PyTuple_Size(argv);
3391 getitem = PyTuple_GetItem;
3392 }
3393 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003394 PyErr_SetString(PyExc_TypeError,
3395 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003396 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003397 return NULL;
3398 }
3399
3400 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003401 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003402 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003403 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003404 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003405 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003406 if (!fsconvert_strdup((*getitem)(argv, i),
3407 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003408 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003409 PyErr_SetString(
3410 PyExc_TypeError,
3411 "spawnv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003412 release_bytes(opath);
Fred Drake137507e2000-06-01 02:02:46 +00003413 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003414 }
3415 }
3416 argvlist[argc] = NULL;
3417
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003418#if defined(PYOS_OS2) && defined(PYCC_GCC)
3419 Py_BEGIN_ALLOW_THREADS
3420 spawnval = spawnv(mode, path, argvlist);
3421 Py_END_ALLOW_THREADS
3422#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003423 if (mode == _OLD_P_OVERLAY)
3424 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003425
Tim Peters25059d32001-12-07 20:35:43 +00003426 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003427 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003428 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003429#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003430
Martin v. Löwis114619e2002-10-07 06:44:21 +00003431 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003432 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003433
Fred Drake699f3522000-06-29 21:12:41 +00003434 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003435 return posix_error();
3436 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003437#if SIZEOF_LONG == SIZEOF_VOID_P
3438 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003439#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003440 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003441#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003442}
3443
3444
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003445PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003446"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003447Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003448\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003449 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003450 path: path of executable file\n\
3451 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003452 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003453
3454static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003455posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003456{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003457 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003458 char *path;
3459 PyObject *argv, *env;
3460 char **argvlist;
3461 char **envlist;
3462 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003463 int mode, pos, envc;
3464 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003465 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003466 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003467 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003468
3469 /* spawnve has four arguments: (mode, path, argv, env), where
3470 argv is a list or tuple of strings and env is a dictionary
3471 like posix.environ. */
3472
Martin v. Löwis011e8422009-05-05 04:43:17 +00003473 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3474 PyUnicode_FSConverter,
3475 &opath, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003476 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003477 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003478 if (PyList_Check(argv)) {
3479 argc = PyList_Size(argv);
3480 getitem = PyList_GetItem;
3481 }
3482 else if (PyTuple_Check(argv)) {
3483 argc = PyTuple_Size(argv);
3484 getitem = PyTuple_GetItem;
3485 }
3486 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003487 PyErr_SetString(PyExc_TypeError,
3488 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003489 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003490 }
3491 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003492 PyErr_SetString(PyExc_TypeError,
3493 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003494 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003495 }
3496
3497 argvlist = PyMem_NEW(char *, argc+1);
3498 if (argvlist == NULL) {
3499 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003500 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003501 }
3502 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003503 if (!fsconvert_strdup((*getitem)(argv, i),
3504 &argvlist[i]))
Guido van Rossuma1065681999-01-25 23:20:23 +00003505 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003506 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003507 goto fail_1;
3508 }
3509 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003510 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003511 argvlist[argc] = NULL;
3512
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003513 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003514 if (i < 0)
3515 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003516 envlist = PyMem_NEW(char *, i + 1);
3517 if (envlist == NULL) {
3518 PyErr_NoMemory();
3519 goto fail_1;
3520 }
3521 envc = 0;
3522 keys = PyMapping_Keys(env);
3523 vals = PyMapping_Values(env);
3524 if (!keys || !vals)
3525 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003526 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3527 PyErr_SetString(PyExc_TypeError,
3528 "spawnve(): env.keys() or env.values() is not a list");
3529 goto fail_2;
3530 }
Tim Peters5aa91602002-01-30 05:46:57 +00003531
Guido van Rossuma1065681999-01-25 23:20:23 +00003532 for (pos = 0; pos < i; pos++) {
3533 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003534 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003535
3536 key = PyList_GetItem(keys, pos);
3537 val = PyList_GetItem(vals, pos);
3538 if (!key || !val)
3539 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003540
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003541 if (!PyArg_Parse(
3542 key,
3543 "s;spawnve() arg 3 contains a non-string key",
3544 &k) ||
3545 !PyArg_Parse(
3546 val,
3547 "s;spawnve() arg 3 contains a non-string value",
3548 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003549 {
3550 goto fail_2;
3551 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003552 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003553 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003554 if (p == NULL) {
3555 PyErr_NoMemory();
3556 goto fail_2;
3557 }
Tim Petersc8996f52001-12-03 20:41:00 +00003558 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003559 envlist[envc++] = p;
3560 }
3561 envlist[envc] = 0;
3562
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003563#if defined(PYOS_OS2) && defined(PYCC_GCC)
3564 Py_BEGIN_ALLOW_THREADS
3565 spawnval = spawnve(mode, path, argvlist, envlist);
3566 Py_END_ALLOW_THREADS
3567#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003568 if (mode == _OLD_P_OVERLAY)
3569 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003570
3571 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003572 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003573 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003574#endif
Tim Peters25059d32001-12-07 20:35:43 +00003575
Fred Drake699f3522000-06-29 21:12:41 +00003576 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003577 (void) posix_error();
3578 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003579#if SIZEOF_LONG == SIZEOF_VOID_P
3580 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003581#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003582 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003583#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003584
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003585 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003586 while (--envc >= 0)
3587 PyMem_DEL(envlist[envc]);
3588 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003589 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003590 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003591 Py_XDECREF(vals);
3592 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003593 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003594 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003595 return res;
3596}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003597
3598/* OS/2 supports spawnvp & spawnvpe natively */
3599#if defined(PYOS_OS2)
3600PyDoc_STRVAR(posix_spawnvp__doc__,
3601"spawnvp(mode, file, args)\n\n\
3602Execute the program 'file' in a new process, using the environment\n\
3603search path to find the file.\n\
3604\n\
3605 mode: mode of process creation\n\
3606 file: executable file name\n\
3607 args: tuple or list of strings");
3608
3609static PyObject *
3610posix_spawnvp(PyObject *self, PyObject *args)
3611{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003612 PyObject *opath;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003613 char *path;
3614 PyObject *argv;
3615 char **argvlist;
3616 int mode, i, argc;
3617 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003618 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003619
3620 /* spawnvp has three arguments: (mode, path, argv), where
3621 argv is a list or tuple of strings. */
3622
Martin v. Löwis011e8422009-05-05 04:43:17 +00003623 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3624 PyUnicode_FSConverter,
3625 &opath, &argv))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003626 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003627 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003628 if (PyList_Check(argv)) {
3629 argc = PyList_Size(argv);
3630 getitem = PyList_GetItem;
3631 }
3632 else if (PyTuple_Check(argv)) {
3633 argc = PyTuple_Size(argv);
3634 getitem = PyTuple_GetItem;
3635 }
3636 else {
3637 PyErr_SetString(PyExc_TypeError,
3638 "spawnvp() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003639 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003640 return NULL;
3641 }
3642
3643 argvlist = PyMem_NEW(char *, argc+1);
3644 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003645 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003646 return PyErr_NoMemory();
3647 }
3648 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003649 if (!fsconvert_strdup((*getitem)(argv, i),
3650 &argvlist[i])) {
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003651 free_string_array(argvlist, i);
3652 PyErr_SetString(
3653 PyExc_TypeError,
3654 "spawnvp() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003655 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003656 return NULL;
3657 }
3658 }
3659 argvlist[argc] = NULL;
3660
3661 Py_BEGIN_ALLOW_THREADS
3662#if defined(PYCC_GCC)
3663 spawnval = spawnvp(mode, path, argvlist);
3664#else
3665 spawnval = _spawnvp(mode, path, argvlist);
3666#endif
3667 Py_END_ALLOW_THREADS
3668
3669 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003670 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003671
3672 if (spawnval == -1)
3673 return posix_error();
3674 else
3675 return Py_BuildValue("l", (long) spawnval);
3676}
3677
3678
3679PyDoc_STRVAR(posix_spawnvpe__doc__,
3680"spawnvpe(mode, file, args, env)\n\n\
3681Execute the program 'file' in a new process, using the environment\n\
3682search path to find the file.\n\
3683\n\
3684 mode: mode of process creation\n\
3685 file: executable file name\n\
3686 args: tuple or list of arguments\n\
3687 env: dictionary of strings mapping to strings");
3688
3689static PyObject *
3690posix_spawnvpe(PyObject *self, PyObject *args)
3691{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003692 PyObject *opath
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003693 char *path;
3694 PyObject *argv, *env;
3695 char **argvlist;
3696 char **envlist;
3697 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3698 int mode, i, pos, argc, envc;
3699 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003700 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003701 int lastarg = 0;
3702
3703 /* spawnvpe has four arguments: (mode, path, argv, env), where
3704 argv is a list or tuple of strings and env is a dictionary
3705 like posix.environ. */
3706
3707 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
Martin v. Löwis011e8422009-05-05 04:43:17 +00003708 PyUnicode_FSConverter,
3709 &opath, &argv, &env))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003710 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003711 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003712 if (PyList_Check(argv)) {
3713 argc = PyList_Size(argv);
3714 getitem = PyList_GetItem;
3715 }
3716 else if (PyTuple_Check(argv)) {
3717 argc = PyTuple_Size(argv);
3718 getitem = PyTuple_GetItem;
3719 }
3720 else {
3721 PyErr_SetString(PyExc_TypeError,
3722 "spawnvpe() arg 2 must be a tuple or list");
3723 goto fail_0;
3724 }
3725 if (!PyMapping_Check(env)) {
3726 PyErr_SetString(PyExc_TypeError,
3727 "spawnvpe() arg 3 must be a mapping object");
3728 goto fail_0;
3729 }
3730
3731 argvlist = PyMem_NEW(char *, argc+1);
3732 if (argvlist == NULL) {
3733 PyErr_NoMemory();
3734 goto fail_0;
3735 }
3736 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003737 if (!fsconvert_strdup((*getitem)(argv, i),
3738 &argvlist[i]))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003739 {
3740 lastarg = i;
3741 goto fail_1;
3742 }
3743 }
3744 lastarg = argc;
3745 argvlist[argc] = NULL;
3746
3747 i = PyMapping_Size(env);
3748 if (i < 0)
3749 goto fail_1;
3750 envlist = PyMem_NEW(char *, i + 1);
3751 if (envlist == NULL) {
3752 PyErr_NoMemory();
3753 goto fail_1;
3754 }
3755 envc = 0;
3756 keys = PyMapping_Keys(env);
3757 vals = PyMapping_Values(env);
3758 if (!keys || !vals)
3759 goto fail_2;
3760 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3761 PyErr_SetString(PyExc_TypeError,
3762 "spawnvpe(): env.keys() or env.values() is not a list");
3763 goto fail_2;
3764 }
3765
3766 for (pos = 0; pos < i; pos++) {
3767 char *p, *k, *v;
3768 size_t len;
3769
3770 key = PyList_GetItem(keys, pos);
3771 val = PyList_GetItem(vals, pos);
3772 if (!key || !val)
3773 goto fail_2;
3774
3775 if (!PyArg_Parse(
3776 key,
3777 "s;spawnvpe() arg 3 contains a non-string key",
3778 &k) ||
3779 !PyArg_Parse(
3780 val,
3781 "s;spawnvpe() arg 3 contains a non-string value",
3782 &v))
3783 {
3784 goto fail_2;
3785 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003786 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003787 p = PyMem_NEW(char, len);
3788 if (p == NULL) {
3789 PyErr_NoMemory();
3790 goto fail_2;
3791 }
3792 PyOS_snprintf(p, len, "%s=%s", k, v);
3793 envlist[envc++] = p;
3794 }
3795 envlist[envc] = 0;
3796
3797 Py_BEGIN_ALLOW_THREADS
3798#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003799 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003800#else
Christian Heimes292d3512008-02-03 16:51:08 +00003801 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003802#endif
3803 Py_END_ALLOW_THREADS
3804
3805 if (spawnval == -1)
3806 (void) posix_error();
3807 else
3808 res = Py_BuildValue("l", (long) spawnval);
3809
3810 fail_2:
3811 while (--envc >= 0)
3812 PyMem_DEL(envlist[envc]);
3813 PyMem_DEL(envlist);
3814 fail_1:
3815 free_string_array(argvlist, lastarg);
3816 Py_XDECREF(vals);
3817 Py_XDECREF(keys);
3818 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003819 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003820 return res;
3821}
3822#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003823#endif /* HAVE_SPAWNV */
3824
3825
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003826#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003827PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003828"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003829Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3830\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003831Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003832
3833static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003834posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003835{
Christian Heimes400adb02008-02-01 08:12:03 +00003836 pid_t pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003837 if (pid == -1)
3838 return posix_error();
Georg Brandl2ee470f2008-07-16 12:55:28 +00003839 if (pid == 0)
3840 PyOS_AfterFork();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003841 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003842}
3843#endif
3844
3845
Guido van Rossumad0ee831995-03-01 10:34:45 +00003846#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003847PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003848"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003849Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003850Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003851
Barry Warsaw53699e91996-12-10 23:23:01 +00003852static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003853posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003854{
Christian Heimes400adb02008-02-01 08:12:03 +00003855 pid_t pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003856 if (pid == -1)
3857 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003858 if (pid == 0)
3859 PyOS_AfterFork();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003860 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003861}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003862#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003863
Neal Norwitzb59798b2003-03-21 01:43:31 +00003864/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003865/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3866#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003867#define DEV_PTY_FILE "/dev/ptc"
3868#define HAVE_DEV_PTMX
3869#else
3870#define DEV_PTY_FILE "/dev/ptmx"
3871#endif
3872
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003873#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003874#ifdef HAVE_PTY_H
3875#include <pty.h>
3876#else
3877#ifdef HAVE_LIBUTIL_H
3878#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003879#endif /* HAVE_LIBUTIL_H */
3880#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003881#ifdef HAVE_STROPTS_H
3882#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003883#endif
3884#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003885
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003886#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003887PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003888"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003889Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003890
3891static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003892posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003893{
3894 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003895#ifndef HAVE_OPENPTY
3896 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003897#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003898#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003899 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003900#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003901 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003902#endif
3903#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003904
Thomas Wouters70c21a12000-07-14 14:28:33 +00003905#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003906 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3907 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003908#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003909 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3910 if (slave_name == NULL)
3911 return posix_error();
3912
3913 slave_fd = open(slave_name, O_RDWR);
3914 if (slave_fd < 0)
3915 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003916#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003917 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003918 if (master_fd < 0)
3919 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003920 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003921 /* change permission of slave */
3922 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003923 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003924 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003925 }
3926 /* unlock slave */
3927 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003928 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003929 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003930 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003931 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003932 slave_name = ptsname(master_fd); /* get name of slave */
3933 if (slave_name == NULL)
3934 return posix_error();
3935 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3936 if (slave_fd < 0)
3937 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003938#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003939 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3940 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003941#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003942 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003943#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003944#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003945#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003946
Fred Drake8cef4cf2000-06-28 16:40:38 +00003947 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003948
Fred Drake8cef4cf2000-06-28 16:40:38 +00003949}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003950#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003951
3952#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003953PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003954"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003955Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3956Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003957To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003958
3959static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003960posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003961{
Christian Heimes400adb02008-02-01 08:12:03 +00003962 int master_fd = -1;
3963 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003964
Fred Drake8cef4cf2000-06-28 16:40:38 +00003965 pid = forkpty(&master_fd, NULL, NULL, NULL);
3966 if (pid == -1)
3967 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003968 if (pid == 0)
3969 PyOS_AfterFork();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003970 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003971}
3972#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003973
Guido van Rossumad0ee831995-03-01 10:34:45 +00003974#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003975PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003976"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003977Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003978
Barry Warsaw53699e91996-12-10 23:23:01 +00003979static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003980posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003981{
Christian Heimes217cfd12007-12-02 14:31:20 +00003982 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003983}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003984#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003985
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003986
Guido van Rossumad0ee831995-03-01 10:34:45 +00003987#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003988PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003989"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003990Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003991
Barry Warsaw53699e91996-12-10 23:23:01 +00003992static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003993posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003994{
Christian Heimes217cfd12007-12-02 14:31:20 +00003995 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003996}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003997#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003998
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003999
Guido van Rossumad0ee831995-03-01 10:34:45 +00004000#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004001PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004002"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004003Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004004
Barry Warsaw53699e91996-12-10 23:23:01 +00004005static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004006posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004007{
Christian Heimes217cfd12007-12-02 14:31:20 +00004008 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004009}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004010#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004011
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004012
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004013PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004014"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004015Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004016
Barry Warsaw53699e91996-12-10 23:23:01 +00004017static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004018posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004019{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004020 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004021}
4022
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004023
Fred Drakec9680921999-12-13 16:37:25 +00004024#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004025PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004026"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004027Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004028
4029static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004030posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004031{
4032 PyObject *result = NULL;
4033
Fred Drakec9680921999-12-13 16:37:25 +00004034#ifdef NGROUPS_MAX
4035#define MAX_GROUPS NGROUPS_MAX
4036#else
4037 /* defined to be 16 on Solaris7, so this should be a small number */
4038#define MAX_GROUPS 64
4039#endif
4040 gid_t grouplist[MAX_GROUPS];
4041 int n;
4042
4043 n = getgroups(MAX_GROUPS, grouplist);
4044 if (n < 0)
4045 posix_error();
4046 else {
4047 result = PyList_New(n);
4048 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00004049 int i;
4050 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004051 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00004052 if (o == NULL) {
4053 Py_DECREF(result);
4054 result = NULL;
4055 break;
4056 }
4057 PyList_SET_ITEM(result, i, o);
4058 }
4059 }
4060 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004061
Fred Drakec9680921999-12-13 16:37:25 +00004062 return result;
4063}
4064#endif
4065
Martin v. Löwis606edc12002-06-13 21:09:11 +00004066#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004067PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004068"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004069Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004070
4071static PyObject *
4072posix_getpgid(PyObject *self, PyObject *args)
4073{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004074 pid_t pid, pgid;
4075 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
Martin v. Löwis606edc12002-06-13 21:09:11 +00004076 return NULL;
4077 pgid = getpgid(pid);
4078 if (pgid < 0)
4079 return posix_error();
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004080 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004081}
4082#endif /* HAVE_GETPGID */
4083
4084
Guido van Rossumb6775db1994-08-01 11:34:53 +00004085#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004086PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004087"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004088Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004089
Barry Warsaw53699e91996-12-10 23:23:01 +00004090static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004091posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004092{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004093#ifdef GETPGRP_HAVE_ARG
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004094 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004095#else /* GETPGRP_HAVE_ARG */
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004096 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004097#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004098}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004099#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004100
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004101
Guido van Rossumb6775db1994-08-01 11:34:53 +00004102#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004103PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004104"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004105Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004106
Barry Warsaw53699e91996-12-10 23:23:01 +00004107static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004108posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004109{
Guido van Rossum64933891994-10-20 21:56:42 +00004110#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00004111 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004112#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004113 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004114#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00004115 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004116 Py_INCREF(Py_None);
4117 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004118}
4119
Guido van Rossumb6775db1994-08-01 11:34:53 +00004120#endif /* HAVE_SETPGRP */
4121
Guido van Rossumad0ee831995-03-01 10:34:45 +00004122#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004123PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004124"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004125Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004126
Barry Warsaw53699e91996-12-10 23:23:01 +00004127static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004128posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004129{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004130 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004131}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004132#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004133
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004134
Fred Drake12c6e2d1999-12-14 21:25:03 +00004135#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004136PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004137"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004138Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004139
4140static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004141posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004142{
Neal Norwitze241ce82003-02-17 18:17:05 +00004143 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00004144 char *name;
4145 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004146
Fred Drakea30680b2000-12-06 21:24:28 +00004147 errno = 0;
4148 name = getlogin();
4149 if (name == NULL) {
4150 if (errno)
4151 posix_error();
4152 else
4153 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00004154 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00004155 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00004156 else
Neal Norwitz93c56822007-08-26 07:10:06 +00004157 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00004158 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004159
Fred Drake12c6e2d1999-12-14 21:25:03 +00004160 return result;
4161}
4162#endif
4163
Guido van Rossumad0ee831995-03-01 10:34:45 +00004164#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004165PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004166"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004167Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004168
Barry Warsaw53699e91996-12-10 23:23:01 +00004169static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004170posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004171{
Christian Heimes217cfd12007-12-02 14:31:20 +00004172 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004173}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004174#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004175
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004176
Guido van Rossumad0ee831995-03-01 10:34:45 +00004177#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004178PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004179"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004180Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004181
Barry Warsaw53699e91996-12-10 23:23:01 +00004182static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004183posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004184{
Christian Heimes292d3512008-02-03 16:51:08 +00004185 pid_t pid;
4186 int sig;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004187 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00004188 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004189#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004190 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4191 APIRET rc;
4192 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004193 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004194
4195 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4196 APIRET rc;
4197 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != 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
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004201 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004202#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004203 if (kill(pid, sig) == -1)
4204 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004205#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004206 Py_INCREF(Py_None);
4207 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004208}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004209#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004210
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004211#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004212PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004213"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004214Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004215
4216static PyObject *
4217posix_killpg(PyObject *self, PyObject *args)
4218{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004219 int sig;
4220 pid_t pgid;
4221 /* XXX some man pages make the `pgid` parameter an int, others
4222 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4223 take the same type. Moreover, pid_t is always at least as wide as
4224 int (else compilation of this module fails), which is safe. */
4225 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004226 return NULL;
4227 if (killpg(pgid, sig) == -1)
4228 return posix_error();
4229 Py_INCREF(Py_None);
4230 return Py_None;
4231}
4232#endif
4233
Guido van Rossumc0125471996-06-28 18:55:32 +00004234#ifdef HAVE_PLOCK
4235
4236#ifdef HAVE_SYS_LOCK_H
4237#include <sys/lock.h>
4238#endif
4239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004240PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004241"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004242Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004243
Barry Warsaw53699e91996-12-10 23:23:01 +00004244static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004245posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004246{
4247 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004248 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004249 return NULL;
4250 if (plock(op) == -1)
4251 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004252 Py_INCREF(Py_None);
4253 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004254}
4255#endif
4256
Guido van Rossumb6775db1994-08-01 11:34:53 +00004257#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004258PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004259"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004260Set the current process's user id.");
4261
Barry Warsaw53699e91996-12-10 23:23:01 +00004262static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004263posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004264{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004265 long uid_arg;
4266 uid_t uid;
4267 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004268 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004269 uid = uid_arg;
4270 if (uid != uid_arg) {
4271 PyErr_SetString(PyExc_OverflowError, "user id too big");
4272 return NULL;
4273 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004274 if (setuid(uid) < 0)
4275 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004276 Py_INCREF(Py_None);
4277 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004278}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004279#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004280
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004281
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004282#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004283PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004284"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004285Set the current process's effective user id.");
4286
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004287static PyObject *
4288posix_seteuid (PyObject *self, PyObject *args)
4289{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004290 long euid_arg;
4291 uid_t euid;
4292 if (!PyArg_ParseTuple(args, "l", &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004293 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004294 euid = euid_arg;
4295 if (euid != euid_arg) {
4296 PyErr_SetString(PyExc_OverflowError, "user id too big");
4297 return NULL;
4298 }
4299 if (seteuid(euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004300 return posix_error();
4301 } else {
4302 Py_INCREF(Py_None);
4303 return Py_None;
4304 }
4305}
4306#endif /* HAVE_SETEUID */
4307
4308#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004309PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004310"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004311Set the current process's effective group id.");
4312
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004313static PyObject *
4314posix_setegid (PyObject *self, PyObject *args)
4315{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004316 long egid_arg;
4317 gid_t egid;
4318 if (!PyArg_ParseTuple(args, "l", &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004319 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004320 egid = egid_arg;
4321 if (egid != egid_arg) {
4322 PyErr_SetString(PyExc_OverflowError, "group id too big");
4323 return NULL;
4324 }
4325 if (setegid(egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004326 return posix_error();
4327 } else {
4328 Py_INCREF(Py_None);
4329 return Py_None;
4330 }
4331}
4332#endif /* HAVE_SETEGID */
4333
4334#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004335PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004336"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004337Set the current process's real and effective user ids.");
4338
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004339static PyObject *
4340posix_setreuid (PyObject *self, PyObject *args)
4341{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004342 long ruid_arg, euid_arg;
4343 uid_t ruid, euid;
4344 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004345 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004346 ruid = ruid_arg;
4347 euid = euid_arg;
4348 if (euid != euid_arg || ruid != ruid_arg) {
4349 PyErr_SetString(PyExc_OverflowError, "user id too big");
4350 return NULL;
4351 }
4352 if (setreuid(ruid, euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004353 return posix_error();
4354 } else {
4355 Py_INCREF(Py_None);
4356 return Py_None;
4357 }
4358}
4359#endif /* HAVE_SETREUID */
4360
4361#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004362PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004363"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004364Set the current process's real and effective group ids.");
4365
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004366static PyObject *
4367posix_setregid (PyObject *self, PyObject *args)
4368{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004369 long rgid_arg, egid_arg;
4370 gid_t rgid, egid;
4371 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004372 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004373 rgid = rgid_arg;
4374 egid = egid_arg;
4375 if (egid != egid_arg || rgid != rgid_arg) {
4376 PyErr_SetString(PyExc_OverflowError, "group id too big");
4377 return NULL;
4378 }
4379 if (setregid(rgid, egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004380 return posix_error();
4381 } else {
4382 Py_INCREF(Py_None);
4383 return Py_None;
4384 }
4385}
4386#endif /* HAVE_SETREGID */
4387
Guido van Rossumb6775db1994-08-01 11:34:53 +00004388#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004389PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004390"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004391Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004392
Barry Warsaw53699e91996-12-10 23:23:01 +00004393static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004394posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004395{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004396 long gid_arg;
4397 gid_t gid;
4398 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004399 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004400 gid = gid_arg;
4401 if (gid != gid_arg) {
4402 PyErr_SetString(PyExc_OverflowError, "group id too big");
4403 return NULL;
4404 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004405 if (setgid(gid) < 0)
4406 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004407 Py_INCREF(Py_None);
4408 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004409}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004410#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004411
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004412#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004413PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004414"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004415Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004416
4417static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004418posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004419{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004420 int i, len;
4421 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004422
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004423 if (!PySequence_Check(groups)) {
4424 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4425 return NULL;
4426 }
4427 len = PySequence_Size(groups);
4428 if (len > MAX_GROUPS) {
4429 PyErr_SetString(PyExc_ValueError, "too many groups");
4430 return NULL;
4431 }
4432 for(i = 0; i < len; i++) {
4433 PyObject *elem;
4434 elem = PySequence_GetItem(groups, i);
4435 if (!elem)
4436 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004437 if (!PyLong_Check(elem)) {
4438 PyErr_SetString(PyExc_TypeError,
4439 "groups must be integers");
4440 Py_DECREF(elem);
4441 return NULL;
4442 } else {
4443 unsigned long x = PyLong_AsUnsignedLong(elem);
4444 if (PyErr_Occurred()) {
4445 PyErr_SetString(PyExc_TypeError,
4446 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004447 Py_DECREF(elem);
4448 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004449 }
Georg Brandla13c2442005-11-22 19:30:31 +00004450 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004451 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004452 if (grouplist[i] != x) {
4453 PyErr_SetString(PyExc_TypeError,
4454 "group id too big");
4455 Py_DECREF(elem);
4456 return NULL;
4457 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004458 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004459 Py_DECREF(elem);
4460 }
4461
4462 if (setgroups(len, grouplist) < 0)
4463 return posix_error();
4464 Py_INCREF(Py_None);
4465 return Py_None;
4466}
4467#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004468
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004469#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4470static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004471wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004472{
4473 PyObject *result;
4474 static PyObject *struct_rusage;
4475
4476 if (pid == -1)
4477 return posix_error();
4478
4479 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004480 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004481 if (m == NULL)
4482 return NULL;
4483 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4484 Py_DECREF(m);
4485 if (struct_rusage == NULL)
4486 return NULL;
4487 }
4488
4489 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4490 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4491 if (!result)
4492 return NULL;
4493
4494#ifndef doubletime
4495#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4496#endif
4497
4498 PyStructSequence_SET_ITEM(result, 0,
4499 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4500 PyStructSequence_SET_ITEM(result, 1,
4501 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4502#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004503 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004504 SET_INT(result, 2, ru->ru_maxrss);
4505 SET_INT(result, 3, ru->ru_ixrss);
4506 SET_INT(result, 4, ru->ru_idrss);
4507 SET_INT(result, 5, ru->ru_isrss);
4508 SET_INT(result, 6, ru->ru_minflt);
4509 SET_INT(result, 7, ru->ru_majflt);
4510 SET_INT(result, 8, ru->ru_nswap);
4511 SET_INT(result, 9, ru->ru_inblock);
4512 SET_INT(result, 10, ru->ru_oublock);
4513 SET_INT(result, 11, ru->ru_msgsnd);
4514 SET_INT(result, 12, ru->ru_msgrcv);
4515 SET_INT(result, 13, ru->ru_nsignals);
4516 SET_INT(result, 14, ru->ru_nvcsw);
4517 SET_INT(result, 15, ru->ru_nivcsw);
4518#undef SET_INT
4519
4520 if (PyErr_Occurred()) {
4521 Py_DECREF(result);
4522 return NULL;
4523 }
4524
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004525 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004526}
4527#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4528
4529#ifdef HAVE_WAIT3
4530PyDoc_STRVAR(posix_wait3__doc__,
4531"wait3(options) -> (pid, status, rusage)\n\n\
4532Wait for completion of a child process.");
4533
4534static PyObject *
4535posix_wait3(PyObject *self, PyObject *args)
4536{
Christian Heimes292d3512008-02-03 16:51:08 +00004537 pid_t pid;
4538 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004539 struct rusage ru;
4540 WAIT_TYPE status;
4541 WAIT_STATUS_INT(status) = 0;
4542
4543 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4544 return NULL;
4545
4546 Py_BEGIN_ALLOW_THREADS
4547 pid = wait3(&status, options, &ru);
4548 Py_END_ALLOW_THREADS
4549
4550 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4551}
4552#endif /* HAVE_WAIT3 */
4553
4554#ifdef HAVE_WAIT4
4555PyDoc_STRVAR(posix_wait4__doc__,
4556"wait4(pid, options) -> (pid, status, rusage)\n\n\
4557Wait for completion of a given child process.");
4558
4559static PyObject *
4560posix_wait4(PyObject *self, PyObject *args)
4561{
Christian Heimes292d3512008-02-03 16:51:08 +00004562 pid_t pid;
4563 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004564 struct rusage ru;
4565 WAIT_TYPE status;
4566 WAIT_STATUS_INT(status) = 0;
4567
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004568 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004569 return NULL;
4570
4571 Py_BEGIN_ALLOW_THREADS
4572 pid = wait4(pid, &status, options, &ru);
4573 Py_END_ALLOW_THREADS
4574
4575 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4576}
4577#endif /* HAVE_WAIT4 */
4578
Guido van Rossumb6775db1994-08-01 11:34:53 +00004579#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004580PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004581"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004582Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004583
Barry Warsaw53699e91996-12-10 23:23:01 +00004584static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004585posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004586{
Christian Heimes292d3512008-02-03 16:51:08 +00004587 pid_t pid;
4588 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004589 WAIT_TYPE status;
4590 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004591
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004592 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004593 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004594 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004595 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004596 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004597 if (pid == -1)
4598 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004599
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004600 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004601}
4602
Tim Petersab034fa2002-02-01 11:27:43 +00004603#elif defined(HAVE_CWAIT)
4604
4605/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004606PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004607"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004608"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004609
4610static PyObject *
4611posix_waitpid(PyObject *self, PyObject *args)
4612{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004613 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004614 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004615
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004616 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Tim Petersab034fa2002-02-01 11:27:43 +00004617 return NULL;
4618 Py_BEGIN_ALLOW_THREADS
4619 pid = _cwait(&status, pid, options);
4620 Py_END_ALLOW_THREADS
4621 if (pid == -1)
4622 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004623
4624 /* shift the status left a byte so this is more like the POSIX waitpid */
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004625 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004626}
4627#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004628
Guido van Rossumad0ee831995-03-01 10:34:45 +00004629#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004630PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004631"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004632Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004633
Barry Warsaw53699e91996-12-10 23:23:01 +00004634static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004635posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004636{
Christian Heimes292d3512008-02-03 16:51:08 +00004637 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004638 WAIT_TYPE status;
4639 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004640
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004641 Py_BEGIN_ALLOW_THREADS
4642 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004643 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004644 if (pid == -1)
4645 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004646
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004647 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004648}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004649#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004650
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004652PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004653"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004654Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004655
Barry Warsaw53699e91996-12-10 23:23:01 +00004656static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004657posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004658{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004659#ifdef HAVE_LSTAT
Martin v. Löwis011e8422009-05-05 04:43:17 +00004660 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004661#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004662#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00004663 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004664#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00004665 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004666#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004667#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004668}
4669
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004670
Guido van Rossumb6775db1994-08-01 11:34:53 +00004671#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004672PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004673"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004674Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004675
Barry Warsaw53699e91996-12-10 23:23:01 +00004676static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004677posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004678{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004679 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004680 char buf[MAXPATHLEN];
Martin v. Löwis011e8422009-05-05 04:43:17 +00004681 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004682 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004683 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004684 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004685
Martin v. Löwis011e8422009-05-05 04:43:17 +00004686 if (!PyArg_ParseTuple(args, "O&:readlink",
4687 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004688 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004689 path = bytes2str(opath, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004690 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004691 if (v == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00004692 release_bytes(opath);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004693 return NULL;
4694 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004695
4696 if (PyUnicode_Check(v)) {
4697 arg_is_unicode = 1;
4698 }
4699 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004700
Barry Warsaw53699e91996-12-10 23:23:01 +00004701 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004702 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004703 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004704 if (n < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004705 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004706
Martin v. Löwis011e8422009-05-05 04:43:17 +00004707 release_bytes(opath);
Christian Heimes72b710a2008-05-26 13:28:38 +00004708 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004709 if (arg_is_unicode) {
4710 PyObject *w;
4711
4712 w = PyUnicode_FromEncodedObject(v,
4713 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00004714 "surrogateescape");
Thomas Wouters89f507f2006-12-13 04:49:30 +00004715 if (w != NULL) {
4716 Py_DECREF(v);
4717 v = w;
4718 }
4719 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00004720 v = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004721 }
4722 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004723 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004724}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004725#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004726
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004727
Guido van Rossumb6775db1994-08-01 11:34:53 +00004728#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004729PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004730"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004731Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004732
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004733static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004734posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004735{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004736 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004737}
4738#endif /* HAVE_SYMLINK */
4739
4740
4741#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004742#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4743static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004744system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004745{
4746 ULONG value = 0;
4747
4748 Py_BEGIN_ALLOW_THREADS
4749 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4750 Py_END_ALLOW_THREADS
4751
4752 return value;
4753}
4754
4755static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004756posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004757{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004758 /* Currently Only Uptime is Provided -- Others Later */
4759 return Py_BuildValue("ddddd",
4760 (double)0 /* t.tms_utime / HZ */,
4761 (double)0 /* t.tms_stime / HZ */,
4762 (double)0 /* t.tms_cutime / HZ */,
4763 (double)0 /* t.tms_cstime / HZ */,
4764 (double)system_uptime() / 1000);
4765}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004766#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004767#define NEED_TICKS_PER_SECOND
4768static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004769static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004770posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004771{
4772 struct tms t;
4773 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004774 errno = 0;
4775 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004776 if (c == (clock_t) -1)
4777 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004778 return Py_BuildValue("ddddd",
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004779 (double)t.tms_utime / ticks_per_second,
4780 (double)t.tms_stime / ticks_per_second,
4781 (double)t.tms_cutime / ticks_per_second,
4782 (double)t.tms_cstime / ticks_per_second,
4783 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004784}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004785#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004786#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004787
4788
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004789#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004790#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004791static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004792posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004793{
4794 FILETIME create, exit, kernel, user;
4795 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004796 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004797 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4798 /* The fields of a FILETIME structure are the hi and lo part
4799 of a 64-bit value expressed in 100 nanosecond units.
4800 1e7 is one second in such units; 1e-7 the inverse.
4801 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4802 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004803 return Py_BuildValue(
4804 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004805 (double)(user.dwHighDateTime*429.4967296 +
4806 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004807 (double)(kernel.dwHighDateTime*429.4967296 +
4808 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004809 (double)0,
4810 (double)0,
4811 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004812}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004813#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004814
4815#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004816PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004817"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004818Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004819#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004820
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004821
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004822#ifdef HAVE_GETSID
4823PyDoc_STRVAR(posix_getsid__doc__,
4824"getsid(pid) -> sid\n\n\
4825Call the system call getsid().");
4826
4827static PyObject *
4828posix_getsid(PyObject *self, PyObject *args)
4829{
Christian Heimes292d3512008-02-03 16:51:08 +00004830 pid_t pid;
4831 int sid;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004832 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004833 return NULL;
4834 sid = getsid(pid);
4835 if (sid < 0)
4836 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004837 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004838}
4839#endif /* HAVE_GETSID */
4840
4841
Guido van Rossumb6775db1994-08-01 11:34:53 +00004842#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004843PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004844"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004845Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004846
Barry Warsaw53699e91996-12-10 23:23:01 +00004847static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004848posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004849{
Guido van Rossum687dd131993-05-17 08:34:16 +00004850 if (setsid() < 0)
4851 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004852 Py_INCREF(Py_None);
4853 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004854}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004855#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004856
Guido van Rossumb6775db1994-08-01 11:34:53 +00004857#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004858PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004859"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004860Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004861
Barry Warsaw53699e91996-12-10 23:23:01 +00004862static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004863posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004864{
Christian Heimes292d3512008-02-03 16:51:08 +00004865 pid_t pid;
4866 int pgrp;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004867 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004868 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004869 if (setpgid(pid, pgrp) < 0)
4870 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004871 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_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004875
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004876
Guido van Rossumb6775db1994-08-01 11:34:53 +00004877#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004878PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004879"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004880Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004881
Barry Warsaw53699e91996-12-10 23:23:01 +00004882static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004883posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004884{
Christian Heimes15ebc882008-02-04 18:48:49 +00004885 int fd;
4886 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004887 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004888 return NULL;
4889 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004890 if (pgid < 0)
4891 return posix_error();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004892 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004893}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004894#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004895
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004896
Guido van Rossumb6775db1994-08-01 11:34:53 +00004897#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004898PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004899"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004900Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004901
Barry Warsaw53699e91996-12-10 23:23:01 +00004902static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004903posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004904{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004905 int fd;
4906 pid_t pgid;
4907 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004908 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004909 if (tcsetpgrp(fd, pgid) < 0)
4910 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004911 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004912 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004913}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004914#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004915
Guido van Rossum687dd131993-05-17 08:34:16 +00004916/* Functions acting on file descriptors */
4917
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004918PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004919"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004920Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004921
Barry Warsaw53699e91996-12-10 23:23:01 +00004922static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004923posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004924{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004925 PyObject *ofile;
4926 char *file;
Guido van Rossum687dd131993-05-17 08:34:16 +00004927 int flag;
4928 int mode = 0777;
4929 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004930
4931#ifdef MS_WINDOWS
4932 if (unicode_file_names()) {
4933 PyUnicodeObject *po;
4934 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4935 Py_BEGIN_ALLOW_THREADS
4936 /* PyUnicode_AS_UNICODE OK without thread
4937 lock as it is a simple dereference. */
4938 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4939 Py_END_ALLOW_THREADS
4940 if (fd < 0)
4941 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004942 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004943 }
4944 /* Drop the argument parsing error as narrow strings
4945 are also valid. */
4946 PyErr_Clear();
4947 }
4948#endif
4949
Martin v. Löwis011e8422009-05-05 04:43:17 +00004950 if (!PyArg_ParseTuple(args, "O&i|i",
4951 PyUnicode_FSConverter, &ofile,
Mark Hammondef8b6542001-05-13 08:04:26 +00004952 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004953 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004954 file = bytes2str(ofile, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00004955 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004956 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004957 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004958 if (fd < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004959 return posix_error_with_allocated_filename(ofile);
4960 release_bytes(ofile);
Christian Heimes217cfd12007-12-02 14:31:20 +00004961 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004962}
4963
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004964
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004965PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004966"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004967Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004968
Barry Warsaw53699e91996-12-10 23:23:01 +00004969static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004970posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004971{
4972 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004973 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004974 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004975 if (!_PyVerify_fd(fd))
4976 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004977 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004978 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004979 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004980 if (res < 0)
4981 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004982 Py_INCREF(Py_None);
4983 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004984}
4985
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004986
Christian Heimesfdab48e2008-01-20 09:06:41 +00004987PyDoc_STRVAR(posix_closerange__doc__,
4988"closerange(fd_low, fd_high)\n\n\
4989Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4990
4991static PyObject *
4992posix_closerange(PyObject *self, PyObject *args)
4993{
4994 int fd_from, fd_to, i;
4995 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4996 return NULL;
4997 Py_BEGIN_ALLOW_THREADS
4998 for (i = fd_from; i < fd_to; i++)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004999 if (_PyVerify_fd(i))
5000 close(i);
Christian Heimesfdab48e2008-01-20 09:06:41 +00005001 Py_END_ALLOW_THREADS
5002 Py_RETURN_NONE;
5003}
5004
5005
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005006PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005007"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005008Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005009
Barry Warsaw53699e91996-12-10 23:23:01 +00005010static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005011posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005012{
5013 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005014 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005015 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005016 if (!_PyVerify_fd(fd))
5017 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005018 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005019 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005020 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005021 if (fd < 0)
5022 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005023 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005024}
5025
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005026
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005027PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005028"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005029Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005030
Barry Warsaw53699e91996-12-10 23:23:01 +00005031static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005032posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005033{
5034 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005035 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005036 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005037 if (!_PyVerify_fd_dup2(fd, fd2))
5038 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005039 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005040 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005041 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005042 if (res < 0)
5043 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005044 Py_INCREF(Py_None);
5045 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005046}
5047
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005048
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005049PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005050"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005051Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005052
Barry Warsaw53699e91996-12-10 23:23:01 +00005053static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005054posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005055{
5056 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005057#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005058 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005059#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005060 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005061#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005062 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005063 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005064 return NULL;
5065#ifdef SEEK_SET
5066 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5067 switch (how) {
5068 case 0: how = SEEK_SET; break;
5069 case 1: how = SEEK_CUR; break;
5070 case 2: how = SEEK_END; break;
5071 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005072#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005073
5074#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005075 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005076#else
5077 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005078 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005079#endif
5080 if (PyErr_Occurred())
5081 return NULL;
5082
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005083 if (!_PyVerify_fd(fd))
5084 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005085 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005086#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005087 res = _lseeki64(fd, pos, how);
5088#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005089 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005090#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005091 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005092 if (res < 0)
5093 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005094
5095#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005096 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005097#else
5098 return PyLong_FromLongLong(res);
5099#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005100}
5101
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005102
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005103PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005104"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005105Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005106
Barry Warsaw53699e91996-12-10 23:23:01 +00005107static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005108posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005109{
Guido van Rossum572dbf82007-04-27 23:53:51 +00005110 int fd, size;
5111 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005112 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005113 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005114 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005115 if (size < 0) {
5116 errno = EINVAL;
5117 return posix_error();
5118 }
Christian Heimes72b710a2008-05-26 13:28:38 +00005119 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005120 if (buffer == NULL)
5121 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005122 if (!_PyVerify_fd(fd))
5123 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005124 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00005125 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005126 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005127 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005128 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005129 return posix_error();
5130 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005131 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00005132 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005133 return buffer;
5134}
5135
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005137PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005138"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005139Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005140
Barry Warsaw53699e91996-12-10 23:23:01 +00005141static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005142posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005143{
Martin v. Löwis423be952008-08-13 15:53:07 +00005144 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005145 int fd;
5146 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005147
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +00005148 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00005149 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005150 if (!_PyVerify_fd(fd))
5151 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005152 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005153 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00005154 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005155 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00005156 if (size < 0)
5157 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005158 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005159}
5160
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005161
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005162PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005163"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005164Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005165
Barry Warsaw53699e91996-12-10 23:23:01 +00005166static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005167posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005168{
5169 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005170 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005171 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005172 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005173 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005174#ifdef __VMS
5175 /* on OpenVMS we must ensure that all bytes are written to the file */
5176 fsync(fd);
5177#endif
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005178 if (!_PyVerify_fd(fd))
5179 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005180 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005181 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005182 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005183 if (res != 0) {
5184#ifdef MS_WINDOWS
5185 return win32_error("fstat", NULL);
5186#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005187 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005188#endif
5189 }
Tim Peters5aa91602002-01-30 05:46:57 +00005190
Martin v. Löwis14694662006-02-03 12:54:16 +00005191 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005192}
5193
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005194PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005195"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005196Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005197connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005198
5199static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005200posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005201{
5202 int fd;
5203 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5204 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005205 if (!_PyVerify_fd(fd))
5206 return PyBool_FromLong(0);
Fred Drake106c1a02002-04-23 15:58:02 +00005207 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005208}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005209
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005210#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005211PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005212"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005213Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005214
Barry Warsaw53699e91996-12-10 23:23:01 +00005215static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005216posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005217{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005218#if defined(PYOS_OS2)
5219 HFILE read, write;
5220 APIRET rc;
5221
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005222 Py_BEGIN_ALLOW_THREADS
5223 rc = DosCreatePipe( &read, &write, 4096);
5224 Py_END_ALLOW_THREADS
5225 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005226 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005227
5228 return Py_BuildValue("(ii)", read, write);
5229#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005230#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005231 int fds[2];
5232 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005233 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005234 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005235 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005236 if (res != 0)
5237 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005238 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005239#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005240 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005241 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005242 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005243 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005244 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005245 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005246 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005247 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005248 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5249 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005250 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005251#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005252#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005253}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005254#endif /* HAVE_PIPE */
5255
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005256
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005257#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005258PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005259"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005260Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005261
Barry Warsaw53699e91996-12-10 23:23:01 +00005262static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005263posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005264{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005265 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005266 int mode = 0666;
5267 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005268 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005269 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005270 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005271 res = mkfifo(filename, mode);
5272 Py_END_ALLOW_THREADS
5273 if (res < 0)
5274 return posix_error();
5275 Py_INCREF(Py_None);
5276 return Py_None;
5277}
5278#endif
5279
5280
Neal Norwitz11690112002-07-30 01:08:28 +00005281#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005282PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005283"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005284Create a filesystem node (file, device special file or named pipe)\n\
5285named filename. mode specifies both the permissions to use and the\n\
5286type of node to be created, being combined (bitwise OR) with one of\n\
5287S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005288device defines the newly created device special file (probably using\n\
5289os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005290
5291
5292static PyObject *
5293posix_mknod(PyObject *self, PyObject *args)
5294{
5295 char *filename;
5296 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005297 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005298 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005299 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005300 return NULL;
5301 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005302 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005303 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005304 if (res < 0)
5305 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005306 Py_INCREF(Py_None);
5307 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005308}
5309#endif
5310
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005311#ifdef HAVE_DEVICE_MACROS
5312PyDoc_STRVAR(posix_major__doc__,
5313"major(device) -> major number\n\
5314Extracts a device major number from a raw device number.");
5315
5316static PyObject *
5317posix_major(PyObject *self, PyObject *args)
5318{
5319 int device;
5320 if (!PyArg_ParseTuple(args, "i:major", &device))
5321 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005322 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005323}
5324
5325PyDoc_STRVAR(posix_minor__doc__,
5326"minor(device) -> minor number\n\
5327Extracts a device minor number from a raw device number.");
5328
5329static PyObject *
5330posix_minor(PyObject *self, PyObject *args)
5331{
5332 int device;
5333 if (!PyArg_ParseTuple(args, "i:minor", &device))
5334 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005335 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005336}
5337
5338PyDoc_STRVAR(posix_makedev__doc__,
5339"makedev(major, minor) -> device number\n\
5340Composes a raw device number from the major and minor device numbers.");
5341
5342static PyObject *
5343posix_makedev(PyObject *self, PyObject *args)
5344{
5345 int major, minor;
5346 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5347 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005348 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005349}
5350#endif /* device macros */
5351
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005352
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005353#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005354PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005355"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005356Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005357
Barry Warsaw53699e91996-12-10 23:23:01 +00005358static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005359posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005360{
5361 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005362 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005363 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005364 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005365
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005366 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005367 return NULL;
5368
5369#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005370 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005371#else
5372 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005373 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005374#endif
5375 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005376 return NULL;
5377
Barry Warsaw53699e91996-12-10 23:23:01 +00005378 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005379 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005380 Py_END_ALLOW_THREADS
Benjamin Peterson9053d752009-01-19 17:53:36 +00005381 if (res < 0)
5382 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005383 Py_INCREF(Py_None);
5384 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005385}
5386#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005387
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005388#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005389PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005390"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005391Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005392
Fred Drake762e2061999-08-26 17:23:54 +00005393/* Save putenv() parameters as values here, so we can collect them when they
5394 * get re-set with another call for the same key. */
5395static PyObject *posix_putenv_garbage;
5396
Tim Peters5aa91602002-01-30 05:46:57 +00005397static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005398posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005399{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005400#ifdef MS_WINDOWS
5401 wchar_t *s1, *s2;
5402 wchar_t *newenv;
5403#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00005404 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005405 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005406 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005407#endif
Fred Drake762e2061999-08-26 17:23:54 +00005408 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005409 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005410
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005411#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00005412 if (!PyArg_ParseTuple(args,
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005413 "uu:putenv",
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005414 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005415 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005416#else
5417 if (!PyArg_ParseTuple(args,
5418 "O&O&:putenv",
5419 PyUnicode_FSConverter, &os1,
5420 PyUnicode_FSConverter, &os2))
5421 return NULL;
5422 s1 = bytes2str(os1, 1);
5423 s2 = bytes2str(os2, 1);
5424#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005425
5426#if defined(PYOS_OS2)
5427 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5428 APIRET rc;
5429
Guido van Rossumd48f2521997-12-05 22:19:34 +00005430 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5431 if (rc != NO_ERROR)
5432 return os2_error(rc);
5433
5434 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5435 APIRET rc;
5436
Guido van Rossumd48f2521997-12-05 22:19:34 +00005437 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5438 if (rc != NO_ERROR)
5439 return os2_error(rc);
5440 } else {
5441#endif
Fred Drake762e2061999-08-26 17:23:54 +00005442 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005443 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005444 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005445#ifdef MS_WINDOWS
5446 len = wcslen(s1) + wcslen(s2) + 2;
5447 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5448#else
5449 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005450 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005451#endif
Fred Drake762e2061999-08-26 17:23:54 +00005452 if (newstr == NULL)
5453 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005454#ifdef MS_WINDOWS
5455 newenv = PyUnicode_AsUnicode(newstr);
5456 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5457 if (_wputenv(newenv)) {
5458 Py_DECREF(newstr);
5459 posix_error();
5460 return NULL;
5461 }
5462#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005463 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005464 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5465 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005466 Py_DECREF(newstr);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005467 release_bytes(os1);
5468 release_bytes(os2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005469 posix_error();
5470 return NULL;
5471 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005472#endif
Fred Drake762e2061999-08-26 17:23:54 +00005473 /* Install the first arg and newstr in posix_putenv_garbage;
5474 * this will cause previous value to be collected. This has to
5475 * happen after the real putenv() call because the old value
5476 * was still accessible until then. */
5477 if (PyDict_SetItem(posix_putenv_garbage,
5478 PyTuple_GET_ITEM(args, 0), newstr)) {
5479 /* really not much we can do; just leak */
5480 PyErr_Clear();
5481 }
5482 else {
5483 Py_DECREF(newstr);
5484 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005485
5486#if defined(PYOS_OS2)
5487 }
5488#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00005489#ifndef MS_WINDOWS
5490 release_bytes(os1);
5491 release_bytes(os2);
5492#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005493 Py_INCREF(Py_None);
5494 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005495}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005496#endif /* putenv */
5497
Guido van Rossumc524d952001-10-19 01:31:59 +00005498#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005499PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005500"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005501Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005502
5503static PyObject *
5504posix_unsetenv(PyObject *self, PyObject *args)
5505{
5506 char *s1;
5507
5508 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5509 return NULL;
5510
5511 unsetenv(s1);
5512
5513 /* Remove the key from posix_putenv_garbage;
5514 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005515 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005516 * old value was still accessible until then.
5517 */
5518 if (PyDict_DelItem(posix_putenv_garbage,
5519 PyTuple_GET_ITEM(args, 0))) {
5520 /* really not much we can do; just leak */
5521 PyErr_Clear();
5522 }
5523
5524 Py_INCREF(Py_None);
5525 return Py_None;
5526}
5527#endif /* unsetenv */
5528
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005529PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005530"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005531Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005532
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005533static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005534posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005535{
5536 int code;
5537 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005538 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005539 return NULL;
5540 message = strerror(code);
5541 if (message == NULL) {
5542 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005543 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005544 return NULL;
5545 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005546 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005547}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005548
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005549
Guido van Rossumc9641791998-08-04 15:26:23 +00005550#ifdef HAVE_SYS_WAIT_H
5551
Fred Drake106c1a02002-04-23 15:58:02 +00005552#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005553PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005554"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005555Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005556
5557static PyObject *
5558posix_WCOREDUMP(PyObject *self, PyObject *args)
5559{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005560 WAIT_TYPE status;
5561 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005562
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005563 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005564 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005565
5566 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005567}
5568#endif /* WCOREDUMP */
5569
5570#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005571PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005572"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005573Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005574job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005575
5576static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005577posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005578{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005579 WAIT_TYPE status;
5580 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005581
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005582 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005583 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005584
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005585 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005586}
5587#endif /* WIFCONTINUED */
5588
Guido van Rossumc9641791998-08-04 15:26:23 +00005589#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005590PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005591"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005592Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005593
5594static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005595posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005596{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005597 WAIT_TYPE status;
5598 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005599
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005600 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005601 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005602
Fred Drake106c1a02002-04-23 15:58:02 +00005603 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005604}
5605#endif /* WIFSTOPPED */
5606
5607#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005608PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005609"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005610Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005611
5612static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005613posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005614{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005615 WAIT_TYPE status;
5616 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005617
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005618 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005619 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005620
Fred Drake106c1a02002-04-23 15:58:02 +00005621 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005622}
5623#endif /* WIFSIGNALED */
5624
5625#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005626PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005627"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005628Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005629system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005630
5631static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005632posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005633{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005634 WAIT_TYPE status;
5635 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005636
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005637 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005638 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005639
Fred Drake106c1a02002-04-23 15:58:02 +00005640 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005641}
5642#endif /* WIFEXITED */
5643
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005644#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005645PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005646"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005647Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005648
5649static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005650posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005651{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005652 WAIT_TYPE status;
5653 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005654
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005655 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005656 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005657
Guido van Rossumc9641791998-08-04 15:26:23 +00005658 return Py_BuildValue("i", WEXITSTATUS(status));
5659}
5660#endif /* WEXITSTATUS */
5661
5662#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005663PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005664"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005665Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005666value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005667
5668static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005669posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005670{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005671 WAIT_TYPE status;
5672 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005673
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005674 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005675 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005676
Guido van Rossumc9641791998-08-04 15:26:23 +00005677 return Py_BuildValue("i", WTERMSIG(status));
5678}
5679#endif /* WTERMSIG */
5680
5681#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005682PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005683"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005684Return the signal that stopped the process that provided\n\
5685the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005686
5687static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005688posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005689{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005690 WAIT_TYPE status;
5691 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005692
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005693 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005694 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005695
Guido van Rossumc9641791998-08-04 15:26:23 +00005696 return Py_BuildValue("i", WSTOPSIG(status));
5697}
5698#endif /* WSTOPSIG */
5699
5700#endif /* HAVE_SYS_WAIT_H */
5701
5702
Thomas Wouters477c8d52006-05-27 19:21:47 +00005703#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005704#ifdef _SCO_DS
5705/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5706 needed definitions in sys/statvfs.h */
5707#define _SVID3
5708#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005709#include <sys/statvfs.h>
5710
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005711static PyObject*
5712_pystatvfs_fromstructstatvfs(struct statvfs st) {
5713 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5714 if (v == NULL)
5715 return NULL;
5716
5717#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005718 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5719 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5720 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5721 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5722 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5723 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5724 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5725 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5726 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5727 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005728#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005729 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5730 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005731 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005732 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005733 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005734 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005735 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005736 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005737 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005738 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005739 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005740 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005741 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005742 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005743 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5744 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005745#endif
5746
5747 return v;
5748}
5749
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005750PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005751"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005752Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005753
5754static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005755posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005756{
5757 int fd, res;
5758 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005759
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005760 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005761 return NULL;
5762 Py_BEGIN_ALLOW_THREADS
5763 res = fstatvfs(fd, &st);
5764 Py_END_ALLOW_THREADS
5765 if (res != 0)
5766 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005767
5768 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005769}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005770#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005771
5772
Thomas Wouters477c8d52006-05-27 19:21:47 +00005773#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005774#include <sys/statvfs.h>
5775
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005776PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005777"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005778Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005779
5780static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005781posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005782{
5783 char *path;
5784 int res;
5785 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005786 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005787 return NULL;
5788 Py_BEGIN_ALLOW_THREADS
5789 res = statvfs(path, &st);
5790 Py_END_ALLOW_THREADS
5791 if (res != 0)
5792 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005793
5794 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005795}
5796#endif /* HAVE_STATVFS */
5797
Fred Drakec9680921999-12-13 16:37:25 +00005798/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5799 * It maps strings representing configuration variable names to
5800 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005801 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005802 * rarely-used constants. There are three separate tables that use
5803 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005804 *
5805 * This code is always included, even if none of the interfaces that
5806 * need it are included. The #if hackery needed to avoid it would be
5807 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005808 */
5809struct constdef {
5810 char *name;
5811 long value;
5812};
5813
Fred Drake12c6e2d1999-12-14 21:25:03 +00005814static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005815conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005816 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005817{
Christian Heimes217cfd12007-12-02 14:31:20 +00005818 if (PyLong_Check(arg)) {
5819 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005820 return 1;
5821 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005822 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005823 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005824 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005825 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005826 size_t hi = tablesize;
5827 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005828 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005829 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005830 PyErr_SetString(PyExc_TypeError,
5831 "configuration names must be strings or integers");
5832 return 0;
5833 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005834 confname = _PyUnicode_AsString(arg);
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005835 if (confname == NULL)
5836 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005837 while (lo < hi) {
5838 mid = (lo + hi) / 2;
5839 cmp = strcmp(confname, table[mid].name);
5840 if (cmp < 0)
5841 hi = mid;
5842 else if (cmp > 0)
5843 lo = mid + 1;
5844 else {
5845 *valuep = table[mid].value;
5846 return 1;
5847 }
5848 }
5849 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005850 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005851 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005852}
5853
5854
5855#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5856static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005857#ifdef _PC_ABI_AIO_XFER_MAX
5858 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5859#endif
5860#ifdef _PC_ABI_ASYNC_IO
5861 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5862#endif
Fred Drakec9680921999-12-13 16:37:25 +00005863#ifdef _PC_ASYNC_IO
5864 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5865#endif
5866#ifdef _PC_CHOWN_RESTRICTED
5867 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5868#endif
5869#ifdef _PC_FILESIZEBITS
5870 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5871#endif
5872#ifdef _PC_LAST
5873 {"PC_LAST", _PC_LAST},
5874#endif
5875#ifdef _PC_LINK_MAX
5876 {"PC_LINK_MAX", _PC_LINK_MAX},
5877#endif
5878#ifdef _PC_MAX_CANON
5879 {"PC_MAX_CANON", _PC_MAX_CANON},
5880#endif
5881#ifdef _PC_MAX_INPUT
5882 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5883#endif
5884#ifdef _PC_NAME_MAX
5885 {"PC_NAME_MAX", _PC_NAME_MAX},
5886#endif
5887#ifdef _PC_NO_TRUNC
5888 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5889#endif
5890#ifdef _PC_PATH_MAX
5891 {"PC_PATH_MAX", _PC_PATH_MAX},
5892#endif
5893#ifdef _PC_PIPE_BUF
5894 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5895#endif
5896#ifdef _PC_PRIO_IO
5897 {"PC_PRIO_IO", _PC_PRIO_IO},
5898#endif
5899#ifdef _PC_SOCK_MAXBUF
5900 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5901#endif
5902#ifdef _PC_SYNC_IO
5903 {"PC_SYNC_IO", _PC_SYNC_IO},
5904#endif
5905#ifdef _PC_VDISABLE
5906 {"PC_VDISABLE", _PC_VDISABLE},
5907#endif
5908};
5909
Fred Drakec9680921999-12-13 16:37:25 +00005910static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005911conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005912{
5913 return conv_confname(arg, valuep, posix_constants_pathconf,
5914 sizeof(posix_constants_pathconf)
5915 / sizeof(struct constdef));
5916}
5917#endif
5918
5919#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005920PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005921"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005922Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005923If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005924
5925static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005926posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005927{
5928 PyObject *result = NULL;
5929 int name, fd;
5930
Fred Drake12c6e2d1999-12-14 21:25:03 +00005931 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5932 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005933 long limit;
5934
5935 errno = 0;
5936 limit = fpathconf(fd, name);
5937 if (limit == -1 && errno != 0)
5938 posix_error();
5939 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005940 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005941 }
5942 return result;
5943}
5944#endif
5945
5946
5947#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005948PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005949"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005950Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005951If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005952
5953static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005954posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005955{
5956 PyObject *result = NULL;
5957 int name;
5958 char *path;
5959
5960 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5961 conv_path_confname, &name)) {
5962 long limit;
5963
5964 errno = 0;
5965 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005966 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005967 if (errno == EINVAL)
5968 /* could be a path or name problem */
5969 posix_error();
5970 else
5971 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005972 }
Fred Drakec9680921999-12-13 16:37:25 +00005973 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005974 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005975 }
5976 return result;
5977}
5978#endif
5979
5980#ifdef HAVE_CONFSTR
5981static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005982#ifdef _CS_ARCHITECTURE
5983 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5984#endif
5985#ifdef _CS_HOSTNAME
5986 {"CS_HOSTNAME", _CS_HOSTNAME},
5987#endif
5988#ifdef _CS_HW_PROVIDER
5989 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5990#endif
5991#ifdef _CS_HW_SERIAL
5992 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5993#endif
5994#ifdef _CS_INITTAB_NAME
5995 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5996#endif
Fred Drakec9680921999-12-13 16:37:25 +00005997#ifdef _CS_LFS64_CFLAGS
5998 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5999#endif
6000#ifdef _CS_LFS64_LDFLAGS
6001 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6002#endif
6003#ifdef _CS_LFS64_LIBS
6004 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6005#endif
6006#ifdef _CS_LFS64_LINTFLAGS
6007 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6008#endif
6009#ifdef _CS_LFS_CFLAGS
6010 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6011#endif
6012#ifdef _CS_LFS_LDFLAGS
6013 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6014#endif
6015#ifdef _CS_LFS_LIBS
6016 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6017#endif
6018#ifdef _CS_LFS_LINTFLAGS
6019 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6020#endif
Fred Draked86ed291999-12-15 15:34:33 +00006021#ifdef _CS_MACHINE
6022 {"CS_MACHINE", _CS_MACHINE},
6023#endif
Fred Drakec9680921999-12-13 16:37:25 +00006024#ifdef _CS_PATH
6025 {"CS_PATH", _CS_PATH},
6026#endif
Fred Draked86ed291999-12-15 15:34:33 +00006027#ifdef _CS_RELEASE
6028 {"CS_RELEASE", _CS_RELEASE},
6029#endif
6030#ifdef _CS_SRPC_DOMAIN
6031 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6032#endif
6033#ifdef _CS_SYSNAME
6034 {"CS_SYSNAME", _CS_SYSNAME},
6035#endif
6036#ifdef _CS_VERSION
6037 {"CS_VERSION", _CS_VERSION},
6038#endif
Fred Drakec9680921999-12-13 16:37:25 +00006039#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6040 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6041#endif
6042#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6043 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6044#endif
6045#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6046 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6047#endif
6048#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6049 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6050#endif
6051#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6052 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6053#endif
6054#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6055 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6056#endif
6057#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6058 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6059#endif
6060#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6061 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6062#endif
6063#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6064 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6065#endif
6066#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6067 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6068#endif
6069#ifdef _CS_XBS5_LP64_OFF64_LIBS
6070 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6071#endif
6072#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6073 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6074#endif
6075#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6076 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6077#endif
6078#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6079 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6080#endif
6081#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6082 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6083#endif
6084#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6085 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6086#endif
Fred Draked86ed291999-12-15 15:34:33 +00006087#ifdef _MIPS_CS_AVAIL_PROCESSORS
6088 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6089#endif
6090#ifdef _MIPS_CS_BASE
6091 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6092#endif
6093#ifdef _MIPS_CS_HOSTID
6094 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6095#endif
6096#ifdef _MIPS_CS_HW_NAME
6097 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6098#endif
6099#ifdef _MIPS_CS_NUM_PROCESSORS
6100 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6101#endif
6102#ifdef _MIPS_CS_OSREL_MAJ
6103 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6104#endif
6105#ifdef _MIPS_CS_OSREL_MIN
6106 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6107#endif
6108#ifdef _MIPS_CS_OSREL_PATCH
6109 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6110#endif
6111#ifdef _MIPS_CS_OS_NAME
6112 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6113#endif
6114#ifdef _MIPS_CS_OS_PROVIDER
6115 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6116#endif
6117#ifdef _MIPS_CS_PROCESSORS
6118 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6119#endif
6120#ifdef _MIPS_CS_SERIAL
6121 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6122#endif
6123#ifdef _MIPS_CS_VENDOR
6124 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6125#endif
Fred Drakec9680921999-12-13 16:37:25 +00006126};
6127
6128static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006129conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006130{
6131 return conv_confname(arg, valuep, posix_constants_confstr,
6132 sizeof(posix_constants_confstr)
6133 / sizeof(struct constdef));
6134}
6135
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006136PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006137"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006138Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006139
6140static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006141posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006142{
6143 PyObject *result = NULL;
6144 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006145 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006146
6147 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006148 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006149
Fred Drakec9680921999-12-13 16:37:25 +00006150 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006151 len = confstr(name, buffer, sizeof(buffer));
6152 if (len == 0) {
6153 if (errno) {
6154 posix_error();
6155 }
6156 else {
6157 result = Py_None;
6158 Py_INCREF(Py_None);
6159 }
Fred Drakec9680921999-12-13 16:37:25 +00006160 }
6161 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006162 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006163 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006164 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006165 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006166 }
6167 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006168 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006169 }
6170 }
6171 return result;
6172}
6173#endif
6174
6175
6176#ifdef HAVE_SYSCONF
6177static struct constdef posix_constants_sysconf[] = {
6178#ifdef _SC_2_CHAR_TERM
6179 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6180#endif
6181#ifdef _SC_2_C_BIND
6182 {"SC_2_C_BIND", _SC_2_C_BIND},
6183#endif
6184#ifdef _SC_2_C_DEV
6185 {"SC_2_C_DEV", _SC_2_C_DEV},
6186#endif
6187#ifdef _SC_2_C_VERSION
6188 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6189#endif
6190#ifdef _SC_2_FORT_DEV
6191 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6192#endif
6193#ifdef _SC_2_FORT_RUN
6194 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6195#endif
6196#ifdef _SC_2_LOCALEDEF
6197 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6198#endif
6199#ifdef _SC_2_SW_DEV
6200 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6201#endif
6202#ifdef _SC_2_UPE
6203 {"SC_2_UPE", _SC_2_UPE},
6204#endif
6205#ifdef _SC_2_VERSION
6206 {"SC_2_VERSION", _SC_2_VERSION},
6207#endif
Fred Draked86ed291999-12-15 15:34:33 +00006208#ifdef _SC_ABI_ASYNCHRONOUS_IO
6209 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6210#endif
6211#ifdef _SC_ACL
6212 {"SC_ACL", _SC_ACL},
6213#endif
Fred Drakec9680921999-12-13 16:37:25 +00006214#ifdef _SC_AIO_LISTIO_MAX
6215 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6216#endif
Fred Drakec9680921999-12-13 16:37:25 +00006217#ifdef _SC_AIO_MAX
6218 {"SC_AIO_MAX", _SC_AIO_MAX},
6219#endif
6220#ifdef _SC_AIO_PRIO_DELTA_MAX
6221 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6222#endif
6223#ifdef _SC_ARG_MAX
6224 {"SC_ARG_MAX", _SC_ARG_MAX},
6225#endif
6226#ifdef _SC_ASYNCHRONOUS_IO
6227 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6228#endif
6229#ifdef _SC_ATEXIT_MAX
6230 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6231#endif
Fred Draked86ed291999-12-15 15:34:33 +00006232#ifdef _SC_AUDIT
6233 {"SC_AUDIT", _SC_AUDIT},
6234#endif
Fred Drakec9680921999-12-13 16:37:25 +00006235#ifdef _SC_AVPHYS_PAGES
6236 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6237#endif
6238#ifdef _SC_BC_BASE_MAX
6239 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6240#endif
6241#ifdef _SC_BC_DIM_MAX
6242 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6243#endif
6244#ifdef _SC_BC_SCALE_MAX
6245 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6246#endif
6247#ifdef _SC_BC_STRING_MAX
6248 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6249#endif
Fred Draked86ed291999-12-15 15:34:33 +00006250#ifdef _SC_CAP
6251 {"SC_CAP", _SC_CAP},
6252#endif
Fred Drakec9680921999-12-13 16:37:25 +00006253#ifdef _SC_CHARCLASS_NAME_MAX
6254 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6255#endif
6256#ifdef _SC_CHAR_BIT
6257 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6258#endif
6259#ifdef _SC_CHAR_MAX
6260 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6261#endif
6262#ifdef _SC_CHAR_MIN
6263 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6264#endif
6265#ifdef _SC_CHILD_MAX
6266 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6267#endif
6268#ifdef _SC_CLK_TCK
6269 {"SC_CLK_TCK", _SC_CLK_TCK},
6270#endif
6271#ifdef _SC_COHER_BLKSZ
6272 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6273#endif
6274#ifdef _SC_COLL_WEIGHTS_MAX
6275 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6276#endif
6277#ifdef _SC_DCACHE_ASSOC
6278 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6279#endif
6280#ifdef _SC_DCACHE_BLKSZ
6281 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6282#endif
6283#ifdef _SC_DCACHE_LINESZ
6284 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6285#endif
6286#ifdef _SC_DCACHE_SZ
6287 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6288#endif
6289#ifdef _SC_DCACHE_TBLKSZ
6290 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6291#endif
6292#ifdef _SC_DELAYTIMER_MAX
6293 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6294#endif
6295#ifdef _SC_EQUIV_CLASS_MAX
6296 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6297#endif
6298#ifdef _SC_EXPR_NEST_MAX
6299 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6300#endif
6301#ifdef _SC_FSYNC
6302 {"SC_FSYNC", _SC_FSYNC},
6303#endif
6304#ifdef _SC_GETGR_R_SIZE_MAX
6305 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6306#endif
6307#ifdef _SC_GETPW_R_SIZE_MAX
6308 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6309#endif
6310#ifdef _SC_ICACHE_ASSOC
6311 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6312#endif
6313#ifdef _SC_ICACHE_BLKSZ
6314 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6315#endif
6316#ifdef _SC_ICACHE_LINESZ
6317 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6318#endif
6319#ifdef _SC_ICACHE_SZ
6320 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6321#endif
Fred Draked86ed291999-12-15 15:34:33 +00006322#ifdef _SC_INF
6323 {"SC_INF", _SC_INF},
6324#endif
Fred Drakec9680921999-12-13 16:37:25 +00006325#ifdef _SC_INT_MAX
6326 {"SC_INT_MAX", _SC_INT_MAX},
6327#endif
6328#ifdef _SC_INT_MIN
6329 {"SC_INT_MIN", _SC_INT_MIN},
6330#endif
6331#ifdef _SC_IOV_MAX
6332 {"SC_IOV_MAX", _SC_IOV_MAX},
6333#endif
Fred Draked86ed291999-12-15 15:34:33 +00006334#ifdef _SC_IP_SECOPTS
6335 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6336#endif
Fred Drakec9680921999-12-13 16:37:25 +00006337#ifdef _SC_JOB_CONTROL
6338 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6339#endif
Fred Draked86ed291999-12-15 15:34:33 +00006340#ifdef _SC_KERN_POINTERS
6341 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6342#endif
6343#ifdef _SC_KERN_SIM
6344 {"SC_KERN_SIM", _SC_KERN_SIM},
6345#endif
Fred Drakec9680921999-12-13 16:37:25 +00006346#ifdef _SC_LINE_MAX
6347 {"SC_LINE_MAX", _SC_LINE_MAX},
6348#endif
6349#ifdef _SC_LOGIN_NAME_MAX
6350 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6351#endif
6352#ifdef _SC_LOGNAME_MAX
6353 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6354#endif
6355#ifdef _SC_LONG_BIT
6356 {"SC_LONG_BIT", _SC_LONG_BIT},
6357#endif
Fred Draked86ed291999-12-15 15:34:33 +00006358#ifdef _SC_MAC
6359 {"SC_MAC", _SC_MAC},
6360#endif
Fred Drakec9680921999-12-13 16:37:25 +00006361#ifdef _SC_MAPPED_FILES
6362 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6363#endif
6364#ifdef _SC_MAXPID
6365 {"SC_MAXPID", _SC_MAXPID},
6366#endif
6367#ifdef _SC_MB_LEN_MAX
6368 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6369#endif
6370#ifdef _SC_MEMLOCK
6371 {"SC_MEMLOCK", _SC_MEMLOCK},
6372#endif
6373#ifdef _SC_MEMLOCK_RANGE
6374 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6375#endif
6376#ifdef _SC_MEMORY_PROTECTION
6377 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6378#endif
6379#ifdef _SC_MESSAGE_PASSING
6380 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6381#endif
Fred Draked86ed291999-12-15 15:34:33 +00006382#ifdef _SC_MMAP_FIXED_ALIGNMENT
6383 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6384#endif
Fred Drakec9680921999-12-13 16:37:25 +00006385#ifdef _SC_MQ_OPEN_MAX
6386 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6387#endif
6388#ifdef _SC_MQ_PRIO_MAX
6389 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6390#endif
Fred Draked86ed291999-12-15 15:34:33 +00006391#ifdef _SC_NACLS_MAX
6392 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6393#endif
Fred Drakec9680921999-12-13 16:37:25 +00006394#ifdef _SC_NGROUPS_MAX
6395 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6396#endif
6397#ifdef _SC_NL_ARGMAX
6398 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6399#endif
6400#ifdef _SC_NL_LANGMAX
6401 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6402#endif
6403#ifdef _SC_NL_MSGMAX
6404 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6405#endif
6406#ifdef _SC_NL_NMAX
6407 {"SC_NL_NMAX", _SC_NL_NMAX},
6408#endif
6409#ifdef _SC_NL_SETMAX
6410 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6411#endif
6412#ifdef _SC_NL_TEXTMAX
6413 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6414#endif
6415#ifdef _SC_NPROCESSORS_CONF
6416 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6417#endif
6418#ifdef _SC_NPROCESSORS_ONLN
6419 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6420#endif
Fred Draked86ed291999-12-15 15:34:33 +00006421#ifdef _SC_NPROC_CONF
6422 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6423#endif
6424#ifdef _SC_NPROC_ONLN
6425 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6426#endif
Fred Drakec9680921999-12-13 16:37:25 +00006427#ifdef _SC_NZERO
6428 {"SC_NZERO", _SC_NZERO},
6429#endif
6430#ifdef _SC_OPEN_MAX
6431 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6432#endif
6433#ifdef _SC_PAGESIZE
6434 {"SC_PAGESIZE", _SC_PAGESIZE},
6435#endif
6436#ifdef _SC_PAGE_SIZE
6437 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6438#endif
6439#ifdef _SC_PASS_MAX
6440 {"SC_PASS_MAX", _SC_PASS_MAX},
6441#endif
6442#ifdef _SC_PHYS_PAGES
6443 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6444#endif
6445#ifdef _SC_PII
6446 {"SC_PII", _SC_PII},
6447#endif
6448#ifdef _SC_PII_INTERNET
6449 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6450#endif
6451#ifdef _SC_PII_INTERNET_DGRAM
6452 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6453#endif
6454#ifdef _SC_PII_INTERNET_STREAM
6455 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6456#endif
6457#ifdef _SC_PII_OSI
6458 {"SC_PII_OSI", _SC_PII_OSI},
6459#endif
6460#ifdef _SC_PII_OSI_CLTS
6461 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6462#endif
6463#ifdef _SC_PII_OSI_COTS
6464 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6465#endif
6466#ifdef _SC_PII_OSI_M
6467 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6468#endif
6469#ifdef _SC_PII_SOCKET
6470 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6471#endif
6472#ifdef _SC_PII_XTI
6473 {"SC_PII_XTI", _SC_PII_XTI},
6474#endif
6475#ifdef _SC_POLL
6476 {"SC_POLL", _SC_POLL},
6477#endif
6478#ifdef _SC_PRIORITIZED_IO
6479 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6480#endif
6481#ifdef _SC_PRIORITY_SCHEDULING
6482 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6483#endif
6484#ifdef _SC_REALTIME_SIGNALS
6485 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6486#endif
6487#ifdef _SC_RE_DUP_MAX
6488 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6489#endif
6490#ifdef _SC_RTSIG_MAX
6491 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6492#endif
6493#ifdef _SC_SAVED_IDS
6494 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6495#endif
6496#ifdef _SC_SCHAR_MAX
6497 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6498#endif
6499#ifdef _SC_SCHAR_MIN
6500 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6501#endif
6502#ifdef _SC_SELECT
6503 {"SC_SELECT", _SC_SELECT},
6504#endif
6505#ifdef _SC_SEMAPHORES
6506 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6507#endif
6508#ifdef _SC_SEM_NSEMS_MAX
6509 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6510#endif
6511#ifdef _SC_SEM_VALUE_MAX
6512 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6513#endif
6514#ifdef _SC_SHARED_MEMORY_OBJECTS
6515 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6516#endif
6517#ifdef _SC_SHRT_MAX
6518 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6519#endif
6520#ifdef _SC_SHRT_MIN
6521 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6522#endif
6523#ifdef _SC_SIGQUEUE_MAX
6524 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6525#endif
6526#ifdef _SC_SIGRT_MAX
6527 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6528#endif
6529#ifdef _SC_SIGRT_MIN
6530 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6531#endif
Fred Draked86ed291999-12-15 15:34:33 +00006532#ifdef _SC_SOFTPOWER
6533 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6534#endif
Fred Drakec9680921999-12-13 16:37:25 +00006535#ifdef _SC_SPLIT_CACHE
6536 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6537#endif
6538#ifdef _SC_SSIZE_MAX
6539 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6540#endif
6541#ifdef _SC_STACK_PROT
6542 {"SC_STACK_PROT", _SC_STACK_PROT},
6543#endif
6544#ifdef _SC_STREAM_MAX
6545 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6546#endif
6547#ifdef _SC_SYNCHRONIZED_IO
6548 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6549#endif
6550#ifdef _SC_THREADS
6551 {"SC_THREADS", _SC_THREADS},
6552#endif
6553#ifdef _SC_THREAD_ATTR_STACKADDR
6554 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6555#endif
6556#ifdef _SC_THREAD_ATTR_STACKSIZE
6557 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6558#endif
6559#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6560 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6561#endif
6562#ifdef _SC_THREAD_KEYS_MAX
6563 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6564#endif
6565#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6566 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6567#endif
6568#ifdef _SC_THREAD_PRIO_INHERIT
6569 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6570#endif
6571#ifdef _SC_THREAD_PRIO_PROTECT
6572 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6573#endif
6574#ifdef _SC_THREAD_PROCESS_SHARED
6575 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6576#endif
6577#ifdef _SC_THREAD_SAFE_FUNCTIONS
6578 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6579#endif
6580#ifdef _SC_THREAD_STACK_MIN
6581 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6582#endif
6583#ifdef _SC_THREAD_THREADS_MAX
6584 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6585#endif
6586#ifdef _SC_TIMERS
6587 {"SC_TIMERS", _SC_TIMERS},
6588#endif
6589#ifdef _SC_TIMER_MAX
6590 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6591#endif
6592#ifdef _SC_TTY_NAME_MAX
6593 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6594#endif
6595#ifdef _SC_TZNAME_MAX
6596 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6597#endif
6598#ifdef _SC_T_IOV_MAX
6599 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6600#endif
6601#ifdef _SC_UCHAR_MAX
6602 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6603#endif
6604#ifdef _SC_UINT_MAX
6605 {"SC_UINT_MAX", _SC_UINT_MAX},
6606#endif
6607#ifdef _SC_UIO_MAXIOV
6608 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6609#endif
6610#ifdef _SC_ULONG_MAX
6611 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6612#endif
6613#ifdef _SC_USHRT_MAX
6614 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6615#endif
6616#ifdef _SC_VERSION
6617 {"SC_VERSION", _SC_VERSION},
6618#endif
6619#ifdef _SC_WORD_BIT
6620 {"SC_WORD_BIT", _SC_WORD_BIT},
6621#endif
6622#ifdef _SC_XBS5_ILP32_OFF32
6623 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6624#endif
6625#ifdef _SC_XBS5_ILP32_OFFBIG
6626 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6627#endif
6628#ifdef _SC_XBS5_LP64_OFF64
6629 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6630#endif
6631#ifdef _SC_XBS5_LPBIG_OFFBIG
6632 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6633#endif
6634#ifdef _SC_XOPEN_CRYPT
6635 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6636#endif
6637#ifdef _SC_XOPEN_ENH_I18N
6638 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6639#endif
6640#ifdef _SC_XOPEN_LEGACY
6641 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6642#endif
6643#ifdef _SC_XOPEN_REALTIME
6644 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6645#endif
6646#ifdef _SC_XOPEN_REALTIME_THREADS
6647 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6648#endif
6649#ifdef _SC_XOPEN_SHM
6650 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6651#endif
6652#ifdef _SC_XOPEN_UNIX
6653 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6654#endif
6655#ifdef _SC_XOPEN_VERSION
6656 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6657#endif
6658#ifdef _SC_XOPEN_XCU_VERSION
6659 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6660#endif
6661#ifdef _SC_XOPEN_XPG2
6662 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6663#endif
6664#ifdef _SC_XOPEN_XPG3
6665 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6666#endif
6667#ifdef _SC_XOPEN_XPG4
6668 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6669#endif
6670};
6671
6672static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006673conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006674{
6675 return conv_confname(arg, valuep, posix_constants_sysconf,
6676 sizeof(posix_constants_sysconf)
6677 / sizeof(struct constdef));
6678}
6679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006680PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006681"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006682Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006683
6684static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006685posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006686{
6687 PyObject *result = NULL;
6688 int name;
6689
6690 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6691 int value;
6692
6693 errno = 0;
6694 value = sysconf(name);
6695 if (value == -1 && errno != 0)
6696 posix_error();
6697 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006698 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006699 }
6700 return result;
6701}
6702#endif
6703
6704
Fred Drakebec628d1999-12-15 18:31:10 +00006705/* This code is used to ensure that the tables of configuration value names
6706 * are in sorted order as required by conv_confname(), and also to build the
6707 * the exported dictionaries that are used to publish information about the
6708 * names available on the host platform.
6709 *
6710 * Sorting the table at runtime ensures that the table is properly ordered
6711 * when used, even for platforms we're not able to test on. It also makes
6712 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006713 */
Fred Drakebec628d1999-12-15 18:31:10 +00006714
6715static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006716cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006717{
6718 const struct constdef *c1 =
6719 (const struct constdef *) v1;
6720 const struct constdef *c2 =
6721 (const struct constdef *) v2;
6722
6723 return strcmp(c1->name, c2->name);
6724}
6725
6726static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006727setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006728 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006729{
Fred Drakebec628d1999-12-15 18:31:10 +00006730 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006731 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006732
6733 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6734 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006735 if (d == NULL)
6736 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006737
Barry Warsaw3155db32000-04-13 15:20:40 +00006738 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006739 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006740 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6741 Py_XDECREF(o);
6742 Py_DECREF(d);
6743 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006744 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006745 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006746 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006747 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006748}
6749
Fred Drakebec628d1999-12-15 18:31:10 +00006750/* Return -1 on failure, 0 on success. */
6751static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006752setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006753{
6754#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006755 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006756 sizeof(posix_constants_pathconf)
6757 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006758 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006759 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006760#endif
6761#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006762 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006763 sizeof(posix_constants_confstr)
6764 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006765 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006766 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006767#endif
6768#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006769 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006770 sizeof(posix_constants_sysconf)
6771 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006772 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006773 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006774#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006775 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006776}
Fred Draked86ed291999-12-15 15:34:33 +00006777
6778
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006779PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006780"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006781Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006782in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006783
6784static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006785posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006786{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006787 abort();
6788 /*NOTREACHED*/
6789 Py_FatalError("abort() called from Python code didn't abort!");
6790 return NULL;
6791}
Fred Drakebec628d1999-12-15 18:31:10 +00006792
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006793#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006794PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006795"startfile(filepath [, operation]) - Start a file with its associated\n\
6796application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006797\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006798When \"operation\" is not specified or \"open\", this acts like\n\
6799double-clicking the file in Explorer, or giving the file name as an\n\
6800argument to the DOS \"start\" command: the file is opened with whatever\n\
6801application (if any) its extension is associated.\n\
6802When another \"operation\" is given, it specifies what should be done with\n\
6803the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006804\n\
6805startfile returns as soon as the associated application is launched.\n\
6806There is no option to wait for the application to close, and no way\n\
6807to retrieve the application's exit status.\n\
6808\n\
6809The filepath is relative to the current directory. If you want to use\n\
6810an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006811the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006812
6813static PyObject *
6814win32_startfile(PyObject *self, PyObject *args)
6815{
Martin v. Löwis011e8422009-05-05 04:43:17 +00006816 PyObject *ofilepath;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006817 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006818 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006819 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006820
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006821 if (unicode_file_names()) {
6822 PyObject *unipath, *woperation = NULL;
6823 if (!PyArg_ParseTuple(args, "U|s:startfile",
6824 &unipath, &operation)) {
6825 PyErr_Clear();
6826 goto normal;
6827 }
6828
6829
6830 if (operation) {
6831 woperation = PyUnicode_DecodeASCII(operation,
6832 strlen(operation), NULL);
6833 if (!woperation) {
6834 PyErr_Clear();
6835 operation = NULL;
6836 goto normal;
6837 }
6838 }
6839
6840 Py_BEGIN_ALLOW_THREADS
6841 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6842 PyUnicode_AS_UNICODE(unipath),
6843 NULL, NULL, SW_SHOWNORMAL);
6844 Py_END_ALLOW_THREADS
6845
6846 Py_XDECREF(woperation);
6847 if (rc <= (HINSTANCE)32) {
6848 PyObject *errval = win32_error_unicode("startfile",
6849 PyUnicode_AS_UNICODE(unipath));
6850 return errval;
6851 }
6852 Py_INCREF(Py_None);
6853 return Py_None;
6854 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006855
6856normal:
Martin v. Löwis011e8422009-05-05 04:43:17 +00006857 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6858 PyUnicode_FSConverter, &ofilepath,
Georg Brandlf4f44152006-02-18 22:29:33 +00006859 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006860 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006861 filepath = bytes2str(ofilepath, 1);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006862 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006863 rc = ShellExecute((HWND)0, operation, filepath,
6864 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006865 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006866 if (rc <= (HINSTANCE)32) {
6867 PyObject *errval = win32_error("startfile", filepath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006868 release_bytes(ofilepath);
Georg Brandle9f8ec92005-09-25 06:16:40 +00006869 return errval;
6870 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00006871 release_bytes(ofilepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006872 Py_INCREF(Py_None);
6873 return Py_None;
6874}
6875#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006876
Martin v. Löwis438b5342002-12-27 10:16:42 +00006877#ifdef HAVE_GETLOADAVG
6878PyDoc_STRVAR(posix_getloadavg__doc__,
6879"getloadavg() -> (float, float, float)\n\n\
6880Return the number of processes in the system run queue averaged over\n\
6881the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6882was unobtainable");
6883
6884static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006885posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006886{
6887 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006888 if (getloadavg(loadavg, 3)!=3) {
6889 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6890 return NULL;
6891 } else
6892 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6893}
6894#endif
6895
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006896#ifdef MS_WINDOWS
6897
6898PyDoc_STRVAR(win32_urandom__doc__,
6899"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006900Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006901
6902typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6903 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6904 DWORD dwFlags );
6905typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6906 BYTE *pbBuffer );
6907
6908static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006909/* This handle is never explicitly released. Instead, the operating
6910 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006911static HCRYPTPROV hCryptProv = 0;
6912
Tim Peters4ad82172004-08-30 17:02:04 +00006913static PyObject*
6914win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006915{
Tim Petersd3115382004-08-30 17:36:46 +00006916 int howMany;
6917 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006918
Tim Peters4ad82172004-08-30 17:02:04 +00006919 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006920 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006921 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006922 if (howMany < 0)
6923 return PyErr_Format(PyExc_ValueError,
6924 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006925
Tim Peters4ad82172004-08-30 17:02:04 +00006926 if (hCryptProv == 0) {
6927 HINSTANCE hAdvAPI32 = NULL;
6928 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006929
Tim Peters4ad82172004-08-30 17:02:04 +00006930 /* Obtain handle to the DLL containing CryptoAPI
6931 This should not fail */
6932 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6933 if(hAdvAPI32 == NULL)
6934 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006935
Tim Peters4ad82172004-08-30 17:02:04 +00006936 /* Obtain pointers to the CryptoAPI functions
6937 This will fail on some early versions of Win95 */
6938 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6939 hAdvAPI32,
6940 "CryptAcquireContextA");
6941 if (pCryptAcquireContext == NULL)
6942 return PyErr_Format(PyExc_NotImplementedError,
6943 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006944
Tim Peters4ad82172004-08-30 17:02:04 +00006945 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6946 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006947 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006948 return PyErr_Format(PyExc_NotImplementedError,
6949 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006950
Tim Peters4ad82172004-08-30 17:02:04 +00006951 /* Acquire context */
6952 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6953 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6954 return win32_error("CryptAcquireContext", NULL);
6955 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006956
Tim Peters4ad82172004-08-30 17:02:04 +00006957 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006958 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006959 if (result != NULL) {
6960 /* Get random data */
Amaury Forgeot d'Arca05ada32008-07-21 21:13:14 +00006961 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00006962 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006963 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006964 Py_DECREF(result);
6965 return win32_error("CryptGenRandom", NULL);
6966 }
Tim Peters4ad82172004-08-30 17:02:04 +00006967 }
Tim Petersd3115382004-08-30 17:36:46 +00006968 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006969}
6970#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006971
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006972PyDoc_STRVAR(device_encoding__doc__,
6973"device_encoding(fd) -> str\n\n\
6974Return a string describing the encoding of the device\n\
6975if the output is a terminal; else return None.");
6976
6977static PyObject *
6978device_encoding(PyObject *self, PyObject *args)
6979{
6980 int fd;
6981 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6982 return NULL;
Kristján Valur Jónsson649170b2009-03-24 14:15:49 +00006983 if (!_PyVerify_fd(fd) || !isatty(fd)) {
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006984 Py_INCREF(Py_None);
6985 return Py_None;
6986 }
6987#if defined(MS_WINDOWS) || defined(MS_WIN64)
6988 if (fd == 0) {
6989 char buf[100];
6990 sprintf(buf, "cp%d", GetConsoleCP());
6991 return PyUnicode_FromString(buf);
6992 }
6993 if (fd == 1 || fd == 2) {
6994 char buf[100];
6995 sprintf(buf, "cp%d", GetConsoleOutputCP());
6996 return PyUnicode_FromString(buf);
6997 }
6998#elif defined(CODESET)
6999 {
7000 char *codeset = nl_langinfo(CODESET);
Mark Dickinsonda2706b2008-12-11 18:03:03 +00007001 if (codeset != NULL && codeset[0] != 0)
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007002 return PyUnicode_FromString(codeset);
7003 }
7004#endif
7005 Py_INCREF(Py_None);
7006 return Py_None;
7007}
7008
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007009#ifdef __VMS
7010/* Use openssl random routine */
7011#include <openssl/rand.h>
7012PyDoc_STRVAR(vms_urandom__doc__,
7013"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007014Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007015
7016static PyObject*
7017vms_urandom(PyObject *self, PyObject *args)
7018{
7019 int howMany;
7020 PyObject* result;
7021
7022 /* Read arguments */
7023 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7024 return NULL;
7025 if (howMany < 0)
7026 return PyErr_Format(PyExc_ValueError,
7027 "negative argument not allowed");
7028
7029 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00007030 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007031 if (result != NULL) {
7032 /* Get random data */
7033 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00007034 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007035 howMany) < 0) {
7036 Py_DECREF(result);
7037 return PyErr_Format(PyExc_ValueError,
7038 "RAND_pseudo_bytes");
7039 }
7040 }
7041 return result;
7042}
7043#endif
7044
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007045static PyMethodDef posix_methods[] = {
7046 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7047#ifdef HAVE_TTYNAME
7048 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7049#endif
7050 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007051#ifdef HAVE_CHFLAGS
7052 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
7053#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007054 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007055#ifdef HAVE_FCHMOD
7056 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
7057#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007058#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007059 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007060#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007061#ifdef HAVE_LCHMOD
7062 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
7063#endif /* HAVE_LCHMOD */
7064#ifdef HAVE_FCHOWN
7065 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
7066#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007067#ifdef HAVE_LCHFLAGS
7068 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
7069#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007070#ifdef HAVE_LCHOWN
7071 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7072#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007073#ifdef HAVE_CHROOT
7074 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7075#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007076#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007077 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007078#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007079#ifdef HAVE_GETCWD
Guido van Rossumf0af3e32008-10-02 18:55:37 +00007080 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7081 METH_NOARGS, posix_getcwd__doc__},
7082 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7083 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007084#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007085#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007086 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007087#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007088 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7089 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7090 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007091#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007092 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007093#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007094#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007095 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007096#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007097 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7098 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7099 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007100 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007101#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007102 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007103#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007104#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007105 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007106#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007107 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007108#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007109 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007110#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007111 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7112 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7113 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007114#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007115 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007116#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007117 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007118#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007119 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7120 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007121#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007122#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007123 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7124 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007125#if defined(PYOS_OS2)
7126 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7127 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7128#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007129#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007130#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007131 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007132#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007133#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007134 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007135#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007136#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007137 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007138#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007139#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007140 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007141#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007142#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007143 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007144#endif /* HAVE_GETEGID */
7145#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007146 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007147#endif /* HAVE_GETEUID */
7148#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007149 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007150#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007151#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007152 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007153#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007154 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007155#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007156 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007157#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007158#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007159 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007160#endif /* HAVE_GETPPID */
7161#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007162 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007163#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007164#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007165 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007166#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007167#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007168 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007169#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007170#ifdef HAVE_KILLPG
7171 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7172#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007173#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007174 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007175#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007176#ifdef MS_WINDOWS
7177 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7178#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007179#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007180 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007181#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007182#ifdef HAVE_SETEUID
7183 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7184#endif /* HAVE_SETEUID */
7185#ifdef HAVE_SETEGID
7186 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7187#endif /* HAVE_SETEGID */
7188#ifdef HAVE_SETREUID
7189 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7190#endif /* HAVE_SETREUID */
7191#ifdef HAVE_SETREGID
7192 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7193#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007194#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007195 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007196#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007197#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00007198 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007199#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007200#ifdef HAVE_GETPGID
7201 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7202#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007203#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007204 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007205#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007206#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007207 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007208#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007209#ifdef HAVE_WAIT3
7210 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7211#endif /* HAVE_WAIT3 */
7212#ifdef HAVE_WAIT4
7213 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7214#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007215#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007216 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007217#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007218#ifdef HAVE_GETSID
7219 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7220#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007221#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007222 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007223#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007224#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007225 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007226#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007227#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007228 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007229#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007230#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007231 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007232#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007233 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7234 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00007235 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007236 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007237 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7238 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7239 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7240 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7241 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7242 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007243 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007244#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007245 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007246#endif
7247#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007248 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007249#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007250#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007251 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7252#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007253#ifdef HAVE_DEVICE_MACROS
7254 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7255 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7256 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7257#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007258#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007259 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007260#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007261#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007262 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007263#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007264#ifdef HAVE_UNSETENV
7265 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7266#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007267 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007268#ifdef HAVE_FCHDIR
7269 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7270#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007271#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007272 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007273#endif
7274#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007275 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007276#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007277#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007278#ifdef WCOREDUMP
7279 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7280#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007281#ifdef WIFCONTINUED
7282 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7283#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007284#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007285 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007286#endif /* WIFSTOPPED */
7287#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007288 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007289#endif /* WIFSIGNALED */
7290#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007291 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007292#endif /* WIFEXITED */
7293#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007294 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007295#endif /* WEXITSTATUS */
7296#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007297 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007298#endif /* WTERMSIG */
7299#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007300 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007301#endif /* WSTOPSIG */
7302#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007303#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007304 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007305#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007306#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007307 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007308#endif
Fred Drakec9680921999-12-13 16:37:25 +00007309#ifdef HAVE_CONFSTR
7310 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7311#endif
7312#ifdef HAVE_SYSCONF
7313 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7314#endif
7315#ifdef HAVE_FPATHCONF
7316 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7317#endif
7318#ifdef HAVE_PATHCONF
7319 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7320#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007321 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007322#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007323 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7324#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007325#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007326 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007327#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007328 #ifdef MS_WINDOWS
7329 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7330 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007331 #ifdef __VMS
7332 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7333 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007334 {NULL, NULL} /* Sentinel */
7335};
7336
7337
Barry Warsaw4a342091996-12-19 23:50:02 +00007338static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007339ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007340{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007341 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007342}
7343
Guido van Rossumd48f2521997-12-05 22:19:34 +00007344#if defined(PYOS_OS2)
7345/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007346static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007347{
7348 APIRET rc;
7349 ULONG values[QSV_MAX+1];
7350 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007351 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007352
7353 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007354 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007355 Py_END_ALLOW_THREADS
7356
7357 if (rc != NO_ERROR) {
7358 os2_error(rc);
7359 return -1;
7360 }
7361
Fred Drake4d1e64b2002-04-15 19:40:07 +00007362 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7363 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7364 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7365 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7366 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7367 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7368 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007369
7370 switch (values[QSV_VERSION_MINOR]) {
7371 case 0: ver = "2.00"; break;
7372 case 10: ver = "2.10"; break;
7373 case 11: ver = "2.11"; break;
7374 case 30: ver = "3.00"; break;
7375 case 40: ver = "4.00"; break;
7376 case 50: ver = "5.00"; break;
7377 default:
Tim Peters885d4572001-11-28 20:27:42 +00007378 PyOS_snprintf(tmp, sizeof(tmp),
7379 "%d-%d", values[QSV_VERSION_MAJOR],
7380 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007381 ver = &tmp[0];
7382 }
7383
7384 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007385 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007386 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007387
7388 /* Add Indicator of Which Drive was Used to Boot the System */
7389 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7390 tmp[1] = ':';
7391 tmp[2] = '\0';
7392
Fred Drake4d1e64b2002-04-15 19:40:07 +00007393 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007394}
7395#endif
7396
Barry Warsaw4a342091996-12-19 23:50:02 +00007397static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007398all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007399{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007400#ifdef F_OK
7401 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007402#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007403#ifdef R_OK
7404 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007405#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007406#ifdef W_OK
7407 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007408#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007409#ifdef X_OK
7410 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007411#endif
Fred Drakec9680921999-12-13 16:37:25 +00007412#ifdef NGROUPS_MAX
7413 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7414#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007415#ifdef TMP_MAX
7416 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7417#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007418#ifdef WCONTINUED
7419 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7420#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007421#ifdef WNOHANG
7422 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007423#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007424#ifdef WUNTRACED
7425 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7426#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007427#ifdef O_RDONLY
7428 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7429#endif
7430#ifdef O_WRONLY
7431 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7432#endif
7433#ifdef O_RDWR
7434 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7435#endif
7436#ifdef O_NDELAY
7437 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7438#endif
7439#ifdef O_NONBLOCK
7440 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7441#endif
7442#ifdef O_APPEND
7443 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7444#endif
7445#ifdef O_DSYNC
7446 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7447#endif
7448#ifdef O_RSYNC
7449 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7450#endif
7451#ifdef O_SYNC
7452 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7453#endif
7454#ifdef O_NOCTTY
7455 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7456#endif
7457#ifdef O_CREAT
7458 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7459#endif
7460#ifdef O_EXCL
7461 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7462#endif
7463#ifdef O_TRUNC
7464 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7465#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007466#ifdef O_BINARY
7467 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7468#endif
7469#ifdef O_TEXT
7470 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7471#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007472#ifdef O_LARGEFILE
7473 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7474#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007475#ifdef O_SHLOCK
7476 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7477#endif
7478#ifdef O_EXLOCK
7479 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7480#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007481
Tim Peters5aa91602002-01-30 05:46:57 +00007482/* MS Windows */
7483#ifdef O_NOINHERIT
7484 /* Don't inherit in child processes. */
7485 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7486#endif
7487#ifdef _O_SHORT_LIVED
7488 /* Optimize for short life (keep in memory). */
7489 /* MS forgot to define this one with a non-underscore form too. */
7490 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7491#endif
7492#ifdef O_TEMPORARY
7493 /* Automatically delete when last handle is closed. */
7494 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7495#endif
7496#ifdef O_RANDOM
7497 /* Optimize for random access. */
7498 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7499#endif
7500#ifdef O_SEQUENTIAL
7501 /* Optimize for sequential access. */
7502 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7503#endif
7504
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007505/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007506#ifdef O_ASYNC
7507 /* Send a SIGIO signal whenever input or output
7508 becomes available on file descriptor */
7509 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7510#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007511#ifdef O_DIRECT
7512 /* Direct disk access. */
7513 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7514#endif
7515#ifdef O_DIRECTORY
7516 /* Must be a directory. */
7517 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7518#endif
7519#ifdef O_NOFOLLOW
7520 /* Do not follow links. */
7521 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7522#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007523#ifdef O_NOATIME
7524 /* Do not update the access time. */
7525 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7526#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007527
Barry Warsaw5676bd12003-01-07 20:57:09 +00007528 /* These come from sysexits.h */
7529#ifdef EX_OK
7530 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007531#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007532#ifdef EX_USAGE
7533 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007534#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007535#ifdef EX_DATAERR
7536 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007537#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007538#ifdef EX_NOINPUT
7539 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007540#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007541#ifdef EX_NOUSER
7542 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007543#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007544#ifdef EX_NOHOST
7545 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007546#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007547#ifdef EX_UNAVAILABLE
7548 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007549#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007550#ifdef EX_SOFTWARE
7551 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007552#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007553#ifdef EX_OSERR
7554 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007555#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007556#ifdef EX_OSFILE
7557 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007558#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007559#ifdef EX_CANTCREAT
7560 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007561#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007562#ifdef EX_IOERR
7563 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007564#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007565#ifdef EX_TEMPFAIL
7566 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007567#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007568#ifdef EX_PROTOCOL
7569 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007570#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007571#ifdef EX_NOPERM
7572 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007573#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007574#ifdef EX_CONFIG
7575 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007576#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007577#ifdef EX_NOTFOUND
7578 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007579#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007580
Guido van Rossum246bc171999-02-01 23:54:31 +00007581#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007582#if defined(PYOS_OS2) && defined(PYCC_GCC)
7583 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7584 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7585 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7586 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7587 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7588 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7589 if (ins(d, "P_PM", (long)P_PM)) return -1;
7590 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7591 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7592 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7593 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7594 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7595 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7596 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7597 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7598 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7599 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7600 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7601 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7602 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7603#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007604 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7605 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7606 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7607 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7608 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007609#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007610#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007611
Guido van Rossumd48f2521997-12-05 22:19:34 +00007612#if defined(PYOS_OS2)
7613 if (insertvalues(d)) return -1;
7614#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007615 return 0;
7616}
7617
7618
Tim Peters5aa91602002-01-30 05:46:57 +00007619#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007620#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007621#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007622
7623#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007624#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007625#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007626
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007627#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007628#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007629#define MODNAME "posix"
7630#endif
7631
Martin v. Löwis1a214512008-06-11 05:26:20 +00007632static struct PyModuleDef posixmodule = {
7633 PyModuleDef_HEAD_INIT,
7634 MODNAME,
7635 posix__doc__,
7636 -1,
7637 posix_methods,
7638 NULL,
7639 NULL,
7640 NULL,
7641 NULL
7642};
7643
7644
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007645PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007646INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007647{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007648 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007649
Martin v. Löwis1a214512008-06-11 05:26:20 +00007650 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007651 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007652 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007653
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007654 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007655 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007656 Py_XINCREF(v);
7657 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007658 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007659 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007660
Fred Drake4d1e64b2002-04-15 19:40:07 +00007661 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007662 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007663
Fred Drake4d1e64b2002-04-15 19:40:07 +00007664 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007665 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007666
Fred Drake4d1e64b2002-04-15 19:40:07 +00007667 Py_INCREF(PyExc_OSError);
7668 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007669
Guido van Rossumb3d39562000-01-31 18:41:26 +00007670#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007671 if (posix_putenv_garbage == NULL)
7672 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007673#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007674
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007675 if (!initialized) {
7676 stat_result_desc.name = MODNAME ".stat_result";
7677 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7678 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7679 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7680 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7681 structseq_new = StatResultType.tp_new;
7682 StatResultType.tp_new = statresult_new;
7683
7684 statvfs_result_desc.name = MODNAME ".statvfs_result";
7685 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007686#ifdef NEED_TICKS_PER_SECOND
7687# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7688 ticks_per_second = sysconf(_SC_CLK_TCK);
7689# elif defined(HZ)
7690 ticks_per_second = HZ;
7691# else
7692 ticks_per_second = 60; /* magic fallback value; may be bogus */
7693# endif
7694#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007695 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007696 Py_INCREF((PyObject*) &StatResultType);
7697 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007698 Py_INCREF((PyObject*) &StatVFSResultType);
7699 PyModule_AddObject(m, "statvfs_result",
7700 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007701 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007702
7703#ifdef __APPLE__
7704 /*
7705 * Step 2 of weak-linking support on Mac OS X.
7706 *
7707 * The code below removes functions that are not available on the
7708 * currently active platform.
7709 *
7710 * This block allow one to use a python binary that was build on
7711 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7712 * OSX 10.4.
7713 */
7714#ifdef HAVE_FSTATVFS
7715 if (fstatvfs == NULL) {
7716 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007717 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007718 }
7719 }
7720#endif /* HAVE_FSTATVFS */
7721
7722#ifdef HAVE_STATVFS
7723 if (statvfs == NULL) {
7724 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007725 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007726 }
7727 }
7728#endif /* HAVE_STATVFS */
7729
7730# ifdef HAVE_LCHOWN
7731 if (lchown == NULL) {
7732 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007733 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007734 }
7735 }
7736#endif /* HAVE_LCHOWN */
7737
7738
7739#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007740 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007741
Guido van Rossumb6775db1994-08-01 11:34:53 +00007742}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007743
7744#ifdef __cplusplus
7745}
7746#endif