blob: cf19a0762848c0ae96887809ad1ca58f99613106 [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 */
307#ifdef SIZEOF_PID_T
308#if SIZEOF_PID_T == SIZEOF_INT
309#define PARSE_PID "i"
310#define PyLong_FromPid PyLong_FromLong
311#define PyLong_AsPid PyLong_AsLong
312#elif SIZEOF_PID_T == SIZEOF_LONG
313#define PARSE_PID "l"
314#define PyLong_FromPid PyLong_FromLong
315#define PyLong_AsPid PyLong_AsLong
316#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
317#define PARSE_PID "L"
318#define PyLong_FromPid PyLong_FromLongLong
319#define PyLong_AsPid PyLong_AsLongLong
320#else
321#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
322#endif
323#endif /* SIZEOF_PID_T */
324
Greg Wardb48bc172000-03-01 21:51:56 +0000325/* Don't use the "_r" form if we don't need it (also, won't have a
326 prototype for it, at least on Solaris -- maybe others as well?). */
327#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
328#define USE_CTERMID_R
329#endif
330
Fred Drake699f3522000-06-29 21:12:41 +0000331/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000332#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000333#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000334# define STAT win32_stat
335# define FSTAT win32_fstat
336# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000337#else
338# define STAT stat
339# define FSTAT fstat
340# define STRUCT_STAT struct stat
341#endif
342
Tim Peters11b23062003-04-23 02:39:17 +0000343#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000344#include <sys/mkdev.h>
345#else
346#if defined(MAJOR_IN_SYSMACROS)
347#include <sys/sysmacros.h>
348#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000349#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
350#include <sys/mkdev.h>
351#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000352#endif
Fred Drake699f3522000-06-29 21:12:41 +0000353
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000354#if defined _MSC_VER && _MSC_VER >= 1400
355/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
356 * valid and throw an assertion if it isn't.
357 * Normally, an invalid fd is likely to be a C program error and therefore
358 * an assertion can be useful, but it does contradict the POSIX standard
359 * which for write(2) states:
360 * "Otherwise, -1 shall be returned and errno set to indicate the error."
361 * "[EBADF] The fildes argument is not a valid file descriptor open for
362 * writing."
363 * Furthermore, python allows the user to enter any old integer
364 * as a fd and should merely raise a python exception on error.
365 * The Microsoft CRT doesn't provide an official way to check for the
366 * validity of a file descriptor, but we can emulate its internal behaviour
367 * by using the exported __pinfo data member and knowledge of the
368 * internal structures involved.
369 * The structures below must be updated for each version of visual studio
370 * according to the file internal.h in the CRT source, until MS comes
371 * up with a less hacky way to do this.
372 * (all of this is to avoid globally modifying the CRT behaviour using
373 * _set_invalid_parameter_handler() and _CrtSetReportMode())
374 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000375/* The actual size of the structure is determined at runtime.
376 * Only the first items must be present.
377 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000378typedef struct {
379 intptr_t osfhnd;
380 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000381} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000382
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000383extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000384#define IOINFO_L2E 5
385#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
386#define IOINFO_ARRAYS 64
387#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
388#define FOPEN 0x01
389#define _NO_CONSOLE_FILENO (intptr_t)-2
390
391/* This function emulates what the windows CRT does to validate file handles */
392int
393_PyVerify_fd(int fd)
394{
395 const int i1 = fd >> IOINFO_L2E;
396 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000397
398 static int sizeof_ioinfo = 0;
399
400 /* Determine the actual size of the ioinfo structure,
401 * as used by the CRT loaded in memory
402 */
403 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
404 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
405 }
406 if (sizeof_ioinfo == 0) {
407 /* This should not happen... */
408 goto fail;
409 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000410
411 /* See that it isn't a special CLEAR fileno */
412 if (fd != _NO_CONSOLE_FILENO) {
413 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
414 * we check pointer validity and other info
415 */
416 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
417 /* finally, check that the file is open */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000418 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
419 if (info->osfile & FOPEN) {
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000420 return 1;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000421 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000422 }
423 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000424 fail:
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000425 errno = EBADF;
426 return 0;
427}
428
429/* the special case of checking dup2. The target fd must be in a sensible range */
430static int
431_PyVerify_fd_dup2(int fd1, int fd2)
432{
433 if (!_PyVerify_fd(fd1))
434 return 0;
435 if (fd2 == _NO_CONSOLE_FILENO)
436 return 0;
437 if ((unsigned)fd2 < _NHANDLE_)
438 return 1;
439 else
440 return 0;
441}
442#else
443/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
444#define _PyVerify_fd_dup2(A, B) (1)
445#endif
446
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000448#ifdef WITH_NEXT_FRAMEWORK
449/* On Darwin/MacOSX a shared library or framework has no access to
450** environ directly, we must obtain it with _NSGetEnviron().
451*/
452#include <crt_externs.h>
453static char **environ;
454#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000455extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000456#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457
Barry Warsaw53699e91996-12-10 23:23:01 +0000458static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000459convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460{
Barry Warsaw53699e91996-12-10 23:23:01 +0000461 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000462#ifdef MS_WINDOWS
463 wchar_t **e;
464#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000466#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000467 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468 if (d == NULL)
469 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000470#ifdef WITH_NEXT_FRAMEWORK
471 if (environ == NULL)
472 environ = *_NSGetEnviron();
473#endif
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000474#ifdef MS_WINDOWS
475 /* _wenviron must be initialized in this way if the program is started
476 through main() instead of wmain(). */
477 _wgetenv(L"");
478 if (_wenviron == NULL)
479 return d;
480 /* This part ignores errors */
481 for (e = _wenviron; *e != NULL; e++) {
482 PyObject *k;
483 PyObject *v;
484 wchar_t *p = wcschr(*e, L'=');
485 if (p == NULL)
486 continue;
487 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
488 if (k == NULL) {
489 PyErr_Clear();
490 continue;
491 }
492 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
493 if (v == NULL) {
494 PyErr_Clear();
495 Py_DECREF(k);
496 continue;
497 }
498 if (PyDict_GetItem(d, k) == NULL) {
499 if (PyDict_SetItem(d, k, v) != 0)
500 PyErr_Clear();
501 }
502 Py_DECREF(k);
503 Py_DECREF(v);
504 }
505#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000506 if (environ == NULL)
507 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000508 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000509 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000510 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000511 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512 char *p = strchr(*e, '=');
513 if (p == NULL)
514 continue;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000515 k = PyUnicode_Decode(*e, (int)(p-*e),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000516 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000517 if (k == NULL) {
518 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000519 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000520 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000521 v = PyUnicode_Decode(p+1, strlen(p+1),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000522 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000523 if (v == NULL) {
524 PyErr_Clear();
525 Py_DECREF(k);
526 continue;
527 }
528 if (PyDict_GetItem(d, k) == NULL) {
529 if (PyDict_SetItem(d, k, v) != 0)
530 PyErr_Clear();
531 }
532 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000533 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000534 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000535#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000536#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000537 {
538 APIRET rc;
539 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
540
541 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000542 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000543 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000544 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000545 Py_DECREF(v);
546 }
547 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
548 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000549 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000550 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000551 Py_DECREF(v);
552 }
553 }
554#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555 return d;
556}
557
Martin v. Löwis011e8422009-05-05 04:43:17 +0000558/* Convert a bytes object to a char*. Optionally lock the buffer if it is a
559 bytes array. */
560
561static char*
562bytes2str(PyObject* o, int lock)
563{
564 if(PyBytes_Check(o))
565 return PyBytes_AsString(o);
566 else if(PyByteArray_Check(o)) {
567 if (lock && PyObject_GetBuffer(o, NULL, 0) < 0)
568 /* On a bytearray, this should not fail. */
569 PyErr_BadInternalCall();
570 return PyByteArray_AsString(o);
571 } else {
572 /* The FS converter should have verified that this
573 is either bytes or bytearray. */
574 Py_FatalError("bad object passed to bytes2str");
575 /* not reached. */
576 return "";
577 }
578}
579
580/* Release the lock, decref the object. */
581static void
582release_bytes(PyObject* o)
583{
584 if (PyByteArray_Check(o))
585 o->ob_type->tp_as_buffer->bf_releasebuffer(NULL, 0);
586 Py_DECREF(o);
587}
588
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590/* Set a POSIX-specific error from errno, and return NULL */
591
Barry Warsawd58d7641998-07-23 16:14:40 +0000592static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000593posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000594{
Barry Warsawca74da41999-02-09 19:31:45 +0000595 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000596}
Barry Warsawd58d7641998-07-23 16:14:40 +0000597static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000598posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000599{
Barry Warsawca74da41999-02-09 19:31:45 +0000600 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000601}
602
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000603#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000604static PyObject *
605posix_error_with_unicode_filename(Py_UNICODE* name)
606{
607 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
608}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000609#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000610
611
Mark Hammondef8b6542001-05-13 08:04:26 +0000612static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000613posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000614{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000615 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError,
616 bytes2str(name, 0));
617 release_bytes(name);
Mark Hammondef8b6542001-05-13 08:04:26 +0000618 return rc;
619}
620
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000621#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000622static PyObject *
623win32_error(char* function, char* filename)
624{
Mark Hammond33a6da92000-08-15 00:46:38 +0000625 /* XXX We should pass the function name along in the future.
Georg Brandl38feaf02008-05-25 07:45:51 +0000626 (winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000627 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000628 Windows error object, which is non-trivial.
629 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000630 errno = GetLastError();
631 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000632 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000633 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000634 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000635}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000636
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000637static PyObject *
638win32_error_unicode(char* function, Py_UNICODE* filename)
639{
640 /* XXX - see win32_error for comments on 'function' */
641 errno = GetLastError();
642 if (filename)
643 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
644 else
645 return PyErr_SetFromWindowsErr(errno);
646}
647
Thomas Wouters477c8d52006-05-27 19:21:47 +0000648static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000649convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000650{
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000651 if (PyUnicode_CheckExact(*param))
652 Py_INCREF(*param);
653 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000654 /* For a Unicode subtype that's not a Unicode object,
655 return a true Unicode object with the same data. */
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000656 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
657 PyUnicode_GET_SIZE(*param));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658 else
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000659 *param = PyUnicode_FromEncodedObject(*param,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000660 Py_FileSystemDefaultEncoding,
661 "strict");
662 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000663}
664
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000665#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000666
Guido van Rossumd48f2521997-12-05 22:19:34 +0000667#if defined(PYOS_OS2)
668/**********************************************************************
669 * Helper Function to Trim and Format OS/2 Messages
670 **********************************************************************/
671 static void
672os2_formatmsg(char *msgbuf, int msglen, char *reason)
673{
674 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
675
676 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
677 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
678
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000679 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000680 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
681 }
682
683 /* Add Optional Reason Text */
684 if (reason) {
685 strcat(msgbuf, " : ");
686 strcat(msgbuf, reason);
687 }
688}
689
690/**********************************************************************
691 * Decode an OS/2 Operating System Error Code
692 *
693 * A convenience function to lookup an OS/2 error code and return a
694 * text message we can use to raise a Python exception.
695 *
696 * Notes:
697 * The messages for errors returned from the OS/2 kernel reside in
698 * the file OSO001.MSG in the \OS2 directory hierarchy.
699 *
700 **********************************************************************/
701 static char *
702os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
703{
704 APIRET rc;
705 ULONG msglen;
706
707 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
708 Py_BEGIN_ALLOW_THREADS
709 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
710 errorcode, "oso001.msg", &msglen);
711 Py_END_ALLOW_THREADS
712
713 if (rc == NO_ERROR)
714 os2_formatmsg(msgbuf, msglen, reason);
715 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000716 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000717 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000718
719 return msgbuf;
720}
721
722/* Set an OS/2-specific error and return NULL. OS/2 kernel
723 errors are not in a global variable e.g. 'errno' nor are
724 they congruent with posix error numbers. */
725
726static PyObject * os2_error(int code)
727{
728 char text[1024];
729 PyObject *v;
730
731 os2_strerror(text, sizeof(text), code, "");
732
733 v = Py_BuildValue("(is)", code, text);
734 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000735 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000736 Py_DECREF(v);
737 }
738 return NULL; /* Signal to Python that an Exception is Pending */
739}
740
741#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000742
743/* POSIX generic methods */
744
Barry Warsaw53699e91996-12-10 23:23:01 +0000745static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000746posix_fildes(PyObject *fdobj, int (*func)(int))
747{
748 int fd;
749 int res;
750 fd = PyObject_AsFileDescriptor(fdobj);
751 if (fd < 0)
752 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000753 if (!_PyVerify_fd(fd))
754 return posix_error();
Fred Drake4d1e64b2002-04-15 19:40:07 +0000755 Py_BEGIN_ALLOW_THREADS
756 res = (*func)(fd);
757 Py_END_ALLOW_THREADS
758 if (res < 0)
759 return posix_error();
760 Py_INCREF(Py_None);
761 return Py_None;
762}
Guido van Rossum21142a01999-01-08 21:05:37 +0000763
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000764#ifdef MS_WINDOWS
Tim Peters11b23062003-04-23 02:39:17 +0000765static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000766unicode_file_names(void)
767{
768 static int canusewide = -1;
769 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000770 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000771 the Windows NT family. */
772 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
773 }
774 return canusewide;
775}
776#endif
Tim Peters11b23062003-04-23 02:39:17 +0000777
Guido van Rossum21142a01999-01-08 21:05:37 +0000778static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000779posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000781 PyObject *opath1 = NULL;
782 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000783 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000784 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000785 PyUnicode_FSConverter, &opath1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000786 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000787 path1 = bytes2str(opath1, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000788 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000789 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000790 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000791 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +0000792 return posix_error_with_allocated_filename(opath1);
793 release_bytes(opath1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000794 Py_INCREF(Py_None);
795 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000796}
797
Barry Warsaw53699e91996-12-10 23:23:01 +0000798static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000799posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000800 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000801 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000802{
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000803 PyObject *opath1 = NULL, *opath2 = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000804 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000805 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000806 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000807 PyUnicode_FSConverter, &opath1,
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000808 PyUnicode_FSConverter, &opath2)) {
809 Py_XDECREF(opath1);
810 Py_XDECREF(opath2);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000811 return NULL;
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000812 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000813 path1 = bytes2str(opath1, 1);
814 path2 = bytes2str(opath2, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000815 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000816 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000817 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +0000818 release_bytes(opath1);
819 release_bytes(opath2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000820 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000821 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000823 Py_INCREF(Py_None);
824 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000825}
826
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000827#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000828static PyObject*
829win32_1str(PyObject* args, char* func,
830 char* format, BOOL (__stdcall *funcA)(LPCSTR),
831 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
832{
833 PyObject *uni;
834 char *ansi;
835 BOOL result;
836 if (unicode_file_names()) {
837 if (!PyArg_ParseTuple(args, wformat, &uni))
838 PyErr_Clear();
839 else {
840 Py_BEGIN_ALLOW_THREADS
841 result = funcW(PyUnicode_AsUnicode(uni));
842 Py_END_ALLOW_THREADS
843 if (!result)
844 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
845 Py_INCREF(Py_None);
846 return Py_None;
847 }
848 }
849 if (!PyArg_ParseTuple(args, format, &ansi))
850 return NULL;
851 Py_BEGIN_ALLOW_THREADS
852 result = funcA(ansi);
853 Py_END_ALLOW_THREADS
854 if (!result)
855 return win32_error(func, ansi);
856 Py_INCREF(Py_None);
857 return Py_None;
858
859}
860
861/* This is a reimplementation of the C library's chdir function,
862 but one that produces Win32 errors instead of DOS error codes.
863 chdir is essentially a wrapper around SetCurrentDirectory; however,
864 it also needs to set "magic" environment variables indicating
865 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000866static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000867win32_chdir(LPCSTR path)
868{
869 char new_path[MAX_PATH+1];
870 int result;
871 char env[4] = "=x:";
872
873 if(!SetCurrentDirectoryA(path))
874 return FALSE;
875 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
876 if (!result)
877 return FALSE;
878 /* In the ANSI API, there should not be any paths longer
879 than MAX_PATH. */
880 assert(result <= MAX_PATH+1);
881 if (strncmp(new_path, "\\\\", 2) == 0 ||
882 strncmp(new_path, "//", 2) == 0)
883 /* UNC path, nothing to do. */
884 return TRUE;
885 env[1] = new_path[0];
886 return SetEnvironmentVariableA(env, new_path);
887}
888
889/* The Unicode version differs from the ANSI version
890 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000891static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892win32_wchdir(LPCWSTR path)
893{
894 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
895 int result;
896 wchar_t env[4] = L"=x:";
897
898 if(!SetCurrentDirectoryW(path))
899 return FALSE;
900 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
901 if (!result)
902 return FALSE;
903 if (result > MAX_PATH+1) {
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000904 new_path = malloc(result * sizeof(wchar_t));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000905 if (!new_path) {
906 SetLastError(ERROR_OUTOFMEMORY);
907 return FALSE;
908 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000909 result = GetCurrentDirectoryW(result, new_path);
910 if (!result) {
911 free(new_path);
912 return FALSE;
913 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000914 }
915 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
916 wcsncmp(new_path, L"//", 2) == 0)
917 /* UNC path, nothing to do. */
918 return TRUE;
919 env[1] = new_path[0];
920 result = SetEnvironmentVariableW(env, new_path);
921 if (new_path != _new_path)
922 free(new_path);
923 return result;
924}
925#endif
926
Martin v. Löwis14694662006-02-03 12:54:16 +0000927#ifdef MS_WINDOWS
928/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
929 - time stamps are restricted to second resolution
930 - file modification times suffer from forth-and-back conversions between
931 UTC and local time
932 Therefore, we implement our own stat, based on the Win32 API directly.
933*/
934#define HAVE_STAT_NSEC 1
935
936struct win32_stat{
937 int st_dev;
938 __int64 st_ino;
939 unsigned short st_mode;
940 int st_nlink;
941 int st_uid;
942 int st_gid;
943 int st_rdev;
944 __int64 st_size;
945 int st_atime;
946 int st_atime_nsec;
947 int st_mtime;
948 int st_mtime_nsec;
949 int st_ctime;
950 int st_ctime_nsec;
951};
952
953static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
954
955static void
956FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
957{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000958 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
959 /* Cannot simply cast and dereference in_ptr,
960 since it might not be aligned properly */
961 __int64 in;
962 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000963 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
964 /* XXX Win32 supports time stamps past 2038; we currently don't */
965 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
966}
967
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968static void
969time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
970{
971 /* XXX endianness */
972 __int64 out;
973 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000974 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000975 memcpy(out_ptr, &out, sizeof(out));
976}
977
Martin v. Löwis14694662006-02-03 12:54:16 +0000978/* Below, we *know* that ugo+r is 0444 */
979#if _S_IREAD != 0400
980#error Unsupported C library
981#endif
982static int
983attributes_to_mode(DWORD attr)
984{
985 int m = 0;
986 if (attr & FILE_ATTRIBUTE_DIRECTORY)
987 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
988 else
989 m |= _S_IFREG;
990 if (attr & FILE_ATTRIBUTE_READONLY)
991 m |= 0444;
992 else
993 m |= 0666;
994 return m;
995}
996
997static int
998attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
999{
1000 memset(result, 0, sizeof(*result));
1001 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1002 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1003 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1004 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1005 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1006
1007 return 0;
1008}
1009
Thomas Wouters89f507f2006-12-13 04:49:30 +00001010/* Emulate GetFileAttributesEx[AW] on Windows 95 */
1011static int checked = 0;
1012static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
1013static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
1014static void
1015check_gfax()
1016{
1017 HINSTANCE hKernel32;
1018 if (checked)
1019 return;
1020 checked = 1;
1021 hKernel32 = GetModuleHandle("KERNEL32");
1022 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
1023 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
1024}
1025
Guido van Rossumd8faa362007-04-27 19:54:29 +00001026static BOOL
1027attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1028{
1029 HANDLE hFindFile;
1030 WIN32_FIND_DATAA FileData;
1031 hFindFile = FindFirstFileA(pszFile, &FileData);
1032 if (hFindFile == INVALID_HANDLE_VALUE)
1033 return FALSE;
1034 FindClose(hFindFile);
1035 pfad->dwFileAttributes = FileData.dwFileAttributes;
1036 pfad->ftCreationTime = FileData.ftCreationTime;
1037 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1038 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1039 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1040 pfad->nFileSizeLow = FileData.nFileSizeLow;
1041 return TRUE;
1042}
1043
1044static BOOL
1045attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1046{
1047 HANDLE hFindFile;
1048 WIN32_FIND_DATAW FileData;
1049 hFindFile = FindFirstFileW(pszFile, &FileData);
1050 if (hFindFile == INVALID_HANDLE_VALUE)
1051 return FALSE;
1052 FindClose(hFindFile);
1053 pfad->dwFileAttributes = FileData.dwFileAttributes;
1054 pfad->ftCreationTime = FileData.ftCreationTime;
1055 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1056 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1057 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1058 pfad->nFileSizeLow = FileData.nFileSizeLow;
1059 return TRUE;
1060}
1061
Thomas Wouters89f507f2006-12-13 04:49:30 +00001062static BOOL WINAPI
1063Py_GetFileAttributesExA(LPCSTR pszFile,
1064 GET_FILEEX_INFO_LEVELS level,
1065 LPVOID pv)
1066{
1067 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001068 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1069 /* First try to use the system's implementation, if that is
1070 available and either succeeds to gives an error other than
1071 that it isn't implemented. */
1072 check_gfax();
1073 if (gfaxa) {
1074 result = gfaxa(pszFile, level, pv);
1075 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1076 return result;
1077 }
1078 /* It's either not present, or not implemented.
1079 Emulate using FindFirstFile. */
1080 if (level != GetFileExInfoStandard) {
1081 SetLastError(ERROR_INVALID_PARAMETER);
1082 return FALSE;
1083 }
1084 /* Use GetFileAttributes to validate that the file name
1085 does not contain wildcards (which FindFirstFile would
1086 accept). */
1087 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
1088 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001089 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001090}
1091
1092static BOOL WINAPI
1093Py_GetFileAttributesExW(LPCWSTR pszFile,
1094 GET_FILEEX_INFO_LEVELS level,
1095 LPVOID pv)
1096{
1097 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001098 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1099 /* First try to use the system's implementation, if that is
1100 available and either succeeds to gives an error other than
1101 that it isn't implemented. */
1102 check_gfax();
1103 if (gfaxa) {
1104 result = gfaxw(pszFile, level, pv);
1105 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1106 return result;
1107 }
1108 /* It's either not present, or not implemented.
1109 Emulate using FindFirstFile. */
1110 if (level != GetFileExInfoStandard) {
1111 SetLastError(ERROR_INVALID_PARAMETER);
1112 return FALSE;
1113 }
1114 /* Use GetFileAttributes to validate that the file name
1115 does not contain wildcards (which FindFirstFile would
1116 accept). */
1117 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
1118 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001119 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001120}
1121
Martin v. Löwis14694662006-02-03 12:54:16 +00001122static int
1123win32_stat(const char* path, struct win32_stat *result)
1124{
1125 WIN32_FILE_ATTRIBUTE_DATA info;
1126 int code;
1127 char *dot;
1128 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001129 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001130 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1131 /* Protocol violation: we explicitly clear errno, instead of
1132 setting it to a POSIX error. Callers should use GetLastError. */
1133 errno = 0;
1134 return -1;
1135 } else {
1136 /* Could not get attributes on open file. Fall back to
1137 reading the directory. */
1138 if (!attributes_from_dir(path, &info)) {
1139 /* Very strange. This should not fail now */
1140 errno = 0;
1141 return -1;
1142 }
1143 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001144 }
1145 code = attribute_data_to_stat(&info, result);
1146 if (code != 0)
1147 return code;
1148 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1149 dot = strrchr(path, '.');
1150 if (dot) {
1151 if (stricmp(dot, ".bat") == 0 ||
1152 stricmp(dot, ".cmd") == 0 ||
1153 stricmp(dot, ".exe") == 0 ||
1154 stricmp(dot, ".com") == 0)
1155 result->st_mode |= 0111;
1156 }
1157 return code;
1158}
1159
1160static int
1161win32_wstat(const wchar_t* path, struct win32_stat *result)
1162{
1163 int code;
1164 const wchar_t *dot;
1165 WIN32_FILE_ATTRIBUTE_DATA info;
1166 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001167 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001168 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1169 /* Protocol violation: we explicitly clear errno, instead of
1170 setting it to a POSIX error. Callers should use GetLastError. */
1171 errno = 0;
1172 return -1;
1173 } else {
1174 /* Could not get attributes on open file. Fall back to
1175 reading the directory. */
1176 if (!attributes_from_dir_w(path, &info)) {
1177 /* Very strange. This should not fail now */
1178 errno = 0;
1179 return -1;
1180 }
1181 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001182 }
1183 code = attribute_data_to_stat(&info, result);
1184 if (code < 0)
1185 return code;
1186 /* Set IFEXEC if it is an .exe, .bat, ... */
1187 dot = wcsrchr(path, '.');
1188 if (dot) {
1189 if (_wcsicmp(dot, L".bat") == 0 ||
1190 _wcsicmp(dot, L".cmd") == 0 ||
1191 _wcsicmp(dot, L".exe") == 0 ||
1192 _wcsicmp(dot, L".com") == 0)
1193 result->st_mode |= 0111;
1194 }
1195 return code;
1196}
1197
1198static int
1199win32_fstat(int file_number, struct win32_stat *result)
1200{
1201 BY_HANDLE_FILE_INFORMATION info;
1202 HANDLE h;
1203 int type;
1204
1205 h = (HANDLE)_get_osfhandle(file_number);
1206
1207 /* Protocol violation: we explicitly clear errno, instead of
1208 setting it to a POSIX error. Callers should use GetLastError. */
1209 errno = 0;
1210
1211 if (h == INVALID_HANDLE_VALUE) {
1212 /* This is really a C library error (invalid file handle).
1213 We set the Win32 error to the closes one matching. */
1214 SetLastError(ERROR_INVALID_HANDLE);
1215 return -1;
1216 }
1217 memset(result, 0, sizeof(*result));
1218
1219 type = GetFileType(h);
1220 if (type == FILE_TYPE_UNKNOWN) {
1221 DWORD error = GetLastError();
1222 if (error != 0) {
1223 return -1;
1224 }
1225 /* else: valid but unknown file */
1226 }
1227
1228 if (type != FILE_TYPE_DISK) {
1229 if (type == FILE_TYPE_CHAR)
1230 result->st_mode = _S_IFCHR;
1231 else if (type == FILE_TYPE_PIPE)
1232 result->st_mode = _S_IFIFO;
1233 return 0;
1234 }
1235
1236 if (!GetFileInformationByHandle(h, &info)) {
1237 return -1;
1238 }
1239
1240 /* similar to stat() */
1241 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1242 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1243 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1244 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1245 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1246 /* specific to fstat() */
1247 result->st_nlink = info.nNumberOfLinks;
1248 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1249 return 0;
1250}
1251
1252#endif /* MS_WINDOWS */
1253
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001254PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001255"stat_result: Result from stat or lstat.\n\n\
1256This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001257 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001258or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1259\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001260Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1261or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001262\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001263See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001264
1265static PyStructSequence_Field stat_result_fields[] = {
1266 {"st_mode", "protection bits"},
1267 {"st_ino", "inode"},
1268 {"st_dev", "device"},
1269 {"st_nlink", "number of hard links"},
1270 {"st_uid", "user ID of owner"},
1271 {"st_gid", "group ID of owner"},
1272 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001273 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1274 {NULL, "integer time of last access"},
1275 {NULL, "integer time of last modification"},
1276 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001277 {"st_atime", "time of last access"},
1278 {"st_mtime", "time of last modification"},
1279 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001280#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001281 {"st_blksize", "blocksize for filesystem I/O"},
1282#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001283#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001284 {"st_blocks", "number of blocks allocated"},
1285#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001286#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001287 {"st_rdev", "device type (if inode device)"},
1288#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001289#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1290 {"st_flags", "user defined flags for file"},
1291#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001292#ifdef HAVE_STRUCT_STAT_ST_GEN
1293 {"st_gen", "generation number"},
1294#endif
1295#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1296 {"st_birthtime", "time of creation"},
1297#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001298 {0}
1299};
1300
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001301#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001302#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001303#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001304#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001305#endif
1306
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001307#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001308#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1309#else
1310#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1311#endif
1312
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001313#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001314#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1315#else
1316#define ST_RDEV_IDX ST_BLOCKS_IDX
1317#endif
1318
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001319#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1320#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1321#else
1322#define ST_FLAGS_IDX ST_RDEV_IDX
1323#endif
1324
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001325#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001326#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001327#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001328#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001329#endif
1330
1331#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1332#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1333#else
1334#define ST_BIRTHTIME_IDX ST_GEN_IDX
1335#endif
1336
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001337static PyStructSequence_Desc stat_result_desc = {
1338 "stat_result", /* name */
1339 stat_result__doc__, /* doc */
1340 stat_result_fields,
1341 10
1342};
1343
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001344PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001345"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1346This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001347 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001348or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001349\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001350See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001351
1352static PyStructSequence_Field statvfs_result_fields[] = {
1353 {"f_bsize", },
1354 {"f_frsize", },
1355 {"f_blocks", },
1356 {"f_bfree", },
1357 {"f_bavail", },
1358 {"f_files", },
1359 {"f_ffree", },
1360 {"f_favail", },
1361 {"f_flag", },
1362 {"f_namemax",},
1363 {0}
1364};
1365
1366static PyStructSequence_Desc statvfs_result_desc = {
1367 "statvfs_result", /* name */
1368 statvfs_result__doc__, /* doc */
1369 statvfs_result_fields,
1370 10
1371};
1372
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001373static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001374static PyTypeObject StatResultType;
1375static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001376static newfunc structseq_new;
1377
1378static PyObject *
1379statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1380{
1381 PyStructSequence *result;
1382 int i;
1383
1384 result = (PyStructSequence*)structseq_new(type, args, kwds);
1385 if (!result)
1386 return NULL;
1387 /* If we have been initialized from a tuple,
1388 st_?time might be set to None. Initialize it
1389 from the int slots. */
1390 for (i = 7; i <= 9; i++) {
1391 if (result->ob_item[i+3] == Py_None) {
1392 Py_DECREF(Py_None);
1393 Py_INCREF(result->ob_item[i]);
1394 result->ob_item[i+3] = result->ob_item[i];
1395 }
1396 }
1397 return (PyObject*)result;
1398}
1399
1400
1401
1402/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001403static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001404
1405PyDoc_STRVAR(stat_float_times__doc__,
1406"stat_float_times([newval]) -> oldval\n\n\
1407Determine whether os.[lf]stat represents time stamps as float objects.\n\
1408If newval is True, future calls to stat() return floats, if it is False,\n\
1409future calls return ints. \n\
1410If newval is omitted, return the current setting.\n");
1411
1412static PyObject*
1413stat_float_times(PyObject* self, PyObject *args)
1414{
1415 int newval = -1;
1416 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1417 return NULL;
1418 if (newval == -1)
1419 /* Return old value */
1420 return PyBool_FromLong(_stat_float_times);
1421 _stat_float_times = newval;
1422 Py_INCREF(Py_None);
1423 return Py_None;
1424}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001425
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001426static void
1427fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1428{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001429 PyObject *fval,*ival;
1430#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001431 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001432#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001433 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001434#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001435 if (!ival)
1436 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001437 if (_stat_float_times) {
1438 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1439 } else {
1440 fval = ival;
1441 Py_INCREF(fval);
1442 }
1443 PyStructSequence_SET_ITEM(v, index, ival);
1444 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001445}
1446
Tim Peters5aa91602002-01-30 05:46:57 +00001447/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001448 (used by posix_stat() and posix_fstat()) */
1449static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001450_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001451{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001452 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001453 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001454 if (v == NULL)
1455 return NULL;
1456
Christian Heimes217cfd12007-12-02 14:31:20 +00001457 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001458#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001459 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001460 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001461#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001462 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001463#endif
1464#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001465 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001466 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001467#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001468 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001469#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00001470 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1471 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1472 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001473#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001474 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001475 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001476#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001477 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001478#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001479
Martin v. Löwis14694662006-02-03 12:54:16 +00001480#if defined(HAVE_STAT_TV_NSEC)
1481 ansec = st->st_atim.tv_nsec;
1482 mnsec = st->st_mtim.tv_nsec;
1483 cnsec = st->st_ctim.tv_nsec;
1484#elif defined(HAVE_STAT_TV_NSEC2)
1485 ansec = st->st_atimespec.tv_nsec;
1486 mnsec = st->st_mtimespec.tv_nsec;
1487 cnsec = st->st_ctimespec.tv_nsec;
1488#elif defined(HAVE_STAT_NSEC)
1489 ansec = st->st_atime_nsec;
1490 mnsec = st->st_mtime_nsec;
1491 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001492#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001493 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001494#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001495 fill_time(v, 7, st->st_atime, ansec);
1496 fill_time(v, 8, st->st_mtime, mnsec);
1497 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001498
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001499#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001500 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001501 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001502#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001503#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001504 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001505 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001506#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001507#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001508 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001509 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001510#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001511#ifdef HAVE_STRUCT_STAT_ST_GEN
1512 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001513 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001514#endif
1515#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1516 {
1517 PyObject *val;
1518 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001519 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001520#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001521 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001522#else
1523 bnsec = 0;
1524#endif
1525 if (_stat_float_times) {
1526 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1527 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001528 val = PyLong_FromLong((long)bsec);
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001529 }
1530 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1531 val);
1532 }
1533#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001534#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1535 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001536 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001537#endif
Fred Drake699f3522000-06-29 21:12:41 +00001538
1539 if (PyErr_Occurred()) {
1540 Py_DECREF(v);
1541 return NULL;
1542 }
1543
1544 return v;
1545}
1546
Martin v. Löwisd8948722004-06-02 09:57:56 +00001547#ifdef MS_WINDOWS
1548
1549/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1550 where / can be used in place of \ and the trailing slash is optional.
1551 Both SERVER and SHARE must have at least one character.
1552*/
1553
1554#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1555#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001556#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001557#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001558#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001559
Tim Peters4ad82172004-08-30 17:02:04 +00001560static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001561IsUNCRootA(char *path, int pathlen)
1562{
1563 #define ISSLASH ISSLASHA
1564
1565 int i, share;
1566
1567 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1568 /* minimum UNCRoot is \\x\y */
1569 return FALSE;
1570 for (i = 2; i < pathlen ; i++)
1571 if (ISSLASH(path[i])) break;
1572 if (i == 2 || i == pathlen)
1573 /* do not allow \\\SHARE or \\SERVER */
1574 return FALSE;
1575 share = i+1;
1576 for (i = share; i < pathlen; i++)
1577 if (ISSLASH(path[i])) break;
1578 return (i != share && (i == pathlen || i == pathlen-1));
1579
1580 #undef ISSLASH
1581}
1582
Tim Peters4ad82172004-08-30 17:02:04 +00001583static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001584IsUNCRootW(Py_UNICODE *path, int pathlen)
1585{
1586 #define ISSLASH ISSLASHW
1587
1588 int i, share;
1589
1590 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1591 /* minimum UNCRoot is \\x\y */
1592 return FALSE;
1593 for (i = 2; i < pathlen ; i++)
1594 if (ISSLASH(path[i])) break;
1595 if (i == 2 || i == pathlen)
1596 /* do not allow \\\SHARE or \\SERVER */
1597 return FALSE;
1598 share = i+1;
1599 for (i = share; i < pathlen; i++)
1600 if (ISSLASH(path[i])) break;
1601 return (i != share && (i == pathlen || i == pathlen-1));
1602
1603 #undef ISSLASH
1604}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001605#endif /* MS_WINDOWS */
1606
Barry Warsaw53699e91996-12-10 23:23:01 +00001607static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001608posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001609 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001610#ifdef __VMS
1611 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1612#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001613 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001614#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001615 char *wformat,
1616 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001617{
Fred Drake699f3522000-06-29 21:12:41 +00001618 STRUCT_STAT st;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001619 PyObject *opath;
1620 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001621 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001622 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001623
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001624#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001625 /* If on wide-character-capable OS see if argument
1626 is Unicode and if so use wide API. */
1627 if (unicode_file_names()) {
1628 PyUnicodeObject *po;
1629 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001630 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1631
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001632 Py_BEGIN_ALLOW_THREADS
1633 /* PyUnicode_AS_UNICODE result OK without
1634 thread lock as it is a simple dereference. */
1635 res = wstatfunc(wpath, &st);
1636 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001637
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001638 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001639 return win32_error_unicode("stat", wpath);
1640 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001641 }
1642 /* Drop the argument parsing error as narrow strings
1643 are also valid. */
1644 PyErr_Clear();
1645 }
1646#endif
1647
Tim Peters5aa91602002-01-30 05:46:57 +00001648 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +00001649 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001650 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001651 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00001652 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001653 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001654 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001655
1656 if (res != 0) {
1657#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001658 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001659#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001660 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001661#endif
1662 }
1663 else
1664 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001665
Martin v. Löwis011e8422009-05-05 04:43:17 +00001666 release_bytes(opath);
Martin v. Löwis14694662006-02-03 12:54:16 +00001667 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001668}
1669
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001670/* POSIX methods */
1671
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001672PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001673"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001674Use the real uid/gid to test for access to a path. Note that most\n\
1675operations will use the effective uid/gid, therefore this routine can\n\
1676be used in a suid/sgid environment to test if the invoking user has the\n\
1677specified access to the path. The mode argument can be F_OK to test\n\
1678existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001679
1680static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001681posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001682{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001683 PyObject *opath;
Guido van Rossum015f22a1999-01-06 22:52:38 +00001684 char *path;
1685 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001686
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001687#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001688 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001689 if (unicode_file_names()) {
1690 PyUnicodeObject *po;
1691 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1692 Py_BEGIN_ALLOW_THREADS
1693 /* PyUnicode_AS_UNICODE OK without thread lock as
1694 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001695 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001696 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001697 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001698 }
1699 /* Drop the argument parsing error as narrow strings
1700 are also valid. */
1701 PyErr_Clear();
1702 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001703 if (!PyArg_ParseTuple(args, "O&i:access",
1704 PyUnicode_FSConverter, &opath, &mode))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001705 return 0;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001706 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001707 Py_BEGIN_ALLOW_THREADS
1708 attr = GetFileAttributesA(path);
1709 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001710 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001711finish:
1712 if (attr == 0xFFFFFFFF)
1713 /* File does not exist, or cannot read attributes */
1714 return PyBool_FromLong(0);
1715 /* Access is possible if either write access wasn't requested, or
Guido van Rossumb00324f2007-12-04 01:13:14 +00001716 the file isn't read-only, or if it's a directory, as there are
1717 no read-only directories on Windows. */
1718 return PyBool_FromLong(!(mode & 2)
1719 || !(attr & FILE_ATTRIBUTE_READONLY)
1720 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001721#else
1722 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001723 if (!PyArg_ParseTuple(args, "O&i:access",
1724 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001725 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001726 path = bytes2str(opath, 1);
Guido van Rossum015f22a1999-01-06 22:52:38 +00001727 Py_BEGIN_ALLOW_THREADS
1728 res = access(path, mode);
1729 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001730 release_bytes(opath);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001731 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001732#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001733}
1734
Guido van Rossumd371ff11999-01-25 16:12:23 +00001735#ifndef F_OK
1736#define F_OK 0
1737#endif
1738#ifndef R_OK
1739#define R_OK 4
1740#endif
1741#ifndef W_OK
1742#define W_OK 2
1743#endif
1744#ifndef X_OK
1745#define X_OK 1
1746#endif
1747
1748#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001749PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001750"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001751Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001752
1753static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001754posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001755{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001756 int id;
1757 char *ret;
1758
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001759 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001760 return NULL;
1761
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001762#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001763 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001764 if (id == 0) {
1765 ret = ttyname();
1766 }
1767 else {
1768 ret = NULL;
1769 }
1770#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001771 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001772#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001773 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001774 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001775 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001776}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001777#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001778
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001779#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001780PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001781"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001782Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001783
1784static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001785posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001786{
1787 char *ret;
1788 char buffer[L_ctermid];
1789
Greg Wardb48bc172000-03-01 21:51:56 +00001790#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001791 ret = ctermid_r(buffer);
1792#else
1793 ret = ctermid(buffer);
1794#endif
1795 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001796 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001797 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001798}
1799#endif
1800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001801PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001802"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001803Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001804
Barry Warsaw53699e91996-12-10 23:23:01 +00001805static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001806posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001807{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001808#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00001809 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001810#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001811 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001812#elif defined(__VMS)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001813 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001814#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001815 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001816#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001817}
1818
Fred Drake4d1e64b2002-04-15 19:40:07 +00001819#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001820PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001821"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001822Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001823opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001824
1825static PyObject *
1826posix_fchdir(PyObject *self, PyObject *fdobj)
1827{
1828 return posix_fildes(fdobj, fchdir);
1829}
1830#endif /* HAVE_FCHDIR */
1831
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001832
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001833PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001834"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001835Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001836
Barry Warsaw53699e91996-12-10 23:23:01 +00001837static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001838posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001839{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001840 PyObject *opath = NULL;
Mark Hammondef8b6542001-05-13 08:04:26 +00001841 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001842 int i;
1843 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001844#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001845 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001846 if (unicode_file_names()) {
1847 PyUnicodeObject *po;
1848 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1849 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001850 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1851 if (attr != 0xFFFFFFFF) {
1852 if (i & _S_IWRITE)
1853 attr &= ~FILE_ATTRIBUTE_READONLY;
1854 else
1855 attr |= FILE_ATTRIBUTE_READONLY;
1856 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1857 }
1858 else
1859 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001860 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001861 if (!res)
1862 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001863 PyUnicode_AS_UNICODE(po));
1864 Py_INCREF(Py_None);
1865 return Py_None;
1866 }
1867 /* Drop the argument parsing error as narrow strings
1868 are also valid. */
1869 PyErr_Clear();
1870 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001871 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1872 &opath, &i))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001873 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001874 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001875 Py_BEGIN_ALLOW_THREADS
1876 attr = GetFileAttributesA(path);
1877 if (attr != 0xFFFFFFFF) {
1878 if (i & _S_IWRITE)
1879 attr &= ~FILE_ATTRIBUTE_READONLY;
1880 else
1881 attr |= FILE_ATTRIBUTE_READONLY;
1882 res = SetFileAttributesA(path, attr);
1883 }
1884 else
1885 res = 0;
1886 Py_END_ALLOW_THREADS
1887 if (!res) {
1888 win32_error("chmod", path);
Martin v. Löwis011e8422009-05-05 04:43:17 +00001889 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001890 return NULL;
1891 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001892 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001893 Py_INCREF(Py_None);
1894 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001895#else /* MS_WINDOWS */
Martin v. Löwis011e8422009-05-05 04:43:17 +00001896 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1897 &opath, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001898 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001899 path = bytes2str(opath, 1);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001900 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001901 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001902 Py_END_ALLOW_THREADS
1903 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001904 return posix_error_with_allocated_filename(opath);
1905 release_bytes(opath);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001906 Py_INCREF(Py_None);
1907 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001908#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001909}
1910
Christian Heimes4e30a842007-11-30 22:12:06 +00001911#ifdef HAVE_FCHMOD
1912PyDoc_STRVAR(posix_fchmod__doc__,
1913"fchmod(fd, mode)\n\n\
1914Change the access permissions of the file given by file\n\
1915descriptor fd.");
1916
1917static PyObject *
1918posix_fchmod(PyObject *self, PyObject *args)
1919{
1920 int fd, mode, res;
1921 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1922 return NULL;
1923 Py_BEGIN_ALLOW_THREADS
1924 res = fchmod(fd, mode);
1925 Py_END_ALLOW_THREADS
1926 if (res < 0)
1927 return posix_error();
1928 Py_RETURN_NONE;
1929}
1930#endif /* HAVE_FCHMOD */
1931
1932#ifdef HAVE_LCHMOD
1933PyDoc_STRVAR(posix_lchmod__doc__,
1934"lchmod(path, mode)\n\n\
1935Change the access permissions of a file. If path is a symlink, this\n\
1936affects the link itself rather than the target.");
1937
1938static PyObject *
1939posix_lchmod(PyObject *self, PyObject *args)
1940{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001941 PyObject *opath;
1942 char *path;
Christian Heimes4e30a842007-11-30 22:12:06 +00001943 int i;
1944 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001945 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1946 &opath, &i))
Christian Heimes4e30a842007-11-30 22:12:06 +00001947 return NULL;
Eric Smith86a05ec2009-05-05 13:07:30 +00001948 path = bytes2str(opath, 1);
Christian Heimes4e30a842007-11-30 22:12:06 +00001949 Py_BEGIN_ALLOW_THREADS
1950 res = lchmod(path, i);
1951 Py_END_ALLOW_THREADS
1952 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001953 return posix_error_with_allocated_filename(opath);
1954 release_bytes(opath);
Christian Heimes4e30a842007-11-30 22:12:06 +00001955 Py_RETURN_NONE;
1956}
1957#endif /* HAVE_LCHMOD */
1958
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001959
Thomas Wouterscf297e42007-02-23 15:07:44 +00001960#ifdef HAVE_CHFLAGS
1961PyDoc_STRVAR(posix_chflags__doc__,
1962"chflags(path, flags)\n\n\
1963Set file flags.");
1964
1965static PyObject *
1966posix_chflags(PyObject *self, PyObject *args)
1967{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001968 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001969 char *path;
1970 unsigned long flags;
1971 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001972 if (!PyArg_ParseTuple(args, "O&k:chflags",
1973 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00001974 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001975 path = bytes2str(opath, 1);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001976 Py_BEGIN_ALLOW_THREADS
1977 res = chflags(path, flags);
1978 Py_END_ALLOW_THREADS
1979 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001980 return posix_error_with_allocated_filename(opath);
1981 release_bytes(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001982 Py_INCREF(Py_None);
1983 return Py_None;
1984}
1985#endif /* HAVE_CHFLAGS */
1986
1987#ifdef HAVE_LCHFLAGS
1988PyDoc_STRVAR(posix_lchflags__doc__,
1989"lchflags(path, flags)\n\n\
1990Set file flags.\n\
1991This function will not follow symbolic links.");
1992
1993static PyObject *
1994posix_lchflags(PyObject *self, PyObject *args)
1995{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001996 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001997 char *path;
1998 unsigned long flags;
1999 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002000 if (!PyArg_ParseTuple(args, "O&k:lchflags",
Martin v. Löwis4adbc342009-05-05 17:17:55 +00002001 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00002002 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002003 path = bytes2str(opath, 1);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002004 Py_BEGIN_ALLOW_THREADS
2005 res = lchflags(path, flags);
2006 Py_END_ALLOW_THREADS
2007 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002008 return posix_error_with_allocated_filename(opath);
2009 release_bytes(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002010 Py_INCREF(Py_None);
2011 return Py_None;
2012}
2013#endif /* HAVE_LCHFLAGS */
2014
Martin v. Löwis244edc82001-10-04 22:44:26 +00002015#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002016PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002017"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002018Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002019
2020static PyObject *
2021posix_chroot(PyObject *self, PyObject *args)
2022{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002023 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002024}
2025#endif
2026
Guido van Rossum21142a01999-01-08 21:05:37 +00002027#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002028PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002029"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002030force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002031
2032static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002033posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002034{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002035 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002036}
2037#endif /* HAVE_FSYNC */
2038
2039#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002040
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002041#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002042extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2043#endif
2044
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002045PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002046"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002047force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002048 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002049
2050static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002051posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002052{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002053 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002054}
2055#endif /* HAVE_FDATASYNC */
2056
2057
Fredrik Lundh10723342000-07-10 16:38:09 +00002058#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002060"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002061Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002062
Barry Warsaw53699e91996-12-10 23:23:01 +00002063static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002064posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002065{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002066 PyObject *opath;
2067 char *path;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002068 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00002069 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002070 if (!PyArg_ParseTuple(args, "O&ll:chown",
2071 PyUnicode_FSConverter, &opath,
Mark Hammondef8b6542001-05-13 08:04:26 +00002072 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00002073 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002074 path = bytes2str(opath, 1);
Fredrik Lundh44328e62000-07-10 15:59:30 +00002075 Py_BEGIN_ALLOW_THREADS
2076 res = chown(path, (uid_t) uid, (gid_t) gid);
2077 Py_END_ALLOW_THREADS
2078 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002079 return posix_error_with_allocated_filename(opath);
2080 release_bytes(opath);
Fredrik Lundh44328e62000-07-10 15:59:30 +00002081 Py_INCREF(Py_None);
2082 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002083}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002084#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002085
Christian Heimes4e30a842007-11-30 22:12:06 +00002086#ifdef HAVE_FCHOWN
2087PyDoc_STRVAR(posix_fchown__doc__,
2088"fchown(fd, uid, gid)\n\n\
2089Change the owner and group id of the file given by file descriptor\n\
2090fd to the numeric uid and gid.");
2091
2092static PyObject *
2093posix_fchown(PyObject *self, PyObject *args)
2094{
2095 int fd, uid, gid;
2096 int res;
2097 if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid))
2098 return NULL;
2099 Py_BEGIN_ALLOW_THREADS
2100 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2101 Py_END_ALLOW_THREADS
2102 if (res < 0)
2103 return posix_error();
2104 Py_RETURN_NONE;
2105}
2106#endif /* HAVE_FCHOWN */
2107
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002108#ifdef HAVE_LCHOWN
2109PyDoc_STRVAR(posix_lchown__doc__,
2110"lchown(path, uid, gid)\n\n\
2111Change the owner and group id of path to the numeric uid and gid.\n\
2112This function will not follow symbolic links.");
2113
2114static PyObject *
2115posix_lchown(PyObject *self, PyObject *args)
2116{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002117 PyObject *opath;
2118 char *path;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002119 int uid, gid;
2120 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002121 if (!PyArg_ParseTuple(args, "O&ii:lchown",
2122 PyUnicode_FSConverter, &opath,
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002123 &uid, &gid))
2124 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002125 path = bytes2str(opath, 1);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002126 Py_BEGIN_ALLOW_THREADS
2127 res = lchown(path, (uid_t) uid, (gid_t) gid);
2128 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00002129 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002130 return posix_error_with_allocated_filename(opath);
2131 release_bytes(opath);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002132 Py_INCREF(Py_None);
2133 return Py_None;
2134}
2135#endif /* HAVE_LCHOWN */
2136
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002137
Guido van Rossum36bc6801995-06-14 22:54:23 +00002138#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002139static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002140posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002141{
2142 char buf[1026];
2143 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002144
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002145#ifdef MS_WINDOWS
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002146 if (!use_bytes && unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002147 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00002148 wchar_t *wbuf2 = wbuf;
2149 PyObject *resobj;
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002150 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002151 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002152 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2153 /* If the buffer is large enough, len does not include the
2154 terminating \0. If the buffer is too small, len includes
2155 the space needed for the terminator. */
2156 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2157 wbuf2 = malloc(len * sizeof(wchar_t));
2158 if (wbuf2)
2159 len = GetCurrentDirectoryW(len, wbuf2);
2160 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002161 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002162 if (!wbuf2) {
2163 PyErr_NoMemory();
2164 return NULL;
2165 }
2166 if (!len) {
2167 if (wbuf2 != wbuf) free(wbuf2);
2168 return win32_error("getcwdu", NULL);
2169 }
2170 resobj = PyUnicode_FromWideChar(wbuf2, len);
2171 if (wbuf2 != wbuf) free(wbuf2);
2172 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002173 }
2174#endif
2175
2176 Py_BEGIN_ALLOW_THREADS
2177#if defined(PYOS_OS2) && defined(PYCC_GCC)
2178 res = _getcwd2(buf, sizeof buf);
2179#else
2180 res = getcwd(buf, sizeof buf);
2181#endif
2182 Py_END_ALLOW_THREADS
2183 if (res == NULL)
2184 return posix_error();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002185 if (use_bytes)
2186 return PyBytes_FromStringAndSize(buf, strlen(buf));
Martin v. Löwis43c57782009-05-10 08:15:24 +00002187 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002188}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002189
2190PyDoc_STRVAR(posix_getcwd__doc__,
2191"getcwd() -> path\n\n\
2192Return a unicode string representing the current working directory.");
2193
2194static PyObject *
2195posix_getcwd_unicode(PyObject *self)
2196{
2197 return posix_getcwd(0);
2198}
2199
2200PyDoc_STRVAR(posix_getcwdb__doc__,
2201"getcwdb() -> path\n\n\
2202Return a bytes string representing the current working directory.");
2203
2204static PyObject *
2205posix_getcwd_bytes(PyObject *self)
2206{
2207 return posix_getcwd(1);
2208}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002209#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002210
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002211
Guido van Rossumb6775db1994-08-01 11:34:53 +00002212#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002213PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002214"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002215Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002216
Barry Warsaw53699e91996-12-10 23:23:01 +00002217static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002218posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002219{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002220 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002221}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002222#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002223
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002224
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002225PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002226"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002227Return a list containing the names of the entries in the directory.\n\
2228\n\
2229 path: path of directory to list\n\
2230\n\
2231The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002232entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002233
Barry Warsaw53699e91996-12-10 23:23:01 +00002234static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002235posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002236{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002237 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002238 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002239#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002240
Barry Warsaw53699e91996-12-10 23:23:01 +00002241 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002242 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002243 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002244 WIN32_FIND_DATA FileData;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002245 PyObject *opath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002246 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002247 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002248 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002249
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002250 /* If on wide-character-capable OS see if argument
2251 is Unicode and if so use wide API. */
2252 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002253 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002254 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2255 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002256 Py_UNICODE *wnamebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002257 /* Overallocate for \\*.*\0 */
2258 len = PyUnicode_GET_SIZE(po);
2259 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2260 if (!wnamebuf) {
2261 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002262 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002263 }
2264 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002265 if (len > 0) {
2266 Py_UNICODE wch = wnamebuf[len-1];
2267 if (wch != L'/' && wch != L'\\' && wch != L':')
2268 wnamebuf[len++] = L'\\';
2269 wcscpy(wnamebuf + len, L"*.*");
2270 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002271 if ((d = PyList_New(0)) == NULL) {
2272 free(wnamebuf);
2273 return NULL;
2274 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002275 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2276 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002277 int error = GetLastError();
2278 if (error == ERROR_FILE_NOT_FOUND) {
2279 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002280 return d;
2281 }
2282 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002283 win32_error_unicode("FindFirstFileW", wnamebuf);
2284 free(wnamebuf);
2285 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002286 }
2287 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002288 /* Skip over . and .. */
2289 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2290 wcscmp(wFileData.cFileName, L"..") != 0) {
2291 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2292 if (v == NULL) {
2293 Py_DECREF(d);
2294 d = NULL;
2295 break;
2296 }
2297 if (PyList_Append(d, v) != 0) {
2298 Py_DECREF(v);
2299 Py_DECREF(d);
2300 d = NULL;
2301 break;
2302 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002303 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002304 }
Georg Brandl622927b2006-03-07 12:48:03 +00002305 Py_BEGIN_ALLOW_THREADS
2306 result = FindNextFileW(hFindFile, &wFileData);
2307 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002308 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2309 it got to the end of the directory. */
2310 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2311 Py_DECREF(d);
2312 win32_error_unicode("FindNextFileW", wnamebuf);
2313 FindClose(hFindFile);
2314 free(wnamebuf);
2315 return NULL;
2316 }
Georg Brandl622927b2006-03-07 12:48:03 +00002317 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002318
2319 if (FindClose(hFindFile) == FALSE) {
2320 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002321 win32_error_unicode("FindClose", wnamebuf);
2322 free(wnamebuf);
2323 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002324 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002325 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002326 return d;
2327 }
2328 /* Drop the argument parsing error as narrow strings
2329 are also valid. */
2330 PyErr_Clear();
2331 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002332
Martin v. Löwis011e8422009-05-05 04:43:17 +00002333 if (!PyArg_ParseTuple(args, "O&:listdir",
2334 PyUnicode_FSConverter, &opath))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002335 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002336 if (PyObject_Size(opath)+1 > MAX_PATH) {
2337 PyErr_SetString(PyExc_ValueError, "path too long");
2338 Py_DECREF(opath);
2339 return NULL;
2340 }
2341 strcpy(namebuf, bytes2str(opath, 0));
2342 len = PyObject_Size(opath);
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002343 if (len > 0) {
2344 char ch = namebuf[len-1];
2345 if (ch != SEP && ch != ALTSEP && ch != ':')
2346 namebuf[len++] = '/';
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002347 strcpy(namebuf + len, "*.*");
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002348 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002349
Barry Warsaw53699e91996-12-10 23:23:01 +00002350 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002351 return NULL;
2352
2353 hFindFile = FindFirstFile(namebuf, &FileData);
2354 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002355 int error = GetLastError();
2356 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002357 return d;
2358 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002359 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002360 }
2361 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002362 /* Skip over . and .. */
2363 if (strcmp(FileData.cFileName, ".") != 0 &&
2364 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002365 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002366 if (v == NULL) {
2367 Py_DECREF(d);
2368 d = NULL;
2369 break;
2370 }
2371 if (PyList_Append(d, v) != 0) {
2372 Py_DECREF(v);
2373 Py_DECREF(d);
2374 d = NULL;
2375 break;
2376 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002377 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002378 }
Georg Brandl622927b2006-03-07 12:48:03 +00002379 Py_BEGIN_ALLOW_THREADS
2380 result = FindNextFile(hFindFile, &FileData);
2381 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002382 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2383 it got to the end of the directory. */
2384 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2385 Py_DECREF(d);
2386 win32_error("FindNextFile", namebuf);
2387 FindClose(hFindFile);
2388 return NULL;
2389 }
Georg Brandl622927b2006-03-07 12:48:03 +00002390 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002391
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002392 if (FindClose(hFindFile) == FALSE) {
2393 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002394 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002395 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002396
2397 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002398
Tim Peters0bb44a42000-09-15 07:44:49 +00002399#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002400
2401#ifndef MAX_PATH
2402#define MAX_PATH CCHMAXPATH
2403#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002404 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002405 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002406 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002407 PyObject *d, *v;
2408 char namebuf[MAX_PATH+5];
2409 HDIR hdir = 1;
2410 ULONG srchcnt = 1;
2411 FILEFINDBUF3 ep;
2412 APIRET rc;
2413
Martin v. Löwis011e8422009-05-05 04:43:17 +00002414 if (!PyArg_ParseTuple(args, "O&:listdir",
2415 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002416 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002417 name = bytes2str(oname);
2418 len = PyObject_Size(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002419 if (len >= MAX_PATH) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002420 release_bytes(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002421 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002422 return NULL;
2423 }
2424 strcpy(namebuf, name);
2425 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002426 if (*pt == ALTSEP)
2427 *pt = SEP;
2428 if (namebuf[len-1] != SEP)
2429 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002430 strcpy(namebuf + len, "*.*");
2431
Neal Norwitz6c913782007-10-14 03:23:09 +00002432 if ((d = PyList_New(0)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002433 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002434 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002435 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002436
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002437 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2438 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002439 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002440 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2441 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2442 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002443
2444 if (rc != NO_ERROR) {
2445 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002446 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002447 }
2448
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002449 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002450 do {
2451 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002452 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002453 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002454
2455 strcpy(namebuf, ep.achName);
2456
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002457 /* Leave Case of Name Alone -- In Native Form */
2458 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002459
Christian Heimes72b710a2008-05-26 13:28:38 +00002460 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002461 if (v == NULL) {
2462 Py_DECREF(d);
2463 d = NULL;
2464 break;
2465 }
2466 if (PyList_Append(d, v) != 0) {
2467 Py_DECREF(v);
2468 Py_DECREF(d);
2469 d = NULL;
2470 break;
2471 }
2472 Py_DECREF(v);
2473 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2474 }
2475
Martin v. Löwis011e8422009-05-05 04:43:17 +00002476 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002477 return d;
2478#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002479 PyObject *oname;
2480 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +00002481 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002482 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002483 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002484 int arg_is_unicode = 1;
2485
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002486 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002487 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2488 arg_is_unicode = 0;
2489 PyErr_Clear();
2490 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002491 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002492 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002493 name = bytes2str(oname, 1);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002494 if ((dirp = opendir(name)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002495 return posix_error_with_allocated_filename(oname);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002496 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002497 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002498 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002499 release_bytes(oname);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002500 return NULL;
2501 }
Georg Brandl622927b2006-03-07 12:48:03 +00002502 for (;;) {
Georg Brandl3dbca812008-07-23 16:10:53 +00002503 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002504 Py_BEGIN_ALLOW_THREADS
2505 ep = readdir(dirp);
2506 Py_END_ALLOW_THREADS
Georg Brandl3dbca812008-07-23 16:10:53 +00002507 if (ep == NULL) {
2508 if (errno == 0) {
2509 break;
2510 } else {
2511 closedir(dirp);
2512 Py_DECREF(d);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002513 return posix_error_with_allocated_filename(oname);
Georg Brandl3dbca812008-07-23 16:10:53 +00002514 }
2515 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002516 if (ep->d_name[0] == '.' &&
2517 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002518 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002519 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002520 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002521 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002522 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002523 d = NULL;
2524 break;
2525 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002526 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002527 PyObject *w;
2528
2529 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002530 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00002531 "surrogateescape");
Martin v. Löwis011e8422009-05-05 04:43:17 +00002532 Py_DECREF(v);
2533 if (w != NULL)
Just van Rossum6a421832003-03-04 19:30:44 +00002534 v = w;
Just van Rossum6a421832003-03-04 19:30:44 +00002535 else {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002536 /* Encoding failed to decode ASCII bytes.
2537 Raise exception. */
2538 Py_DECREF(d);
2539 d = NULL;
2540 break;
Just van Rossum46c97842003-02-25 21:42:15 +00002541 }
Just van Rossum46c97842003-02-25 21:42:15 +00002542 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002543 if (PyList_Append(d, v) != 0) {
2544 Py_DECREF(v);
2545 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002546 d = NULL;
2547 break;
2548 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002549 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002550 }
2551 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002552 release_bytes(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002553
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002554 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002555
Tim Peters0bb44a42000-09-15 07:44:49 +00002556#endif /* which OS */
2557} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002558
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002559#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002560/* A helper function for abspath on win32 */
2561static PyObject *
2562posix__getfullpathname(PyObject *self, PyObject *args)
2563{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002564 PyObject *opath;
2565 char *path;
Mark Hammondef8b6542001-05-13 08:04:26 +00002566 char outbuf[MAX_PATH*2];
2567 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002568#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002569 if (unicode_file_names()) {
2570 PyUnicodeObject *po;
2571 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
Benjamin Petersonf608c612008-11-16 18:33:53 +00002572 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2573 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002574 Py_UNICODE *wtemp;
Benjamin Petersonf608c612008-11-16 18:33:53 +00002575 DWORD result;
2576 PyObject *v;
2577 result = GetFullPathNameW(wpath,
2578 sizeof(woutbuf)/sizeof(woutbuf[0]),
2579 woutbuf, &wtemp);
2580 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2581 woutbufp = malloc(result * sizeof(Py_UNICODE));
2582 if (!woutbufp)
2583 return PyErr_NoMemory();
2584 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2585 }
2586 if (result)
2587 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2588 else
2589 v = win32_error_unicode("GetFullPathNameW", wpath);
2590 if (woutbufp != woutbuf)
2591 free(woutbufp);
2592 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002593 }
2594 /* Drop the argument parsing error as narrow strings
2595 are also valid. */
2596 PyErr_Clear();
2597 }
2598#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002599 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2600 PyUnicode_FSConverter, &opath))
Mark Hammondef8b6542001-05-13 08:04:26 +00002601 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002602 path = bytes2str(opath, 1);
2603 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2604 outbuf, &temp)) {
2605 win32_error("GetFullPathName", path);
2606 release_bytes(opath);
2607 return NULL;
2608 }
2609 release_bytes(opath);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002610 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2611 return PyUnicode_Decode(outbuf, strlen(outbuf),
2612 Py_FileSystemDefaultEncoding, NULL);
2613 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002614 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002615} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002616#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002617
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002618PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002619"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002620Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002621
Barry Warsaw53699e91996-12-10 23:23:01 +00002622static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002623posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002624{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002625 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002626 PyObject *opath;
2627 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002628 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002629
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002630#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002631 if (unicode_file_names()) {
2632 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002633 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002634 Py_BEGIN_ALLOW_THREADS
2635 /* PyUnicode_AS_UNICODE OK without thread lock as
2636 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002637 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002638 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002639 if (!res)
2640 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002641 Py_INCREF(Py_None);
2642 return Py_None;
2643 }
2644 /* Drop the argument parsing error as narrow strings
2645 are also valid. */
2646 PyErr_Clear();
2647 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002648 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2649 PyUnicode_FSConverter, &opath, &mode))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002650 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002651 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002652 Py_BEGIN_ALLOW_THREADS
2653 /* PyUnicode_AS_UNICODE OK without thread lock as
2654 it is a simple dereference. */
2655 res = CreateDirectoryA(path, NULL);
2656 Py_END_ALLOW_THREADS
2657 if (!res) {
2658 win32_error("mkdir", path);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002659 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002660 return NULL;
2661 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002662 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002663 Py_INCREF(Py_None);
2664 return Py_None;
2665#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002666
Martin v. Löwis011e8422009-05-05 04:43:17 +00002667 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2668 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002669 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002670 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00002671 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002672#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002673 res = mkdir(path);
2674#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002675 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002676#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002677 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002678 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002679 return posix_error_with_allocated_filename(opath);
2680 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002681 Py_INCREF(Py_None);
2682 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002683#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002684}
2685
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002686
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002687/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2688#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002689#include <sys/resource.h>
2690#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002691
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002692
2693#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002694PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002695"nice(inc) -> new_priority\n\n\
2696Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002697
Barry Warsaw53699e91996-12-10 23:23:01 +00002698static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002699posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002700{
2701 int increment, value;
2702
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002703 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002704 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002705
2706 /* There are two flavours of 'nice': one that returns the new
2707 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002708 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2709 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002710
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002711 If we are of the nice family that returns the new priority, we
2712 need to clear errno before the call, and check if errno is filled
2713 before calling posix_error() on a returnvalue of -1, because the
2714 -1 may be the actual new priority! */
2715
2716 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002717 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002718#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002719 if (value == 0)
2720 value = getpriority(PRIO_PROCESS, 0);
2721#endif
2722 if (value == -1 && errno != 0)
2723 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002724 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002725 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002726}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002727#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002728
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002729PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002730"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002731Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002732
Barry Warsaw53699e91996-12-10 23:23:01 +00002733static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002734posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002735{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002736#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002737 PyObject *o1, *o2;
2738 char *p1, *p2;
2739 BOOL result;
2740 if (unicode_file_names()) {
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002741 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2742 goto error;
2743 if (!convert_to_unicode(&o1))
2744 goto error;
2745 if (!convert_to_unicode(&o2)) {
2746 Py_DECREF(o1);
2747 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002748 }
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002749 Py_BEGIN_ALLOW_THREADS
2750 result = MoveFileW(PyUnicode_AsUnicode(o1),
2751 PyUnicode_AsUnicode(o2));
2752 Py_END_ALLOW_THREADS
2753 Py_DECREF(o1);
2754 Py_DECREF(o2);
2755 if (!result)
2756 return win32_error("rename", NULL);
2757 Py_INCREF(Py_None);
2758 return Py_None;
2759error:
2760 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002761 }
2762 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2763 return NULL;
2764 Py_BEGIN_ALLOW_THREADS
2765 result = MoveFileA(p1, p2);
2766 Py_END_ALLOW_THREADS
2767 if (!result)
2768 return win32_error("rename", NULL);
2769 Py_INCREF(Py_None);
2770 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002771#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002772 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002773#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002774}
2775
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002777PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002778"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002779Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002780
Barry Warsaw53699e91996-12-10 23:23:01 +00002781static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002782posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002783{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002784#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002785 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002786#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002787 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002788#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002789}
2790
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002791
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002792PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002793"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002794Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002795
Barry Warsaw53699e91996-12-10 23:23:01 +00002796static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002797posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002798{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002799#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00002800 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002801#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002802 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002803#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002804}
2805
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002806
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002807#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002808PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002809"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002810Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002811
Barry Warsaw53699e91996-12-10 23:23:01 +00002812static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002813posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002814{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002815 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002816#ifdef MS_WINDOWS
2817 wchar_t *command;
2818 if (!PyArg_ParseTuple(args, "u:system", &command))
2819 return NULL;
2820#else
2821 char *command;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002822 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002823 return NULL;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002824#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002825 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002826#ifdef MS_WINDOWS
2827 sts = _wsystem(command);
2828#else
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002829 sts = system(command);
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002830#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002831 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +00002832 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002833}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002834#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002835
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002836
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002837PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002838"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002839Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002840
Barry Warsaw53699e91996-12-10 23:23:01 +00002841static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002842posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002843{
2844 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002845 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002846 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002847 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002848 if (i < 0)
2849 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002850 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002851}
2852
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002853
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002854PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002855"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002856Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002857
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002858PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002859"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002860Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002861
Barry Warsaw53699e91996-12-10 23:23:01 +00002862static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002863posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002864{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002865#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002866 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002867#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002868 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002869#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002870}
2871
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002872
Guido van Rossumb6775db1994-08-01 11:34:53 +00002873#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002874PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002875"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002876Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002877
Barry Warsaw53699e91996-12-10 23:23:01 +00002878static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002879posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002880{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002881 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002882 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002883
Barry Warsaw53699e91996-12-10 23:23:01 +00002884 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002885 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002886 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002887 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002888 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002889 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002890 u.sysname,
2891 u.nodename,
2892 u.release,
2893 u.version,
2894 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002895}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002896#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002897
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002898static int
2899extract_time(PyObject *t, long* sec, long* usec)
2900{
2901 long intval;
2902 if (PyFloat_Check(t)) {
2903 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002904 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002905 if (!intobj)
2906 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002907 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002908 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002909 if (intval == -1 && PyErr_Occurred())
2910 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002911 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002912 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002913 if (*usec < 0)
2914 /* If rounding gave us a negative number,
2915 truncate. */
2916 *usec = 0;
2917 return 0;
2918 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002919 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002920 if (intval == -1 && PyErr_Occurred())
2921 return -1;
2922 *sec = intval;
2923 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002924 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002925}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002926
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002927PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002928"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002929utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002930Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002931second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002932
Barry Warsaw53699e91996-12-10 23:23:01 +00002933static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002934posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002935{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002936#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002937 PyObject *arg;
2938 PyUnicodeObject *obwpath;
2939 wchar_t *wpath = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002940 PyObject *oapath;
2941 char *apath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002942 HANDLE hFile;
2943 long atimesec, mtimesec, ausec, musec;
2944 FILETIME atime, mtime;
2945 PyObject *result = NULL;
2946
2947 if (unicode_file_names()) {
2948 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2949 wpath = PyUnicode_AS_UNICODE(obwpath);
2950 Py_BEGIN_ALLOW_THREADS
2951 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002952 NULL, OPEN_EXISTING,
2953 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002954 Py_END_ALLOW_THREADS
2955 if (hFile == INVALID_HANDLE_VALUE)
2956 return win32_error_unicode("utime", wpath);
2957 } else
2958 /* Drop the argument parsing error as narrow strings
2959 are also valid. */
2960 PyErr_Clear();
2961 }
2962 if (!wpath) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002963 if (!PyArg_ParseTuple(args, "O&O:utime",
2964 PyUnicode_FSConverter, &oapath, &arg))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002965 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002966 apath = bytes2str(oapath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002967 Py_BEGIN_ALLOW_THREADS
2968 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002969 NULL, OPEN_EXISTING,
2970 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002971 Py_END_ALLOW_THREADS
2972 if (hFile == INVALID_HANDLE_VALUE) {
2973 win32_error("utime", apath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002974 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002975 return NULL;
2976 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002977 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002978 }
2979
2980 if (arg == Py_None) {
2981 SYSTEMTIME now;
2982 GetSystemTime(&now);
2983 if (!SystemTimeToFileTime(&now, &mtime) ||
2984 !SystemTimeToFileTime(&now, &atime)) {
2985 win32_error("utime", NULL);
2986 goto done;
2987 }
2988 }
2989 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2990 PyErr_SetString(PyExc_TypeError,
2991 "utime() arg 2 must be a tuple (atime, mtime)");
2992 goto done;
2993 }
2994 else {
2995 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2996 &atimesec, &ausec) == -1)
2997 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002998 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002999 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3000 &mtimesec, &musec) == -1)
3001 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003002 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003003 }
3004 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3005 /* Avoid putting the file name into the error here,
3006 as that may confuse the user into believing that
3007 something is wrong with the file, when it also
3008 could be the time stamp that gives a problem. */
3009 win32_error("utime", NULL);
3010 }
3011 Py_INCREF(Py_None);
3012 result = Py_None;
3013done:
3014 CloseHandle(hFile);
3015 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003016#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003017
Martin v. Löwis011e8422009-05-05 04:43:17 +00003018 PyObject *opath;
3019 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003020 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00003021 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00003022 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003023
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003024#if defined(HAVE_UTIMES)
3025 struct timeval buf[2];
3026#define ATIME buf[0].tv_sec
3027#define MTIME buf[1].tv_sec
3028#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003029/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003030 struct utimbuf buf;
3031#define ATIME buf.actime
3032#define MTIME buf.modtime
3033#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003034#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003035 time_t buf[2];
3036#define ATIME buf[0]
3037#define MTIME buf[1]
3038#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003039#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003040
Mark Hammond817c9292003-12-03 01:22:38 +00003041
Martin v. Löwis011e8422009-05-05 04:43:17 +00003042 if (!PyArg_ParseTuple(args, "O&O:utime",
3043 PyUnicode_FSConverter, &opath, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003044 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003045 path = bytes2str(opath, 1);
Barry Warsaw3cef8562000-05-01 16:17:24 +00003046 if (arg == Py_None) {
3047 /* optional time values not given */
3048 Py_BEGIN_ALLOW_THREADS
3049 res = utime(path, NULL);
3050 Py_END_ALLOW_THREADS
3051 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003052 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00003053 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003054 "utime() arg 2 must be a tuple (atime, mtime)");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003055 release_bytes(opath);
Barry Warsaw3cef8562000-05-01 16:17:24 +00003056 return NULL;
3057 }
3058 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003059 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00003060 &atime, &ausec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003061 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003062 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00003063 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003064 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00003065 &mtime, &musec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003066 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003067 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00003068 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00003069 ATIME = atime;
3070 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003071#ifdef HAVE_UTIMES
3072 buf[0].tv_usec = ausec;
3073 buf[1].tv_usec = musec;
3074 Py_BEGIN_ALLOW_THREADS
3075 res = utimes(path, buf);
3076 Py_END_ALLOW_THREADS
3077#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00003078 Py_BEGIN_ALLOW_THREADS
3079 res = utime(path, UTIME_ARG);
3080 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003081#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00003082 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00003083 if (res < 0) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003084 return posix_error_with_allocated_filename(opath);
Mark Hammond2d5914b2004-05-04 08:10:37 +00003085 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00003086 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00003087 Py_INCREF(Py_None);
3088 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003089#undef UTIME_ARG
3090#undef ATIME
3091#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003092#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003093}
3094
Guido van Rossum85e3b011991-06-03 12:42:10 +00003095
Guido van Rossum3b066191991-06-04 19:40:25 +00003096/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003097
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003098PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003099"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003100Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003101
Barry Warsaw53699e91996-12-10 23:23:01 +00003102static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003103posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003104{
3105 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003106 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003107 return NULL;
3108 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00003109 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003110}
3111
Martin v. Löwis114619e2002-10-07 06:44:21 +00003112#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3113static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003114free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003115{
Martin v. Löwis725507b2006-03-07 12:08:51 +00003116 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00003117 for (i = 0; i < count; i++)
3118 PyMem_Free(array[i]);
3119 PyMem_DEL(array);
3120}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003121
3122int fsconvert_strdup(PyObject *o, char**out)
3123{
3124 PyObject *bytes;
3125 Py_ssize_t size;
3126 if (!PyUnicode_FSConverter(o, &bytes))
3127 return 0;
3128 size = PyObject_Size(bytes);
3129 *out = PyMem_Malloc(size+1);
3130 if (!*out)
3131 return 0;
3132 /* Don't lock bytes, as we hold the GIL */
3133 memcpy(*out, bytes2str(bytes, 0), size+1);
3134 Py_DECREF(bytes);
3135 return 1;
3136}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003137#endif
3138
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003139
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003140#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003141PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003142"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003143Execute an executable path with arguments, replacing current process.\n\
3144\n\
3145 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003146 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003147
Barry Warsaw53699e91996-12-10 23:23:01 +00003148static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003149posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003150{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003151 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003152 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003153 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003154 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003155 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003156 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003157
Guido van Rossum89b33251993-10-22 14:26:06 +00003158 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00003159 argv is a list or tuple of strings. */
3160
Martin v. Löwis011e8422009-05-05 04:43:17 +00003161 if (!PyArg_ParseTuple(args, "O&O:execv",
3162 PyUnicode_FSConverter,
3163 &opath, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003164 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003165 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003166 if (PyList_Check(argv)) {
3167 argc = PyList_Size(argv);
3168 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003169 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003170 else if (PyTuple_Check(argv)) {
3171 argc = PyTuple_Size(argv);
3172 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003173 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003174 else {
Fred Drake661ea262000-10-24 19:57:45 +00003175 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003176 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003177 return NULL;
3178 }
Thomas Heller6790d602007-08-30 17:15:14 +00003179 if (argc < 1) {
3180 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003181 release_bytes(opath);
Thomas Heller6790d602007-08-30 17:15:14 +00003182 return NULL;
3183 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003184
Barry Warsaw53699e91996-12-10 23:23:01 +00003185 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003186 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003187 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003188 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003189 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003190 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003191 if (!fsconvert_strdup((*getitem)(argv, i),
3192 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003193 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00003194 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003195 "execv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003196 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003197 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003198
Guido van Rossum85e3b011991-06-03 12:42:10 +00003199 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003200 }
3201 argvlist[argc] = NULL;
3202
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003203 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003204
Guido van Rossum85e3b011991-06-03 12:42:10 +00003205 /* If we get here it's definitely an error */
3206
Martin v. Löwis114619e2002-10-07 06:44:21 +00003207 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003208 release_bytes(opath);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003209 return posix_error();
3210}
3211
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003213PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003214"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003215Execute a path with arguments and environment, replacing current process.\n\
3216\n\
3217 path: path of executable file\n\
3218 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003219 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003220
Barry Warsaw53699e91996-12-10 23:23:01 +00003221static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003222posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003223{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003224 PyObject *opath;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003225 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003226 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003227 char **argvlist;
3228 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003229 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003230 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003231 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003232 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003233
3234 /* execve has three arguments: (path, argv, env), where
3235 argv is a list or tuple of strings and env is a dictionary
3236 like posix.environ. */
3237
Martin v. Löwis011e8422009-05-05 04:43:17 +00003238 if (!PyArg_ParseTuple(args, "O&OO:execve",
3239 PyUnicode_FSConverter,
3240 &opath, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003241 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003242 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003243 if (PyList_Check(argv)) {
3244 argc = PyList_Size(argv);
3245 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003246 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003247 else if (PyTuple_Check(argv)) {
3248 argc = PyTuple_Size(argv);
3249 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003250 }
3251 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003252 PyErr_SetString(PyExc_TypeError,
3253 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003254 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003255 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003256 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003257 PyErr_SetString(PyExc_TypeError,
3258 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003259 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003260 }
3261
Barry Warsaw53699e91996-12-10 23:23:01 +00003262 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003263 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003264 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003265 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003266 }
3267 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003268 if (!fsconvert_strdup((*getitem)(argv, i),
3269 &argvlist[i]))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003270 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003271 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003272 goto fail_1;
3273 }
3274 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003275 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003276 argvlist[argc] = NULL;
3277
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003278 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003279 if (i < 0)
3280 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003281 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003282 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003283 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003284 goto fail_1;
3285 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003286 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003287 keys = PyMapping_Keys(env);
3288 vals = PyMapping_Values(env);
3289 if (!keys || !vals)
3290 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003291 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3292 PyErr_SetString(PyExc_TypeError,
3293 "execve(): env.keys() or env.values() is not a list");
3294 goto fail_2;
3295 }
Tim Peters5aa91602002-01-30 05:46:57 +00003296
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003297 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003298 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003299 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003300
3301 key = PyList_GetItem(keys, pos);
3302 val = PyList_GetItem(vals, pos);
3303 if (!key || !val)
3304 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003305
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003306 if (!PyArg_Parse(
3307 key,
3308 "s;execve() arg 3 contains a non-string key",
3309 &k) ||
3310 !PyArg_Parse(
3311 val,
3312 "s;execve() arg 3 contains a non-string value",
3313 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003314 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003315 goto fail_2;
3316 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003317
3318#if defined(PYOS_OS2)
3319 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3320 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3321#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003322 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003323 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003324 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003325 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003326 goto fail_2;
3327 }
Tim Petersc8996f52001-12-03 20:41:00 +00003328 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003329 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003330#if defined(PYOS_OS2)
3331 }
3332#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003333 }
3334 envlist[envc] = 0;
3335
3336 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003337
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003338 /* If we get here it's definitely an error */
3339
3340 (void) posix_error();
3341
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003342 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003343 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003344 PyMem_DEL(envlist[envc]);
3345 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003346 fail_1:
3347 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003348 Py_XDECREF(vals);
3349 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003350 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003351 release_bytes(opath);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003352 return NULL;
3353}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003354#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003355
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003356
Guido van Rossuma1065681999-01-25 23:20:23 +00003357#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003358PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003359"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003360Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003361\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003362 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003363 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003364 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003365
3366static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003367posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003368{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003369 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003370 char *path;
3371 PyObject *argv;
3372 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003373 int mode, i;
3374 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003375 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003376 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003377
3378 /* spawnv has three arguments: (mode, path, argv), where
3379 argv is a list or tuple of strings. */
3380
Martin v. Löwis011e8422009-05-05 04:43:17 +00003381 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3382 PyUnicode_FSConverter,
3383 &opath, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003384 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003385 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003386 if (PyList_Check(argv)) {
3387 argc = PyList_Size(argv);
3388 getitem = PyList_GetItem;
3389 }
3390 else if (PyTuple_Check(argv)) {
3391 argc = PyTuple_Size(argv);
3392 getitem = PyTuple_GetItem;
3393 }
3394 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003395 PyErr_SetString(PyExc_TypeError,
3396 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003397 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003398 return NULL;
3399 }
3400
3401 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003402 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003403 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003404 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003405 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003406 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003407 if (!fsconvert_strdup((*getitem)(argv, i),
3408 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003409 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003410 PyErr_SetString(
3411 PyExc_TypeError,
3412 "spawnv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003413 release_bytes(opath);
Fred Drake137507e2000-06-01 02:02:46 +00003414 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003415 }
3416 }
3417 argvlist[argc] = NULL;
3418
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003419#if defined(PYOS_OS2) && defined(PYCC_GCC)
3420 Py_BEGIN_ALLOW_THREADS
3421 spawnval = spawnv(mode, path, argvlist);
3422 Py_END_ALLOW_THREADS
3423#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003424 if (mode == _OLD_P_OVERLAY)
3425 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003426
Tim Peters25059d32001-12-07 20:35:43 +00003427 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003428 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003429 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003430#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003431
Martin v. Löwis114619e2002-10-07 06:44:21 +00003432 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003433 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003434
Fred Drake699f3522000-06-29 21:12:41 +00003435 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003436 return posix_error();
3437 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003438#if SIZEOF_LONG == SIZEOF_VOID_P
3439 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003440#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003441 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003442#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003443}
3444
3445
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003446PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003447"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003448Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003449\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003450 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003451 path: path of executable file\n\
3452 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003453 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003454
3455static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003456posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003457{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003458 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003459 char *path;
3460 PyObject *argv, *env;
3461 char **argvlist;
3462 char **envlist;
3463 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003464 int mode, pos, envc;
3465 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003466 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003467 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003468 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003469
3470 /* spawnve has four arguments: (mode, path, argv, env), where
3471 argv is a list or tuple of strings and env is a dictionary
3472 like posix.environ. */
3473
Martin v. Löwis011e8422009-05-05 04:43:17 +00003474 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3475 PyUnicode_FSConverter,
3476 &opath, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003477 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003478 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003479 if (PyList_Check(argv)) {
3480 argc = PyList_Size(argv);
3481 getitem = PyList_GetItem;
3482 }
3483 else if (PyTuple_Check(argv)) {
3484 argc = PyTuple_Size(argv);
3485 getitem = PyTuple_GetItem;
3486 }
3487 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003488 PyErr_SetString(PyExc_TypeError,
3489 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003490 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003491 }
3492 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003493 PyErr_SetString(PyExc_TypeError,
3494 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003495 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003496 }
3497
3498 argvlist = PyMem_NEW(char *, argc+1);
3499 if (argvlist == NULL) {
3500 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003501 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003502 }
3503 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003504 if (!fsconvert_strdup((*getitem)(argv, i),
3505 &argvlist[i]))
Guido van Rossuma1065681999-01-25 23:20:23 +00003506 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003507 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003508 goto fail_1;
3509 }
3510 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003511 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003512 argvlist[argc] = NULL;
3513
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003514 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003515 if (i < 0)
3516 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003517 envlist = PyMem_NEW(char *, i + 1);
3518 if (envlist == NULL) {
3519 PyErr_NoMemory();
3520 goto fail_1;
3521 }
3522 envc = 0;
3523 keys = PyMapping_Keys(env);
3524 vals = PyMapping_Values(env);
3525 if (!keys || !vals)
3526 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003527 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3528 PyErr_SetString(PyExc_TypeError,
3529 "spawnve(): env.keys() or env.values() is not a list");
3530 goto fail_2;
3531 }
Tim Peters5aa91602002-01-30 05:46:57 +00003532
Guido van Rossuma1065681999-01-25 23:20:23 +00003533 for (pos = 0; pos < i; pos++) {
3534 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003535 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003536
3537 key = PyList_GetItem(keys, pos);
3538 val = PyList_GetItem(vals, pos);
3539 if (!key || !val)
3540 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003541
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003542 if (!PyArg_Parse(
3543 key,
3544 "s;spawnve() arg 3 contains a non-string key",
3545 &k) ||
3546 !PyArg_Parse(
3547 val,
3548 "s;spawnve() arg 3 contains a non-string value",
3549 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003550 {
3551 goto fail_2;
3552 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003553 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003554 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003555 if (p == NULL) {
3556 PyErr_NoMemory();
3557 goto fail_2;
3558 }
Tim Petersc8996f52001-12-03 20:41:00 +00003559 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003560 envlist[envc++] = p;
3561 }
3562 envlist[envc] = 0;
3563
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003564#if defined(PYOS_OS2) && defined(PYCC_GCC)
3565 Py_BEGIN_ALLOW_THREADS
3566 spawnval = spawnve(mode, path, argvlist, envlist);
3567 Py_END_ALLOW_THREADS
3568#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003569 if (mode == _OLD_P_OVERLAY)
3570 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003571
3572 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003573 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003574 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003575#endif
Tim Peters25059d32001-12-07 20:35:43 +00003576
Fred Drake699f3522000-06-29 21:12:41 +00003577 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003578 (void) posix_error();
3579 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003580#if SIZEOF_LONG == SIZEOF_VOID_P
3581 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003582#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003583 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003584#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003585
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003586 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003587 while (--envc >= 0)
3588 PyMem_DEL(envlist[envc]);
3589 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003590 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003591 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003592 Py_XDECREF(vals);
3593 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003594 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003595 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003596 return res;
3597}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003598
3599/* OS/2 supports spawnvp & spawnvpe natively */
3600#if defined(PYOS_OS2)
3601PyDoc_STRVAR(posix_spawnvp__doc__,
3602"spawnvp(mode, file, args)\n\n\
3603Execute the program 'file' in a new process, using the environment\n\
3604search path to find the file.\n\
3605\n\
3606 mode: mode of process creation\n\
3607 file: executable file name\n\
3608 args: tuple or list of strings");
3609
3610static PyObject *
3611posix_spawnvp(PyObject *self, PyObject *args)
3612{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003613 PyObject *opath;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003614 char *path;
3615 PyObject *argv;
3616 char **argvlist;
3617 int mode, i, argc;
3618 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003619 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003620
3621 /* spawnvp has three arguments: (mode, path, argv), where
3622 argv is a list or tuple of strings. */
3623
Martin v. Löwis011e8422009-05-05 04:43:17 +00003624 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3625 PyUnicode_FSConverter,
3626 &opath, &argv))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003627 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003628 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003629 if (PyList_Check(argv)) {
3630 argc = PyList_Size(argv);
3631 getitem = PyList_GetItem;
3632 }
3633 else if (PyTuple_Check(argv)) {
3634 argc = PyTuple_Size(argv);
3635 getitem = PyTuple_GetItem;
3636 }
3637 else {
3638 PyErr_SetString(PyExc_TypeError,
3639 "spawnvp() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003640 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003641 return NULL;
3642 }
3643
3644 argvlist = PyMem_NEW(char *, argc+1);
3645 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003646 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003647 return PyErr_NoMemory();
3648 }
3649 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003650 if (!fsconvert_strdup((*getitem)(argv, i),
3651 &argvlist[i])) {
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003652 free_string_array(argvlist, i);
3653 PyErr_SetString(
3654 PyExc_TypeError,
3655 "spawnvp() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003656 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003657 return NULL;
3658 }
3659 }
3660 argvlist[argc] = NULL;
3661
3662 Py_BEGIN_ALLOW_THREADS
3663#if defined(PYCC_GCC)
3664 spawnval = spawnvp(mode, path, argvlist);
3665#else
3666 spawnval = _spawnvp(mode, path, argvlist);
3667#endif
3668 Py_END_ALLOW_THREADS
3669
3670 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003671 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003672
3673 if (spawnval == -1)
3674 return posix_error();
3675 else
3676 return Py_BuildValue("l", (long) spawnval);
3677}
3678
3679
3680PyDoc_STRVAR(posix_spawnvpe__doc__,
3681"spawnvpe(mode, file, args, env)\n\n\
3682Execute the program 'file' in a new process, using the environment\n\
3683search path to find the file.\n\
3684\n\
3685 mode: mode of process creation\n\
3686 file: executable file name\n\
3687 args: tuple or list of arguments\n\
3688 env: dictionary of strings mapping to strings");
3689
3690static PyObject *
3691posix_spawnvpe(PyObject *self, PyObject *args)
3692{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003693 PyObject *opath
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003694 char *path;
3695 PyObject *argv, *env;
3696 char **argvlist;
3697 char **envlist;
3698 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3699 int mode, i, pos, argc, envc;
3700 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003701 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003702 int lastarg = 0;
3703
3704 /* spawnvpe has four arguments: (mode, path, argv, env), where
3705 argv is a list or tuple of strings and env is a dictionary
3706 like posix.environ. */
3707
3708 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
Martin v. Löwis011e8422009-05-05 04:43:17 +00003709 PyUnicode_FSConverter,
3710 &opath, &argv, &env))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003711 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003712 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003713 if (PyList_Check(argv)) {
3714 argc = PyList_Size(argv);
3715 getitem = PyList_GetItem;
3716 }
3717 else if (PyTuple_Check(argv)) {
3718 argc = PyTuple_Size(argv);
3719 getitem = PyTuple_GetItem;
3720 }
3721 else {
3722 PyErr_SetString(PyExc_TypeError,
3723 "spawnvpe() arg 2 must be a tuple or list");
3724 goto fail_0;
3725 }
3726 if (!PyMapping_Check(env)) {
3727 PyErr_SetString(PyExc_TypeError,
3728 "spawnvpe() arg 3 must be a mapping object");
3729 goto fail_0;
3730 }
3731
3732 argvlist = PyMem_NEW(char *, argc+1);
3733 if (argvlist == NULL) {
3734 PyErr_NoMemory();
3735 goto fail_0;
3736 }
3737 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003738 if (!fsconvert_strdup((*getitem)(argv, i),
3739 &argvlist[i]))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003740 {
3741 lastarg = i;
3742 goto fail_1;
3743 }
3744 }
3745 lastarg = argc;
3746 argvlist[argc] = NULL;
3747
3748 i = PyMapping_Size(env);
3749 if (i < 0)
3750 goto fail_1;
3751 envlist = PyMem_NEW(char *, i + 1);
3752 if (envlist == NULL) {
3753 PyErr_NoMemory();
3754 goto fail_1;
3755 }
3756 envc = 0;
3757 keys = PyMapping_Keys(env);
3758 vals = PyMapping_Values(env);
3759 if (!keys || !vals)
3760 goto fail_2;
3761 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3762 PyErr_SetString(PyExc_TypeError,
3763 "spawnvpe(): env.keys() or env.values() is not a list");
3764 goto fail_2;
3765 }
3766
3767 for (pos = 0; pos < i; pos++) {
3768 char *p, *k, *v;
3769 size_t len;
3770
3771 key = PyList_GetItem(keys, pos);
3772 val = PyList_GetItem(vals, pos);
3773 if (!key || !val)
3774 goto fail_2;
3775
3776 if (!PyArg_Parse(
3777 key,
3778 "s;spawnvpe() arg 3 contains a non-string key",
3779 &k) ||
3780 !PyArg_Parse(
3781 val,
3782 "s;spawnvpe() arg 3 contains a non-string value",
3783 &v))
3784 {
3785 goto fail_2;
3786 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003787 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003788 p = PyMem_NEW(char, len);
3789 if (p == NULL) {
3790 PyErr_NoMemory();
3791 goto fail_2;
3792 }
3793 PyOS_snprintf(p, len, "%s=%s", k, v);
3794 envlist[envc++] = p;
3795 }
3796 envlist[envc] = 0;
3797
3798 Py_BEGIN_ALLOW_THREADS
3799#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003800 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003801#else
Christian Heimes292d3512008-02-03 16:51:08 +00003802 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003803#endif
3804 Py_END_ALLOW_THREADS
3805
3806 if (spawnval == -1)
3807 (void) posix_error();
3808 else
3809 res = Py_BuildValue("l", (long) spawnval);
3810
3811 fail_2:
3812 while (--envc >= 0)
3813 PyMem_DEL(envlist[envc]);
3814 PyMem_DEL(envlist);
3815 fail_1:
3816 free_string_array(argvlist, lastarg);
3817 Py_XDECREF(vals);
3818 Py_XDECREF(keys);
3819 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003820 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003821 return res;
3822}
3823#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003824#endif /* HAVE_SPAWNV */
3825
3826
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003827#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003828PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003829"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003830Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3831\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003832Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003833
3834static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003835posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003836{
Christian Heimes400adb02008-02-01 08:12:03 +00003837 pid_t pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003838 if (pid == -1)
3839 return posix_error();
Georg Brandl2ee470f2008-07-16 12:55:28 +00003840 if (pid == 0)
3841 PyOS_AfterFork();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003842 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003843}
3844#endif
3845
3846
Guido van Rossumad0ee831995-03-01 10:34:45 +00003847#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003848PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003849"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003850Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003851Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003852
Barry Warsaw53699e91996-12-10 23:23:01 +00003853static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003854posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003855{
Christian Heimes400adb02008-02-01 08:12:03 +00003856 pid_t pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003857 if (pid == -1)
3858 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003859 if (pid == 0)
3860 PyOS_AfterFork();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003861 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003862}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003863#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003864
Neal Norwitzb59798b2003-03-21 01:43:31 +00003865/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003866/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3867#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003868#define DEV_PTY_FILE "/dev/ptc"
3869#define HAVE_DEV_PTMX
3870#else
3871#define DEV_PTY_FILE "/dev/ptmx"
3872#endif
3873
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003874#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003875#ifdef HAVE_PTY_H
3876#include <pty.h>
3877#else
3878#ifdef HAVE_LIBUTIL_H
3879#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003880#endif /* HAVE_LIBUTIL_H */
3881#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003882#ifdef HAVE_STROPTS_H
3883#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003884#endif
3885#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003886
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003887#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003888PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003889"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003890Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003891
3892static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003893posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003894{
3895 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003896#ifndef HAVE_OPENPTY
3897 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003898#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003899#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003900 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003901#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003902 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003903#endif
3904#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003905
Thomas Wouters70c21a12000-07-14 14:28:33 +00003906#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003907 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3908 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003909#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003910 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3911 if (slave_name == NULL)
3912 return posix_error();
3913
3914 slave_fd = open(slave_name, O_RDWR);
3915 if (slave_fd < 0)
3916 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003917#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003918 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003919 if (master_fd < 0)
3920 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003921 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003922 /* change permission of slave */
3923 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003924 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003925 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003926 }
3927 /* unlock slave */
3928 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003929 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003930 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003931 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003932 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003933 slave_name = ptsname(master_fd); /* get name of slave */
3934 if (slave_name == NULL)
3935 return posix_error();
3936 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3937 if (slave_fd < 0)
3938 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003939#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003940 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3941 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003942#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003943 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003944#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003945#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003946#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003947
Fred Drake8cef4cf2000-06-28 16:40:38 +00003948 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003949
Fred Drake8cef4cf2000-06-28 16:40:38 +00003950}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003951#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003952
3953#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003954PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003955"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003956Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3957Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003958To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003959
3960static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003961posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003962{
Christian Heimes400adb02008-02-01 08:12:03 +00003963 int master_fd = -1;
3964 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003965
Fred Drake8cef4cf2000-06-28 16:40:38 +00003966 pid = forkpty(&master_fd, NULL, NULL, NULL);
3967 if (pid == -1)
3968 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003969 if (pid == 0)
3970 PyOS_AfterFork();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003971 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003972}
3973#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003974
Guido van Rossumad0ee831995-03-01 10:34:45 +00003975#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003976PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003977"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003978Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003979
Barry Warsaw53699e91996-12-10 23:23:01 +00003980static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003981posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003982{
Christian Heimes217cfd12007-12-02 14:31:20 +00003983 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003984}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003985#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003986
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003987
Guido van Rossumad0ee831995-03-01 10:34:45 +00003988#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003989PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003990"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003991Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003992
Barry Warsaw53699e91996-12-10 23:23:01 +00003993static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003994posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003995{
Christian Heimes217cfd12007-12-02 14:31:20 +00003996 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003997}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003998#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003999
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004000
Guido van Rossumad0ee831995-03-01 10:34:45 +00004001#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004002PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004003"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004004Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004005
Barry Warsaw53699e91996-12-10 23:23:01 +00004006static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004007posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004008{
Christian Heimes217cfd12007-12-02 14:31:20 +00004009 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004010}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004011#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004012
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004013
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004014PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004015"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004016Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004017
Barry Warsaw53699e91996-12-10 23:23:01 +00004018static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004019posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004020{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004021 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004022}
4023
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004024
Fred Drakec9680921999-12-13 16:37:25 +00004025#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004026PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004027"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004028Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004029
4030static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004031posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004032{
4033 PyObject *result = NULL;
4034
Fred Drakec9680921999-12-13 16:37:25 +00004035#ifdef NGROUPS_MAX
4036#define MAX_GROUPS NGROUPS_MAX
4037#else
4038 /* defined to be 16 on Solaris7, so this should be a small number */
4039#define MAX_GROUPS 64
4040#endif
4041 gid_t grouplist[MAX_GROUPS];
4042 int n;
4043
4044 n = getgroups(MAX_GROUPS, grouplist);
4045 if (n < 0)
4046 posix_error();
4047 else {
4048 result = PyList_New(n);
4049 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00004050 int i;
4051 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004052 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00004053 if (o == NULL) {
4054 Py_DECREF(result);
4055 result = NULL;
4056 break;
4057 }
4058 PyList_SET_ITEM(result, i, o);
4059 }
4060 }
4061 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004062
Fred Drakec9680921999-12-13 16:37:25 +00004063 return result;
4064}
4065#endif
4066
Martin v. Löwis606edc12002-06-13 21:09:11 +00004067#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004068PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004069"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004070Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004071
4072static PyObject *
4073posix_getpgid(PyObject *self, PyObject *args)
4074{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004075 pid_t pid, pgid;
4076 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
Martin v. Löwis606edc12002-06-13 21:09:11 +00004077 return NULL;
4078 pgid = getpgid(pid);
4079 if (pgid < 0)
4080 return posix_error();
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004081 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004082}
4083#endif /* HAVE_GETPGID */
4084
4085
Guido van Rossumb6775db1994-08-01 11:34:53 +00004086#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004087PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004088"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004089Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004090
Barry Warsaw53699e91996-12-10 23:23:01 +00004091static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004092posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004093{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004094#ifdef GETPGRP_HAVE_ARG
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004095 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004096#else /* GETPGRP_HAVE_ARG */
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004097 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004098#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004099}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004100#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004101
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004102
Guido van Rossumb6775db1994-08-01 11:34:53 +00004103#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004104PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004105"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004106Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004107
Barry Warsaw53699e91996-12-10 23:23:01 +00004108static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004109posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004110{
Guido van Rossum64933891994-10-20 21:56:42 +00004111#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00004112 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004113#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004114 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004115#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00004116 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004117 Py_INCREF(Py_None);
4118 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004119}
4120
Guido van Rossumb6775db1994-08-01 11:34:53 +00004121#endif /* HAVE_SETPGRP */
4122
Guido van Rossumad0ee831995-03-01 10:34:45 +00004123#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004124PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004125"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004126Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004127
Barry Warsaw53699e91996-12-10 23:23:01 +00004128static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004129posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004130{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004131 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004132}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004133#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004134
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004135
Fred Drake12c6e2d1999-12-14 21:25:03 +00004136#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004137PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004138"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004139Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004140
4141static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004142posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004143{
Neal Norwitze241ce82003-02-17 18:17:05 +00004144 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00004145 char *name;
4146 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004147
Fred Drakea30680b2000-12-06 21:24:28 +00004148 errno = 0;
4149 name = getlogin();
4150 if (name == NULL) {
4151 if (errno)
4152 posix_error();
4153 else
4154 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00004155 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00004156 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00004157 else
Neal Norwitz93c56822007-08-26 07:10:06 +00004158 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00004159 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004160
Fred Drake12c6e2d1999-12-14 21:25:03 +00004161 return result;
4162}
4163#endif
4164
Guido van Rossumad0ee831995-03-01 10:34:45 +00004165#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004166PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004167"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004168Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004169
Barry Warsaw53699e91996-12-10 23:23:01 +00004170static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004171posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004172{
Christian Heimes217cfd12007-12-02 14:31:20 +00004173 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004174}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004175#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004176
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004177
Guido van Rossumad0ee831995-03-01 10:34:45 +00004178#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004179PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004180"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004181Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004182
Barry Warsaw53699e91996-12-10 23:23:01 +00004183static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004184posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004185{
Christian Heimes292d3512008-02-03 16:51:08 +00004186 pid_t pid;
4187 int sig;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004188 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00004189 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004190#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004191 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4192 APIRET rc;
4193 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004194 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004195
4196 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4197 APIRET rc;
4198 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004199 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004200
4201 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004202 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004203#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004204 if (kill(pid, sig) == -1)
4205 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004206#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004207 Py_INCREF(Py_None);
4208 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004209}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004210#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004211
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004212#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004213PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004214"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004215Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004216
4217static PyObject *
4218posix_killpg(PyObject *self, PyObject *args)
4219{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004220 int sig;
4221 pid_t pgid;
4222 /* XXX some man pages make the `pgid` parameter an int, others
4223 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4224 take the same type. Moreover, pid_t is always at least as wide as
4225 int (else compilation of this module fails), which is safe. */
4226 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004227 return NULL;
4228 if (killpg(pgid, sig) == -1)
4229 return posix_error();
4230 Py_INCREF(Py_None);
4231 return Py_None;
4232}
4233#endif
4234
Guido van Rossumc0125471996-06-28 18:55:32 +00004235#ifdef HAVE_PLOCK
4236
4237#ifdef HAVE_SYS_LOCK_H
4238#include <sys/lock.h>
4239#endif
4240
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004241PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004242"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004243Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004244
Barry Warsaw53699e91996-12-10 23:23:01 +00004245static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004246posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004247{
4248 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004249 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004250 return NULL;
4251 if (plock(op) == -1)
4252 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004253 Py_INCREF(Py_None);
4254 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004255}
4256#endif
4257
Guido van Rossumb6775db1994-08-01 11:34:53 +00004258#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004259PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004260"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004261Set the current process's user id.");
4262
Barry Warsaw53699e91996-12-10 23:23:01 +00004263static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004264posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004265{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004266 long uid_arg;
4267 uid_t uid;
4268 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004269 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004270 uid = uid_arg;
4271 if (uid != uid_arg) {
4272 PyErr_SetString(PyExc_OverflowError, "user id too big");
4273 return NULL;
4274 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004275 if (setuid(uid) < 0)
4276 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004277 Py_INCREF(Py_None);
4278 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004279}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004280#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004281
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004282
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004283#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004284PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004285"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004286Set the current process's effective user id.");
4287
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004288static PyObject *
4289posix_seteuid (PyObject *self, PyObject *args)
4290{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004291 long euid_arg;
4292 uid_t euid;
4293 if (!PyArg_ParseTuple(args, "l", &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004294 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004295 euid = euid_arg;
4296 if (euid != euid_arg) {
4297 PyErr_SetString(PyExc_OverflowError, "user id too big");
4298 return NULL;
4299 }
4300 if (seteuid(euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004301 return posix_error();
4302 } else {
4303 Py_INCREF(Py_None);
4304 return Py_None;
4305 }
4306}
4307#endif /* HAVE_SETEUID */
4308
4309#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004310PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004311"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004312Set the current process's effective group id.");
4313
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004314static PyObject *
4315posix_setegid (PyObject *self, PyObject *args)
4316{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004317 long egid_arg;
4318 gid_t egid;
4319 if (!PyArg_ParseTuple(args, "l", &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004320 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004321 egid = egid_arg;
4322 if (egid != egid_arg) {
4323 PyErr_SetString(PyExc_OverflowError, "group id too big");
4324 return NULL;
4325 }
4326 if (setegid(egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004327 return posix_error();
4328 } else {
4329 Py_INCREF(Py_None);
4330 return Py_None;
4331 }
4332}
4333#endif /* HAVE_SETEGID */
4334
4335#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004336PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004337"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004338Set the current process's real and effective user ids.");
4339
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004340static PyObject *
4341posix_setreuid (PyObject *self, PyObject *args)
4342{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004343 long ruid_arg, euid_arg;
4344 uid_t ruid, euid;
4345 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004346 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004347 ruid = ruid_arg;
4348 euid = euid_arg;
4349 if (euid != euid_arg || ruid != ruid_arg) {
4350 PyErr_SetString(PyExc_OverflowError, "user id too big");
4351 return NULL;
4352 }
4353 if (setreuid(ruid, euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004354 return posix_error();
4355 } else {
4356 Py_INCREF(Py_None);
4357 return Py_None;
4358 }
4359}
4360#endif /* HAVE_SETREUID */
4361
4362#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004363PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004364"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004365Set the current process's real and effective group ids.");
4366
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004367static PyObject *
4368posix_setregid (PyObject *self, PyObject *args)
4369{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004370 long rgid_arg, egid_arg;
4371 gid_t rgid, egid;
4372 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004373 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004374 rgid = rgid_arg;
4375 egid = egid_arg;
4376 if (egid != egid_arg || rgid != rgid_arg) {
4377 PyErr_SetString(PyExc_OverflowError, "group id too big");
4378 return NULL;
4379 }
4380 if (setregid(rgid, egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004381 return posix_error();
4382 } else {
4383 Py_INCREF(Py_None);
4384 return Py_None;
4385 }
4386}
4387#endif /* HAVE_SETREGID */
4388
Guido van Rossumb6775db1994-08-01 11:34:53 +00004389#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004390PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004391"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004392Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004393
Barry Warsaw53699e91996-12-10 23:23:01 +00004394static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004395posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004396{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004397 long gid_arg;
4398 gid_t gid;
4399 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004400 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004401 gid = gid_arg;
4402 if (gid != gid_arg) {
4403 PyErr_SetString(PyExc_OverflowError, "group id too big");
4404 return NULL;
4405 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004406 if (setgid(gid) < 0)
4407 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004408 Py_INCREF(Py_None);
4409 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004410}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004411#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004412
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004413#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004414PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004415"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004416Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004417
4418static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004419posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004420{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004421 int i, len;
4422 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004423
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004424 if (!PySequence_Check(groups)) {
4425 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4426 return NULL;
4427 }
4428 len = PySequence_Size(groups);
4429 if (len > MAX_GROUPS) {
4430 PyErr_SetString(PyExc_ValueError, "too many groups");
4431 return NULL;
4432 }
4433 for(i = 0; i < len; i++) {
4434 PyObject *elem;
4435 elem = PySequence_GetItem(groups, i);
4436 if (!elem)
4437 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004438 if (!PyLong_Check(elem)) {
4439 PyErr_SetString(PyExc_TypeError,
4440 "groups must be integers");
4441 Py_DECREF(elem);
4442 return NULL;
4443 } else {
4444 unsigned long x = PyLong_AsUnsignedLong(elem);
4445 if (PyErr_Occurred()) {
4446 PyErr_SetString(PyExc_TypeError,
4447 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004448 Py_DECREF(elem);
4449 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004450 }
Georg Brandla13c2442005-11-22 19:30:31 +00004451 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004452 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004453 if (grouplist[i] != x) {
4454 PyErr_SetString(PyExc_TypeError,
4455 "group id too big");
4456 Py_DECREF(elem);
4457 return NULL;
4458 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004459 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004460 Py_DECREF(elem);
4461 }
4462
4463 if (setgroups(len, grouplist) < 0)
4464 return posix_error();
4465 Py_INCREF(Py_None);
4466 return Py_None;
4467}
4468#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004469
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004470#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4471static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004472wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004473{
4474 PyObject *result;
4475 static PyObject *struct_rusage;
4476
4477 if (pid == -1)
4478 return posix_error();
4479
4480 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004481 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004482 if (m == NULL)
4483 return NULL;
4484 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4485 Py_DECREF(m);
4486 if (struct_rusage == NULL)
4487 return NULL;
4488 }
4489
4490 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4491 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4492 if (!result)
4493 return NULL;
4494
4495#ifndef doubletime
4496#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4497#endif
4498
4499 PyStructSequence_SET_ITEM(result, 0,
4500 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4501 PyStructSequence_SET_ITEM(result, 1,
4502 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4503#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004504 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004505 SET_INT(result, 2, ru->ru_maxrss);
4506 SET_INT(result, 3, ru->ru_ixrss);
4507 SET_INT(result, 4, ru->ru_idrss);
4508 SET_INT(result, 5, ru->ru_isrss);
4509 SET_INT(result, 6, ru->ru_minflt);
4510 SET_INT(result, 7, ru->ru_majflt);
4511 SET_INT(result, 8, ru->ru_nswap);
4512 SET_INT(result, 9, ru->ru_inblock);
4513 SET_INT(result, 10, ru->ru_oublock);
4514 SET_INT(result, 11, ru->ru_msgsnd);
4515 SET_INT(result, 12, ru->ru_msgrcv);
4516 SET_INT(result, 13, ru->ru_nsignals);
4517 SET_INT(result, 14, ru->ru_nvcsw);
4518 SET_INT(result, 15, ru->ru_nivcsw);
4519#undef SET_INT
4520
4521 if (PyErr_Occurred()) {
4522 Py_DECREF(result);
4523 return NULL;
4524 }
4525
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004526 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004527}
4528#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4529
4530#ifdef HAVE_WAIT3
4531PyDoc_STRVAR(posix_wait3__doc__,
4532"wait3(options) -> (pid, status, rusage)\n\n\
4533Wait for completion of a child process.");
4534
4535static PyObject *
4536posix_wait3(PyObject *self, PyObject *args)
4537{
Christian Heimes292d3512008-02-03 16:51:08 +00004538 pid_t pid;
4539 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004540 struct rusage ru;
4541 WAIT_TYPE status;
4542 WAIT_STATUS_INT(status) = 0;
4543
4544 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4545 return NULL;
4546
4547 Py_BEGIN_ALLOW_THREADS
4548 pid = wait3(&status, options, &ru);
4549 Py_END_ALLOW_THREADS
4550
4551 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4552}
4553#endif /* HAVE_WAIT3 */
4554
4555#ifdef HAVE_WAIT4
4556PyDoc_STRVAR(posix_wait4__doc__,
4557"wait4(pid, options) -> (pid, status, rusage)\n\n\
4558Wait for completion of a given child process.");
4559
4560static PyObject *
4561posix_wait4(PyObject *self, PyObject *args)
4562{
Christian Heimes292d3512008-02-03 16:51:08 +00004563 pid_t pid;
4564 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004565 struct rusage ru;
4566 WAIT_TYPE status;
4567 WAIT_STATUS_INT(status) = 0;
4568
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004569 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004570 return NULL;
4571
4572 Py_BEGIN_ALLOW_THREADS
4573 pid = wait4(pid, &status, options, &ru);
4574 Py_END_ALLOW_THREADS
4575
4576 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4577}
4578#endif /* HAVE_WAIT4 */
4579
Guido van Rossumb6775db1994-08-01 11:34:53 +00004580#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004581PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004582"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004583Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004584
Barry Warsaw53699e91996-12-10 23:23:01 +00004585static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004586posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004587{
Christian Heimes292d3512008-02-03 16:51:08 +00004588 pid_t pid;
4589 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004590 WAIT_TYPE status;
4591 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004592
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004593 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004594 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004595 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004596 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004597 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004598 if (pid == -1)
4599 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004600
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004601 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004602}
4603
Tim Petersab034fa2002-02-01 11:27:43 +00004604#elif defined(HAVE_CWAIT)
4605
4606/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004607PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004608"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004609"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004610
4611static PyObject *
4612posix_waitpid(PyObject *self, PyObject *args)
4613{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004614 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004615 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004616
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004617 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Tim Petersab034fa2002-02-01 11:27:43 +00004618 return NULL;
4619 Py_BEGIN_ALLOW_THREADS
4620 pid = _cwait(&status, pid, options);
4621 Py_END_ALLOW_THREADS
4622 if (pid == -1)
4623 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004624
4625 /* shift the status left a byte so this is more like the POSIX waitpid */
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004626 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004627}
4628#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004629
Guido van Rossumad0ee831995-03-01 10:34:45 +00004630#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004631PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004632"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004633Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004634
Barry Warsaw53699e91996-12-10 23:23:01 +00004635static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004636posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004637{
Christian Heimes292d3512008-02-03 16:51:08 +00004638 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004639 WAIT_TYPE status;
4640 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004641
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004642 Py_BEGIN_ALLOW_THREADS
4643 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004644 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004645 if (pid == -1)
4646 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004647
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004648 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004649}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004650#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004651
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004652
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004653PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004654"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004655Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004656
Barry Warsaw53699e91996-12-10 23:23:01 +00004657static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004658posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004659{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004660#ifdef HAVE_LSTAT
Martin v. Löwis011e8422009-05-05 04:43:17 +00004661 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004662#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004663#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00004664 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004665#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00004666 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004667#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004668#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004669}
4670
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004671
Guido van Rossumb6775db1994-08-01 11:34:53 +00004672#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004673PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004674"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004675Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004676
Barry Warsaw53699e91996-12-10 23:23:01 +00004677static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004678posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004679{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004680 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004681 char buf[MAXPATHLEN];
Martin v. Löwis011e8422009-05-05 04:43:17 +00004682 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004683 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004684 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004685 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004686
Martin v. Löwis011e8422009-05-05 04:43:17 +00004687 if (!PyArg_ParseTuple(args, "O&:readlink",
4688 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004689 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004690 path = bytes2str(opath, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004691 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004692 if (v == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00004693 release_bytes(opath);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004694 return NULL;
4695 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004696
4697 if (PyUnicode_Check(v)) {
4698 arg_is_unicode = 1;
4699 }
4700 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004701
Barry Warsaw53699e91996-12-10 23:23:01 +00004702 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004703 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004704 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004705 if (n < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004706 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004707
Martin v. Löwis011e8422009-05-05 04:43:17 +00004708 release_bytes(opath);
Christian Heimes72b710a2008-05-26 13:28:38 +00004709 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004710 if (arg_is_unicode) {
4711 PyObject *w;
4712
4713 w = PyUnicode_FromEncodedObject(v,
4714 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00004715 "surrogateescape");
Thomas Wouters89f507f2006-12-13 04:49:30 +00004716 if (w != NULL) {
4717 Py_DECREF(v);
4718 v = w;
4719 }
4720 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00004721 v = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004722 }
4723 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004724 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004725}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004726#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004727
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004728
Guido van Rossumb6775db1994-08-01 11:34:53 +00004729#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004730PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004731"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004732Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004733
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004734static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004735posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004736{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004737 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004738}
4739#endif /* HAVE_SYMLINK */
4740
4741
4742#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004743#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4744static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004745system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004746{
4747 ULONG value = 0;
4748
4749 Py_BEGIN_ALLOW_THREADS
4750 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4751 Py_END_ALLOW_THREADS
4752
4753 return value;
4754}
4755
4756static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004757posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004758{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004759 /* Currently Only Uptime is Provided -- Others Later */
4760 return Py_BuildValue("ddddd",
4761 (double)0 /* t.tms_utime / HZ */,
4762 (double)0 /* t.tms_stime / HZ */,
4763 (double)0 /* t.tms_cutime / HZ */,
4764 (double)0 /* t.tms_cstime / HZ */,
4765 (double)system_uptime() / 1000);
4766}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004767#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004768#define NEED_TICKS_PER_SECOND
4769static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004770static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004771posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004772{
4773 struct tms t;
4774 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004775 errno = 0;
4776 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004777 if (c == (clock_t) -1)
4778 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004779 return Py_BuildValue("ddddd",
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004780 (double)t.tms_utime / ticks_per_second,
4781 (double)t.tms_stime / ticks_per_second,
4782 (double)t.tms_cutime / ticks_per_second,
4783 (double)t.tms_cstime / ticks_per_second,
4784 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004785}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004786#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004787#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004788
4789
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004790#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004791#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004792static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004793posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004794{
4795 FILETIME create, exit, kernel, user;
4796 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004797 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004798 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4799 /* The fields of a FILETIME structure are the hi and lo part
4800 of a 64-bit value expressed in 100 nanosecond units.
4801 1e7 is one second in such units; 1e-7 the inverse.
4802 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4803 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004804 return Py_BuildValue(
4805 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004806 (double)(user.dwHighDateTime*429.4967296 +
4807 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004808 (double)(kernel.dwHighDateTime*429.4967296 +
4809 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004810 (double)0,
4811 (double)0,
4812 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004813}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004814#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004815
4816#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004817PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004818"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004819Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004820#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004821
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004822
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004823#ifdef HAVE_GETSID
4824PyDoc_STRVAR(posix_getsid__doc__,
4825"getsid(pid) -> sid\n\n\
4826Call the system call getsid().");
4827
4828static PyObject *
4829posix_getsid(PyObject *self, PyObject *args)
4830{
Christian Heimes292d3512008-02-03 16:51:08 +00004831 pid_t pid;
4832 int sid;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004833 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004834 return NULL;
4835 sid = getsid(pid);
4836 if (sid < 0)
4837 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004838 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004839}
4840#endif /* HAVE_GETSID */
4841
4842
Guido van Rossumb6775db1994-08-01 11:34:53 +00004843#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004844PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004845"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004846Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004847
Barry Warsaw53699e91996-12-10 23:23:01 +00004848static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004849posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004850{
Guido van Rossum687dd131993-05-17 08:34:16 +00004851 if (setsid() < 0)
4852 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004853 Py_INCREF(Py_None);
4854 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004855}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004856#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004857
Guido van Rossumb6775db1994-08-01 11:34:53 +00004858#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004859PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004860"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004861Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004862
Barry Warsaw53699e91996-12-10 23:23:01 +00004863static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004864posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004865{
Christian Heimes292d3512008-02-03 16:51:08 +00004866 pid_t pid;
4867 int pgrp;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004868 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004869 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004870 if (setpgid(pid, pgrp) < 0)
4871 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004872 Py_INCREF(Py_None);
4873 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004874}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004875#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004876
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004877
Guido van Rossumb6775db1994-08-01 11:34:53 +00004878#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004879PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004880"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004881Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004882
Barry Warsaw53699e91996-12-10 23:23:01 +00004883static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004884posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004885{
Christian Heimes15ebc882008-02-04 18:48:49 +00004886 int fd;
4887 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004888 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004889 return NULL;
4890 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004891 if (pgid < 0)
4892 return posix_error();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004893 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004894}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004895#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004896
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004897
Guido van Rossumb6775db1994-08-01 11:34:53 +00004898#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004899PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004900"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004901Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004902
Barry Warsaw53699e91996-12-10 23:23:01 +00004903static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004904posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004905{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004906 int fd;
4907 pid_t pgid;
4908 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004909 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004910 if (tcsetpgrp(fd, pgid) < 0)
4911 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004912 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004913 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004914}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004915#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004916
Guido van Rossum687dd131993-05-17 08:34:16 +00004917/* Functions acting on file descriptors */
4918
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004919PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004920"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004921Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004922
Barry Warsaw53699e91996-12-10 23:23:01 +00004923static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004924posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004925{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004926 PyObject *ofile;
4927 char *file;
Guido van Rossum687dd131993-05-17 08:34:16 +00004928 int flag;
4929 int mode = 0777;
4930 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004931
4932#ifdef MS_WINDOWS
4933 if (unicode_file_names()) {
4934 PyUnicodeObject *po;
4935 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4936 Py_BEGIN_ALLOW_THREADS
4937 /* PyUnicode_AS_UNICODE OK without thread
4938 lock as it is a simple dereference. */
4939 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4940 Py_END_ALLOW_THREADS
4941 if (fd < 0)
4942 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004943 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004944 }
4945 /* Drop the argument parsing error as narrow strings
4946 are also valid. */
4947 PyErr_Clear();
4948 }
4949#endif
4950
Martin v. Löwis011e8422009-05-05 04:43:17 +00004951 if (!PyArg_ParseTuple(args, "O&i|i",
4952 PyUnicode_FSConverter, &ofile,
Mark Hammondef8b6542001-05-13 08:04:26 +00004953 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004954 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004955 file = bytes2str(ofile, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00004956 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004957 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004958 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004959 if (fd < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004960 return posix_error_with_allocated_filename(ofile);
4961 release_bytes(ofile);
Christian Heimes217cfd12007-12-02 14:31:20 +00004962 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004963}
4964
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004965
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004966PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004967"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004968Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004969
Barry Warsaw53699e91996-12-10 23:23:01 +00004970static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004971posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004972{
4973 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004974 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004975 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004976 if (!_PyVerify_fd(fd))
4977 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004978 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004979 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004980 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004981 if (res < 0)
4982 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004983 Py_INCREF(Py_None);
4984 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004985}
4986
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004987
Christian Heimesfdab48e2008-01-20 09:06:41 +00004988PyDoc_STRVAR(posix_closerange__doc__,
4989"closerange(fd_low, fd_high)\n\n\
4990Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4991
4992static PyObject *
4993posix_closerange(PyObject *self, PyObject *args)
4994{
4995 int fd_from, fd_to, i;
4996 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4997 return NULL;
4998 Py_BEGIN_ALLOW_THREADS
4999 for (i = fd_from; i < fd_to; i++)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005000 if (_PyVerify_fd(i))
5001 close(i);
Christian Heimesfdab48e2008-01-20 09:06:41 +00005002 Py_END_ALLOW_THREADS
5003 Py_RETURN_NONE;
5004}
5005
5006
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005007PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005008"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005009Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005010
Barry Warsaw53699e91996-12-10 23:23:01 +00005011static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005012posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005013{
5014 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005015 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005016 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005017 if (!_PyVerify_fd(fd))
5018 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005019 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005020 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005021 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005022 if (fd < 0)
5023 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005024 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005025}
5026
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005027
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005028PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005029"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005030Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005031
Barry Warsaw53699e91996-12-10 23:23:01 +00005032static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005033posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005034{
5035 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005036 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005037 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005038 if (!_PyVerify_fd_dup2(fd, fd2))
5039 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005040 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005041 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005042 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005043 if (res < 0)
5044 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005045 Py_INCREF(Py_None);
5046 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005047}
5048
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005049
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005050PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005051"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005052Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005053
Barry Warsaw53699e91996-12-10 23:23:01 +00005054static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005055posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005056{
5057 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005058#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005059 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005060#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005061 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005062#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005063 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005064 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005065 return NULL;
5066#ifdef SEEK_SET
5067 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5068 switch (how) {
5069 case 0: how = SEEK_SET; break;
5070 case 1: how = SEEK_CUR; break;
5071 case 2: how = SEEK_END; break;
5072 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005073#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005074
5075#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005076 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005077#else
5078 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005079 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005080#endif
5081 if (PyErr_Occurred())
5082 return NULL;
5083
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005084 if (!_PyVerify_fd(fd))
5085 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005086 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005087#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005088 res = _lseeki64(fd, pos, how);
5089#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005090 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005091#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005092 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005093 if (res < 0)
5094 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005095
5096#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005097 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005098#else
5099 return PyLong_FromLongLong(res);
5100#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005101}
5102
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005103
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005104PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005105"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005106Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005107
Barry Warsaw53699e91996-12-10 23:23:01 +00005108static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005109posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005110{
Guido van Rossum572dbf82007-04-27 23:53:51 +00005111 int fd, size;
5112 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005113 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005114 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005115 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005116 if (size < 0) {
5117 errno = EINVAL;
5118 return posix_error();
5119 }
Christian Heimes72b710a2008-05-26 13:28:38 +00005120 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005121 if (buffer == NULL)
5122 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005123 if (!_PyVerify_fd(fd))
5124 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005125 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00005126 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005127 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005128 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005129 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005130 return posix_error();
5131 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005132 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00005133 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005134 return buffer;
5135}
5136
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005137
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005138PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005139"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005140Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005141
Barry Warsaw53699e91996-12-10 23:23:01 +00005142static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005143posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005144{
Martin v. Löwis423be952008-08-13 15:53:07 +00005145 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005146 int fd;
5147 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005148
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +00005149 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00005150 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005151 if (!_PyVerify_fd(fd))
5152 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005153 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005154 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00005155 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005156 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00005157 if (size < 0)
5158 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005159 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005160}
5161
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005162
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005163PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005164"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005165Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005166
Barry Warsaw53699e91996-12-10 23:23:01 +00005167static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005168posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005169{
5170 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005171 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005172 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005173 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005174 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005175#ifdef __VMS
5176 /* on OpenVMS we must ensure that all bytes are written to the file */
5177 fsync(fd);
5178#endif
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005179 if (!_PyVerify_fd(fd))
5180 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005181 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005182 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005183 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005184 if (res != 0) {
5185#ifdef MS_WINDOWS
5186 return win32_error("fstat", NULL);
5187#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005188 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005189#endif
5190 }
Tim Peters5aa91602002-01-30 05:46:57 +00005191
Martin v. Löwis14694662006-02-03 12:54:16 +00005192 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005193}
5194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005195PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005196"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005197Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005198connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005199
5200static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005201posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005202{
5203 int fd;
5204 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5205 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005206 if (!_PyVerify_fd(fd))
5207 return PyBool_FromLong(0);
Fred Drake106c1a02002-04-23 15:58:02 +00005208 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005209}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005210
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005211#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005212PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005213"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005214Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005215
Barry Warsaw53699e91996-12-10 23:23:01 +00005216static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005217posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005218{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005219#if defined(PYOS_OS2)
5220 HFILE read, write;
5221 APIRET rc;
5222
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005223 Py_BEGIN_ALLOW_THREADS
5224 rc = DosCreatePipe( &read, &write, 4096);
5225 Py_END_ALLOW_THREADS
5226 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005227 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005228
5229 return Py_BuildValue("(ii)", read, write);
5230#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005231#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005232 int fds[2];
5233 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005234 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005235 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005236 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005237 if (res != 0)
5238 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005239 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005240#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005241 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005242 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005243 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005244 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005245 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005246 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005247 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005248 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005249 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5250 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005251 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005252#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005253#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005254}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005255#endif /* HAVE_PIPE */
5256
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005257
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005258#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005259PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005260"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005261Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005262
Barry Warsaw53699e91996-12-10 23:23:01 +00005263static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005264posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005265{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005266 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005267 int mode = 0666;
5268 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005269 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005270 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005271 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005272 res = mkfifo(filename, mode);
5273 Py_END_ALLOW_THREADS
5274 if (res < 0)
5275 return posix_error();
5276 Py_INCREF(Py_None);
5277 return Py_None;
5278}
5279#endif
5280
5281
Neal Norwitz11690112002-07-30 01:08:28 +00005282#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005283PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005284"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005285Create a filesystem node (file, device special file or named pipe)\n\
5286named filename. mode specifies both the permissions to use and the\n\
5287type of node to be created, being combined (bitwise OR) with one of\n\
5288S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005289device defines the newly created device special file (probably using\n\
5290os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005291
5292
5293static PyObject *
5294posix_mknod(PyObject *self, PyObject *args)
5295{
5296 char *filename;
5297 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005298 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005299 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005300 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005301 return NULL;
5302 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005303 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005304 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005305 if (res < 0)
5306 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005307 Py_INCREF(Py_None);
5308 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005309}
5310#endif
5311
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005312#ifdef HAVE_DEVICE_MACROS
5313PyDoc_STRVAR(posix_major__doc__,
5314"major(device) -> major number\n\
5315Extracts a device major number from a raw device number.");
5316
5317static PyObject *
5318posix_major(PyObject *self, PyObject *args)
5319{
5320 int device;
5321 if (!PyArg_ParseTuple(args, "i:major", &device))
5322 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005323 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005324}
5325
5326PyDoc_STRVAR(posix_minor__doc__,
5327"minor(device) -> minor number\n\
5328Extracts a device minor number from a raw device number.");
5329
5330static PyObject *
5331posix_minor(PyObject *self, PyObject *args)
5332{
5333 int device;
5334 if (!PyArg_ParseTuple(args, "i:minor", &device))
5335 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005336 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005337}
5338
5339PyDoc_STRVAR(posix_makedev__doc__,
5340"makedev(major, minor) -> device number\n\
5341Composes a raw device number from the major and minor device numbers.");
5342
5343static PyObject *
5344posix_makedev(PyObject *self, PyObject *args)
5345{
5346 int major, minor;
5347 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5348 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005349 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005350}
5351#endif /* device macros */
5352
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005353
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005354#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005355PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005356"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005357Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005358
Barry Warsaw53699e91996-12-10 23:23:01 +00005359static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005360posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005361{
5362 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005363 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005364 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005365 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005366
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005367 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005368 return NULL;
5369
5370#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005371 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005372#else
5373 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005374 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005375#endif
5376 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005377 return NULL;
5378
Barry Warsaw53699e91996-12-10 23:23:01 +00005379 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005380 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005381 Py_END_ALLOW_THREADS
Benjamin Peterson9053d752009-01-19 17:53:36 +00005382 if (res < 0)
5383 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005384 Py_INCREF(Py_None);
5385 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005386}
5387#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005388
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005389#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005390PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005391"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005392Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005393
Fred Drake762e2061999-08-26 17:23:54 +00005394/* Save putenv() parameters as values here, so we can collect them when they
5395 * get re-set with another call for the same key. */
5396static PyObject *posix_putenv_garbage;
5397
Tim Peters5aa91602002-01-30 05:46:57 +00005398static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005399posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005400{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005401#ifdef MS_WINDOWS
5402 wchar_t *s1, *s2;
5403 wchar_t *newenv;
5404#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00005405 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005406 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005407 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005408#endif
Fred Drake762e2061999-08-26 17:23:54 +00005409 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005410 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005411
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005412#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00005413 if (!PyArg_ParseTuple(args,
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005414 "uu:putenv",
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005415 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005416 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005417#else
5418 if (!PyArg_ParseTuple(args,
5419 "O&O&:putenv",
5420 PyUnicode_FSConverter, &os1,
5421 PyUnicode_FSConverter, &os2))
5422 return NULL;
5423 s1 = bytes2str(os1, 1);
5424 s2 = bytes2str(os2, 1);
5425#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005426
5427#if defined(PYOS_OS2)
5428 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5429 APIRET rc;
5430
Guido van Rossumd48f2521997-12-05 22:19:34 +00005431 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5432 if (rc != NO_ERROR)
5433 return os2_error(rc);
5434
5435 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5436 APIRET rc;
5437
Guido van Rossumd48f2521997-12-05 22:19:34 +00005438 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5439 if (rc != NO_ERROR)
5440 return os2_error(rc);
5441 } else {
5442#endif
Fred Drake762e2061999-08-26 17:23:54 +00005443 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005444 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005445 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005446#ifdef MS_WINDOWS
5447 len = wcslen(s1) + wcslen(s2) + 2;
5448 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5449#else
5450 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005451 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005452#endif
Fred Drake762e2061999-08-26 17:23:54 +00005453 if (newstr == NULL)
5454 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005455#ifdef MS_WINDOWS
5456 newenv = PyUnicode_AsUnicode(newstr);
5457 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5458 if (_wputenv(newenv)) {
5459 Py_DECREF(newstr);
5460 posix_error();
5461 return NULL;
5462 }
5463#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005464 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005465 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5466 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005467 Py_DECREF(newstr);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005468 release_bytes(os1);
5469 release_bytes(os2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005470 posix_error();
5471 return NULL;
5472 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005473#endif
Fred Drake762e2061999-08-26 17:23:54 +00005474 /* Install the first arg and newstr in posix_putenv_garbage;
5475 * this will cause previous value to be collected. This has to
5476 * happen after the real putenv() call because the old value
5477 * was still accessible until then. */
5478 if (PyDict_SetItem(posix_putenv_garbage,
5479 PyTuple_GET_ITEM(args, 0), newstr)) {
5480 /* really not much we can do; just leak */
5481 PyErr_Clear();
5482 }
5483 else {
5484 Py_DECREF(newstr);
5485 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005486
5487#if defined(PYOS_OS2)
5488 }
5489#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00005490#ifndef MS_WINDOWS
5491 release_bytes(os1);
5492 release_bytes(os2);
5493#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005494 Py_INCREF(Py_None);
5495 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005496}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005497#endif /* putenv */
5498
Guido van Rossumc524d952001-10-19 01:31:59 +00005499#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005500PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005501"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005502Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005503
5504static PyObject *
5505posix_unsetenv(PyObject *self, PyObject *args)
5506{
5507 char *s1;
5508
5509 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5510 return NULL;
5511
5512 unsetenv(s1);
5513
5514 /* Remove the key from posix_putenv_garbage;
5515 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005516 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005517 * old value was still accessible until then.
5518 */
5519 if (PyDict_DelItem(posix_putenv_garbage,
5520 PyTuple_GET_ITEM(args, 0))) {
5521 /* really not much we can do; just leak */
5522 PyErr_Clear();
5523 }
5524
5525 Py_INCREF(Py_None);
5526 return Py_None;
5527}
5528#endif /* unsetenv */
5529
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005530PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005531"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005532Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005533
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005534static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005535posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005536{
5537 int code;
5538 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005539 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005540 return NULL;
5541 message = strerror(code);
5542 if (message == NULL) {
5543 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005544 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005545 return NULL;
5546 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005547 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005548}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005549
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005550
Guido van Rossumc9641791998-08-04 15:26:23 +00005551#ifdef HAVE_SYS_WAIT_H
5552
Fred Drake106c1a02002-04-23 15:58:02 +00005553#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005554PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005555"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005556Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005557
5558static PyObject *
5559posix_WCOREDUMP(PyObject *self, PyObject *args)
5560{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005561 WAIT_TYPE status;
5562 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005563
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005564 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005565 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005566
5567 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005568}
5569#endif /* WCOREDUMP */
5570
5571#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005572PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005573"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005574Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005575job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005576
5577static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005578posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005579{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005580 WAIT_TYPE status;
5581 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005582
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005583 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005584 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005585
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005586 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005587}
5588#endif /* WIFCONTINUED */
5589
Guido van Rossumc9641791998-08-04 15:26:23 +00005590#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005591PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005592"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005593Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005594
5595static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005596posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005597{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005598 WAIT_TYPE status;
5599 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005600
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005601 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005602 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005603
Fred Drake106c1a02002-04-23 15:58:02 +00005604 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005605}
5606#endif /* WIFSTOPPED */
5607
5608#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005609PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005610"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005611Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005612
5613static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005614posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005615{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005616 WAIT_TYPE status;
5617 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005618
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005619 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005620 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005621
Fred Drake106c1a02002-04-23 15:58:02 +00005622 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005623}
5624#endif /* WIFSIGNALED */
5625
5626#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005627PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005628"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005629Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005630system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005631
5632static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005633posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005634{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005635 WAIT_TYPE status;
5636 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005637
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005638 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005639 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005640
Fred Drake106c1a02002-04-23 15:58:02 +00005641 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005642}
5643#endif /* WIFEXITED */
5644
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005645#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005646PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005647"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005648Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005649
5650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005651posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005652{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005653 WAIT_TYPE status;
5654 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005655
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005656 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005657 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005658
Guido van Rossumc9641791998-08-04 15:26:23 +00005659 return Py_BuildValue("i", WEXITSTATUS(status));
5660}
5661#endif /* WEXITSTATUS */
5662
5663#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005664PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005665"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005666Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005667value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005668
5669static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005670posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005671{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005672 WAIT_TYPE status;
5673 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005674
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005675 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005676 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005677
Guido van Rossumc9641791998-08-04 15:26:23 +00005678 return Py_BuildValue("i", WTERMSIG(status));
5679}
5680#endif /* WTERMSIG */
5681
5682#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005683PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005684"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005685Return the signal that stopped the process that provided\n\
5686the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005687
5688static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005689posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005690{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005691 WAIT_TYPE status;
5692 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005693
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005694 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005695 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005696
Guido van Rossumc9641791998-08-04 15:26:23 +00005697 return Py_BuildValue("i", WSTOPSIG(status));
5698}
5699#endif /* WSTOPSIG */
5700
5701#endif /* HAVE_SYS_WAIT_H */
5702
5703
Thomas Wouters477c8d52006-05-27 19:21:47 +00005704#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005705#ifdef _SCO_DS
5706/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5707 needed definitions in sys/statvfs.h */
5708#define _SVID3
5709#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005710#include <sys/statvfs.h>
5711
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005712static PyObject*
5713_pystatvfs_fromstructstatvfs(struct statvfs st) {
5714 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5715 if (v == NULL)
5716 return NULL;
5717
5718#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005719 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5720 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5721 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5722 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5723 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5724 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5725 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5726 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5727 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5728 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005729#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005730 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5731 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005732 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005733 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005734 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005735 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005736 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005737 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005738 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005739 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005740 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005741 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005742 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005743 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005744 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5745 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005746#endif
5747
5748 return v;
5749}
5750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005751PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005752"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005753Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005754
5755static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005756posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005757{
5758 int fd, res;
5759 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005760
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005761 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005762 return NULL;
5763 Py_BEGIN_ALLOW_THREADS
5764 res = fstatvfs(fd, &st);
5765 Py_END_ALLOW_THREADS
5766 if (res != 0)
5767 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005768
5769 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005770}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005771#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005772
5773
Thomas Wouters477c8d52006-05-27 19:21:47 +00005774#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005775#include <sys/statvfs.h>
5776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005777PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005778"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005779Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005780
5781static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005782posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005783{
5784 char *path;
5785 int res;
5786 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005787 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005788 return NULL;
5789 Py_BEGIN_ALLOW_THREADS
5790 res = statvfs(path, &st);
5791 Py_END_ALLOW_THREADS
5792 if (res != 0)
5793 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005794
5795 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005796}
5797#endif /* HAVE_STATVFS */
5798
Fred Drakec9680921999-12-13 16:37:25 +00005799/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5800 * It maps strings representing configuration variable names to
5801 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005802 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005803 * rarely-used constants. There are three separate tables that use
5804 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005805 *
5806 * This code is always included, even if none of the interfaces that
5807 * need it are included. The #if hackery needed to avoid it would be
5808 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005809 */
5810struct constdef {
5811 char *name;
5812 long value;
5813};
5814
Fred Drake12c6e2d1999-12-14 21:25:03 +00005815static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005816conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005817 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005818{
Christian Heimes217cfd12007-12-02 14:31:20 +00005819 if (PyLong_Check(arg)) {
5820 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005821 return 1;
5822 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005823 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005824 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005825 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005826 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005827 size_t hi = tablesize;
5828 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005829 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005830 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005831 PyErr_SetString(PyExc_TypeError,
5832 "configuration names must be strings or integers");
5833 return 0;
5834 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005835 confname = _PyUnicode_AsString(arg);
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005836 if (confname == NULL)
5837 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005838 while (lo < hi) {
5839 mid = (lo + hi) / 2;
5840 cmp = strcmp(confname, table[mid].name);
5841 if (cmp < 0)
5842 hi = mid;
5843 else if (cmp > 0)
5844 lo = mid + 1;
5845 else {
5846 *valuep = table[mid].value;
5847 return 1;
5848 }
5849 }
5850 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005851 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005852 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005853}
5854
5855
5856#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5857static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005858#ifdef _PC_ABI_AIO_XFER_MAX
5859 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5860#endif
5861#ifdef _PC_ABI_ASYNC_IO
5862 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5863#endif
Fred Drakec9680921999-12-13 16:37:25 +00005864#ifdef _PC_ASYNC_IO
5865 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5866#endif
5867#ifdef _PC_CHOWN_RESTRICTED
5868 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5869#endif
5870#ifdef _PC_FILESIZEBITS
5871 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5872#endif
5873#ifdef _PC_LAST
5874 {"PC_LAST", _PC_LAST},
5875#endif
5876#ifdef _PC_LINK_MAX
5877 {"PC_LINK_MAX", _PC_LINK_MAX},
5878#endif
5879#ifdef _PC_MAX_CANON
5880 {"PC_MAX_CANON", _PC_MAX_CANON},
5881#endif
5882#ifdef _PC_MAX_INPUT
5883 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5884#endif
5885#ifdef _PC_NAME_MAX
5886 {"PC_NAME_MAX", _PC_NAME_MAX},
5887#endif
5888#ifdef _PC_NO_TRUNC
5889 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5890#endif
5891#ifdef _PC_PATH_MAX
5892 {"PC_PATH_MAX", _PC_PATH_MAX},
5893#endif
5894#ifdef _PC_PIPE_BUF
5895 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5896#endif
5897#ifdef _PC_PRIO_IO
5898 {"PC_PRIO_IO", _PC_PRIO_IO},
5899#endif
5900#ifdef _PC_SOCK_MAXBUF
5901 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5902#endif
5903#ifdef _PC_SYNC_IO
5904 {"PC_SYNC_IO", _PC_SYNC_IO},
5905#endif
5906#ifdef _PC_VDISABLE
5907 {"PC_VDISABLE", _PC_VDISABLE},
5908#endif
5909};
5910
Fred Drakec9680921999-12-13 16:37:25 +00005911static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005912conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005913{
5914 return conv_confname(arg, valuep, posix_constants_pathconf,
5915 sizeof(posix_constants_pathconf)
5916 / sizeof(struct constdef));
5917}
5918#endif
5919
5920#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005921PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005922"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005923Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005924If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005925
5926static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005927posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005928{
5929 PyObject *result = NULL;
5930 int name, fd;
5931
Fred Drake12c6e2d1999-12-14 21:25:03 +00005932 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5933 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005934 long limit;
5935
5936 errno = 0;
5937 limit = fpathconf(fd, name);
5938 if (limit == -1 && errno != 0)
5939 posix_error();
5940 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005941 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005942 }
5943 return result;
5944}
5945#endif
5946
5947
5948#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005949PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005950"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005951Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005952If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005953
5954static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005955posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005956{
5957 PyObject *result = NULL;
5958 int name;
5959 char *path;
5960
5961 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5962 conv_path_confname, &name)) {
5963 long limit;
5964
5965 errno = 0;
5966 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005967 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005968 if (errno == EINVAL)
5969 /* could be a path or name problem */
5970 posix_error();
5971 else
5972 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005973 }
Fred Drakec9680921999-12-13 16:37:25 +00005974 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005975 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005976 }
5977 return result;
5978}
5979#endif
5980
5981#ifdef HAVE_CONFSTR
5982static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005983#ifdef _CS_ARCHITECTURE
5984 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5985#endif
5986#ifdef _CS_HOSTNAME
5987 {"CS_HOSTNAME", _CS_HOSTNAME},
5988#endif
5989#ifdef _CS_HW_PROVIDER
5990 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5991#endif
5992#ifdef _CS_HW_SERIAL
5993 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5994#endif
5995#ifdef _CS_INITTAB_NAME
5996 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5997#endif
Fred Drakec9680921999-12-13 16:37:25 +00005998#ifdef _CS_LFS64_CFLAGS
5999 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6000#endif
6001#ifdef _CS_LFS64_LDFLAGS
6002 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6003#endif
6004#ifdef _CS_LFS64_LIBS
6005 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6006#endif
6007#ifdef _CS_LFS64_LINTFLAGS
6008 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6009#endif
6010#ifdef _CS_LFS_CFLAGS
6011 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6012#endif
6013#ifdef _CS_LFS_LDFLAGS
6014 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6015#endif
6016#ifdef _CS_LFS_LIBS
6017 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6018#endif
6019#ifdef _CS_LFS_LINTFLAGS
6020 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6021#endif
Fred Draked86ed291999-12-15 15:34:33 +00006022#ifdef _CS_MACHINE
6023 {"CS_MACHINE", _CS_MACHINE},
6024#endif
Fred Drakec9680921999-12-13 16:37:25 +00006025#ifdef _CS_PATH
6026 {"CS_PATH", _CS_PATH},
6027#endif
Fred Draked86ed291999-12-15 15:34:33 +00006028#ifdef _CS_RELEASE
6029 {"CS_RELEASE", _CS_RELEASE},
6030#endif
6031#ifdef _CS_SRPC_DOMAIN
6032 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6033#endif
6034#ifdef _CS_SYSNAME
6035 {"CS_SYSNAME", _CS_SYSNAME},
6036#endif
6037#ifdef _CS_VERSION
6038 {"CS_VERSION", _CS_VERSION},
6039#endif
Fred Drakec9680921999-12-13 16:37:25 +00006040#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6041 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6042#endif
6043#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6044 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6045#endif
6046#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6047 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6048#endif
6049#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6050 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6051#endif
6052#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6053 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6054#endif
6055#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6056 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6057#endif
6058#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6059 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6060#endif
6061#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6062 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6063#endif
6064#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6065 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6066#endif
6067#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6068 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6069#endif
6070#ifdef _CS_XBS5_LP64_OFF64_LIBS
6071 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6072#endif
6073#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6074 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6075#endif
6076#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6077 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6078#endif
6079#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6080 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6081#endif
6082#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6083 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6084#endif
6085#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6086 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6087#endif
Fred Draked86ed291999-12-15 15:34:33 +00006088#ifdef _MIPS_CS_AVAIL_PROCESSORS
6089 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6090#endif
6091#ifdef _MIPS_CS_BASE
6092 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6093#endif
6094#ifdef _MIPS_CS_HOSTID
6095 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6096#endif
6097#ifdef _MIPS_CS_HW_NAME
6098 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6099#endif
6100#ifdef _MIPS_CS_NUM_PROCESSORS
6101 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6102#endif
6103#ifdef _MIPS_CS_OSREL_MAJ
6104 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6105#endif
6106#ifdef _MIPS_CS_OSREL_MIN
6107 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6108#endif
6109#ifdef _MIPS_CS_OSREL_PATCH
6110 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6111#endif
6112#ifdef _MIPS_CS_OS_NAME
6113 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6114#endif
6115#ifdef _MIPS_CS_OS_PROVIDER
6116 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6117#endif
6118#ifdef _MIPS_CS_PROCESSORS
6119 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6120#endif
6121#ifdef _MIPS_CS_SERIAL
6122 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6123#endif
6124#ifdef _MIPS_CS_VENDOR
6125 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6126#endif
Fred Drakec9680921999-12-13 16:37:25 +00006127};
6128
6129static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006130conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006131{
6132 return conv_confname(arg, valuep, posix_constants_confstr,
6133 sizeof(posix_constants_confstr)
6134 / sizeof(struct constdef));
6135}
6136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006137PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006138"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006139Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006140
6141static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006142posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006143{
6144 PyObject *result = NULL;
6145 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006146 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006147
6148 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006149 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006150
Fred Drakec9680921999-12-13 16:37:25 +00006151 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006152 len = confstr(name, buffer, sizeof(buffer));
6153 if (len == 0) {
6154 if (errno) {
6155 posix_error();
6156 }
6157 else {
6158 result = Py_None;
6159 Py_INCREF(Py_None);
6160 }
Fred Drakec9680921999-12-13 16:37:25 +00006161 }
6162 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006163 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006164 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006165 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006166 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006167 }
6168 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006169 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006170 }
6171 }
6172 return result;
6173}
6174#endif
6175
6176
6177#ifdef HAVE_SYSCONF
6178static struct constdef posix_constants_sysconf[] = {
6179#ifdef _SC_2_CHAR_TERM
6180 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6181#endif
6182#ifdef _SC_2_C_BIND
6183 {"SC_2_C_BIND", _SC_2_C_BIND},
6184#endif
6185#ifdef _SC_2_C_DEV
6186 {"SC_2_C_DEV", _SC_2_C_DEV},
6187#endif
6188#ifdef _SC_2_C_VERSION
6189 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6190#endif
6191#ifdef _SC_2_FORT_DEV
6192 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6193#endif
6194#ifdef _SC_2_FORT_RUN
6195 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6196#endif
6197#ifdef _SC_2_LOCALEDEF
6198 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6199#endif
6200#ifdef _SC_2_SW_DEV
6201 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6202#endif
6203#ifdef _SC_2_UPE
6204 {"SC_2_UPE", _SC_2_UPE},
6205#endif
6206#ifdef _SC_2_VERSION
6207 {"SC_2_VERSION", _SC_2_VERSION},
6208#endif
Fred Draked86ed291999-12-15 15:34:33 +00006209#ifdef _SC_ABI_ASYNCHRONOUS_IO
6210 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6211#endif
6212#ifdef _SC_ACL
6213 {"SC_ACL", _SC_ACL},
6214#endif
Fred Drakec9680921999-12-13 16:37:25 +00006215#ifdef _SC_AIO_LISTIO_MAX
6216 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6217#endif
Fred Drakec9680921999-12-13 16:37:25 +00006218#ifdef _SC_AIO_MAX
6219 {"SC_AIO_MAX", _SC_AIO_MAX},
6220#endif
6221#ifdef _SC_AIO_PRIO_DELTA_MAX
6222 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6223#endif
6224#ifdef _SC_ARG_MAX
6225 {"SC_ARG_MAX", _SC_ARG_MAX},
6226#endif
6227#ifdef _SC_ASYNCHRONOUS_IO
6228 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6229#endif
6230#ifdef _SC_ATEXIT_MAX
6231 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6232#endif
Fred Draked86ed291999-12-15 15:34:33 +00006233#ifdef _SC_AUDIT
6234 {"SC_AUDIT", _SC_AUDIT},
6235#endif
Fred Drakec9680921999-12-13 16:37:25 +00006236#ifdef _SC_AVPHYS_PAGES
6237 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6238#endif
6239#ifdef _SC_BC_BASE_MAX
6240 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6241#endif
6242#ifdef _SC_BC_DIM_MAX
6243 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6244#endif
6245#ifdef _SC_BC_SCALE_MAX
6246 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6247#endif
6248#ifdef _SC_BC_STRING_MAX
6249 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6250#endif
Fred Draked86ed291999-12-15 15:34:33 +00006251#ifdef _SC_CAP
6252 {"SC_CAP", _SC_CAP},
6253#endif
Fred Drakec9680921999-12-13 16:37:25 +00006254#ifdef _SC_CHARCLASS_NAME_MAX
6255 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6256#endif
6257#ifdef _SC_CHAR_BIT
6258 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6259#endif
6260#ifdef _SC_CHAR_MAX
6261 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6262#endif
6263#ifdef _SC_CHAR_MIN
6264 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6265#endif
6266#ifdef _SC_CHILD_MAX
6267 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6268#endif
6269#ifdef _SC_CLK_TCK
6270 {"SC_CLK_TCK", _SC_CLK_TCK},
6271#endif
6272#ifdef _SC_COHER_BLKSZ
6273 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6274#endif
6275#ifdef _SC_COLL_WEIGHTS_MAX
6276 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6277#endif
6278#ifdef _SC_DCACHE_ASSOC
6279 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6280#endif
6281#ifdef _SC_DCACHE_BLKSZ
6282 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6283#endif
6284#ifdef _SC_DCACHE_LINESZ
6285 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6286#endif
6287#ifdef _SC_DCACHE_SZ
6288 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6289#endif
6290#ifdef _SC_DCACHE_TBLKSZ
6291 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6292#endif
6293#ifdef _SC_DELAYTIMER_MAX
6294 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6295#endif
6296#ifdef _SC_EQUIV_CLASS_MAX
6297 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6298#endif
6299#ifdef _SC_EXPR_NEST_MAX
6300 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6301#endif
6302#ifdef _SC_FSYNC
6303 {"SC_FSYNC", _SC_FSYNC},
6304#endif
6305#ifdef _SC_GETGR_R_SIZE_MAX
6306 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6307#endif
6308#ifdef _SC_GETPW_R_SIZE_MAX
6309 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6310#endif
6311#ifdef _SC_ICACHE_ASSOC
6312 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6313#endif
6314#ifdef _SC_ICACHE_BLKSZ
6315 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6316#endif
6317#ifdef _SC_ICACHE_LINESZ
6318 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6319#endif
6320#ifdef _SC_ICACHE_SZ
6321 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6322#endif
Fred Draked86ed291999-12-15 15:34:33 +00006323#ifdef _SC_INF
6324 {"SC_INF", _SC_INF},
6325#endif
Fred Drakec9680921999-12-13 16:37:25 +00006326#ifdef _SC_INT_MAX
6327 {"SC_INT_MAX", _SC_INT_MAX},
6328#endif
6329#ifdef _SC_INT_MIN
6330 {"SC_INT_MIN", _SC_INT_MIN},
6331#endif
6332#ifdef _SC_IOV_MAX
6333 {"SC_IOV_MAX", _SC_IOV_MAX},
6334#endif
Fred Draked86ed291999-12-15 15:34:33 +00006335#ifdef _SC_IP_SECOPTS
6336 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6337#endif
Fred Drakec9680921999-12-13 16:37:25 +00006338#ifdef _SC_JOB_CONTROL
6339 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6340#endif
Fred Draked86ed291999-12-15 15:34:33 +00006341#ifdef _SC_KERN_POINTERS
6342 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6343#endif
6344#ifdef _SC_KERN_SIM
6345 {"SC_KERN_SIM", _SC_KERN_SIM},
6346#endif
Fred Drakec9680921999-12-13 16:37:25 +00006347#ifdef _SC_LINE_MAX
6348 {"SC_LINE_MAX", _SC_LINE_MAX},
6349#endif
6350#ifdef _SC_LOGIN_NAME_MAX
6351 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6352#endif
6353#ifdef _SC_LOGNAME_MAX
6354 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6355#endif
6356#ifdef _SC_LONG_BIT
6357 {"SC_LONG_BIT", _SC_LONG_BIT},
6358#endif
Fred Draked86ed291999-12-15 15:34:33 +00006359#ifdef _SC_MAC
6360 {"SC_MAC", _SC_MAC},
6361#endif
Fred Drakec9680921999-12-13 16:37:25 +00006362#ifdef _SC_MAPPED_FILES
6363 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6364#endif
6365#ifdef _SC_MAXPID
6366 {"SC_MAXPID", _SC_MAXPID},
6367#endif
6368#ifdef _SC_MB_LEN_MAX
6369 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6370#endif
6371#ifdef _SC_MEMLOCK
6372 {"SC_MEMLOCK", _SC_MEMLOCK},
6373#endif
6374#ifdef _SC_MEMLOCK_RANGE
6375 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6376#endif
6377#ifdef _SC_MEMORY_PROTECTION
6378 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6379#endif
6380#ifdef _SC_MESSAGE_PASSING
6381 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6382#endif
Fred Draked86ed291999-12-15 15:34:33 +00006383#ifdef _SC_MMAP_FIXED_ALIGNMENT
6384 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6385#endif
Fred Drakec9680921999-12-13 16:37:25 +00006386#ifdef _SC_MQ_OPEN_MAX
6387 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6388#endif
6389#ifdef _SC_MQ_PRIO_MAX
6390 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6391#endif
Fred Draked86ed291999-12-15 15:34:33 +00006392#ifdef _SC_NACLS_MAX
6393 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6394#endif
Fred Drakec9680921999-12-13 16:37:25 +00006395#ifdef _SC_NGROUPS_MAX
6396 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6397#endif
6398#ifdef _SC_NL_ARGMAX
6399 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6400#endif
6401#ifdef _SC_NL_LANGMAX
6402 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6403#endif
6404#ifdef _SC_NL_MSGMAX
6405 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6406#endif
6407#ifdef _SC_NL_NMAX
6408 {"SC_NL_NMAX", _SC_NL_NMAX},
6409#endif
6410#ifdef _SC_NL_SETMAX
6411 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6412#endif
6413#ifdef _SC_NL_TEXTMAX
6414 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6415#endif
6416#ifdef _SC_NPROCESSORS_CONF
6417 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6418#endif
6419#ifdef _SC_NPROCESSORS_ONLN
6420 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6421#endif
Fred Draked86ed291999-12-15 15:34:33 +00006422#ifdef _SC_NPROC_CONF
6423 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6424#endif
6425#ifdef _SC_NPROC_ONLN
6426 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6427#endif
Fred Drakec9680921999-12-13 16:37:25 +00006428#ifdef _SC_NZERO
6429 {"SC_NZERO", _SC_NZERO},
6430#endif
6431#ifdef _SC_OPEN_MAX
6432 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6433#endif
6434#ifdef _SC_PAGESIZE
6435 {"SC_PAGESIZE", _SC_PAGESIZE},
6436#endif
6437#ifdef _SC_PAGE_SIZE
6438 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6439#endif
6440#ifdef _SC_PASS_MAX
6441 {"SC_PASS_MAX", _SC_PASS_MAX},
6442#endif
6443#ifdef _SC_PHYS_PAGES
6444 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6445#endif
6446#ifdef _SC_PII
6447 {"SC_PII", _SC_PII},
6448#endif
6449#ifdef _SC_PII_INTERNET
6450 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6451#endif
6452#ifdef _SC_PII_INTERNET_DGRAM
6453 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6454#endif
6455#ifdef _SC_PII_INTERNET_STREAM
6456 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6457#endif
6458#ifdef _SC_PII_OSI
6459 {"SC_PII_OSI", _SC_PII_OSI},
6460#endif
6461#ifdef _SC_PII_OSI_CLTS
6462 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6463#endif
6464#ifdef _SC_PII_OSI_COTS
6465 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6466#endif
6467#ifdef _SC_PII_OSI_M
6468 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6469#endif
6470#ifdef _SC_PII_SOCKET
6471 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6472#endif
6473#ifdef _SC_PII_XTI
6474 {"SC_PII_XTI", _SC_PII_XTI},
6475#endif
6476#ifdef _SC_POLL
6477 {"SC_POLL", _SC_POLL},
6478#endif
6479#ifdef _SC_PRIORITIZED_IO
6480 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6481#endif
6482#ifdef _SC_PRIORITY_SCHEDULING
6483 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6484#endif
6485#ifdef _SC_REALTIME_SIGNALS
6486 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6487#endif
6488#ifdef _SC_RE_DUP_MAX
6489 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6490#endif
6491#ifdef _SC_RTSIG_MAX
6492 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6493#endif
6494#ifdef _SC_SAVED_IDS
6495 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6496#endif
6497#ifdef _SC_SCHAR_MAX
6498 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6499#endif
6500#ifdef _SC_SCHAR_MIN
6501 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6502#endif
6503#ifdef _SC_SELECT
6504 {"SC_SELECT", _SC_SELECT},
6505#endif
6506#ifdef _SC_SEMAPHORES
6507 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6508#endif
6509#ifdef _SC_SEM_NSEMS_MAX
6510 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6511#endif
6512#ifdef _SC_SEM_VALUE_MAX
6513 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6514#endif
6515#ifdef _SC_SHARED_MEMORY_OBJECTS
6516 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6517#endif
6518#ifdef _SC_SHRT_MAX
6519 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6520#endif
6521#ifdef _SC_SHRT_MIN
6522 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6523#endif
6524#ifdef _SC_SIGQUEUE_MAX
6525 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6526#endif
6527#ifdef _SC_SIGRT_MAX
6528 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6529#endif
6530#ifdef _SC_SIGRT_MIN
6531 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6532#endif
Fred Draked86ed291999-12-15 15:34:33 +00006533#ifdef _SC_SOFTPOWER
6534 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6535#endif
Fred Drakec9680921999-12-13 16:37:25 +00006536#ifdef _SC_SPLIT_CACHE
6537 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6538#endif
6539#ifdef _SC_SSIZE_MAX
6540 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6541#endif
6542#ifdef _SC_STACK_PROT
6543 {"SC_STACK_PROT", _SC_STACK_PROT},
6544#endif
6545#ifdef _SC_STREAM_MAX
6546 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6547#endif
6548#ifdef _SC_SYNCHRONIZED_IO
6549 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6550#endif
6551#ifdef _SC_THREADS
6552 {"SC_THREADS", _SC_THREADS},
6553#endif
6554#ifdef _SC_THREAD_ATTR_STACKADDR
6555 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6556#endif
6557#ifdef _SC_THREAD_ATTR_STACKSIZE
6558 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6559#endif
6560#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6561 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6562#endif
6563#ifdef _SC_THREAD_KEYS_MAX
6564 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6565#endif
6566#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6567 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6568#endif
6569#ifdef _SC_THREAD_PRIO_INHERIT
6570 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6571#endif
6572#ifdef _SC_THREAD_PRIO_PROTECT
6573 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6574#endif
6575#ifdef _SC_THREAD_PROCESS_SHARED
6576 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6577#endif
6578#ifdef _SC_THREAD_SAFE_FUNCTIONS
6579 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6580#endif
6581#ifdef _SC_THREAD_STACK_MIN
6582 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6583#endif
6584#ifdef _SC_THREAD_THREADS_MAX
6585 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6586#endif
6587#ifdef _SC_TIMERS
6588 {"SC_TIMERS", _SC_TIMERS},
6589#endif
6590#ifdef _SC_TIMER_MAX
6591 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6592#endif
6593#ifdef _SC_TTY_NAME_MAX
6594 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6595#endif
6596#ifdef _SC_TZNAME_MAX
6597 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6598#endif
6599#ifdef _SC_T_IOV_MAX
6600 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6601#endif
6602#ifdef _SC_UCHAR_MAX
6603 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6604#endif
6605#ifdef _SC_UINT_MAX
6606 {"SC_UINT_MAX", _SC_UINT_MAX},
6607#endif
6608#ifdef _SC_UIO_MAXIOV
6609 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6610#endif
6611#ifdef _SC_ULONG_MAX
6612 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6613#endif
6614#ifdef _SC_USHRT_MAX
6615 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6616#endif
6617#ifdef _SC_VERSION
6618 {"SC_VERSION", _SC_VERSION},
6619#endif
6620#ifdef _SC_WORD_BIT
6621 {"SC_WORD_BIT", _SC_WORD_BIT},
6622#endif
6623#ifdef _SC_XBS5_ILP32_OFF32
6624 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6625#endif
6626#ifdef _SC_XBS5_ILP32_OFFBIG
6627 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6628#endif
6629#ifdef _SC_XBS5_LP64_OFF64
6630 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6631#endif
6632#ifdef _SC_XBS5_LPBIG_OFFBIG
6633 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6634#endif
6635#ifdef _SC_XOPEN_CRYPT
6636 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6637#endif
6638#ifdef _SC_XOPEN_ENH_I18N
6639 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6640#endif
6641#ifdef _SC_XOPEN_LEGACY
6642 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6643#endif
6644#ifdef _SC_XOPEN_REALTIME
6645 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6646#endif
6647#ifdef _SC_XOPEN_REALTIME_THREADS
6648 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6649#endif
6650#ifdef _SC_XOPEN_SHM
6651 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6652#endif
6653#ifdef _SC_XOPEN_UNIX
6654 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6655#endif
6656#ifdef _SC_XOPEN_VERSION
6657 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6658#endif
6659#ifdef _SC_XOPEN_XCU_VERSION
6660 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6661#endif
6662#ifdef _SC_XOPEN_XPG2
6663 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6664#endif
6665#ifdef _SC_XOPEN_XPG3
6666 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6667#endif
6668#ifdef _SC_XOPEN_XPG4
6669 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6670#endif
6671};
6672
6673static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006674conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006675{
6676 return conv_confname(arg, valuep, posix_constants_sysconf,
6677 sizeof(posix_constants_sysconf)
6678 / sizeof(struct constdef));
6679}
6680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006681PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006682"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006683Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006684
6685static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006686posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006687{
6688 PyObject *result = NULL;
6689 int name;
6690
6691 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6692 int value;
6693
6694 errno = 0;
6695 value = sysconf(name);
6696 if (value == -1 && errno != 0)
6697 posix_error();
6698 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006699 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006700 }
6701 return result;
6702}
6703#endif
6704
6705
Fred Drakebec628d1999-12-15 18:31:10 +00006706/* This code is used to ensure that the tables of configuration value names
6707 * are in sorted order as required by conv_confname(), and also to build the
6708 * the exported dictionaries that are used to publish information about the
6709 * names available on the host platform.
6710 *
6711 * Sorting the table at runtime ensures that the table is properly ordered
6712 * when used, even for platforms we're not able to test on. It also makes
6713 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006714 */
Fred Drakebec628d1999-12-15 18:31:10 +00006715
6716static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006717cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006718{
6719 const struct constdef *c1 =
6720 (const struct constdef *) v1;
6721 const struct constdef *c2 =
6722 (const struct constdef *) v2;
6723
6724 return strcmp(c1->name, c2->name);
6725}
6726
6727static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006728setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006729 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006730{
Fred Drakebec628d1999-12-15 18:31:10 +00006731 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006732 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006733
6734 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6735 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006736 if (d == NULL)
6737 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006738
Barry Warsaw3155db32000-04-13 15:20:40 +00006739 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006740 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006741 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6742 Py_XDECREF(o);
6743 Py_DECREF(d);
6744 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006745 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006746 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006747 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006748 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006749}
6750
Fred Drakebec628d1999-12-15 18:31:10 +00006751/* Return -1 on failure, 0 on success. */
6752static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006753setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006754{
6755#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006756 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006757 sizeof(posix_constants_pathconf)
6758 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006759 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006760 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006761#endif
6762#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006763 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006764 sizeof(posix_constants_confstr)
6765 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006766 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006767 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006768#endif
6769#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006770 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006771 sizeof(posix_constants_sysconf)
6772 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006773 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006774 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006775#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006776 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006777}
Fred Draked86ed291999-12-15 15:34:33 +00006778
6779
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006780PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006781"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006782Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006783in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006784
6785static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006786posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006787{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006788 abort();
6789 /*NOTREACHED*/
6790 Py_FatalError("abort() called from Python code didn't abort!");
6791 return NULL;
6792}
Fred Drakebec628d1999-12-15 18:31:10 +00006793
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006794#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006795PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006796"startfile(filepath [, operation]) - Start a file with its associated\n\
6797application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006798\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006799When \"operation\" is not specified or \"open\", this acts like\n\
6800double-clicking the file in Explorer, or giving the file name as an\n\
6801argument to the DOS \"start\" command: the file is opened with whatever\n\
6802application (if any) its extension is associated.\n\
6803When another \"operation\" is given, it specifies what should be done with\n\
6804the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006805\n\
6806startfile returns as soon as the associated application is launched.\n\
6807There is no option to wait for the application to close, and no way\n\
6808to retrieve the application's exit status.\n\
6809\n\
6810The filepath is relative to the current directory. If you want to use\n\
6811an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006812the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006813
6814static PyObject *
6815win32_startfile(PyObject *self, PyObject *args)
6816{
Martin v. Löwis011e8422009-05-05 04:43:17 +00006817 PyObject *ofilepath;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006818 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006819 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006820 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006821
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006822 if (unicode_file_names()) {
6823 PyObject *unipath, *woperation = NULL;
6824 if (!PyArg_ParseTuple(args, "U|s:startfile",
6825 &unipath, &operation)) {
6826 PyErr_Clear();
6827 goto normal;
6828 }
6829
6830
6831 if (operation) {
6832 woperation = PyUnicode_DecodeASCII(operation,
6833 strlen(operation), NULL);
6834 if (!woperation) {
6835 PyErr_Clear();
6836 operation = NULL;
6837 goto normal;
6838 }
6839 }
6840
6841 Py_BEGIN_ALLOW_THREADS
6842 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6843 PyUnicode_AS_UNICODE(unipath),
6844 NULL, NULL, SW_SHOWNORMAL);
6845 Py_END_ALLOW_THREADS
6846
6847 Py_XDECREF(woperation);
6848 if (rc <= (HINSTANCE)32) {
6849 PyObject *errval = win32_error_unicode("startfile",
6850 PyUnicode_AS_UNICODE(unipath));
6851 return errval;
6852 }
6853 Py_INCREF(Py_None);
6854 return Py_None;
6855 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006856
6857normal:
Martin v. Löwis011e8422009-05-05 04:43:17 +00006858 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6859 PyUnicode_FSConverter, &ofilepath,
Georg Brandlf4f44152006-02-18 22:29:33 +00006860 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006861 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006862 filepath = bytes2str(ofilepath, 1);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006863 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006864 rc = ShellExecute((HWND)0, operation, filepath,
6865 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006866 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006867 if (rc <= (HINSTANCE)32) {
6868 PyObject *errval = win32_error("startfile", filepath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006869 release_bytes(ofilepath);
Georg Brandle9f8ec92005-09-25 06:16:40 +00006870 return errval;
6871 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00006872 release_bytes(ofilepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006873 Py_INCREF(Py_None);
6874 return Py_None;
6875}
6876#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006877
Martin v. Löwis438b5342002-12-27 10:16:42 +00006878#ifdef HAVE_GETLOADAVG
6879PyDoc_STRVAR(posix_getloadavg__doc__,
6880"getloadavg() -> (float, float, float)\n\n\
6881Return the number of processes in the system run queue averaged over\n\
6882the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6883was unobtainable");
6884
6885static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006886posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006887{
6888 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006889 if (getloadavg(loadavg, 3)!=3) {
6890 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6891 return NULL;
6892 } else
6893 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6894}
6895#endif
6896
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006897#ifdef MS_WINDOWS
6898
6899PyDoc_STRVAR(win32_urandom__doc__,
6900"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006901Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006902
6903typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6904 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6905 DWORD dwFlags );
6906typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6907 BYTE *pbBuffer );
6908
6909static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006910/* This handle is never explicitly released. Instead, the operating
6911 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006912static HCRYPTPROV hCryptProv = 0;
6913
Tim Peters4ad82172004-08-30 17:02:04 +00006914static PyObject*
6915win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006916{
Tim Petersd3115382004-08-30 17:36:46 +00006917 int howMany;
6918 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006919
Tim Peters4ad82172004-08-30 17:02:04 +00006920 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006921 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006922 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006923 if (howMany < 0)
6924 return PyErr_Format(PyExc_ValueError,
6925 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006926
Tim Peters4ad82172004-08-30 17:02:04 +00006927 if (hCryptProv == 0) {
6928 HINSTANCE hAdvAPI32 = NULL;
6929 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006930
Tim Peters4ad82172004-08-30 17:02:04 +00006931 /* Obtain handle to the DLL containing CryptoAPI
6932 This should not fail */
6933 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6934 if(hAdvAPI32 == NULL)
6935 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006936
Tim Peters4ad82172004-08-30 17:02:04 +00006937 /* Obtain pointers to the CryptoAPI functions
6938 This will fail on some early versions of Win95 */
6939 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6940 hAdvAPI32,
6941 "CryptAcquireContextA");
6942 if (pCryptAcquireContext == NULL)
6943 return PyErr_Format(PyExc_NotImplementedError,
6944 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006945
Tim Peters4ad82172004-08-30 17:02:04 +00006946 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6947 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006948 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006949 return PyErr_Format(PyExc_NotImplementedError,
6950 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006951
Tim Peters4ad82172004-08-30 17:02:04 +00006952 /* Acquire context */
6953 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6954 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6955 return win32_error("CryptAcquireContext", NULL);
6956 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006957
Tim Peters4ad82172004-08-30 17:02:04 +00006958 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006959 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006960 if (result != NULL) {
6961 /* Get random data */
Amaury Forgeot d'Arca05ada32008-07-21 21:13:14 +00006962 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00006963 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006964 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006965 Py_DECREF(result);
6966 return win32_error("CryptGenRandom", NULL);
6967 }
Tim Peters4ad82172004-08-30 17:02:04 +00006968 }
Tim Petersd3115382004-08-30 17:36:46 +00006969 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006970}
6971#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006972
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006973PyDoc_STRVAR(device_encoding__doc__,
6974"device_encoding(fd) -> str\n\n\
6975Return a string describing the encoding of the device\n\
6976if the output is a terminal; else return None.");
6977
6978static PyObject *
6979device_encoding(PyObject *self, PyObject *args)
6980{
6981 int fd;
6982 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6983 return NULL;
Kristján Valur Jónsson649170b2009-03-24 14:15:49 +00006984 if (!_PyVerify_fd(fd) || !isatty(fd)) {
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006985 Py_INCREF(Py_None);
6986 return Py_None;
6987 }
6988#if defined(MS_WINDOWS) || defined(MS_WIN64)
6989 if (fd == 0) {
6990 char buf[100];
6991 sprintf(buf, "cp%d", GetConsoleCP());
6992 return PyUnicode_FromString(buf);
6993 }
6994 if (fd == 1 || fd == 2) {
6995 char buf[100];
6996 sprintf(buf, "cp%d", GetConsoleOutputCP());
6997 return PyUnicode_FromString(buf);
6998 }
6999#elif defined(CODESET)
7000 {
7001 char *codeset = nl_langinfo(CODESET);
Mark Dickinsonda2706b2008-12-11 18:03:03 +00007002 if (codeset != NULL && codeset[0] != 0)
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007003 return PyUnicode_FromString(codeset);
7004 }
7005#endif
7006 Py_INCREF(Py_None);
7007 return Py_None;
7008}
7009
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007010#ifdef __VMS
7011/* Use openssl random routine */
7012#include <openssl/rand.h>
7013PyDoc_STRVAR(vms_urandom__doc__,
7014"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007015Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007016
7017static PyObject*
7018vms_urandom(PyObject *self, PyObject *args)
7019{
7020 int howMany;
7021 PyObject* result;
7022
7023 /* Read arguments */
7024 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7025 return NULL;
7026 if (howMany < 0)
7027 return PyErr_Format(PyExc_ValueError,
7028 "negative argument not allowed");
7029
7030 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00007031 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007032 if (result != NULL) {
7033 /* Get random data */
7034 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00007035 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007036 howMany) < 0) {
7037 Py_DECREF(result);
7038 return PyErr_Format(PyExc_ValueError,
7039 "RAND_pseudo_bytes");
7040 }
7041 }
7042 return result;
7043}
7044#endif
7045
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007046static PyMethodDef posix_methods[] = {
7047 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7048#ifdef HAVE_TTYNAME
7049 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7050#endif
7051 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007052#ifdef HAVE_CHFLAGS
7053 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
7054#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007055 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007056#ifdef HAVE_FCHMOD
7057 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
7058#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007059#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007060 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007061#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007062#ifdef HAVE_LCHMOD
7063 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
7064#endif /* HAVE_LCHMOD */
7065#ifdef HAVE_FCHOWN
7066 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
7067#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007068#ifdef HAVE_LCHFLAGS
7069 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
7070#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007071#ifdef HAVE_LCHOWN
7072 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7073#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007074#ifdef HAVE_CHROOT
7075 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7076#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007077#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007078 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007079#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007080#ifdef HAVE_GETCWD
Guido van Rossumf0af3e32008-10-02 18:55:37 +00007081 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7082 METH_NOARGS, posix_getcwd__doc__},
7083 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7084 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007085#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007086#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007087 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007088#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007089 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7090 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7091 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007092#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007093 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007094#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007095#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007096 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007097#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007098 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7099 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7100 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007101 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007102#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007103 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007104#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007105#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007106 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007107#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007108 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007109#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007110 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007111#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007112 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7113 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7114 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007115#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007116 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007117#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007118 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007119#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007120 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7121 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007122#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007123#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007124 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7125 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007126#if defined(PYOS_OS2)
7127 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7128 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7129#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007130#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007131#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007132 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007133#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007134#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007135 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007136#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007137#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007138 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007139#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007140#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007141 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007142#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007143#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007144 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007145#endif /* HAVE_GETEGID */
7146#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007147 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007148#endif /* HAVE_GETEUID */
7149#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007150 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007151#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007152#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007153 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007154#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007155 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007156#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007157 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007158#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007159#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007160 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007161#endif /* HAVE_GETPPID */
7162#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007163 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007164#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007165#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007166 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007167#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007168#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007169 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007170#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007171#ifdef HAVE_KILLPG
7172 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7173#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007174#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007175 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007176#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007177#ifdef MS_WINDOWS
7178 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7179#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007180#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007181 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007182#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007183#ifdef HAVE_SETEUID
7184 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7185#endif /* HAVE_SETEUID */
7186#ifdef HAVE_SETEGID
7187 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7188#endif /* HAVE_SETEGID */
7189#ifdef HAVE_SETREUID
7190 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7191#endif /* HAVE_SETREUID */
7192#ifdef HAVE_SETREGID
7193 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7194#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007195#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007196 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007197#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007198#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00007199 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007200#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007201#ifdef HAVE_GETPGID
7202 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7203#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007204#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007205 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007206#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007207#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007208 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007209#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007210#ifdef HAVE_WAIT3
7211 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7212#endif /* HAVE_WAIT3 */
7213#ifdef HAVE_WAIT4
7214 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7215#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007216#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007217 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007218#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007219#ifdef HAVE_GETSID
7220 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7221#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007222#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007223 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007224#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007225#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007226 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007227#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007228#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007229 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007230#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007231#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007232 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007233#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007234 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7235 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00007236 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007237 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007238 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7239 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7240 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7241 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7242 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7243 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007244 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007245#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007246 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007247#endif
7248#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007249 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007250#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007251#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007252 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7253#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007254#ifdef HAVE_DEVICE_MACROS
7255 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7256 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7257 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7258#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007259#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007260 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007261#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007262#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007263 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007264#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007265#ifdef HAVE_UNSETENV
7266 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7267#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007268 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007269#ifdef HAVE_FCHDIR
7270 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7271#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007272#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007273 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007274#endif
7275#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007276 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007277#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007278#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007279#ifdef WCOREDUMP
7280 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7281#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007282#ifdef WIFCONTINUED
7283 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7284#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007285#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007286 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007287#endif /* WIFSTOPPED */
7288#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007289 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007290#endif /* WIFSIGNALED */
7291#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007292 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007293#endif /* WIFEXITED */
7294#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007295 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007296#endif /* WEXITSTATUS */
7297#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007298 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007299#endif /* WTERMSIG */
7300#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007301 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007302#endif /* WSTOPSIG */
7303#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007304#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007305 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007306#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007307#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007308 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007309#endif
Fred Drakec9680921999-12-13 16:37:25 +00007310#ifdef HAVE_CONFSTR
7311 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7312#endif
7313#ifdef HAVE_SYSCONF
7314 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7315#endif
7316#ifdef HAVE_FPATHCONF
7317 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7318#endif
7319#ifdef HAVE_PATHCONF
7320 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7321#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007322 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007323#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007324 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7325#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007326#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007327 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007328#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007329 #ifdef MS_WINDOWS
7330 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7331 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007332 #ifdef __VMS
7333 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7334 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007335 {NULL, NULL} /* Sentinel */
7336};
7337
7338
Barry Warsaw4a342091996-12-19 23:50:02 +00007339static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007340ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007341{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007342 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007343}
7344
Guido van Rossumd48f2521997-12-05 22:19:34 +00007345#if defined(PYOS_OS2)
7346/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007347static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007348{
7349 APIRET rc;
7350 ULONG values[QSV_MAX+1];
7351 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007352 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007353
7354 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007355 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007356 Py_END_ALLOW_THREADS
7357
7358 if (rc != NO_ERROR) {
7359 os2_error(rc);
7360 return -1;
7361 }
7362
Fred Drake4d1e64b2002-04-15 19:40:07 +00007363 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7364 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7365 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7366 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7367 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7368 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7369 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007370
7371 switch (values[QSV_VERSION_MINOR]) {
7372 case 0: ver = "2.00"; break;
7373 case 10: ver = "2.10"; break;
7374 case 11: ver = "2.11"; break;
7375 case 30: ver = "3.00"; break;
7376 case 40: ver = "4.00"; break;
7377 case 50: ver = "5.00"; break;
7378 default:
Tim Peters885d4572001-11-28 20:27:42 +00007379 PyOS_snprintf(tmp, sizeof(tmp),
7380 "%d-%d", values[QSV_VERSION_MAJOR],
7381 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007382 ver = &tmp[0];
7383 }
7384
7385 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007386 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007387 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007388
7389 /* Add Indicator of Which Drive was Used to Boot the System */
7390 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7391 tmp[1] = ':';
7392 tmp[2] = '\0';
7393
Fred Drake4d1e64b2002-04-15 19:40:07 +00007394 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007395}
7396#endif
7397
Barry Warsaw4a342091996-12-19 23:50:02 +00007398static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007399all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007400{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007401#ifdef F_OK
7402 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007403#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007404#ifdef R_OK
7405 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007406#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007407#ifdef W_OK
7408 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007409#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007410#ifdef X_OK
7411 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007412#endif
Fred Drakec9680921999-12-13 16:37:25 +00007413#ifdef NGROUPS_MAX
7414 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7415#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007416#ifdef TMP_MAX
7417 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7418#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007419#ifdef WCONTINUED
7420 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7421#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007422#ifdef WNOHANG
7423 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007424#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007425#ifdef WUNTRACED
7426 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7427#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007428#ifdef O_RDONLY
7429 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7430#endif
7431#ifdef O_WRONLY
7432 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7433#endif
7434#ifdef O_RDWR
7435 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7436#endif
7437#ifdef O_NDELAY
7438 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7439#endif
7440#ifdef O_NONBLOCK
7441 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7442#endif
7443#ifdef O_APPEND
7444 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7445#endif
7446#ifdef O_DSYNC
7447 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7448#endif
7449#ifdef O_RSYNC
7450 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7451#endif
7452#ifdef O_SYNC
7453 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7454#endif
7455#ifdef O_NOCTTY
7456 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7457#endif
7458#ifdef O_CREAT
7459 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7460#endif
7461#ifdef O_EXCL
7462 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7463#endif
7464#ifdef O_TRUNC
7465 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7466#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007467#ifdef O_BINARY
7468 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7469#endif
7470#ifdef O_TEXT
7471 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7472#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007473#ifdef O_LARGEFILE
7474 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7475#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007476#ifdef O_SHLOCK
7477 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7478#endif
7479#ifdef O_EXLOCK
7480 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7481#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007482
Tim Peters5aa91602002-01-30 05:46:57 +00007483/* MS Windows */
7484#ifdef O_NOINHERIT
7485 /* Don't inherit in child processes. */
7486 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7487#endif
7488#ifdef _O_SHORT_LIVED
7489 /* Optimize for short life (keep in memory). */
7490 /* MS forgot to define this one with a non-underscore form too. */
7491 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7492#endif
7493#ifdef O_TEMPORARY
7494 /* Automatically delete when last handle is closed. */
7495 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7496#endif
7497#ifdef O_RANDOM
7498 /* Optimize for random access. */
7499 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7500#endif
7501#ifdef O_SEQUENTIAL
7502 /* Optimize for sequential access. */
7503 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7504#endif
7505
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007506/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007507#ifdef O_ASYNC
7508 /* Send a SIGIO signal whenever input or output
7509 becomes available on file descriptor */
7510 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7511#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007512#ifdef O_DIRECT
7513 /* Direct disk access. */
7514 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7515#endif
7516#ifdef O_DIRECTORY
7517 /* Must be a directory. */
7518 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7519#endif
7520#ifdef O_NOFOLLOW
7521 /* Do not follow links. */
7522 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7523#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007524#ifdef O_NOATIME
7525 /* Do not update the access time. */
7526 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7527#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007528
Barry Warsaw5676bd12003-01-07 20:57:09 +00007529 /* These come from sysexits.h */
7530#ifdef EX_OK
7531 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007532#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007533#ifdef EX_USAGE
7534 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007535#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007536#ifdef EX_DATAERR
7537 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007538#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007539#ifdef EX_NOINPUT
7540 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007541#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007542#ifdef EX_NOUSER
7543 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007544#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007545#ifdef EX_NOHOST
7546 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007547#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007548#ifdef EX_UNAVAILABLE
7549 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007550#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007551#ifdef EX_SOFTWARE
7552 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007553#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007554#ifdef EX_OSERR
7555 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007556#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007557#ifdef EX_OSFILE
7558 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007559#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007560#ifdef EX_CANTCREAT
7561 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007562#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007563#ifdef EX_IOERR
7564 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007565#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007566#ifdef EX_TEMPFAIL
7567 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007568#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007569#ifdef EX_PROTOCOL
7570 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007571#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007572#ifdef EX_NOPERM
7573 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007574#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007575#ifdef EX_CONFIG
7576 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007577#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007578#ifdef EX_NOTFOUND
7579 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007580#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007581
Guido van Rossum246bc171999-02-01 23:54:31 +00007582#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007583#if defined(PYOS_OS2) && defined(PYCC_GCC)
7584 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7585 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7586 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7587 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7588 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7589 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7590 if (ins(d, "P_PM", (long)P_PM)) return -1;
7591 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7592 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7593 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7594 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7595 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7596 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7597 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7598 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7599 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7600 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7601 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7602 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7603 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7604#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007605 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7606 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7607 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7608 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7609 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007610#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007611#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007612
Guido van Rossumd48f2521997-12-05 22:19:34 +00007613#if defined(PYOS_OS2)
7614 if (insertvalues(d)) return -1;
7615#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007616 return 0;
7617}
7618
7619
Tim Peters5aa91602002-01-30 05:46:57 +00007620#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007621#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007622#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007623
7624#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007625#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007626#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007627
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007628#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007629#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007630#define MODNAME "posix"
7631#endif
7632
Martin v. Löwis1a214512008-06-11 05:26:20 +00007633static struct PyModuleDef posixmodule = {
7634 PyModuleDef_HEAD_INIT,
7635 MODNAME,
7636 posix__doc__,
7637 -1,
7638 posix_methods,
7639 NULL,
7640 NULL,
7641 NULL,
7642 NULL
7643};
7644
7645
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007646PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007647INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007648{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007649 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007650
Martin v. Löwis1a214512008-06-11 05:26:20 +00007651 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007652 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007653 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007654
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007655 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007656 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007657 Py_XINCREF(v);
7658 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007659 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007660 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007661
Fred Drake4d1e64b2002-04-15 19:40:07 +00007662 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007663 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007664
Fred Drake4d1e64b2002-04-15 19:40:07 +00007665 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007666 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007667
Fred Drake4d1e64b2002-04-15 19:40:07 +00007668 Py_INCREF(PyExc_OSError);
7669 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007670
Guido van Rossumb3d39562000-01-31 18:41:26 +00007671#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007672 if (posix_putenv_garbage == NULL)
7673 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007674#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007675
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007676 if (!initialized) {
7677 stat_result_desc.name = MODNAME ".stat_result";
7678 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7679 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7680 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7681 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7682 structseq_new = StatResultType.tp_new;
7683 StatResultType.tp_new = statresult_new;
7684
7685 statvfs_result_desc.name = MODNAME ".statvfs_result";
7686 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007687#ifdef NEED_TICKS_PER_SECOND
7688# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7689 ticks_per_second = sysconf(_SC_CLK_TCK);
7690# elif defined(HZ)
7691 ticks_per_second = HZ;
7692# else
7693 ticks_per_second = 60; /* magic fallback value; may be bogus */
7694# endif
7695#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007696 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007697 Py_INCREF((PyObject*) &StatResultType);
7698 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007699 Py_INCREF((PyObject*) &StatVFSResultType);
7700 PyModule_AddObject(m, "statvfs_result",
7701 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007702 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007703
7704#ifdef __APPLE__
7705 /*
7706 * Step 2 of weak-linking support on Mac OS X.
7707 *
7708 * The code below removes functions that are not available on the
7709 * currently active platform.
7710 *
7711 * This block allow one to use a python binary that was build on
7712 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7713 * OSX 10.4.
7714 */
7715#ifdef HAVE_FSTATVFS
7716 if (fstatvfs == NULL) {
7717 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007718 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007719 }
7720 }
7721#endif /* HAVE_FSTATVFS */
7722
7723#ifdef HAVE_STATVFS
7724 if (statvfs == NULL) {
7725 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007726 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007727 }
7728 }
7729#endif /* HAVE_STATVFS */
7730
7731# ifdef HAVE_LCHOWN
7732 if (lchown == NULL) {
7733 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007734 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007735 }
7736 }
7737#endif /* HAVE_LCHOWN */
7738
7739
7740#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007741 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007742
Guido van Rossumb6775db1994-08-01 11:34:53 +00007743}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007744
7745#ifdef __cplusplus
7746}
7747#endif