blob: 1a2c35d6b06d7e04d5829029ba31d1761bc539b4 [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
Greg Wardb48bc172000-03-01 21:51:56 +0000306/* Don't use the "_r" form if we don't need it (also, won't have a
307 prototype for it, at least on Solaris -- maybe others as well?). */
308#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
309#define USE_CTERMID_R
310#endif
311
Fred Drake699f3522000-06-29 21:12:41 +0000312/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000313#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000314#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000315# define STAT win32_stat
316# define FSTAT win32_fstat
317# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000318#else
319# define STAT stat
320# define FSTAT fstat
321# define STRUCT_STAT struct stat
322#endif
323
Tim Peters11b23062003-04-23 02:39:17 +0000324#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000325#include <sys/mkdev.h>
326#else
327#if defined(MAJOR_IN_SYSMACROS)
328#include <sys/sysmacros.h>
329#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000330#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
331#include <sys/mkdev.h>
332#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000333#endif
Fred Drake699f3522000-06-29 21:12:41 +0000334
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000335#if defined _MSC_VER && _MSC_VER >= 1400
336/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
337 * valid and throw an assertion if it isn't.
338 * Normally, an invalid fd is likely to be a C program error and therefore
339 * an assertion can be useful, but it does contradict the POSIX standard
340 * which for write(2) states:
341 * "Otherwise, -1 shall be returned and errno set to indicate the error."
342 * "[EBADF] The fildes argument is not a valid file descriptor open for
343 * writing."
344 * Furthermore, python allows the user to enter any old integer
345 * as a fd and should merely raise a python exception on error.
346 * The Microsoft CRT doesn't provide an official way to check for the
347 * validity of a file descriptor, but we can emulate its internal behaviour
348 * by using the exported __pinfo data member and knowledge of the
349 * internal structures involved.
350 * The structures below must be updated for each version of visual studio
351 * according to the file internal.h in the CRT source, until MS comes
352 * up with a less hacky way to do this.
353 * (all of this is to avoid globally modifying the CRT behaviour using
354 * _set_invalid_parameter_handler() and _CrtSetReportMode())
355 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000356/* The actual size of the structure is determined at runtime.
357 * Only the first items must be present.
358 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000359typedef struct {
360 intptr_t osfhnd;
361 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000362} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000363
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000364extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000365#define IOINFO_L2E 5
366#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
367#define IOINFO_ARRAYS 64
368#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
369#define FOPEN 0x01
370#define _NO_CONSOLE_FILENO (intptr_t)-2
371
372/* This function emulates what the windows CRT does to validate file handles */
373int
374_PyVerify_fd(int fd)
375{
376 const int i1 = fd >> IOINFO_L2E;
377 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000378
379 static int sizeof_ioinfo = 0;
380
381 /* Determine the actual size of the ioinfo structure,
382 * as used by the CRT loaded in memory
383 */
384 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
385 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
386 }
387 if (sizeof_ioinfo == 0) {
388 /* This should not happen... */
389 goto fail;
390 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000391
392 /* See that it isn't a special CLEAR fileno */
393 if (fd != _NO_CONSOLE_FILENO) {
394 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
395 * we check pointer validity and other info
396 */
397 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
398 /* finally, check that the file is open */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000399 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
400 if (info->osfile & FOPEN) {
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000401 return 1;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000402 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000403 }
404 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000405 fail:
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000406 errno = EBADF;
407 return 0;
408}
409
410/* the special case of checking dup2. The target fd must be in a sensible range */
411static int
412_PyVerify_fd_dup2(int fd1, int fd2)
413{
414 if (!_PyVerify_fd(fd1))
415 return 0;
416 if (fd2 == _NO_CONSOLE_FILENO)
417 return 0;
418 if ((unsigned)fd2 < _NHANDLE_)
419 return 1;
420 else
421 return 0;
422}
423#else
424/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
425#define _PyVerify_fd_dup2(A, B) (1)
426#endif
427
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000429#ifdef WITH_NEXT_FRAMEWORK
430/* On Darwin/MacOSX a shared library or framework has no access to
431** environ directly, we must obtain it with _NSGetEnviron().
432*/
433#include <crt_externs.h>
434static char **environ;
435#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000437#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438
Barry Warsaw53699e91996-12-10 23:23:01 +0000439static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000440convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
Barry Warsaw53699e91996-12-10 23:23:01 +0000442 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000443#ifdef MS_WINDOWS
444 wchar_t **e;
445#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000447#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000448 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449 if (d == NULL)
450 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000451#ifdef WITH_NEXT_FRAMEWORK
452 if (environ == NULL)
453 environ = *_NSGetEnviron();
454#endif
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000455#ifdef MS_WINDOWS
456 /* _wenviron must be initialized in this way if the program is started
457 through main() instead of wmain(). */
458 _wgetenv(L"");
459 if (_wenviron == NULL)
460 return d;
461 /* This part ignores errors */
462 for (e = _wenviron; *e != NULL; e++) {
463 PyObject *k;
464 PyObject *v;
465 wchar_t *p = wcschr(*e, L'=');
466 if (p == NULL)
467 continue;
468 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
469 if (k == NULL) {
470 PyErr_Clear();
471 continue;
472 }
473 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
474 if (v == NULL) {
475 PyErr_Clear();
476 Py_DECREF(k);
477 continue;
478 }
479 if (PyDict_GetItem(d, k) == NULL) {
480 if (PyDict_SetItem(d, k, v) != 0)
481 PyErr_Clear();
482 }
483 Py_DECREF(k);
484 Py_DECREF(v);
485 }
486#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487 if (environ == NULL)
488 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000489 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000490 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000491 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000492 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493 char *p = strchr(*e, '=');
494 if (p == NULL)
495 continue;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000496 k = PyUnicode_Decode(*e, (int)(p-*e),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000497 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000498 if (k == NULL) {
499 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000500 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000501 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000502 v = PyUnicode_Decode(p+1, strlen(p+1),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000503 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000504 if (v == NULL) {
505 PyErr_Clear();
506 Py_DECREF(k);
507 continue;
508 }
509 if (PyDict_GetItem(d, k) == NULL) {
510 if (PyDict_SetItem(d, k, v) != 0)
511 PyErr_Clear();
512 }
513 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000514 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000515 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000516#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000517#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000518 {
519 APIRET rc;
520 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
521
522 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000523 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000524 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000525 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000526 Py_DECREF(v);
527 }
528 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
529 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000530 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000531 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000532 Py_DECREF(v);
533 }
534 }
535#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000536 return d;
537}
538
Martin v. Löwis011e8422009-05-05 04:43:17 +0000539/* Convert a bytes object to a char*. Optionally lock the buffer if it is a
540 bytes array. */
541
542static char*
543bytes2str(PyObject* o, int lock)
544{
545 if(PyBytes_Check(o))
546 return PyBytes_AsString(o);
547 else if(PyByteArray_Check(o)) {
548 if (lock && PyObject_GetBuffer(o, NULL, 0) < 0)
549 /* On a bytearray, this should not fail. */
550 PyErr_BadInternalCall();
551 return PyByteArray_AsString(o);
552 } else {
553 /* The FS converter should have verified that this
554 is either bytes or bytearray. */
555 Py_FatalError("bad object passed to bytes2str");
556 /* not reached. */
557 return "";
558 }
559}
560
561/* Release the lock, decref the object. */
562static void
563release_bytes(PyObject* o)
564{
565 if (PyByteArray_Check(o))
Antoine Pitrou1119a642010-01-17 12:16:23 +0000566 o->ob_type->tp_as_buffer->bf_releasebuffer(o, 0);
Martin v. Löwis011e8422009-05-05 04:43:17 +0000567 Py_DECREF(o);
568}
569
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000570
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571/* Set a POSIX-specific error from errno, and return NULL */
572
Barry Warsawd58d7641998-07-23 16:14:40 +0000573static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000574posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000575{
Barry Warsawca74da41999-02-09 19:31:45 +0000576 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577}
Barry Warsawd58d7641998-07-23 16:14:40 +0000578static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000579posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000580{
Barry Warsawca74da41999-02-09 19:31:45 +0000581 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000582}
583
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000584#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000585static PyObject *
586posix_error_with_unicode_filename(Py_UNICODE* name)
587{
588 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
589}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000590#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000591
592
Mark Hammondef8b6542001-05-13 08:04:26 +0000593static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000594posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000595{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000596 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError,
597 bytes2str(name, 0));
598 release_bytes(name);
Mark Hammondef8b6542001-05-13 08:04:26 +0000599 return rc;
600}
601
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000602#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000603static PyObject *
604win32_error(char* function, char* filename)
605{
Mark Hammond33a6da92000-08-15 00:46:38 +0000606 /* XXX We should pass the function name along in the future.
Georg Brandl38feaf02008-05-25 07:45:51 +0000607 (winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000608 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000609 Windows error object, which is non-trivial.
610 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000611 errno = GetLastError();
612 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000613 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000614 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000615 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000616}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000617
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000618static PyObject *
619win32_error_unicode(char* function, Py_UNICODE* filename)
620{
621 /* XXX - see win32_error for comments on 'function' */
622 errno = GetLastError();
623 if (filename)
624 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
625 else
626 return PyErr_SetFromWindowsErr(errno);
627}
628
Thomas Wouters477c8d52006-05-27 19:21:47 +0000629static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000630convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000631{
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000632 if (PyUnicode_CheckExact(*param))
633 Py_INCREF(*param);
634 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000635 /* For a Unicode subtype that's not a Unicode object,
636 return a true Unicode object with the same data. */
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000637 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
638 PyUnicode_GET_SIZE(*param));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000639 else
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000640 *param = PyUnicode_FromEncodedObject(*param,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000641 Py_FileSystemDefaultEncoding,
642 "strict");
643 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000644}
645
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000646#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647
Guido van Rossumd48f2521997-12-05 22:19:34 +0000648#if defined(PYOS_OS2)
649/**********************************************************************
650 * Helper Function to Trim and Format OS/2 Messages
651 **********************************************************************/
652 static void
653os2_formatmsg(char *msgbuf, int msglen, char *reason)
654{
655 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
656
657 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
658 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
659
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000660 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000661 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
662 }
663
664 /* Add Optional Reason Text */
665 if (reason) {
666 strcat(msgbuf, " : ");
667 strcat(msgbuf, reason);
668 }
669}
670
671/**********************************************************************
672 * Decode an OS/2 Operating System Error Code
673 *
674 * A convenience function to lookup an OS/2 error code and return a
675 * text message we can use to raise a Python exception.
676 *
677 * Notes:
678 * The messages for errors returned from the OS/2 kernel reside in
679 * the file OSO001.MSG in the \OS2 directory hierarchy.
680 *
681 **********************************************************************/
682 static char *
683os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
684{
685 APIRET rc;
686 ULONG msglen;
687
688 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
689 Py_BEGIN_ALLOW_THREADS
690 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
691 errorcode, "oso001.msg", &msglen);
692 Py_END_ALLOW_THREADS
693
694 if (rc == NO_ERROR)
695 os2_formatmsg(msgbuf, msglen, reason);
696 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000697 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000698 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000699
700 return msgbuf;
701}
702
703/* Set an OS/2-specific error and return NULL. OS/2 kernel
704 errors are not in a global variable e.g. 'errno' nor are
705 they congruent with posix error numbers. */
706
707static PyObject * os2_error(int code)
708{
709 char text[1024];
710 PyObject *v;
711
712 os2_strerror(text, sizeof(text), code, "");
713
714 v = Py_BuildValue("(is)", code, text);
715 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000716 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000717 Py_DECREF(v);
718 }
719 return NULL; /* Signal to Python that an Exception is Pending */
720}
721
722#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000723
724/* POSIX generic methods */
725
Barry Warsaw53699e91996-12-10 23:23:01 +0000726static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000727posix_fildes(PyObject *fdobj, int (*func)(int))
728{
729 int fd;
730 int res;
731 fd = PyObject_AsFileDescriptor(fdobj);
732 if (fd < 0)
733 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000734 if (!_PyVerify_fd(fd))
735 return posix_error();
Fred Drake4d1e64b2002-04-15 19:40:07 +0000736 Py_BEGIN_ALLOW_THREADS
737 res = (*func)(fd);
738 Py_END_ALLOW_THREADS
739 if (res < 0)
740 return posix_error();
741 Py_INCREF(Py_None);
742 return Py_None;
743}
Guido van Rossum21142a01999-01-08 21:05:37 +0000744
745static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000746posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000747{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000748 PyObject *opath1 = NULL;
749 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000750 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000751 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000752 PyUnicode_FSConverter, &opath1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000753 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000754 path1 = bytes2str(opath1, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000755 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000756 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000757 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000758 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +0000759 return posix_error_with_allocated_filename(opath1);
760 release_bytes(opath1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000761 Py_INCREF(Py_None);
762 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000763}
764
Barry Warsaw53699e91996-12-10 23:23:01 +0000765static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000766posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000767 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000768 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000769{
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000770 PyObject *opath1 = NULL, *opath2 = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000771 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000772 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000773 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000774 PyUnicode_FSConverter, &opath1,
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000775 PyUnicode_FSConverter, &opath2)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000776 return NULL;
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000777 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000778 path1 = bytes2str(opath1, 1);
779 path2 = bytes2str(opath2, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000780 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000781 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000782 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +0000783 release_bytes(opath1);
784 release_bytes(opath2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000785 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000786 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000787 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000788 Py_INCREF(Py_None);
789 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000790}
791
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000792#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000793static PyObject*
794win32_1str(PyObject* args, char* func,
795 char* format, BOOL (__stdcall *funcA)(LPCSTR),
796 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
797{
798 PyObject *uni;
799 char *ansi;
800 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000801
802 if (!PyArg_ParseTuple(args, wformat, &uni))
803 PyErr_Clear();
804 else {
805 Py_BEGIN_ALLOW_THREADS
806 result = funcW(PyUnicode_AsUnicode(uni));
807 Py_END_ALLOW_THREADS
808 if (!result)
809 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
810 Py_INCREF(Py_None);
811 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000812 }
813 if (!PyArg_ParseTuple(args, format, &ansi))
814 return NULL;
815 Py_BEGIN_ALLOW_THREADS
816 result = funcA(ansi);
817 Py_END_ALLOW_THREADS
818 if (!result)
819 return win32_error(func, ansi);
820 Py_INCREF(Py_None);
821 return Py_None;
822
823}
824
825/* This is a reimplementation of the C library's chdir function,
826 but one that produces Win32 errors instead of DOS error codes.
827 chdir is essentially a wrapper around SetCurrentDirectory; however,
828 it also needs to set "magic" environment variables indicating
829 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000830static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000831win32_chdir(LPCSTR path)
832{
833 char new_path[MAX_PATH+1];
834 int result;
835 char env[4] = "=x:";
836
837 if(!SetCurrentDirectoryA(path))
838 return FALSE;
839 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
840 if (!result)
841 return FALSE;
842 /* In the ANSI API, there should not be any paths longer
843 than MAX_PATH. */
844 assert(result <= MAX_PATH+1);
845 if (strncmp(new_path, "\\\\", 2) == 0 ||
846 strncmp(new_path, "//", 2) == 0)
847 /* UNC path, nothing to do. */
848 return TRUE;
849 env[1] = new_path[0];
850 return SetEnvironmentVariableA(env, new_path);
851}
852
853/* The Unicode version differs from the ANSI version
854 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000855static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000856win32_wchdir(LPCWSTR path)
857{
858 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
859 int result;
860 wchar_t env[4] = L"=x:";
861
862 if(!SetCurrentDirectoryW(path))
863 return FALSE;
864 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
865 if (!result)
866 return FALSE;
867 if (result > MAX_PATH+1) {
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000868 new_path = malloc(result * sizeof(wchar_t));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000869 if (!new_path) {
870 SetLastError(ERROR_OUTOFMEMORY);
871 return FALSE;
872 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000873 result = GetCurrentDirectoryW(result, new_path);
874 if (!result) {
875 free(new_path);
876 return FALSE;
877 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000878 }
879 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
880 wcsncmp(new_path, L"//", 2) == 0)
881 /* UNC path, nothing to do. */
882 return TRUE;
883 env[1] = new_path[0];
884 result = SetEnvironmentVariableW(env, new_path);
885 if (new_path != _new_path)
886 free(new_path);
887 return result;
888}
889#endif
890
Martin v. Löwis14694662006-02-03 12:54:16 +0000891#ifdef MS_WINDOWS
892/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
893 - time stamps are restricted to second resolution
894 - file modification times suffer from forth-and-back conversions between
895 UTC and local time
896 Therefore, we implement our own stat, based on the Win32 API directly.
897*/
898#define HAVE_STAT_NSEC 1
899
900struct win32_stat{
901 int st_dev;
902 __int64 st_ino;
903 unsigned short st_mode;
904 int st_nlink;
905 int st_uid;
906 int st_gid;
907 int st_rdev;
908 __int64 st_size;
909 int st_atime;
910 int st_atime_nsec;
911 int st_mtime;
912 int st_mtime_nsec;
913 int st_ctime;
914 int st_ctime_nsec;
915};
916
917static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
918
919static void
920FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
921{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000922 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
923 /* Cannot simply cast and dereference in_ptr,
924 since it might not be aligned properly */
925 __int64 in;
926 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000927 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
928 /* XXX Win32 supports time stamps past 2038; we currently don't */
929 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
930}
931
Thomas Wouters477c8d52006-05-27 19:21:47 +0000932static void
933time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
934{
935 /* XXX endianness */
936 __int64 out;
937 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000938 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000939 memcpy(out_ptr, &out, sizeof(out));
940}
941
Martin v. Löwis14694662006-02-03 12:54:16 +0000942/* Below, we *know* that ugo+r is 0444 */
943#if _S_IREAD != 0400
944#error Unsupported C library
945#endif
946static int
947attributes_to_mode(DWORD attr)
948{
949 int m = 0;
950 if (attr & FILE_ATTRIBUTE_DIRECTORY)
951 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
952 else
953 m |= _S_IFREG;
954 if (attr & FILE_ATTRIBUTE_READONLY)
955 m |= 0444;
956 else
957 m |= 0666;
958 return m;
959}
960
961static int
962attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
963{
964 memset(result, 0, sizeof(*result));
965 result->st_mode = attributes_to_mode(info->dwFileAttributes);
966 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
967 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
968 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
969 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
970
971 return 0;
972}
973
Guido van Rossumd8faa362007-04-27 19:54:29 +0000974static BOOL
975attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
976{
977 HANDLE hFindFile;
978 WIN32_FIND_DATAA FileData;
979 hFindFile = FindFirstFileA(pszFile, &FileData);
980 if (hFindFile == INVALID_HANDLE_VALUE)
981 return FALSE;
982 FindClose(hFindFile);
983 pfad->dwFileAttributes = FileData.dwFileAttributes;
984 pfad->ftCreationTime = FileData.ftCreationTime;
985 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
986 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
987 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
988 pfad->nFileSizeLow = FileData.nFileSizeLow;
989 return TRUE;
990}
991
992static BOOL
993attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
994{
995 HANDLE hFindFile;
996 WIN32_FIND_DATAW FileData;
997 hFindFile = FindFirstFileW(pszFile, &FileData);
998 if (hFindFile == INVALID_HANDLE_VALUE)
999 return FALSE;
1000 FindClose(hFindFile);
1001 pfad->dwFileAttributes = FileData.dwFileAttributes;
1002 pfad->ftCreationTime = FileData.ftCreationTime;
1003 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1004 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1005 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1006 pfad->nFileSizeLow = FileData.nFileSizeLow;
1007 return TRUE;
1008}
1009
Martin v. Löwis14694662006-02-03 12:54:16 +00001010static int
1011win32_stat(const char* path, struct win32_stat *result)
1012{
1013 WIN32_FILE_ATTRIBUTE_DATA info;
1014 int code;
1015 char *dot;
Hirokazu Yamamoto6fbdfda2009-06-29 11:37:19 +00001016 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001017 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1018 /* Protocol violation: we explicitly clear errno, instead of
1019 setting it to a POSIX error. Callers should use GetLastError. */
1020 errno = 0;
1021 return -1;
1022 } else {
1023 /* Could not get attributes on open file. Fall back to
1024 reading the directory. */
1025 if (!attributes_from_dir(path, &info)) {
1026 /* Very strange. This should not fail now */
1027 errno = 0;
1028 return -1;
1029 }
1030 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001031 }
1032 code = attribute_data_to_stat(&info, result);
1033 if (code != 0)
1034 return code;
1035 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1036 dot = strrchr(path, '.');
1037 if (dot) {
1038 if (stricmp(dot, ".bat") == 0 ||
1039 stricmp(dot, ".cmd") == 0 ||
1040 stricmp(dot, ".exe") == 0 ||
1041 stricmp(dot, ".com") == 0)
1042 result->st_mode |= 0111;
1043 }
1044 return code;
1045}
1046
1047static int
1048win32_wstat(const wchar_t* path, struct win32_stat *result)
1049{
1050 int code;
1051 const wchar_t *dot;
1052 WIN32_FILE_ATTRIBUTE_DATA info;
Hirokazu Yamamoto6fbdfda2009-06-29 11:37:19 +00001053 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001054 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1055 /* Protocol violation: we explicitly clear errno, instead of
1056 setting it to a POSIX error. Callers should use GetLastError. */
1057 errno = 0;
1058 return -1;
1059 } else {
1060 /* Could not get attributes on open file. Fall back to
1061 reading the directory. */
1062 if (!attributes_from_dir_w(path, &info)) {
1063 /* Very strange. This should not fail now */
1064 errno = 0;
1065 return -1;
1066 }
1067 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001068 }
1069 code = attribute_data_to_stat(&info, result);
1070 if (code < 0)
1071 return code;
1072 /* Set IFEXEC if it is an .exe, .bat, ... */
1073 dot = wcsrchr(path, '.');
1074 if (dot) {
1075 if (_wcsicmp(dot, L".bat") == 0 ||
1076 _wcsicmp(dot, L".cmd") == 0 ||
1077 _wcsicmp(dot, L".exe") == 0 ||
1078 _wcsicmp(dot, L".com") == 0)
1079 result->st_mode |= 0111;
1080 }
1081 return code;
1082}
1083
1084static int
1085win32_fstat(int file_number, struct win32_stat *result)
1086{
1087 BY_HANDLE_FILE_INFORMATION info;
1088 HANDLE h;
1089 int type;
1090
1091 h = (HANDLE)_get_osfhandle(file_number);
1092
1093 /* Protocol violation: we explicitly clear errno, instead of
1094 setting it to a POSIX error. Callers should use GetLastError. */
1095 errno = 0;
1096
1097 if (h == INVALID_HANDLE_VALUE) {
1098 /* This is really a C library error (invalid file handle).
1099 We set the Win32 error to the closes one matching. */
1100 SetLastError(ERROR_INVALID_HANDLE);
1101 return -1;
1102 }
1103 memset(result, 0, sizeof(*result));
1104
1105 type = GetFileType(h);
1106 if (type == FILE_TYPE_UNKNOWN) {
1107 DWORD error = GetLastError();
1108 if (error != 0) {
1109 return -1;
1110 }
1111 /* else: valid but unknown file */
1112 }
1113
1114 if (type != FILE_TYPE_DISK) {
1115 if (type == FILE_TYPE_CHAR)
1116 result->st_mode = _S_IFCHR;
1117 else if (type == FILE_TYPE_PIPE)
1118 result->st_mode = _S_IFIFO;
1119 return 0;
1120 }
1121
1122 if (!GetFileInformationByHandle(h, &info)) {
1123 return -1;
1124 }
1125
1126 /* similar to stat() */
1127 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1128 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1129 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1130 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1131 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1132 /* specific to fstat() */
1133 result->st_nlink = info.nNumberOfLinks;
1134 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1135 return 0;
1136}
1137
1138#endif /* MS_WINDOWS */
1139
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001140PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001141"stat_result: Result from stat or lstat.\n\n\
1142This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001143 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001144or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1145\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001146Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1147or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001148\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001149See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001150
1151static PyStructSequence_Field stat_result_fields[] = {
1152 {"st_mode", "protection bits"},
1153 {"st_ino", "inode"},
1154 {"st_dev", "device"},
1155 {"st_nlink", "number of hard links"},
1156 {"st_uid", "user ID of owner"},
1157 {"st_gid", "group ID of owner"},
1158 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001159 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1160 {NULL, "integer time of last access"},
1161 {NULL, "integer time of last modification"},
1162 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001163 {"st_atime", "time of last access"},
1164 {"st_mtime", "time of last modification"},
1165 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001166#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001167 {"st_blksize", "blocksize for filesystem I/O"},
1168#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001169#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001170 {"st_blocks", "number of blocks allocated"},
1171#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001172#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001173 {"st_rdev", "device type (if inode device)"},
1174#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001175#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1176 {"st_flags", "user defined flags for file"},
1177#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001178#ifdef HAVE_STRUCT_STAT_ST_GEN
1179 {"st_gen", "generation number"},
1180#endif
1181#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1182 {"st_birthtime", "time of creation"},
1183#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001184 {0}
1185};
1186
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001187#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001188#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001189#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001190#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001191#endif
1192
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001193#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001194#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1195#else
1196#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1197#endif
1198
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001199#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001200#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1201#else
1202#define ST_RDEV_IDX ST_BLOCKS_IDX
1203#endif
1204
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001205#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1206#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1207#else
1208#define ST_FLAGS_IDX ST_RDEV_IDX
1209#endif
1210
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001211#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001212#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001213#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001214#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001215#endif
1216
1217#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1218#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1219#else
1220#define ST_BIRTHTIME_IDX ST_GEN_IDX
1221#endif
1222
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001223static PyStructSequence_Desc stat_result_desc = {
1224 "stat_result", /* name */
1225 stat_result__doc__, /* doc */
1226 stat_result_fields,
1227 10
1228};
1229
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001230PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001231"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1232This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001233 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001234or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001235\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001236See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001237
1238static PyStructSequence_Field statvfs_result_fields[] = {
1239 {"f_bsize", },
1240 {"f_frsize", },
1241 {"f_blocks", },
1242 {"f_bfree", },
1243 {"f_bavail", },
1244 {"f_files", },
1245 {"f_ffree", },
1246 {"f_favail", },
1247 {"f_flag", },
1248 {"f_namemax",},
1249 {0}
1250};
1251
1252static PyStructSequence_Desc statvfs_result_desc = {
1253 "statvfs_result", /* name */
1254 statvfs_result__doc__, /* doc */
1255 statvfs_result_fields,
1256 10
1257};
1258
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001259static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001260static PyTypeObject StatResultType;
1261static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001262static newfunc structseq_new;
1263
1264static PyObject *
1265statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1266{
1267 PyStructSequence *result;
1268 int i;
1269
1270 result = (PyStructSequence*)structseq_new(type, args, kwds);
1271 if (!result)
1272 return NULL;
1273 /* If we have been initialized from a tuple,
1274 st_?time might be set to None. Initialize it
1275 from the int slots. */
1276 for (i = 7; i <= 9; i++) {
1277 if (result->ob_item[i+3] == Py_None) {
1278 Py_DECREF(Py_None);
1279 Py_INCREF(result->ob_item[i]);
1280 result->ob_item[i+3] = result->ob_item[i];
1281 }
1282 }
1283 return (PyObject*)result;
1284}
1285
1286
1287
1288/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001289static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001290
1291PyDoc_STRVAR(stat_float_times__doc__,
1292"stat_float_times([newval]) -> oldval\n\n\
1293Determine whether os.[lf]stat represents time stamps as float objects.\n\
1294If newval is True, future calls to stat() return floats, if it is False,\n\
1295future calls return ints. \n\
1296If newval is omitted, return the current setting.\n");
1297
1298static PyObject*
1299stat_float_times(PyObject* self, PyObject *args)
1300{
1301 int newval = -1;
1302 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1303 return NULL;
1304 if (newval == -1)
1305 /* Return old value */
1306 return PyBool_FromLong(_stat_float_times);
1307 _stat_float_times = newval;
1308 Py_INCREF(Py_None);
1309 return Py_None;
1310}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001311
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001312static void
1313fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1314{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001315 PyObject *fval,*ival;
1316#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001317 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001318#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001319 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001320#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001321 if (!ival)
1322 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001323 if (_stat_float_times) {
1324 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1325 } else {
1326 fval = ival;
1327 Py_INCREF(fval);
1328 }
1329 PyStructSequence_SET_ITEM(v, index, ival);
1330 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001331}
1332
Tim Peters5aa91602002-01-30 05:46:57 +00001333/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001334 (used by posix_stat() and posix_fstat()) */
1335static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001336_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001337{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001338 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001339 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001340 if (v == NULL)
1341 return NULL;
1342
Christian Heimes217cfd12007-12-02 14:31:20 +00001343 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001344#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001345 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001346 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001347#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001348 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001349#endif
1350#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001351 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001352 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001353#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001354 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001355#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00001356 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1357 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1358 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001359#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001360 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001361 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001362#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001363 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001364#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001365
Martin v. Löwis14694662006-02-03 12:54:16 +00001366#if defined(HAVE_STAT_TV_NSEC)
1367 ansec = st->st_atim.tv_nsec;
1368 mnsec = st->st_mtim.tv_nsec;
1369 cnsec = st->st_ctim.tv_nsec;
1370#elif defined(HAVE_STAT_TV_NSEC2)
1371 ansec = st->st_atimespec.tv_nsec;
1372 mnsec = st->st_mtimespec.tv_nsec;
1373 cnsec = st->st_ctimespec.tv_nsec;
1374#elif defined(HAVE_STAT_NSEC)
1375 ansec = st->st_atime_nsec;
1376 mnsec = st->st_mtime_nsec;
1377 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001378#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001379 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001380#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001381 fill_time(v, 7, st->st_atime, ansec);
1382 fill_time(v, 8, st->st_mtime, mnsec);
1383 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001384
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001385#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001386 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001387 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001388#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001389#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001390 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001391 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001392#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001393#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001394 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001395 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001396#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001397#ifdef HAVE_STRUCT_STAT_ST_GEN
1398 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001399 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001400#endif
1401#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1402 {
1403 PyObject *val;
1404 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001405 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001406#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001407 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001408#else
1409 bnsec = 0;
1410#endif
1411 if (_stat_float_times) {
1412 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1413 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001414 val = PyLong_FromLong((long)bsec);
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001415 }
1416 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1417 val);
1418 }
1419#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001420#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1421 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001422 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001423#endif
Fred Drake699f3522000-06-29 21:12:41 +00001424
1425 if (PyErr_Occurred()) {
1426 Py_DECREF(v);
1427 return NULL;
1428 }
1429
1430 return v;
1431}
1432
Martin v. Löwisd8948722004-06-02 09:57:56 +00001433#ifdef MS_WINDOWS
1434
1435/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1436 where / can be used in place of \ and the trailing slash is optional.
1437 Both SERVER and SHARE must have at least one character.
1438*/
1439
1440#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1441#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001442#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001443#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001444#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001445
Tim Peters4ad82172004-08-30 17:02:04 +00001446static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001447IsUNCRootA(char *path, int pathlen)
1448{
1449 #define ISSLASH ISSLASHA
1450
1451 int i, share;
1452
1453 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1454 /* minimum UNCRoot is \\x\y */
1455 return FALSE;
1456 for (i = 2; i < pathlen ; i++)
1457 if (ISSLASH(path[i])) break;
1458 if (i == 2 || i == pathlen)
1459 /* do not allow \\\SHARE or \\SERVER */
1460 return FALSE;
1461 share = i+1;
1462 for (i = share; i < pathlen; i++)
1463 if (ISSLASH(path[i])) break;
1464 return (i != share && (i == pathlen || i == pathlen-1));
1465
1466 #undef ISSLASH
1467}
1468
Tim Peters4ad82172004-08-30 17:02:04 +00001469static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001470IsUNCRootW(Py_UNICODE *path, int pathlen)
1471{
1472 #define ISSLASH ISSLASHW
1473
1474 int i, share;
1475
1476 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1477 /* minimum UNCRoot is \\x\y */
1478 return FALSE;
1479 for (i = 2; i < pathlen ; i++)
1480 if (ISSLASH(path[i])) break;
1481 if (i == 2 || i == pathlen)
1482 /* do not allow \\\SHARE or \\SERVER */
1483 return FALSE;
1484 share = i+1;
1485 for (i = share; i < pathlen; i++)
1486 if (ISSLASH(path[i])) break;
1487 return (i != share && (i == pathlen || i == pathlen-1));
1488
1489 #undef ISSLASH
1490}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001491#endif /* MS_WINDOWS */
1492
Barry Warsaw53699e91996-12-10 23:23:01 +00001493static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001494posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001495 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001496#ifdef __VMS
1497 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1498#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001499 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001500#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001501 char *wformat,
1502 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001503{
Fred Drake699f3522000-06-29 21:12:41 +00001504 STRUCT_STAT st;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001505 PyObject *opath;
1506 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001507 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001508 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001509
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001510#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001511 PyUnicodeObject *po;
1512 if (PyArg_ParseTuple(args, wformat, &po)) {
1513 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001514
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001515 Py_BEGIN_ALLOW_THREADS
1516 /* PyUnicode_AS_UNICODE result OK without
1517 thread lock as it is a simple dereference. */
1518 res = wstatfunc(wpath, &st);
1519 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001520
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001521 if (res != 0)
1522 return win32_error_unicode("stat", wpath);
1523 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001524 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001525 /* Drop the argument parsing error as narrow strings
1526 are also valid. */
1527 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001528#endif
1529
Tim Peters5aa91602002-01-30 05:46:57 +00001530 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +00001531 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001532 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001533 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00001534 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001535 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001536 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001537
1538 if (res != 0) {
1539#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001540 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001541#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001542 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001543#endif
1544 }
1545 else
1546 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001547
Martin v. Löwis011e8422009-05-05 04:43:17 +00001548 release_bytes(opath);
Martin v. Löwis14694662006-02-03 12:54:16 +00001549 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001550}
1551
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001552/* POSIX methods */
1553
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001554PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001555"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001556Use the real uid/gid to test for access to a path. Note that most\n\
1557operations will use the effective uid/gid, therefore this routine can\n\
1558be used in a suid/sgid environment to test if the invoking user has the\n\
1559specified access to the path. The mode argument can be F_OK to test\n\
1560existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001561
1562static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001563posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001564{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001565 PyObject *opath;
Guido van Rossum015f22a1999-01-06 22:52:38 +00001566 char *path;
1567 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001568
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001569#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001570 DWORD attr;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001571 PyUnicodeObject *po;
1572 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1573 Py_BEGIN_ALLOW_THREADS
1574 /* PyUnicode_AS_UNICODE OK without thread lock as
1575 it is a simple dereference. */
1576 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1577 Py_END_ALLOW_THREADS
1578 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001579 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001580 /* Drop the argument parsing error as narrow strings
1581 are also valid. */
1582 PyErr_Clear();
Martin v. Löwis011e8422009-05-05 04:43:17 +00001583 if (!PyArg_ParseTuple(args, "O&i:access",
1584 PyUnicode_FSConverter, &opath, &mode))
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001585 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001586 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001587 Py_BEGIN_ALLOW_THREADS
1588 attr = GetFileAttributesA(path);
1589 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001590 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001591finish:
1592 if (attr == 0xFFFFFFFF)
1593 /* File does not exist, or cannot read attributes */
1594 return PyBool_FromLong(0);
1595 /* Access is possible if either write access wasn't requested, or
Guido van Rossumb00324f2007-12-04 01:13:14 +00001596 the file isn't read-only, or if it's a directory, as there are
1597 no read-only directories on Windows. */
1598 return PyBool_FromLong(!(mode & 2)
1599 || !(attr & FILE_ATTRIBUTE_READONLY)
1600 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001601#else
1602 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001603 if (!PyArg_ParseTuple(args, "O&i:access",
1604 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001605 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001606 path = bytes2str(opath, 1);
Guido van Rossum015f22a1999-01-06 22:52:38 +00001607 Py_BEGIN_ALLOW_THREADS
1608 res = access(path, mode);
1609 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001610 release_bytes(opath);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001611 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001612#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001613}
1614
Guido van Rossumd371ff11999-01-25 16:12:23 +00001615#ifndef F_OK
1616#define F_OK 0
1617#endif
1618#ifndef R_OK
1619#define R_OK 4
1620#endif
1621#ifndef W_OK
1622#define W_OK 2
1623#endif
1624#ifndef X_OK
1625#define X_OK 1
1626#endif
1627
1628#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001629PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001630"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001631Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001632
1633static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001634posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001635{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001636 int id;
1637 char *ret;
1638
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001639 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001640 return NULL;
1641
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001642#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001643 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001644 if (id == 0) {
1645 ret = ttyname();
1646 }
1647 else {
1648 ret = NULL;
1649 }
1650#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001651 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001652#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001653 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001654 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001655 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001656}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001657#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001658
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001659#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001660PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001661"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001662Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001663
1664static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001665posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001666{
1667 char *ret;
1668 char buffer[L_ctermid];
1669
Greg Wardb48bc172000-03-01 21:51:56 +00001670#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001671 ret = ctermid_r(buffer);
1672#else
1673 ret = ctermid(buffer);
1674#endif
1675 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001676 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001677 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001678}
1679#endif
1680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001681PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001682"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001683Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001684
Barry Warsaw53699e91996-12-10 23:23:01 +00001685static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001686posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001687{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001688#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00001689 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001690#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001691 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001692#elif defined(__VMS)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001693 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001694#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001695 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001696#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001697}
1698
Fred Drake4d1e64b2002-04-15 19:40:07 +00001699#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001700PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001701"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001702Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001703opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001704
1705static PyObject *
1706posix_fchdir(PyObject *self, PyObject *fdobj)
1707{
1708 return posix_fildes(fdobj, fchdir);
1709}
1710#endif /* HAVE_FCHDIR */
1711
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001713PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001714"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001715Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001716
Barry Warsaw53699e91996-12-10 23:23:01 +00001717static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001718posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001719{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001720 PyObject *opath = NULL;
Mark Hammondef8b6542001-05-13 08:04:26 +00001721 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001722 int i;
1723 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001724#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001725 DWORD attr;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001726 PyUnicodeObject *po;
1727 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1728 Py_BEGIN_ALLOW_THREADS
1729 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1730 if (attr != 0xFFFFFFFF) {
1731 if (i & _S_IWRITE)
1732 attr &= ~FILE_ATTRIBUTE_READONLY;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001733 else
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001734 attr |= FILE_ATTRIBUTE_READONLY;
1735 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
Mark Hammond817c9292003-12-03 01:22:38 +00001736 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001737 else
1738 res = 0;
1739 Py_END_ALLOW_THREADS
1740 if (!res)
1741 return win32_error_unicode("chmod",
1742 PyUnicode_AS_UNICODE(po));
1743 Py_INCREF(Py_None);
1744 return Py_None;
Mark Hammond817c9292003-12-03 01:22:38 +00001745 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001746 /* Drop the argument parsing error as narrow strings
1747 are also valid. */
1748 PyErr_Clear();
1749
Martin v. Löwis011e8422009-05-05 04:43:17 +00001750 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1751 &opath, &i))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001752 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001753 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001754 Py_BEGIN_ALLOW_THREADS
1755 attr = GetFileAttributesA(path);
1756 if (attr != 0xFFFFFFFF) {
1757 if (i & _S_IWRITE)
1758 attr &= ~FILE_ATTRIBUTE_READONLY;
1759 else
1760 attr |= FILE_ATTRIBUTE_READONLY;
1761 res = SetFileAttributesA(path, attr);
1762 }
1763 else
1764 res = 0;
1765 Py_END_ALLOW_THREADS
1766 if (!res) {
1767 win32_error("chmod", path);
Martin v. Löwis011e8422009-05-05 04:43:17 +00001768 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001769 return NULL;
1770 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001771 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001772 Py_INCREF(Py_None);
1773 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001774#else /* MS_WINDOWS */
Martin v. Löwis011e8422009-05-05 04:43:17 +00001775 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1776 &opath, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001777 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001778 path = bytes2str(opath, 1);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001779 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001780 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001781 Py_END_ALLOW_THREADS
1782 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001783 return posix_error_with_allocated_filename(opath);
1784 release_bytes(opath);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001785 Py_INCREF(Py_None);
1786 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001787#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001788}
1789
Christian Heimes4e30a842007-11-30 22:12:06 +00001790#ifdef HAVE_FCHMOD
1791PyDoc_STRVAR(posix_fchmod__doc__,
1792"fchmod(fd, mode)\n\n\
1793Change the access permissions of the file given by file\n\
1794descriptor fd.");
1795
1796static PyObject *
1797posix_fchmod(PyObject *self, PyObject *args)
1798{
1799 int fd, mode, res;
1800 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1801 return NULL;
1802 Py_BEGIN_ALLOW_THREADS
1803 res = fchmod(fd, mode);
1804 Py_END_ALLOW_THREADS
1805 if (res < 0)
1806 return posix_error();
1807 Py_RETURN_NONE;
1808}
1809#endif /* HAVE_FCHMOD */
1810
1811#ifdef HAVE_LCHMOD
1812PyDoc_STRVAR(posix_lchmod__doc__,
1813"lchmod(path, mode)\n\n\
1814Change the access permissions of a file. If path is a symlink, this\n\
1815affects the link itself rather than the target.");
1816
1817static PyObject *
1818posix_lchmod(PyObject *self, PyObject *args)
1819{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001820 PyObject *opath;
1821 char *path;
Christian Heimes4e30a842007-11-30 22:12:06 +00001822 int i;
1823 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001824 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1825 &opath, &i))
Christian Heimes4e30a842007-11-30 22:12:06 +00001826 return NULL;
Eric Smith86a05ec2009-05-05 13:07:30 +00001827 path = bytes2str(opath, 1);
Christian Heimes4e30a842007-11-30 22:12:06 +00001828 Py_BEGIN_ALLOW_THREADS
1829 res = lchmod(path, i);
1830 Py_END_ALLOW_THREADS
1831 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001832 return posix_error_with_allocated_filename(opath);
1833 release_bytes(opath);
Christian Heimes4e30a842007-11-30 22:12:06 +00001834 Py_RETURN_NONE;
1835}
1836#endif /* HAVE_LCHMOD */
1837
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001838
Thomas Wouterscf297e42007-02-23 15:07:44 +00001839#ifdef HAVE_CHFLAGS
1840PyDoc_STRVAR(posix_chflags__doc__,
1841"chflags(path, flags)\n\n\
1842Set file flags.");
1843
1844static PyObject *
1845posix_chflags(PyObject *self, PyObject *args)
1846{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001847 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001848 char *path;
1849 unsigned long flags;
1850 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001851 if (!PyArg_ParseTuple(args, "O&k:chflags",
1852 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00001853 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001854 path = bytes2str(opath, 1);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001855 Py_BEGIN_ALLOW_THREADS
1856 res = chflags(path, flags);
1857 Py_END_ALLOW_THREADS
1858 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001859 return posix_error_with_allocated_filename(opath);
1860 release_bytes(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001861 Py_INCREF(Py_None);
1862 return Py_None;
1863}
1864#endif /* HAVE_CHFLAGS */
1865
1866#ifdef HAVE_LCHFLAGS
1867PyDoc_STRVAR(posix_lchflags__doc__,
1868"lchflags(path, flags)\n\n\
1869Set file flags.\n\
1870This function will not follow symbolic links.");
1871
1872static PyObject *
1873posix_lchflags(PyObject *self, PyObject *args)
1874{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001875 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001876 char *path;
1877 unsigned long flags;
1878 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001879 if (!PyArg_ParseTuple(args, "O&k:lchflags",
Martin v. Löwis4adbc342009-05-05 17:17:55 +00001880 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00001881 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001882 path = bytes2str(opath, 1);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001883 Py_BEGIN_ALLOW_THREADS
1884 res = lchflags(path, flags);
1885 Py_END_ALLOW_THREADS
1886 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001887 return posix_error_with_allocated_filename(opath);
1888 release_bytes(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001889 Py_INCREF(Py_None);
1890 return Py_None;
1891}
1892#endif /* HAVE_LCHFLAGS */
1893
Martin v. Löwis244edc82001-10-04 22:44:26 +00001894#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001895PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001896"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001897Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001898
1899static PyObject *
1900posix_chroot(PyObject *self, PyObject *args)
1901{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001902 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001903}
1904#endif
1905
Guido van Rossum21142a01999-01-08 21:05:37 +00001906#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001907PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001908"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001909force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001910
1911static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001912posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001913{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001914 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001915}
1916#endif /* HAVE_FSYNC */
1917
1918#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001919
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001920#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001921extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1922#endif
1923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001924PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001925"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001926force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001927 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001928
1929static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001930posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001931{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001932 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001933}
1934#endif /* HAVE_FDATASYNC */
1935
1936
Fredrik Lundh10723342000-07-10 16:38:09 +00001937#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001938PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001939"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001940Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001941
Barry Warsaw53699e91996-12-10 23:23:01 +00001942static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001943posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001944{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001945 PyObject *opath;
1946 char *path;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00001947 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001948 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001949 if (!PyArg_ParseTuple(args, "O&ll:chown",
1950 PyUnicode_FSConverter, &opath,
Mark Hammondef8b6542001-05-13 08:04:26 +00001951 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001952 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001953 path = bytes2str(opath, 1);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001954 Py_BEGIN_ALLOW_THREADS
1955 res = chown(path, (uid_t) uid, (gid_t) gid);
1956 Py_END_ALLOW_THREADS
1957 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001958 return posix_error_with_allocated_filename(opath);
1959 release_bytes(opath);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001960 Py_INCREF(Py_None);
1961 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001962}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001963#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001964
Christian Heimes4e30a842007-11-30 22:12:06 +00001965#ifdef HAVE_FCHOWN
1966PyDoc_STRVAR(posix_fchown__doc__,
1967"fchown(fd, uid, gid)\n\n\
1968Change the owner and group id of the file given by file descriptor\n\
1969fd to the numeric uid and gid.");
1970
1971static PyObject *
1972posix_fchown(PyObject *self, PyObject *args)
1973{
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001974 int fd;
1975 long uid, gid;
Christian Heimes4e30a842007-11-30 22:12:06 +00001976 int res;
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001977 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
Christian Heimes4e30a842007-11-30 22:12:06 +00001978 return NULL;
1979 Py_BEGIN_ALLOW_THREADS
1980 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1981 Py_END_ALLOW_THREADS
1982 if (res < 0)
1983 return posix_error();
1984 Py_RETURN_NONE;
1985}
1986#endif /* HAVE_FCHOWN */
1987
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001988#ifdef HAVE_LCHOWN
1989PyDoc_STRVAR(posix_lchown__doc__,
1990"lchown(path, uid, gid)\n\n\
1991Change the owner and group id of path to the numeric uid and gid.\n\
1992This function will not follow symbolic links.");
1993
1994static PyObject *
1995posix_lchown(PyObject *self, PyObject *args)
1996{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001997 PyObject *opath;
1998 char *path;
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001999 long uid, gid;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002000 int res;
Benjamin Peterson1baf4652009-12-31 03:11:23 +00002001 if (!PyArg_ParseTuple(args, "O&ll:lchown",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002002 PyUnicode_FSConverter, &opath,
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002003 &uid, &gid))
2004 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002005 path = bytes2str(opath, 1);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002006 Py_BEGIN_ALLOW_THREADS
2007 res = lchown(path, (uid_t) uid, (gid_t) gid);
2008 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00002009 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002010 return posix_error_with_allocated_filename(opath);
2011 release_bytes(opath);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002012 Py_INCREF(Py_None);
2013 return Py_None;
2014}
2015#endif /* HAVE_LCHOWN */
2016
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002017
Guido van Rossum36bc6801995-06-14 22:54:23 +00002018#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002019static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002020posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002021{
2022 char buf[1026];
2023 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002024
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002025#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002026 if (!use_bytes) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002027 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00002028 wchar_t *wbuf2 = wbuf;
2029 PyObject *resobj;
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002030 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002031 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002032 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2033 /* If the buffer is large enough, len does not include the
2034 terminating \0. If the buffer is too small, len includes
2035 the space needed for the terminator. */
2036 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2037 wbuf2 = malloc(len * sizeof(wchar_t));
2038 if (wbuf2)
2039 len = GetCurrentDirectoryW(len, wbuf2);
2040 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002041 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002042 if (!wbuf2) {
2043 PyErr_NoMemory();
2044 return NULL;
2045 }
2046 if (!len) {
2047 if (wbuf2 != wbuf) free(wbuf2);
2048 return win32_error("getcwdu", NULL);
2049 }
2050 resobj = PyUnicode_FromWideChar(wbuf2, len);
2051 if (wbuf2 != wbuf) free(wbuf2);
2052 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002053 }
2054#endif
2055
2056 Py_BEGIN_ALLOW_THREADS
2057#if defined(PYOS_OS2) && defined(PYCC_GCC)
2058 res = _getcwd2(buf, sizeof buf);
2059#else
2060 res = getcwd(buf, sizeof buf);
2061#endif
2062 Py_END_ALLOW_THREADS
2063 if (res == NULL)
2064 return posix_error();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002065 if (use_bytes)
2066 return PyBytes_FromStringAndSize(buf, strlen(buf));
Martin v. Löwis43c57782009-05-10 08:15:24 +00002067 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002068}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002069
2070PyDoc_STRVAR(posix_getcwd__doc__,
2071"getcwd() -> path\n\n\
2072Return a unicode string representing the current working directory.");
2073
2074static PyObject *
2075posix_getcwd_unicode(PyObject *self)
2076{
2077 return posix_getcwd(0);
2078}
2079
2080PyDoc_STRVAR(posix_getcwdb__doc__,
2081"getcwdb() -> path\n\n\
2082Return a bytes string representing the current working directory.");
2083
2084static PyObject *
2085posix_getcwd_bytes(PyObject *self)
2086{
2087 return posix_getcwd(1);
2088}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002089#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002090
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002091
Guido van Rossumb6775db1994-08-01 11:34:53 +00002092#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002093PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002094"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002095Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002096
Barry Warsaw53699e91996-12-10 23:23:01 +00002097static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002098posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002099{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002100 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002101}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002102#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002103
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002104
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002105PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002106"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002107Return a list containing the names of the entries in the directory.\n\
2108\n\
2109 path: path of directory to list\n\
2110\n\
2111The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002112entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002113
Barry Warsaw53699e91996-12-10 23:23:01 +00002114static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002115posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002116{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002117 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002118 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002119#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002120
Barry Warsaw53699e91996-12-10 23:23:01 +00002121 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002122 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002123 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002124 WIN32_FIND_DATA FileData;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002125 PyObject *opath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002126 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002127 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002128 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002129
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002130 PyObject *po;
2131 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2132 WIN32_FIND_DATAW wFileData;
2133 Py_UNICODE *wnamebuf;
2134 /* Overallocate for \\*.*\0 */
2135 len = PyUnicode_GET_SIZE(po);
2136 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2137 if (!wnamebuf) {
2138 PyErr_NoMemory();
2139 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002140 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002141 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2142 if (len > 0) {
2143 Py_UNICODE wch = wnamebuf[len-1];
2144 if (wch != L'/' && wch != L'\\' && wch != L':')
2145 wnamebuf[len++] = L'\\';
2146 wcscpy(wnamebuf + len, L"*.*");
2147 }
2148 if ((d = PyList_New(0)) == NULL) {
2149 free(wnamebuf);
2150 return NULL;
2151 }
2152 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2153 if (hFindFile == INVALID_HANDLE_VALUE) {
2154 int error = GetLastError();
2155 if (error == ERROR_FILE_NOT_FOUND) {
2156 free(wnamebuf);
2157 return d;
2158 }
2159 Py_DECREF(d);
2160 win32_error_unicode("FindFirstFileW", wnamebuf);
2161 free(wnamebuf);
2162 return NULL;
2163 }
2164 do {
2165 /* Skip over . and .. */
2166 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2167 wcscmp(wFileData.cFileName, L"..") != 0) {
2168 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2169 if (v == NULL) {
2170 Py_DECREF(d);
2171 d = NULL;
2172 break;
2173 }
2174 if (PyList_Append(d, v) != 0) {
2175 Py_DECREF(v);
2176 Py_DECREF(d);
2177 d = NULL;
2178 break;
2179 }
2180 Py_DECREF(v);
2181 }
2182 Py_BEGIN_ALLOW_THREADS
2183 result = FindNextFileW(hFindFile, &wFileData);
2184 Py_END_ALLOW_THREADS
2185 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2186 it got to the end of the directory. */
2187 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2188 Py_DECREF(d);
2189 win32_error_unicode("FindNextFileW", wnamebuf);
2190 FindClose(hFindFile);
2191 free(wnamebuf);
2192 return NULL;
2193 }
2194 } while (result == TRUE);
2195
2196 if (FindClose(hFindFile) == FALSE) {
2197 Py_DECREF(d);
2198 win32_error_unicode("FindClose", wnamebuf);
2199 free(wnamebuf);
2200 return NULL;
2201 }
2202 free(wnamebuf);
2203 return d;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002204 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002205 /* Drop the argument parsing error as narrow strings
2206 are also valid. */
2207 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002208
Martin v. Löwis011e8422009-05-05 04:43:17 +00002209 if (!PyArg_ParseTuple(args, "O&:listdir",
2210 PyUnicode_FSConverter, &opath))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002211 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002212 if (PyObject_Size(opath)+1 > MAX_PATH) {
2213 PyErr_SetString(PyExc_ValueError, "path too long");
2214 Py_DECREF(opath);
2215 return NULL;
2216 }
2217 strcpy(namebuf, bytes2str(opath, 0));
2218 len = PyObject_Size(opath);
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002219 if (len > 0) {
2220 char ch = namebuf[len-1];
2221 if (ch != SEP && ch != ALTSEP && ch != ':')
2222 namebuf[len++] = '/';
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002223 strcpy(namebuf + len, "*.*");
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002224 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002225
Barry Warsaw53699e91996-12-10 23:23:01 +00002226 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002227 return NULL;
2228
2229 hFindFile = FindFirstFile(namebuf, &FileData);
2230 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002231 int error = GetLastError();
2232 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002233 return d;
2234 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002235 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002236 }
2237 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002238 /* Skip over . and .. */
2239 if (strcmp(FileData.cFileName, ".") != 0 &&
2240 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002241 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002242 if (v == NULL) {
2243 Py_DECREF(d);
2244 d = NULL;
2245 break;
2246 }
2247 if (PyList_Append(d, v) != 0) {
2248 Py_DECREF(v);
2249 Py_DECREF(d);
2250 d = NULL;
2251 break;
2252 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002253 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002254 }
Georg Brandl622927b2006-03-07 12:48:03 +00002255 Py_BEGIN_ALLOW_THREADS
2256 result = FindNextFile(hFindFile, &FileData);
2257 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002258 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2259 it got to the end of the directory. */
2260 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2261 Py_DECREF(d);
2262 win32_error("FindNextFile", namebuf);
2263 FindClose(hFindFile);
2264 return NULL;
2265 }
Georg Brandl622927b2006-03-07 12:48:03 +00002266 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002267
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002268 if (FindClose(hFindFile) == FALSE) {
2269 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002270 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002271 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002272
2273 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002274
Tim Peters0bb44a42000-09-15 07:44:49 +00002275#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002276
2277#ifndef MAX_PATH
2278#define MAX_PATH CCHMAXPATH
2279#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002280 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002281 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002282 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002283 PyObject *d, *v;
2284 char namebuf[MAX_PATH+5];
2285 HDIR hdir = 1;
2286 ULONG srchcnt = 1;
2287 FILEFINDBUF3 ep;
2288 APIRET rc;
2289
Martin v. Löwis011e8422009-05-05 04:43:17 +00002290 if (!PyArg_ParseTuple(args, "O&:listdir",
2291 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002292 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002293 name = bytes2str(oname);
2294 len = PyObject_Size(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002295 if (len >= MAX_PATH) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002296 release_bytes(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002297 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002298 return NULL;
2299 }
2300 strcpy(namebuf, name);
2301 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002302 if (*pt == ALTSEP)
2303 *pt = SEP;
2304 if (namebuf[len-1] != SEP)
2305 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002306 strcpy(namebuf + len, "*.*");
2307
Neal Norwitz6c913782007-10-14 03:23:09 +00002308 if ((d = PyList_New(0)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002309 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002310 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002311 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002312
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002313 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2314 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002315 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002316 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2317 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2318 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002319
2320 if (rc != NO_ERROR) {
2321 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002322 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002323 }
2324
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002325 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002326 do {
2327 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002328 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002329 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002330
2331 strcpy(namebuf, ep.achName);
2332
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002333 /* Leave Case of Name Alone -- In Native Form */
2334 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002335
Christian Heimes72b710a2008-05-26 13:28:38 +00002336 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002337 if (v == NULL) {
2338 Py_DECREF(d);
2339 d = NULL;
2340 break;
2341 }
2342 if (PyList_Append(d, v) != 0) {
2343 Py_DECREF(v);
2344 Py_DECREF(d);
2345 d = NULL;
2346 break;
2347 }
2348 Py_DECREF(v);
2349 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2350 }
2351
Martin v. Löwis011e8422009-05-05 04:43:17 +00002352 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002353 return d;
2354#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002355 PyObject *oname;
2356 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +00002357 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002358 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002359 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002360 int arg_is_unicode = 1;
2361
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002362 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002363 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2364 arg_is_unicode = 0;
2365 PyErr_Clear();
2366 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002367 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002368 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002369 name = bytes2str(oname, 1);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002370 if ((dirp = opendir(name)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002371 return posix_error_with_allocated_filename(oname);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002372 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002373 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002374 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002375 release_bytes(oname);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002376 return NULL;
2377 }
Georg Brandl622927b2006-03-07 12:48:03 +00002378 for (;;) {
Georg Brandl3dbca812008-07-23 16:10:53 +00002379 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002380 Py_BEGIN_ALLOW_THREADS
2381 ep = readdir(dirp);
2382 Py_END_ALLOW_THREADS
Georg Brandl3dbca812008-07-23 16:10:53 +00002383 if (ep == NULL) {
2384 if (errno == 0) {
2385 break;
2386 } else {
2387 closedir(dirp);
2388 Py_DECREF(d);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002389 return posix_error_with_allocated_filename(oname);
Georg Brandl3dbca812008-07-23 16:10:53 +00002390 }
2391 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002392 if (ep->d_name[0] == '.' &&
2393 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002394 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002395 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002396 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002397 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002398 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002399 d = NULL;
2400 break;
2401 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002402 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002403 PyObject *w;
2404
2405 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002406 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00002407 "surrogateescape");
Martin v. Löwis011e8422009-05-05 04:43:17 +00002408 Py_DECREF(v);
2409 if (w != NULL)
Just van Rossum6a421832003-03-04 19:30:44 +00002410 v = w;
Just van Rossum6a421832003-03-04 19:30:44 +00002411 else {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002412 /* Encoding failed to decode ASCII bytes.
2413 Raise exception. */
2414 Py_DECREF(d);
2415 d = NULL;
2416 break;
Just van Rossum46c97842003-02-25 21:42:15 +00002417 }
Just van Rossum46c97842003-02-25 21:42:15 +00002418 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002419 if (PyList_Append(d, v) != 0) {
2420 Py_DECREF(v);
2421 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002422 d = NULL;
2423 break;
2424 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002425 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002426 }
2427 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002428 release_bytes(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002429
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002430 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002431
Tim Peters0bb44a42000-09-15 07:44:49 +00002432#endif /* which OS */
2433} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002434
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002435#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002436/* A helper function for abspath on win32 */
2437static PyObject *
2438posix__getfullpathname(PyObject *self, PyObject *args)
2439{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002440 PyObject *opath;
2441 char *path;
Mark Hammondef8b6542001-05-13 08:04:26 +00002442 char outbuf[MAX_PATH*2];
2443 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002444#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002445 PyUnicodeObject *po;
2446 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2447 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2448 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2449 Py_UNICODE *wtemp;
2450 DWORD result;
2451 PyObject *v;
2452 result = GetFullPathNameW(wpath,
2453 sizeof(woutbuf)/sizeof(woutbuf[0]),
2454 woutbuf, &wtemp);
2455 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2456 woutbufp = malloc(result * sizeof(Py_UNICODE));
2457 if (!woutbufp)
2458 return PyErr_NoMemory();
2459 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002460 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002461 if (result)
2462 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2463 else
2464 v = win32_error_unicode("GetFullPathNameW", wpath);
2465 if (woutbufp != woutbuf)
2466 free(woutbufp);
2467 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002468 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002469 /* Drop the argument parsing error as narrow strings
2470 are also valid. */
2471 PyErr_Clear();
2472
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002473#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002474 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2475 PyUnicode_FSConverter, &opath))
Mark Hammondef8b6542001-05-13 08:04:26 +00002476 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002477 path = bytes2str(opath, 1);
2478 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2479 outbuf, &temp)) {
2480 win32_error("GetFullPathName", path);
2481 release_bytes(opath);
2482 return NULL;
2483 }
2484 release_bytes(opath);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002485 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2486 return PyUnicode_Decode(outbuf, strlen(outbuf),
2487 Py_FileSystemDefaultEncoding, NULL);
2488 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002489 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002490} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002491#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002492
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002493PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002494"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002495Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002496
Barry Warsaw53699e91996-12-10 23:23:01 +00002497static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002498posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002499{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002500 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002501 PyObject *opath;
2502 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002503 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002504
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002505#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002506 PyUnicodeObject *po;
2507 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2508 Py_BEGIN_ALLOW_THREADS
2509 /* PyUnicode_AS_UNICODE OK without thread lock as
2510 it is a simple dereference. */
2511 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2512 Py_END_ALLOW_THREADS
2513 if (!res)
2514 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2515 Py_INCREF(Py_None);
2516 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002517 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002518 /* Drop the argument parsing error as narrow strings
2519 are also valid. */
2520 PyErr_Clear();
Martin v. Löwis011e8422009-05-05 04:43:17 +00002521 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2522 PyUnicode_FSConverter, &opath, &mode))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002523 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002524 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002525 Py_BEGIN_ALLOW_THREADS
2526 /* PyUnicode_AS_UNICODE OK without thread lock as
2527 it is a simple dereference. */
2528 res = CreateDirectoryA(path, NULL);
2529 Py_END_ALLOW_THREADS
2530 if (!res) {
2531 win32_error("mkdir", path);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002532 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002533 return NULL;
2534 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002535 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002536 Py_INCREF(Py_None);
2537 return Py_None;
2538#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002539
Martin v. Löwis011e8422009-05-05 04:43:17 +00002540 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2541 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002542 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002543 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00002544 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002545#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002546 res = mkdir(path);
2547#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002548 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002549#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002550 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002551 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002552 return posix_error_with_allocated_filename(opath);
2553 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002554 Py_INCREF(Py_None);
2555 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002556#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002557}
2558
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002559
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002560/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2561#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002562#include <sys/resource.h>
2563#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002564
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002565
2566#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002567PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002568"nice(inc) -> new_priority\n\n\
2569Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002570
Barry Warsaw53699e91996-12-10 23:23:01 +00002571static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002572posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002573{
2574 int increment, value;
2575
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002576 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002577 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002578
2579 /* There are two flavours of 'nice': one that returns the new
2580 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002581 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2582 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002583
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002584 If we are of the nice family that returns the new priority, we
2585 need to clear errno before the call, and check if errno is filled
2586 before calling posix_error() on a returnvalue of -1, because the
2587 -1 may be the actual new priority! */
2588
2589 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002590 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002591#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002592 if (value == 0)
2593 value = getpriority(PRIO_PROCESS, 0);
2594#endif
2595 if (value == -1 && errno != 0)
2596 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002597 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002598 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002599}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002600#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002601
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002602PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002603"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002604Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002605
Barry Warsaw53699e91996-12-10 23:23:01 +00002606static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002607posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002608{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002609#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002610 PyObject *o1, *o2;
2611 char *p1, *p2;
2612 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002613 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002614 goto error;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002615 if (!convert_to_unicode(&o1))
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002616 goto error;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002617 if (!convert_to_unicode(&o2)) {
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002618 Py_DECREF(o1);
2619 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002620 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002621 Py_BEGIN_ALLOW_THREADS
2622 result = MoveFileW(PyUnicode_AsUnicode(o1),
2623 PyUnicode_AsUnicode(o2));
2624 Py_END_ALLOW_THREADS
2625 Py_DECREF(o1);
2626 Py_DECREF(o2);
2627 if (!result)
2628 return win32_error("rename", NULL);
2629 Py_INCREF(Py_None);
2630 return Py_None;
2631error:
2632 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002633 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2634 return NULL;
2635 Py_BEGIN_ALLOW_THREADS
2636 result = MoveFileA(p1, p2);
2637 Py_END_ALLOW_THREADS
2638 if (!result)
2639 return win32_error("rename", NULL);
2640 Py_INCREF(Py_None);
2641 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002642#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002643 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002644#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002645}
2646
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002647
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002648PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002649"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002650Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002651
Barry Warsaw53699e91996-12-10 23:23:01 +00002652static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002653posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002654{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002655#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002656 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002657#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002658 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002659#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002660}
2661
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002662
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002663PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002664"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002665Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002666
Barry Warsaw53699e91996-12-10 23:23:01 +00002667static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002668posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002669{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002670#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00002671 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002672#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002673 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002674#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002675}
2676
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002677
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002678#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002679PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002680"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002681Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002682
Barry Warsaw53699e91996-12-10 23:23:01 +00002683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002684posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002685{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002686 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002687#ifdef MS_WINDOWS
2688 wchar_t *command;
2689 if (!PyArg_ParseTuple(args, "u:system", &command))
2690 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002691
Barry Warsaw53699e91996-12-10 23:23:01 +00002692 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002693 sts = _wsystem(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002694 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00002695#else
2696 PyObject *command_obj;
2697 char *command;
2698 if (!PyArg_ParseTuple(args, "O&:system",
2699 PyUnicode_FSConverter, &command_obj))
2700 return NULL;
2701
2702 command = bytes2str(command_obj, 1);
2703 Py_BEGIN_ALLOW_THREADS
2704 sts = system(command);
2705 Py_END_ALLOW_THREADS
2706 release_bytes(command_obj);
2707#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00002708 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002709}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002710#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002711
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002713PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002714"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002715Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002716
Barry Warsaw53699e91996-12-10 23:23:01 +00002717static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002718posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002719{
2720 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002721 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002722 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002723 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002724 if (i < 0)
2725 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002726 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002727}
2728
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002730PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002731"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002732Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002733
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002734PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002735"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002736Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002737
Barry Warsaw53699e91996-12-10 23:23:01 +00002738static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002739posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002740{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002741#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002742 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002743#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002744 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002745#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002746}
2747
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002748
Guido van Rossumb6775db1994-08-01 11:34:53 +00002749#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002750PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002751"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002752Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002753
Barry Warsaw53699e91996-12-10 23:23:01 +00002754static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002755posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002756{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002757 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002758 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002759
Barry Warsaw53699e91996-12-10 23:23:01 +00002760 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002761 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002762 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002763 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002764 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002765 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002766 u.sysname,
2767 u.nodename,
2768 u.release,
2769 u.version,
2770 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002771}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002772#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002773
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002774static int
2775extract_time(PyObject *t, long* sec, long* usec)
2776{
2777 long intval;
2778 if (PyFloat_Check(t)) {
2779 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002780 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002781 if (!intobj)
2782 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002783 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002784 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002785 if (intval == -1 && PyErr_Occurred())
2786 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002787 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002788 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002789 if (*usec < 0)
2790 /* If rounding gave us a negative number,
2791 truncate. */
2792 *usec = 0;
2793 return 0;
2794 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002795 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002796 if (intval == -1 && PyErr_Occurred())
2797 return -1;
2798 *sec = intval;
2799 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002800 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002801}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002803PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002804"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002805utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002806Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002807second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002808
Barry Warsaw53699e91996-12-10 23:23:01 +00002809static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002810posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002811{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002812#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002813 PyObject *arg;
2814 PyUnicodeObject *obwpath;
2815 wchar_t *wpath = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002816 PyObject *oapath;
2817 char *apath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002818 HANDLE hFile;
2819 long atimesec, mtimesec, ausec, musec;
2820 FILETIME atime, mtime;
2821 PyObject *result = NULL;
2822
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002823 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2824 wpath = PyUnicode_AS_UNICODE(obwpath);
2825 Py_BEGIN_ALLOW_THREADS
2826 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2827 NULL, OPEN_EXISTING,
2828 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2829 Py_END_ALLOW_THREADS
2830 if (hFile == INVALID_HANDLE_VALUE)
2831 return win32_error_unicode("utime", wpath);
2832 } else
2833 /* Drop the argument parsing error as narrow strings
2834 are also valid. */
2835 PyErr_Clear();
2836
Thomas Wouters477c8d52006-05-27 19:21:47 +00002837 if (!wpath) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002838 if (!PyArg_ParseTuple(args, "O&O:utime",
2839 PyUnicode_FSConverter, &oapath, &arg))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002840 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002841 apath = bytes2str(oapath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002842 Py_BEGIN_ALLOW_THREADS
2843 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002844 NULL, OPEN_EXISTING,
2845 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002846 Py_END_ALLOW_THREADS
2847 if (hFile == INVALID_HANDLE_VALUE) {
2848 win32_error("utime", apath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002849 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002850 return NULL;
2851 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002852 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002853 }
2854
2855 if (arg == Py_None) {
2856 SYSTEMTIME now;
2857 GetSystemTime(&now);
2858 if (!SystemTimeToFileTime(&now, &mtime) ||
2859 !SystemTimeToFileTime(&now, &atime)) {
2860 win32_error("utime", NULL);
2861 goto done;
2862 }
2863 }
2864 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2865 PyErr_SetString(PyExc_TypeError,
2866 "utime() arg 2 must be a tuple (atime, mtime)");
2867 goto done;
2868 }
2869 else {
2870 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2871 &atimesec, &ausec) == -1)
2872 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002873 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002874 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2875 &mtimesec, &musec) == -1)
2876 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002877 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002878 }
2879 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2880 /* Avoid putting the file name into the error here,
2881 as that may confuse the user into believing that
2882 something is wrong with the file, when it also
2883 could be the time stamp that gives a problem. */
2884 win32_error("utime", NULL);
2885 }
2886 Py_INCREF(Py_None);
2887 result = Py_None;
2888done:
2889 CloseHandle(hFile);
2890 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002891#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002892
Martin v. Löwis011e8422009-05-05 04:43:17 +00002893 PyObject *opath;
2894 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002895 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002896 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002897 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002898
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002899#if defined(HAVE_UTIMES)
2900 struct timeval buf[2];
2901#define ATIME buf[0].tv_sec
2902#define MTIME buf[1].tv_sec
2903#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002904/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002905 struct utimbuf buf;
2906#define ATIME buf.actime
2907#define MTIME buf.modtime
2908#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002909#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002910 time_t buf[2];
2911#define ATIME buf[0]
2912#define MTIME buf[1]
2913#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002914#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002915
Mark Hammond817c9292003-12-03 01:22:38 +00002916
Martin v. Löwis011e8422009-05-05 04:43:17 +00002917 if (!PyArg_ParseTuple(args, "O&O:utime",
2918 PyUnicode_FSConverter, &opath, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002919 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002920 path = bytes2str(opath, 1);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002921 if (arg == Py_None) {
2922 /* optional time values not given */
2923 Py_BEGIN_ALLOW_THREADS
2924 res = utime(path, NULL);
2925 Py_END_ALLOW_THREADS
2926 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002927 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002928 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002929 "utime() arg 2 must be a tuple (atime, mtime)");
Martin v. Löwis011e8422009-05-05 04:43:17 +00002930 release_bytes(opath);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002931 return NULL;
2932 }
2933 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002934 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002935 &atime, &ausec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002936 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002937 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002938 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002939 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002940 &mtime, &musec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002941 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002942 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002943 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002944 ATIME = atime;
2945 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002946#ifdef HAVE_UTIMES
2947 buf[0].tv_usec = ausec;
2948 buf[1].tv_usec = musec;
2949 Py_BEGIN_ALLOW_THREADS
2950 res = utimes(path, buf);
2951 Py_END_ALLOW_THREADS
2952#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002953 Py_BEGIN_ALLOW_THREADS
2954 res = utime(path, UTIME_ARG);
2955 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002956#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002957 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002958 if (res < 0) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002959 return posix_error_with_allocated_filename(opath);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002960 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002961 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002962 Py_INCREF(Py_None);
2963 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002964#undef UTIME_ARG
2965#undef ATIME
2966#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002967#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002968}
2969
Guido van Rossum85e3b011991-06-03 12:42:10 +00002970
Guido van Rossum3b066191991-06-04 19:40:25 +00002971/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002973PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002974"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002975Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002976
Barry Warsaw53699e91996-12-10 23:23:01 +00002977static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002978posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002979{
2980 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002981 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002982 return NULL;
2983 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002984 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002985}
2986
Martin v. Löwis114619e2002-10-07 06:44:21 +00002987#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2988static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002989free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002990{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002991 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002992 for (i = 0; i < count; i++)
2993 PyMem_Free(array[i]);
2994 PyMem_DEL(array);
2995}
Martin v. Löwis011e8422009-05-05 04:43:17 +00002996
Antoine Pitrou69f71142009-05-24 21:25:49 +00002997static
Martin v. Löwis011e8422009-05-05 04:43:17 +00002998int fsconvert_strdup(PyObject *o, char**out)
2999{
3000 PyObject *bytes;
3001 Py_ssize_t size;
3002 if (!PyUnicode_FSConverter(o, &bytes))
3003 return 0;
3004 size = PyObject_Size(bytes);
3005 *out = PyMem_Malloc(size+1);
3006 if (!*out)
3007 return 0;
3008 /* Don't lock bytes, as we hold the GIL */
3009 memcpy(*out, bytes2str(bytes, 0), size+1);
3010 Py_DECREF(bytes);
3011 return 1;
3012}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003013#endif
3014
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003015
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003016#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003017PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003018"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003019Execute an executable path with arguments, replacing current process.\n\
3020\n\
3021 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003022 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003023
Barry Warsaw53699e91996-12-10 23:23:01 +00003024static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003025posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003026{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003027 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003028 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003029 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003030 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003031 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003032 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003033
Guido van Rossum89b33251993-10-22 14:26:06 +00003034 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00003035 argv is a list or tuple of strings. */
3036
Martin v. Löwis011e8422009-05-05 04:43:17 +00003037 if (!PyArg_ParseTuple(args, "O&O:execv",
3038 PyUnicode_FSConverter,
3039 &opath, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003040 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003041 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003042 if (PyList_Check(argv)) {
3043 argc = PyList_Size(argv);
3044 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003045 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003046 else if (PyTuple_Check(argv)) {
3047 argc = PyTuple_Size(argv);
3048 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003049 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003050 else {
Fred Drake661ea262000-10-24 19:57:45 +00003051 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003052 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003053 return NULL;
3054 }
Thomas Heller6790d602007-08-30 17:15:14 +00003055 if (argc < 1) {
3056 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003057 release_bytes(opath);
Thomas Heller6790d602007-08-30 17:15:14 +00003058 return NULL;
3059 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003060
Barry Warsaw53699e91996-12-10 23:23:01 +00003061 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003062 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003063 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003064 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003065 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003066 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003067 if (!fsconvert_strdup((*getitem)(argv, i),
3068 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003069 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00003070 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003071 "execv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003072 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003073 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003074
Guido van Rossum85e3b011991-06-03 12:42:10 +00003075 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003076 }
3077 argvlist[argc] = NULL;
3078
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003079 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003080
Guido van Rossum85e3b011991-06-03 12:42:10 +00003081 /* If we get here it's definitely an error */
3082
Martin v. Löwis114619e2002-10-07 06:44:21 +00003083 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003084 release_bytes(opath);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003085 return posix_error();
3086}
3087
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003088
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003089PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003090"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003091Execute a path with arguments and environment, replacing current process.\n\
3092\n\
3093 path: path of executable file\n\
3094 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003095 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003096
Barry Warsaw53699e91996-12-10 23:23:01 +00003097static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003098posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003099{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003100 PyObject *opath;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003101 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003102 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003103 char **argvlist;
3104 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003105 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003106 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003107 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003108 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003109
3110 /* execve has three arguments: (path, argv, env), where
3111 argv is a list or tuple of strings and env is a dictionary
3112 like posix.environ. */
3113
Martin v. Löwis011e8422009-05-05 04:43:17 +00003114 if (!PyArg_ParseTuple(args, "O&OO:execve",
3115 PyUnicode_FSConverter,
3116 &opath, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003117 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003118 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003119 if (PyList_Check(argv)) {
3120 argc = PyList_Size(argv);
3121 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003122 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003123 else if (PyTuple_Check(argv)) {
3124 argc = PyTuple_Size(argv);
3125 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003126 }
3127 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003128 PyErr_SetString(PyExc_TypeError,
3129 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003130 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003131 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003132 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003133 PyErr_SetString(PyExc_TypeError,
3134 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003135 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003136 }
3137
Barry Warsaw53699e91996-12-10 23:23:01 +00003138 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003139 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003140 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003141 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003142 }
3143 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003144 if (!fsconvert_strdup((*getitem)(argv, i),
3145 &argvlist[i]))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003146 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003147 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003148 goto fail_1;
3149 }
3150 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003151 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003152 argvlist[argc] = NULL;
3153
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003154 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003155 if (i < 0)
3156 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003157 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003158 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003159 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003160 goto fail_1;
3161 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003162 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003163 keys = PyMapping_Keys(env);
3164 vals = PyMapping_Values(env);
3165 if (!keys || !vals)
3166 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003167 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3168 PyErr_SetString(PyExc_TypeError,
3169 "execve(): env.keys() or env.values() is not a list");
3170 goto fail_2;
3171 }
Tim Peters5aa91602002-01-30 05:46:57 +00003172
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003173 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003174 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003175 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003176
3177 key = PyList_GetItem(keys, pos);
3178 val = PyList_GetItem(vals, pos);
3179 if (!key || !val)
3180 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003181
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003182 if (!PyArg_Parse(
3183 key,
3184 "s;execve() arg 3 contains a non-string key",
3185 &k) ||
3186 !PyArg_Parse(
3187 val,
3188 "s;execve() arg 3 contains a non-string value",
3189 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003190 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003191 goto fail_2;
3192 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003193
3194#if defined(PYOS_OS2)
3195 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3196 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3197#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003198 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003199 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003200 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003201 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003202 goto fail_2;
3203 }
Tim Petersc8996f52001-12-03 20:41:00 +00003204 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003205 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003206#if defined(PYOS_OS2)
3207 }
3208#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003209 }
3210 envlist[envc] = 0;
3211
3212 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003213
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003214 /* If we get here it's definitely an error */
3215
3216 (void) posix_error();
3217
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003218 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003219 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003220 PyMem_DEL(envlist[envc]);
3221 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003222 fail_1:
3223 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003224 Py_XDECREF(vals);
3225 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003226 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003227 release_bytes(opath);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003228 return NULL;
3229}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003230#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003231
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003232
Guido van Rossuma1065681999-01-25 23:20:23 +00003233#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003234PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003235"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003236Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003237\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003238 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003239 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003240 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003241
3242static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003243posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003244{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003245 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003246 char *path;
3247 PyObject *argv;
3248 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003249 int mode, i;
3250 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003251 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003252 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003253
3254 /* spawnv has three arguments: (mode, path, argv), where
3255 argv is a list or tuple of strings. */
3256
Martin v. Löwis011e8422009-05-05 04:43:17 +00003257 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3258 PyUnicode_FSConverter,
3259 &opath, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003260 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003261 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003262 if (PyList_Check(argv)) {
3263 argc = PyList_Size(argv);
3264 getitem = PyList_GetItem;
3265 }
3266 else if (PyTuple_Check(argv)) {
3267 argc = PyTuple_Size(argv);
3268 getitem = PyTuple_GetItem;
3269 }
3270 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003271 PyErr_SetString(PyExc_TypeError,
3272 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003273 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003274 return NULL;
3275 }
3276
3277 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003278 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003279 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003280 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003281 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003282 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003283 if (!fsconvert_strdup((*getitem)(argv, i),
3284 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003285 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003286 PyErr_SetString(
3287 PyExc_TypeError,
3288 "spawnv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003289 release_bytes(opath);
Fred Drake137507e2000-06-01 02:02:46 +00003290 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003291 }
3292 }
3293 argvlist[argc] = NULL;
3294
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003295#if defined(PYOS_OS2) && defined(PYCC_GCC)
3296 Py_BEGIN_ALLOW_THREADS
3297 spawnval = spawnv(mode, path, argvlist);
3298 Py_END_ALLOW_THREADS
3299#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003300 if (mode == _OLD_P_OVERLAY)
3301 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003302
Tim Peters25059d32001-12-07 20:35:43 +00003303 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003304 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003305 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003306#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003307
Martin v. Löwis114619e2002-10-07 06:44:21 +00003308 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003309 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003310
Fred Drake699f3522000-06-29 21:12:41 +00003311 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003312 return posix_error();
3313 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003314#if SIZEOF_LONG == SIZEOF_VOID_P
3315 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003316#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003317 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003318#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003319}
3320
3321
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003322PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003323"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003324Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003325\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003326 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003327 path: path of executable file\n\
3328 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003329 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003330
3331static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003332posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003333{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003334 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003335 char *path;
3336 PyObject *argv, *env;
3337 char **argvlist;
3338 char **envlist;
3339 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003340 int mode, pos, envc;
3341 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003342 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003343 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003344 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003345
3346 /* spawnve has four arguments: (mode, path, argv, env), where
3347 argv is a list or tuple of strings and env is a dictionary
3348 like posix.environ. */
3349
Martin v. Löwis011e8422009-05-05 04:43:17 +00003350 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3351 PyUnicode_FSConverter,
3352 &opath, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003353 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003354 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003355 if (PyList_Check(argv)) {
3356 argc = PyList_Size(argv);
3357 getitem = PyList_GetItem;
3358 }
3359 else if (PyTuple_Check(argv)) {
3360 argc = PyTuple_Size(argv);
3361 getitem = PyTuple_GetItem;
3362 }
3363 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003364 PyErr_SetString(PyExc_TypeError,
3365 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003366 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003367 }
3368 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003369 PyErr_SetString(PyExc_TypeError,
3370 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003371 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003372 }
3373
3374 argvlist = PyMem_NEW(char *, argc+1);
3375 if (argvlist == NULL) {
3376 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003377 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003378 }
3379 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003380 if (!fsconvert_strdup((*getitem)(argv, i),
3381 &argvlist[i]))
Guido van Rossuma1065681999-01-25 23:20:23 +00003382 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003383 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003384 goto fail_1;
3385 }
3386 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003387 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003388 argvlist[argc] = NULL;
3389
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003390 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003391 if (i < 0)
3392 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003393 envlist = PyMem_NEW(char *, i + 1);
3394 if (envlist == NULL) {
3395 PyErr_NoMemory();
3396 goto fail_1;
3397 }
3398 envc = 0;
3399 keys = PyMapping_Keys(env);
3400 vals = PyMapping_Values(env);
3401 if (!keys || !vals)
3402 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003403 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3404 PyErr_SetString(PyExc_TypeError,
3405 "spawnve(): env.keys() or env.values() is not a list");
3406 goto fail_2;
3407 }
Tim Peters5aa91602002-01-30 05:46:57 +00003408
Guido van Rossuma1065681999-01-25 23:20:23 +00003409 for (pos = 0; pos < i; pos++) {
3410 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003411 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003412
3413 key = PyList_GetItem(keys, pos);
3414 val = PyList_GetItem(vals, pos);
3415 if (!key || !val)
3416 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003417
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003418 if (!PyArg_Parse(
3419 key,
3420 "s;spawnve() arg 3 contains a non-string key",
3421 &k) ||
3422 !PyArg_Parse(
3423 val,
3424 "s;spawnve() arg 3 contains a non-string value",
3425 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003426 {
3427 goto fail_2;
3428 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003429 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003430 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003431 if (p == NULL) {
3432 PyErr_NoMemory();
3433 goto fail_2;
3434 }
Tim Petersc8996f52001-12-03 20:41:00 +00003435 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003436 envlist[envc++] = p;
3437 }
3438 envlist[envc] = 0;
3439
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003440#if defined(PYOS_OS2) && defined(PYCC_GCC)
3441 Py_BEGIN_ALLOW_THREADS
3442 spawnval = spawnve(mode, path, argvlist, envlist);
3443 Py_END_ALLOW_THREADS
3444#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003445 if (mode == _OLD_P_OVERLAY)
3446 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003447
3448 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003449 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003450 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003451#endif
Tim Peters25059d32001-12-07 20:35:43 +00003452
Fred Drake699f3522000-06-29 21:12:41 +00003453 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003454 (void) posix_error();
3455 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003456#if SIZEOF_LONG == SIZEOF_VOID_P
3457 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003458#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003459 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003460#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003461
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003462 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003463 while (--envc >= 0)
3464 PyMem_DEL(envlist[envc]);
3465 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003466 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003467 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003468 Py_XDECREF(vals);
3469 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003470 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003471 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003472 return res;
3473}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003474
3475/* OS/2 supports spawnvp & spawnvpe natively */
3476#if defined(PYOS_OS2)
3477PyDoc_STRVAR(posix_spawnvp__doc__,
3478"spawnvp(mode, file, args)\n\n\
3479Execute the program 'file' in a new process, using the environment\n\
3480search path to find the file.\n\
3481\n\
3482 mode: mode of process creation\n\
3483 file: executable file name\n\
3484 args: tuple or list of strings");
3485
3486static PyObject *
3487posix_spawnvp(PyObject *self, PyObject *args)
3488{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003489 PyObject *opath;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003490 char *path;
3491 PyObject *argv;
3492 char **argvlist;
3493 int mode, i, argc;
3494 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003495 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003496
3497 /* spawnvp has three arguments: (mode, path, argv), where
3498 argv is a list or tuple of strings. */
3499
Martin v. Löwis011e8422009-05-05 04:43:17 +00003500 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3501 PyUnicode_FSConverter,
3502 &opath, &argv))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003503 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003504 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003505 if (PyList_Check(argv)) {
3506 argc = PyList_Size(argv);
3507 getitem = PyList_GetItem;
3508 }
3509 else if (PyTuple_Check(argv)) {
3510 argc = PyTuple_Size(argv);
3511 getitem = PyTuple_GetItem;
3512 }
3513 else {
3514 PyErr_SetString(PyExc_TypeError,
3515 "spawnvp() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003516 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003517 return NULL;
3518 }
3519
3520 argvlist = PyMem_NEW(char *, argc+1);
3521 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003522 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003523 return PyErr_NoMemory();
3524 }
3525 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003526 if (!fsconvert_strdup((*getitem)(argv, i),
3527 &argvlist[i])) {
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003528 free_string_array(argvlist, i);
3529 PyErr_SetString(
3530 PyExc_TypeError,
3531 "spawnvp() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003532 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003533 return NULL;
3534 }
3535 }
3536 argvlist[argc] = NULL;
3537
3538 Py_BEGIN_ALLOW_THREADS
3539#if defined(PYCC_GCC)
3540 spawnval = spawnvp(mode, path, argvlist);
3541#else
3542 spawnval = _spawnvp(mode, path, argvlist);
3543#endif
3544 Py_END_ALLOW_THREADS
3545
3546 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003547 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003548
3549 if (spawnval == -1)
3550 return posix_error();
3551 else
3552 return Py_BuildValue("l", (long) spawnval);
3553}
3554
3555
3556PyDoc_STRVAR(posix_spawnvpe__doc__,
3557"spawnvpe(mode, file, args, env)\n\n\
3558Execute the program 'file' in a new process, using the environment\n\
3559search path to find the file.\n\
3560\n\
3561 mode: mode of process creation\n\
3562 file: executable file name\n\
3563 args: tuple or list of arguments\n\
3564 env: dictionary of strings mapping to strings");
3565
3566static PyObject *
3567posix_spawnvpe(PyObject *self, PyObject *args)
3568{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003569 PyObject *opath
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003570 char *path;
3571 PyObject *argv, *env;
3572 char **argvlist;
3573 char **envlist;
3574 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3575 int mode, i, pos, argc, envc;
3576 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003577 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003578 int lastarg = 0;
3579
3580 /* spawnvpe has four arguments: (mode, path, argv, env), where
3581 argv is a list or tuple of strings and env is a dictionary
3582 like posix.environ. */
3583
3584 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
Martin v. Löwis011e8422009-05-05 04:43:17 +00003585 PyUnicode_FSConverter,
3586 &opath, &argv, &env))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003587 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003588 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003589 if (PyList_Check(argv)) {
3590 argc = PyList_Size(argv);
3591 getitem = PyList_GetItem;
3592 }
3593 else if (PyTuple_Check(argv)) {
3594 argc = PyTuple_Size(argv);
3595 getitem = PyTuple_GetItem;
3596 }
3597 else {
3598 PyErr_SetString(PyExc_TypeError,
3599 "spawnvpe() arg 2 must be a tuple or list");
3600 goto fail_0;
3601 }
3602 if (!PyMapping_Check(env)) {
3603 PyErr_SetString(PyExc_TypeError,
3604 "spawnvpe() arg 3 must be a mapping object");
3605 goto fail_0;
3606 }
3607
3608 argvlist = PyMem_NEW(char *, argc+1);
3609 if (argvlist == NULL) {
3610 PyErr_NoMemory();
3611 goto fail_0;
3612 }
3613 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003614 if (!fsconvert_strdup((*getitem)(argv, i),
3615 &argvlist[i]))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003616 {
3617 lastarg = i;
3618 goto fail_1;
3619 }
3620 }
3621 lastarg = argc;
3622 argvlist[argc] = NULL;
3623
3624 i = PyMapping_Size(env);
3625 if (i < 0)
3626 goto fail_1;
3627 envlist = PyMem_NEW(char *, i + 1);
3628 if (envlist == NULL) {
3629 PyErr_NoMemory();
3630 goto fail_1;
3631 }
3632 envc = 0;
3633 keys = PyMapping_Keys(env);
3634 vals = PyMapping_Values(env);
3635 if (!keys || !vals)
3636 goto fail_2;
3637 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3638 PyErr_SetString(PyExc_TypeError,
3639 "spawnvpe(): env.keys() or env.values() is not a list");
3640 goto fail_2;
3641 }
3642
3643 for (pos = 0; pos < i; pos++) {
3644 char *p, *k, *v;
3645 size_t len;
3646
3647 key = PyList_GetItem(keys, pos);
3648 val = PyList_GetItem(vals, pos);
3649 if (!key || !val)
3650 goto fail_2;
3651
3652 if (!PyArg_Parse(
3653 key,
3654 "s;spawnvpe() arg 3 contains a non-string key",
3655 &k) ||
3656 !PyArg_Parse(
3657 val,
3658 "s;spawnvpe() arg 3 contains a non-string value",
3659 &v))
3660 {
3661 goto fail_2;
3662 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003663 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003664 p = PyMem_NEW(char, len);
3665 if (p == NULL) {
3666 PyErr_NoMemory();
3667 goto fail_2;
3668 }
3669 PyOS_snprintf(p, len, "%s=%s", k, v);
3670 envlist[envc++] = p;
3671 }
3672 envlist[envc] = 0;
3673
3674 Py_BEGIN_ALLOW_THREADS
3675#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003676 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003677#else
Christian Heimes292d3512008-02-03 16:51:08 +00003678 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003679#endif
3680 Py_END_ALLOW_THREADS
3681
3682 if (spawnval == -1)
3683 (void) posix_error();
3684 else
3685 res = Py_BuildValue("l", (long) spawnval);
3686
3687 fail_2:
3688 while (--envc >= 0)
3689 PyMem_DEL(envlist[envc]);
3690 PyMem_DEL(envlist);
3691 fail_1:
3692 free_string_array(argvlist, lastarg);
3693 Py_XDECREF(vals);
3694 Py_XDECREF(keys);
3695 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003696 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003697 return res;
3698}
3699#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003700#endif /* HAVE_SPAWNV */
3701
3702
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003703#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003704PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003705"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003706Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3707\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003708Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003709
3710static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003711posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003712{
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003713 pid_t pid;
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003714 int result = 0;
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003715 _PyImport_AcquireLock();
3716 pid = fork1();
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003717 if (pid == 0) {
3718 /* child: this clobbers and resets the import lock. */
3719 PyOS_AfterFork();
3720 } else {
3721 /* parent: release the import lock. */
3722 result = _PyImport_ReleaseLock();
3723 }
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003724 if (pid == -1)
3725 return posix_error();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003726 if (result < 0) {
3727 /* Don't clobber the OSError if the fork failed. */
3728 PyErr_SetString(PyExc_RuntimeError,
3729 "not holding the import lock");
3730 return NULL;
3731 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003732 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003733}
3734#endif
3735
3736
Guido van Rossumad0ee831995-03-01 10:34:45 +00003737#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003738PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003739"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003740Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003741Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003742
Barry Warsaw53699e91996-12-10 23:23:01 +00003743static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003744posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003745{
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003746 pid_t pid;
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003747 int result = 0;
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003748 _PyImport_AcquireLock();
3749 pid = fork();
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003750 if (pid == 0) {
3751 /* child: this clobbers and resets the import lock. */
3752 PyOS_AfterFork();
3753 } else {
3754 /* parent: release the import lock. */
3755 result = _PyImport_ReleaseLock();
3756 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003757 if (pid == -1)
3758 return posix_error();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003759 if (result < 0) {
3760 /* Don't clobber the OSError if the fork failed. */
3761 PyErr_SetString(PyExc_RuntimeError,
3762 "not holding the import lock");
3763 return NULL;
3764 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003765 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003766}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003767#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003768
Neal Norwitzb59798b2003-03-21 01:43:31 +00003769/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003770/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3771#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003772#define DEV_PTY_FILE "/dev/ptc"
3773#define HAVE_DEV_PTMX
3774#else
3775#define DEV_PTY_FILE "/dev/ptmx"
3776#endif
3777
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003778#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003779#ifdef HAVE_PTY_H
3780#include <pty.h>
3781#else
3782#ifdef HAVE_LIBUTIL_H
3783#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00003784#else
3785#ifdef HAVE_UTIL_H
3786#include <util.h>
3787#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003788#endif /* HAVE_LIBUTIL_H */
3789#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003790#ifdef HAVE_STROPTS_H
3791#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003792#endif
3793#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003794
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003795#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003796PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003797"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003798Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003799
3800static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003801posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003802{
3803 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003804#ifndef HAVE_OPENPTY
3805 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003806#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003807#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003808 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003809#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003810 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003811#endif
3812#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003813
Thomas Wouters70c21a12000-07-14 14:28:33 +00003814#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003815 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3816 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003817#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003818 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3819 if (slave_name == NULL)
3820 return posix_error();
3821
3822 slave_fd = open(slave_name, O_RDWR);
3823 if (slave_fd < 0)
3824 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003825#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003826 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003827 if (master_fd < 0)
3828 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003829 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003830 /* change permission of slave */
3831 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003832 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003833 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003834 }
3835 /* unlock slave */
3836 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003837 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003838 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003839 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003840 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003841 slave_name = ptsname(master_fd); /* get name of slave */
3842 if (slave_name == NULL)
3843 return posix_error();
3844 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3845 if (slave_fd < 0)
3846 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003847#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003848 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3849 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003850#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003851 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003852#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003853#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003854#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003855
Fred Drake8cef4cf2000-06-28 16:40:38 +00003856 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003857
Fred Drake8cef4cf2000-06-28 16:40:38 +00003858}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003859#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003860
3861#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003862PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003863"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003864Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3865Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003866To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003867
3868static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003869posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003870{
Gregory P. Smith2a1c0272010-03-01 17:04:45 +00003871 int master_fd = -1, result = 0;
Christian Heimes400adb02008-02-01 08:12:03 +00003872 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003873
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003874 _PyImport_AcquireLock();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003875 pid = forkpty(&master_fd, NULL, NULL, NULL);
Gregory P. Smith2a1c0272010-03-01 17:04:45 +00003876 if (pid == 0) {
3877 /* child: this clobbers and resets the import lock. */
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003878 PyOS_AfterFork();
Gregory P. Smith2a1c0272010-03-01 17:04:45 +00003879 } else {
3880 /* parent: release the import lock. */
3881 result = _PyImport_ReleaseLock();
3882 }
Fred Drake8cef4cf2000-06-28 16:40:38 +00003883 if (pid == -1)
3884 return posix_error();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003885 if (result < 0) {
3886 /* Don't clobber the OSError if the fork failed. */
3887 PyErr_SetString(PyExc_RuntimeError,
3888 "not holding the import lock");
3889 return NULL;
3890 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003891 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003892}
3893#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003894
Guido van Rossumad0ee831995-03-01 10:34:45 +00003895#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003896PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003897"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003898Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003899
Barry Warsaw53699e91996-12-10 23:23:01 +00003900static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003901posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003902{
Christian Heimes217cfd12007-12-02 14:31:20 +00003903 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003904}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003905#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003906
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003907
Guido van Rossumad0ee831995-03-01 10:34:45 +00003908#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003909PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003910"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003911Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003912
Barry Warsaw53699e91996-12-10 23:23:01 +00003913static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003914posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003915{
Christian Heimes217cfd12007-12-02 14:31:20 +00003916 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003917}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003918#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003919
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003920
Guido van Rossumad0ee831995-03-01 10:34:45 +00003921#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003922PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003923"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003924Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003925
Barry Warsaw53699e91996-12-10 23:23:01 +00003926static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003927posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003928{
Christian Heimes217cfd12007-12-02 14:31:20 +00003929 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003930}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003931#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003932
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003933
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003934PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003935"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003936Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003937
Barry Warsaw53699e91996-12-10 23:23:01 +00003938static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003939posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003940{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003941 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003942}
3943
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003944
Fred Drakec9680921999-12-13 16:37:25 +00003945#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003946PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003947"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003948Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003949
3950static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003951posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003952{
3953 PyObject *result = NULL;
3954
Fred Drakec9680921999-12-13 16:37:25 +00003955#ifdef NGROUPS_MAX
3956#define MAX_GROUPS NGROUPS_MAX
3957#else
3958 /* defined to be 16 on Solaris7, so this should be a small number */
3959#define MAX_GROUPS 64
3960#endif
3961 gid_t grouplist[MAX_GROUPS];
3962 int n;
3963
3964 n = getgroups(MAX_GROUPS, grouplist);
3965 if (n < 0)
3966 posix_error();
3967 else {
3968 result = PyList_New(n);
3969 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003970 int i;
3971 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003972 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003973 if (o == NULL) {
3974 Py_DECREF(result);
3975 result = NULL;
3976 break;
3977 }
3978 PyList_SET_ITEM(result, i, o);
3979 }
3980 }
3981 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003982
Fred Drakec9680921999-12-13 16:37:25 +00003983 return result;
3984}
3985#endif
3986
Antoine Pitroub7572f02009-12-02 20:46:48 +00003987#ifdef HAVE_INITGROUPS
3988PyDoc_STRVAR(posix_initgroups__doc__,
3989"initgroups(username, gid) -> None\n\n\
3990Call the system initgroups() to initialize the group access list with all of\n\
3991the groups of which the specified username is a member, plus the specified\n\
3992group id.");
3993
3994static PyObject *
3995posix_initgroups(PyObject *self, PyObject *args)
3996{
3997 char *username;
3998 long gid;
3999
4000 if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid))
4001 return NULL;
4002
4003 if (initgroups(username, (gid_t) gid) == -1)
4004 return PyErr_SetFromErrno(PyExc_OSError);
4005
4006 Py_INCREF(Py_None);
4007 return Py_None;
4008}
4009#endif
4010
Martin v. Löwis606edc12002-06-13 21:09:11 +00004011#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004012PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004013"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004014Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004015
4016static PyObject *
4017posix_getpgid(PyObject *self, PyObject *args)
4018{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004019 pid_t pid, pgid;
Gregory P. Smith84508572010-03-14 18:56:11 +00004020 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
Martin v. Löwis606edc12002-06-13 21:09:11 +00004021 return NULL;
4022 pgid = getpgid(pid);
4023 if (pgid < 0)
4024 return posix_error();
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004025 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004026}
4027#endif /* HAVE_GETPGID */
4028
4029
Guido van Rossumb6775db1994-08-01 11:34:53 +00004030#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004031PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004032"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004033Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004034
Barry Warsaw53699e91996-12-10 23:23:01 +00004035static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004036posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004037{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004038#ifdef GETPGRP_HAVE_ARG
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004039 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004040#else /* GETPGRP_HAVE_ARG */
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004041 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004042#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004043}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004044#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004045
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004046
Guido van Rossumb6775db1994-08-01 11:34:53 +00004047#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004048PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004049"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004050Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004051
Barry Warsaw53699e91996-12-10 23:23:01 +00004052static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004053posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004054{
Guido van Rossum64933891994-10-20 21:56:42 +00004055#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00004056 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004057#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004058 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004059#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00004060 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004061 Py_INCREF(Py_None);
4062 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004063}
4064
Guido van Rossumb6775db1994-08-01 11:34:53 +00004065#endif /* HAVE_SETPGRP */
4066
Guido van Rossumad0ee831995-03-01 10:34:45 +00004067#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004068PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004069"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004070Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004071
Barry Warsaw53699e91996-12-10 23:23:01 +00004072static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004073posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004074{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004075 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004076}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004077#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004078
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004079
Fred Drake12c6e2d1999-12-14 21:25:03 +00004080#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004081PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004082"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004083Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004084
4085static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004086posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004087{
Neal Norwitze241ce82003-02-17 18:17:05 +00004088 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00004089 char *name;
4090 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004091
Fred Drakea30680b2000-12-06 21:24:28 +00004092 errno = 0;
4093 name = getlogin();
4094 if (name == NULL) {
4095 if (errno)
4096 posix_error();
4097 else
4098 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00004099 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00004100 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00004101 else
Neal Norwitz93c56822007-08-26 07:10:06 +00004102 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00004103 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004104
Fred Drake12c6e2d1999-12-14 21:25:03 +00004105 return result;
4106}
4107#endif
4108
Guido van Rossumad0ee831995-03-01 10:34:45 +00004109#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004110PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004111"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004112Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004113
Barry Warsaw53699e91996-12-10 23:23:01 +00004114static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004115posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004116{
Christian Heimes217cfd12007-12-02 14:31:20 +00004117 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004118}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004119#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004120
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004121
Guido van Rossumad0ee831995-03-01 10:34:45 +00004122#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004123PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004124"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004125Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004126
Barry Warsaw53699e91996-12-10 23:23:01 +00004127static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004128posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004129{
Christian Heimes292d3512008-02-03 16:51:08 +00004130 pid_t pid;
4131 int sig;
Gregory P. Smith84508572010-03-14 18:56:11 +00004132 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00004133 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004134#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004135 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4136 APIRET rc;
4137 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004138 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004139
4140 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4141 APIRET rc;
4142 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004143 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004144
4145 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004146 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004147#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004148 if (kill(pid, sig) == -1)
4149 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004150#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004151 Py_INCREF(Py_None);
4152 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004153}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004154#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004155
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004156#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004157PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004158"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004159Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004160
4161static PyObject *
4162posix_killpg(PyObject *self, PyObject *args)
4163{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004164 int sig;
4165 pid_t pgid;
4166 /* XXX some man pages make the `pgid` parameter an int, others
4167 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4168 take the same type. Moreover, pid_t is always at least as wide as
4169 int (else compilation of this module fails), which is safe. */
Gregory P. Smith84508572010-03-14 18:56:11 +00004170 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004171 return NULL;
4172 if (killpg(pgid, sig) == -1)
4173 return posix_error();
4174 Py_INCREF(Py_None);
4175 return Py_None;
4176}
4177#endif
4178
Brian Curtineb24d742010-04-12 17:16:38 +00004179#ifdef MS_WINDOWS
4180PyDoc_STRVAR(win32_kill__doc__,
4181"kill(pid, sig)\n\n\
4182Kill a process with a signal.");
4183
4184static PyObject *
4185win32_kill(PyObject *self, PyObject *args)
4186{
4187 PyObject *result, handle_obj;
4188 DWORD pid, sig, err;
4189 HANDLE handle;
4190
4191 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4192 return NULL;
4193
4194 /* Console processes which share a common console can be sent CTRL+C or
4195 CTRL+BREAK events, provided they handle said events. */
4196 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4197 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4198 err = GetLastError();
4199 PyErr_SetFromWindowsErr(err);
4200 }
4201 else
4202 Py_RETURN_NONE;
4203 }
4204
4205 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4206 attempt to open and terminate the process. */
4207 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4208 if (handle == NULL) {
4209 err = GetLastError();
4210 return PyErr_SetFromWindowsErr(err);
4211 }
4212
4213 if (TerminateProcess(handle, sig) == 0) {
4214 err = GetLastError();
4215 result = PyErr_SetFromWindowsErr(err);
4216 } else {
4217 Py_INCREF(Py_None);
4218 result = Py_None;
4219 }
4220
4221 CloseHandle(handle);
4222 return result;
4223}
4224#endif /* MS_WINDOWS */
4225
Guido van Rossumc0125471996-06-28 18:55:32 +00004226#ifdef HAVE_PLOCK
4227
4228#ifdef HAVE_SYS_LOCK_H
4229#include <sys/lock.h>
4230#endif
4231
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004232PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004233"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004234Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004235
Barry Warsaw53699e91996-12-10 23:23:01 +00004236static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004237posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004238{
4239 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004240 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004241 return NULL;
4242 if (plock(op) == -1)
4243 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004244 Py_INCREF(Py_None);
4245 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004246}
4247#endif
4248
Guido van Rossumb6775db1994-08-01 11:34:53 +00004249#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004250PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004251"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004252Set the current process's user id.");
4253
Barry Warsaw53699e91996-12-10 23:23:01 +00004254static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004255posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004256{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004257 long uid_arg;
4258 uid_t uid;
4259 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004260 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004261 uid = uid_arg;
4262 if (uid != uid_arg) {
4263 PyErr_SetString(PyExc_OverflowError, "user id too big");
4264 return NULL;
4265 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004266 if (setuid(uid) < 0)
4267 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004268 Py_INCREF(Py_None);
4269 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004270}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004271#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004272
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004273
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004274#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004275PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004276"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004277Set the current process's effective user id.");
4278
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004279static PyObject *
4280posix_seteuid (PyObject *self, PyObject *args)
4281{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004282 long euid_arg;
4283 uid_t euid;
4284 if (!PyArg_ParseTuple(args, "l", &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004285 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004286 euid = euid_arg;
4287 if (euid != euid_arg) {
4288 PyErr_SetString(PyExc_OverflowError, "user id too big");
4289 return NULL;
4290 }
4291 if (seteuid(euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004292 return posix_error();
4293 } else {
4294 Py_INCREF(Py_None);
4295 return Py_None;
4296 }
4297}
4298#endif /* HAVE_SETEUID */
4299
4300#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004301PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004302"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004303Set the current process's effective group id.");
4304
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004305static PyObject *
4306posix_setegid (PyObject *self, PyObject *args)
4307{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004308 long egid_arg;
4309 gid_t egid;
4310 if (!PyArg_ParseTuple(args, "l", &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004311 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004312 egid = egid_arg;
4313 if (egid != egid_arg) {
4314 PyErr_SetString(PyExc_OverflowError, "group id too big");
4315 return NULL;
4316 }
4317 if (setegid(egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004318 return posix_error();
4319 } else {
4320 Py_INCREF(Py_None);
4321 return Py_None;
4322 }
4323}
4324#endif /* HAVE_SETEGID */
4325
4326#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004327PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004328"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004329Set the current process's real and effective user ids.");
4330
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004331static PyObject *
4332posix_setreuid (PyObject *self, PyObject *args)
4333{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004334 long ruid_arg, euid_arg;
4335 uid_t ruid, euid;
4336 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004337 return NULL;
Gregory P. Smithc78d79c2010-03-01 05:54:14 +00004338 if (ruid_arg == -1)
4339 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4340 else
4341 ruid = ruid_arg; /* otherwise, assign from our long */
4342 if (euid_arg == -1)
4343 euid = (uid_t)-1;
4344 else
4345 euid = euid_arg;
4346 if ((euid_arg != -1 && euid != euid_arg) ||
4347 (ruid_arg != -1 && ruid != ruid_arg)) {
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004348 PyErr_SetString(PyExc_OverflowError, "user id too big");
4349 return NULL;
4350 }
4351 if (setreuid(ruid, euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004352 return posix_error();
4353 } else {
4354 Py_INCREF(Py_None);
4355 return Py_None;
4356 }
4357}
4358#endif /* HAVE_SETREUID */
4359
4360#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004361PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004362"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004363Set the current process's real and effective group ids.");
4364
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004365static PyObject *
4366posix_setregid (PyObject *self, PyObject *args)
4367{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004368 long rgid_arg, egid_arg;
4369 gid_t rgid, egid;
4370 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004371 return NULL;
Gregory P. Smithc78d79c2010-03-01 05:54:14 +00004372 if (rgid_arg == -1)
4373 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4374 else
4375 rgid = rgid_arg; /* otherwise, assign from our long */
4376 if (egid_arg == -1)
4377 egid = (gid_t)-1;
4378 else
4379 egid = egid_arg;
4380 if ((egid_arg != -1 && egid != egid_arg) ||
4381 (rgid_arg != -1 && rgid != rgid_arg)) {
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004382 PyErr_SetString(PyExc_OverflowError, "group id too big");
4383 return NULL;
4384 }
4385 if (setregid(rgid, egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004386 return posix_error();
4387 } else {
4388 Py_INCREF(Py_None);
4389 return Py_None;
4390 }
4391}
4392#endif /* HAVE_SETREGID */
4393
Guido van Rossumb6775db1994-08-01 11:34:53 +00004394#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004395PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004396"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004397Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004398
Barry Warsaw53699e91996-12-10 23:23:01 +00004399static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004400posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004401{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004402 long gid_arg;
4403 gid_t gid;
4404 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004405 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004406 gid = gid_arg;
4407 if (gid != gid_arg) {
4408 PyErr_SetString(PyExc_OverflowError, "group id too big");
4409 return NULL;
4410 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004411 if (setgid(gid) < 0)
4412 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004413 Py_INCREF(Py_None);
4414 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004415}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004416#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004417
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004418#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004419PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004420"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004421Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004422
4423static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004424posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004425{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004426 int i, len;
4427 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004428
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004429 if (!PySequence_Check(groups)) {
4430 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4431 return NULL;
4432 }
4433 len = PySequence_Size(groups);
4434 if (len > MAX_GROUPS) {
4435 PyErr_SetString(PyExc_ValueError, "too many groups");
4436 return NULL;
4437 }
4438 for(i = 0; i < len; i++) {
4439 PyObject *elem;
4440 elem = PySequence_GetItem(groups, i);
4441 if (!elem)
4442 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004443 if (!PyLong_Check(elem)) {
4444 PyErr_SetString(PyExc_TypeError,
4445 "groups must be integers");
4446 Py_DECREF(elem);
4447 return NULL;
4448 } else {
4449 unsigned long x = PyLong_AsUnsignedLong(elem);
4450 if (PyErr_Occurred()) {
4451 PyErr_SetString(PyExc_TypeError,
4452 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004453 Py_DECREF(elem);
4454 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004455 }
Georg Brandla13c2442005-11-22 19:30:31 +00004456 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004457 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004458 if (grouplist[i] != x) {
4459 PyErr_SetString(PyExc_TypeError,
4460 "group id too big");
4461 Py_DECREF(elem);
4462 return NULL;
4463 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004464 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004465 Py_DECREF(elem);
4466 }
4467
4468 if (setgroups(len, grouplist) < 0)
4469 return posix_error();
4470 Py_INCREF(Py_None);
4471 return Py_None;
4472}
4473#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004474
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004475#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4476static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004477wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004478{
4479 PyObject *result;
4480 static PyObject *struct_rusage;
4481
4482 if (pid == -1)
4483 return posix_error();
4484
4485 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004486 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004487 if (m == NULL)
4488 return NULL;
4489 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4490 Py_DECREF(m);
4491 if (struct_rusage == NULL)
4492 return NULL;
4493 }
4494
4495 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4496 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4497 if (!result)
4498 return NULL;
4499
4500#ifndef doubletime
4501#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4502#endif
4503
4504 PyStructSequence_SET_ITEM(result, 0,
4505 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4506 PyStructSequence_SET_ITEM(result, 1,
4507 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4508#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004509 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004510 SET_INT(result, 2, ru->ru_maxrss);
4511 SET_INT(result, 3, ru->ru_ixrss);
4512 SET_INT(result, 4, ru->ru_idrss);
4513 SET_INT(result, 5, ru->ru_isrss);
4514 SET_INT(result, 6, ru->ru_minflt);
4515 SET_INT(result, 7, ru->ru_majflt);
4516 SET_INT(result, 8, ru->ru_nswap);
4517 SET_INT(result, 9, ru->ru_inblock);
4518 SET_INT(result, 10, ru->ru_oublock);
4519 SET_INT(result, 11, ru->ru_msgsnd);
4520 SET_INT(result, 12, ru->ru_msgrcv);
4521 SET_INT(result, 13, ru->ru_nsignals);
4522 SET_INT(result, 14, ru->ru_nvcsw);
4523 SET_INT(result, 15, ru->ru_nivcsw);
4524#undef SET_INT
4525
4526 if (PyErr_Occurred()) {
4527 Py_DECREF(result);
4528 return NULL;
4529 }
4530
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004531 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004532}
4533#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4534
4535#ifdef HAVE_WAIT3
4536PyDoc_STRVAR(posix_wait3__doc__,
4537"wait3(options) -> (pid, status, rusage)\n\n\
4538Wait for completion of a child process.");
4539
4540static PyObject *
4541posix_wait3(PyObject *self, PyObject *args)
4542{
Christian Heimes292d3512008-02-03 16:51:08 +00004543 pid_t pid;
4544 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004545 struct rusage ru;
4546 WAIT_TYPE status;
4547 WAIT_STATUS_INT(status) = 0;
4548
4549 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4550 return NULL;
4551
4552 Py_BEGIN_ALLOW_THREADS
4553 pid = wait3(&status, options, &ru);
4554 Py_END_ALLOW_THREADS
4555
4556 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4557}
4558#endif /* HAVE_WAIT3 */
4559
4560#ifdef HAVE_WAIT4
4561PyDoc_STRVAR(posix_wait4__doc__,
4562"wait4(pid, options) -> (pid, status, rusage)\n\n\
4563Wait for completion of a given child process.");
4564
4565static PyObject *
4566posix_wait4(PyObject *self, PyObject *args)
4567{
Christian Heimes292d3512008-02-03 16:51:08 +00004568 pid_t pid;
4569 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004570 struct rusage ru;
4571 WAIT_TYPE status;
4572 WAIT_STATUS_INT(status) = 0;
4573
Gregory P. Smith84508572010-03-14 18:56:11 +00004574 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004575 return NULL;
4576
4577 Py_BEGIN_ALLOW_THREADS
4578 pid = wait4(pid, &status, options, &ru);
4579 Py_END_ALLOW_THREADS
4580
4581 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4582}
4583#endif /* HAVE_WAIT4 */
4584
Guido van Rossumb6775db1994-08-01 11:34:53 +00004585#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004586PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004587"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004588Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004589
Barry Warsaw53699e91996-12-10 23:23:01 +00004590static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004591posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004592{
Christian Heimes292d3512008-02-03 16:51:08 +00004593 pid_t pid;
4594 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004595 WAIT_TYPE status;
4596 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004597
Gregory P. Smith84508572010-03-14 18:56:11 +00004598 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004599 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004600 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004601 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004602 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004603 if (pid == -1)
4604 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004605
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004606 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004607}
4608
Tim Petersab034fa2002-02-01 11:27:43 +00004609#elif defined(HAVE_CWAIT)
4610
4611/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004612PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004613"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004614"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004615
4616static PyObject *
4617posix_waitpid(PyObject *self, PyObject *args)
4618{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004619 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004620 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004621
Gregory P. Smith84508572010-03-14 18:56:11 +00004622 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
Tim Petersab034fa2002-02-01 11:27:43 +00004623 return NULL;
4624 Py_BEGIN_ALLOW_THREADS
4625 pid = _cwait(&status, pid, options);
4626 Py_END_ALLOW_THREADS
4627 if (pid == -1)
4628 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004629
4630 /* shift the status left a byte so this is more like the POSIX waitpid */
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004631 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004632}
4633#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004634
Guido van Rossumad0ee831995-03-01 10:34:45 +00004635#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004636PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004637"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004638Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004639
Barry Warsaw53699e91996-12-10 23:23:01 +00004640static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004641posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004642{
Christian Heimes292d3512008-02-03 16:51:08 +00004643 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004644 WAIT_TYPE status;
4645 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004646
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004647 Py_BEGIN_ALLOW_THREADS
4648 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004649 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004650 if (pid == -1)
4651 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004652
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004653 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004654}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004655#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004656
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004657
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004658PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004659"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004660Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004661
Barry Warsaw53699e91996-12-10 23:23:01 +00004662static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004663posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004664{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004665#ifdef HAVE_LSTAT
Martin v. Löwis011e8422009-05-05 04:43:17 +00004666 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004667#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004668#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00004669 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004670#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00004671 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004672#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004673#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004674}
4675
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004676
Guido van Rossumb6775db1994-08-01 11:34:53 +00004677#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004678PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004679"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004680Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004681
Barry Warsaw53699e91996-12-10 23:23:01 +00004682static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004683posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004684{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004685 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004686 char buf[MAXPATHLEN];
Martin v. Löwis011e8422009-05-05 04:43:17 +00004687 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004688 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004689 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004690 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004691
Martin v. Löwis011e8422009-05-05 04:43:17 +00004692 if (!PyArg_ParseTuple(args, "O&:readlink",
4693 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004694 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004695 path = bytes2str(opath, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004696 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004697 if (v == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00004698 release_bytes(opath);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004699 return NULL;
4700 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004701
4702 if (PyUnicode_Check(v)) {
4703 arg_is_unicode = 1;
4704 }
4705 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004706
Barry Warsaw53699e91996-12-10 23:23:01 +00004707 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004708 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004709 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004710 if (n < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004711 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004712
Martin v. Löwis011e8422009-05-05 04:43:17 +00004713 release_bytes(opath);
Christian Heimes72b710a2008-05-26 13:28:38 +00004714 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004715 if (arg_is_unicode) {
4716 PyObject *w;
4717
4718 w = PyUnicode_FromEncodedObject(v,
4719 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00004720 "surrogateescape");
Thomas Wouters89f507f2006-12-13 04:49:30 +00004721 if (w != NULL) {
4722 Py_DECREF(v);
4723 v = w;
4724 }
4725 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00004726 v = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004727 }
4728 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004729 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004730}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004731#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004732
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004733
Guido van Rossumb6775db1994-08-01 11:34:53 +00004734#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004735PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004736"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004737Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004738
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004739static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004740posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004741{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004742 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004743}
4744#endif /* HAVE_SYMLINK */
4745
4746
4747#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004748#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4749static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004750system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004751{
4752 ULONG value = 0;
4753
4754 Py_BEGIN_ALLOW_THREADS
4755 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4756 Py_END_ALLOW_THREADS
4757
4758 return value;
4759}
4760
4761static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004762posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004763{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004764 /* Currently Only Uptime is Provided -- Others Later */
4765 return Py_BuildValue("ddddd",
4766 (double)0 /* t.tms_utime / HZ */,
4767 (double)0 /* t.tms_stime / HZ */,
4768 (double)0 /* t.tms_cutime / HZ */,
4769 (double)0 /* t.tms_cstime / HZ */,
4770 (double)system_uptime() / 1000);
4771}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004772#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004773#define NEED_TICKS_PER_SECOND
4774static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004775static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004776posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004777{
4778 struct tms t;
4779 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004780 errno = 0;
4781 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004782 if (c == (clock_t) -1)
4783 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004784 return Py_BuildValue("ddddd",
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004785 (double)t.tms_utime / ticks_per_second,
4786 (double)t.tms_stime / ticks_per_second,
4787 (double)t.tms_cutime / ticks_per_second,
4788 (double)t.tms_cstime / ticks_per_second,
4789 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004790}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004791#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004792#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004793
4794
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004795#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004796#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004797static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004798posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004799{
4800 FILETIME create, exit, kernel, user;
4801 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004802 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004803 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4804 /* The fields of a FILETIME structure are the hi and lo part
4805 of a 64-bit value expressed in 100 nanosecond units.
4806 1e7 is one second in such units; 1e-7 the inverse.
4807 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4808 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004809 return Py_BuildValue(
4810 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004811 (double)(user.dwHighDateTime*429.4967296 +
4812 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004813 (double)(kernel.dwHighDateTime*429.4967296 +
4814 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004815 (double)0,
4816 (double)0,
4817 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004818}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004819#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004820
4821#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004822PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004823"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004824Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004825#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004826
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004827
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004828#ifdef HAVE_GETSID
4829PyDoc_STRVAR(posix_getsid__doc__,
4830"getsid(pid) -> sid\n\n\
4831Call the system call getsid().");
4832
4833static PyObject *
4834posix_getsid(PyObject *self, PyObject *args)
4835{
Christian Heimes292d3512008-02-03 16:51:08 +00004836 pid_t pid;
4837 int sid;
Gregory P. Smith84508572010-03-14 18:56:11 +00004838 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004839 return NULL;
4840 sid = getsid(pid);
4841 if (sid < 0)
4842 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004843 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004844}
4845#endif /* HAVE_GETSID */
4846
4847
Guido van Rossumb6775db1994-08-01 11:34:53 +00004848#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004849PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004850"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004851Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004852
Barry Warsaw53699e91996-12-10 23:23:01 +00004853static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004854posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004855{
Guido van Rossum687dd131993-05-17 08:34:16 +00004856 if (setsid() < 0)
4857 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004858 Py_INCREF(Py_None);
4859 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004860}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004861#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004862
Guido van Rossumb6775db1994-08-01 11:34:53 +00004863#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004864PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004865"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004866Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004867
Barry Warsaw53699e91996-12-10 23:23:01 +00004868static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004869posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004870{
Christian Heimes292d3512008-02-03 16:51:08 +00004871 pid_t pid;
4872 int pgrp;
Gregory P. Smith84508572010-03-14 18:56:11 +00004873 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004874 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004875 if (setpgid(pid, pgrp) < 0)
4876 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004877 Py_INCREF(Py_None);
4878 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004879}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004880#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004881
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004882
Guido van Rossumb6775db1994-08-01 11:34:53 +00004883#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004884PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004885"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004886Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004887
Barry Warsaw53699e91996-12-10 23:23:01 +00004888static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004889posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004890{
Christian Heimes15ebc882008-02-04 18:48:49 +00004891 int fd;
4892 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004893 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004894 return NULL;
4895 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004896 if (pgid < 0)
4897 return posix_error();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004898 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004899}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004900#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004901
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004902
Guido van Rossumb6775db1994-08-01 11:34:53 +00004903#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004904PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004905"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004906Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004907
Barry Warsaw53699e91996-12-10 23:23:01 +00004908static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004909posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004910{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004911 int fd;
4912 pid_t pgid;
Gregory P. Smith84508572010-03-14 18:56:11 +00004913 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004914 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004915 if (tcsetpgrp(fd, pgid) < 0)
4916 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004917 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004918 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004919}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004920#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004921
Guido van Rossum687dd131993-05-17 08:34:16 +00004922/* Functions acting on file descriptors */
4923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004924PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004925"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004926Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004927
Barry Warsaw53699e91996-12-10 23:23:01 +00004928static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004929posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004930{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004931 PyObject *ofile;
4932 char *file;
Guido van Rossum687dd131993-05-17 08:34:16 +00004933 int flag;
4934 int mode = 0777;
4935 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004936
4937#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00004938 PyUnicodeObject *po;
4939 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4940 Py_BEGIN_ALLOW_THREADS
4941 /* PyUnicode_AS_UNICODE OK without thread
4942 lock as it is a simple dereference. */
4943 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4944 Py_END_ALLOW_THREADS
4945 if (fd < 0)
4946 return posix_error();
4947 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004948 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00004949 /* Drop the argument parsing error as narrow strings
4950 are also valid. */
4951 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004952#endif
4953
Martin v. Löwis011e8422009-05-05 04:43:17 +00004954 if (!PyArg_ParseTuple(args, "O&i|i",
4955 PyUnicode_FSConverter, &ofile,
Mark Hammondef8b6542001-05-13 08:04:26 +00004956 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004957 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004958 file = bytes2str(ofile, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00004959 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004960 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004961 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004962 if (fd < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004963 return posix_error_with_allocated_filename(ofile);
4964 release_bytes(ofile);
Christian Heimes217cfd12007-12-02 14:31:20 +00004965 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004966}
4967
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004968
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004969PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004970"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004971Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004972
Barry Warsaw53699e91996-12-10 23:23:01 +00004973static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004974posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004975{
4976 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004977 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004978 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004979 if (!_PyVerify_fd(fd))
4980 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004981 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004982 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004983 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004984 if (res < 0)
4985 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004986 Py_INCREF(Py_None);
4987 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004988}
4989
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004990
Christian Heimesfdab48e2008-01-20 09:06:41 +00004991PyDoc_STRVAR(posix_closerange__doc__,
4992"closerange(fd_low, fd_high)\n\n\
4993Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4994
4995static PyObject *
4996posix_closerange(PyObject *self, PyObject *args)
4997{
4998 int fd_from, fd_to, i;
4999 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5000 return NULL;
5001 Py_BEGIN_ALLOW_THREADS
5002 for (i = fd_from; i < fd_to; i++)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005003 if (_PyVerify_fd(i))
5004 close(i);
Christian Heimesfdab48e2008-01-20 09:06:41 +00005005 Py_END_ALLOW_THREADS
5006 Py_RETURN_NONE;
5007}
5008
5009
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005010PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005011"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005012Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005013
Barry Warsaw53699e91996-12-10 23:23:01 +00005014static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005015posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005016{
5017 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005018 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005019 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005020 if (!_PyVerify_fd(fd))
5021 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005022 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005023 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005024 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005025 if (fd < 0)
5026 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005027 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005028}
5029
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005031PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005032"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005033Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005034
Barry Warsaw53699e91996-12-10 23:23:01 +00005035static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005036posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005037{
5038 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005039 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005040 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005041 if (!_PyVerify_fd_dup2(fd, fd2))
5042 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005043 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005044 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005045 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005046 if (res < 0)
5047 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005048 Py_INCREF(Py_None);
5049 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005050}
5051
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005052
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005053PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005054"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005055Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005056
Barry Warsaw53699e91996-12-10 23:23:01 +00005057static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005058posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005059{
5060 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005061#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005062 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005063#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005064 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005065#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005066 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005067 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005068 return NULL;
5069#ifdef SEEK_SET
5070 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5071 switch (how) {
5072 case 0: how = SEEK_SET; break;
5073 case 1: how = SEEK_CUR; break;
5074 case 2: how = SEEK_END; break;
5075 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005076#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005077
5078#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005079 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005080#else
5081 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005082 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005083#endif
5084 if (PyErr_Occurred())
5085 return NULL;
5086
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005087 if (!_PyVerify_fd(fd))
5088 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005089 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005090#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005091 res = _lseeki64(fd, pos, how);
5092#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005093 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005094#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005095 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005096 if (res < 0)
5097 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005098
5099#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005100 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005101#else
5102 return PyLong_FromLongLong(res);
5103#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005104}
5105
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005106
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005107PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005108"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005109Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005110
Barry Warsaw53699e91996-12-10 23:23:01 +00005111static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005112posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005113{
Guido van Rossum572dbf82007-04-27 23:53:51 +00005114 int fd, size;
5115 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005116 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005117 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005118 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005119 if (size < 0) {
5120 errno = EINVAL;
5121 return posix_error();
5122 }
Christian Heimes72b710a2008-05-26 13:28:38 +00005123 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005124 if (buffer == NULL)
5125 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005126 if (!_PyVerify_fd(fd))
5127 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005128 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00005129 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005130 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005131 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005132 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005133 return posix_error();
5134 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005135 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00005136 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005137 return buffer;
5138}
5139
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005140
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005141PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005142"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005143Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005144
Barry Warsaw53699e91996-12-10 23:23:01 +00005145static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005146posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005147{
Martin v. Löwis423be952008-08-13 15:53:07 +00005148 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005149 int fd;
5150 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005151
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +00005152 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00005153 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005154 if (!_PyVerify_fd(fd))
5155 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005156 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005157 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00005158 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005159 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00005160 if (size < 0)
5161 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005162 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005163}
5164
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005165
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005166PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005167"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005168Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005169
Barry Warsaw53699e91996-12-10 23:23:01 +00005170static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005171posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005172{
5173 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005174 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005175 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005176 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005177 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005178#ifdef __VMS
5179 /* on OpenVMS we must ensure that all bytes are written to the file */
5180 fsync(fd);
5181#endif
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005182 if (!_PyVerify_fd(fd))
5183 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005184 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005185 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005186 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005187 if (res != 0) {
5188#ifdef MS_WINDOWS
5189 return win32_error("fstat", NULL);
5190#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005191 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005192#endif
5193 }
Tim Peters5aa91602002-01-30 05:46:57 +00005194
Martin v. Löwis14694662006-02-03 12:54:16 +00005195 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005196}
5197
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005198PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005199"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005200Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005201connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005202
5203static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005204posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005205{
5206 int fd;
5207 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5208 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005209 if (!_PyVerify_fd(fd))
5210 return PyBool_FromLong(0);
Fred Drake106c1a02002-04-23 15:58:02 +00005211 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005212}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005213
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005214#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005215PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005216"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005217Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005218
Barry Warsaw53699e91996-12-10 23:23:01 +00005219static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005220posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005221{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005222#if defined(PYOS_OS2)
5223 HFILE read, write;
5224 APIRET rc;
5225
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005226 Py_BEGIN_ALLOW_THREADS
5227 rc = DosCreatePipe( &read, &write, 4096);
5228 Py_END_ALLOW_THREADS
5229 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005230 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005231
5232 return Py_BuildValue("(ii)", read, write);
5233#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005234#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005235 int fds[2];
5236 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005237 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005238 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005239 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005240 if (res != 0)
5241 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005242 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005243#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005244 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005245 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005246 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005247 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005248 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005249 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005250 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005251 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005252 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5253 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005254 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005255#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005256#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005257}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005258#endif /* HAVE_PIPE */
5259
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005260
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005261#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005262PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005263"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005264Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005265
Barry Warsaw53699e91996-12-10 23:23:01 +00005266static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005267posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005268{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005269 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005270 int mode = 0666;
5271 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005272 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005273 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005274 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005275 res = mkfifo(filename, mode);
5276 Py_END_ALLOW_THREADS
5277 if (res < 0)
5278 return posix_error();
5279 Py_INCREF(Py_None);
5280 return Py_None;
5281}
5282#endif
5283
5284
Neal Norwitz11690112002-07-30 01:08:28 +00005285#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005286PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005287"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005288Create a filesystem node (file, device special file or named pipe)\n\
5289named filename. mode specifies both the permissions to use and the\n\
5290type of node to be created, being combined (bitwise OR) with one of\n\
5291S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005292device defines the newly created device special file (probably using\n\
5293os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005294
5295
5296static PyObject *
5297posix_mknod(PyObject *self, PyObject *args)
5298{
5299 char *filename;
5300 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005301 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005302 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005303 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005304 return NULL;
5305 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005306 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005307 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005308 if (res < 0)
5309 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005310 Py_INCREF(Py_None);
5311 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005312}
5313#endif
5314
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005315#ifdef HAVE_DEVICE_MACROS
5316PyDoc_STRVAR(posix_major__doc__,
5317"major(device) -> major number\n\
5318Extracts a device major number from a raw device number.");
5319
5320static PyObject *
5321posix_major(PyObject *self, PyObject *args)
5322{
5323 int device;
5324 if (!PyArg_ParseTuple(args, "i:major", &device))
5325 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005326 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005327}
5328
5329PyDoc_STRVAR(posix_minor__doc__,
5330"minor(device) -> minor number\n\
5331Extracts a device minor number from a raw device number.");
5332
5333static PyObject *
5334posix_minor(PyObject *self, PyObject *args)
5335{
5336 int device;
5337 if (!PyArg_ParseTuple(args, "i:minor", &device))
5338 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005339 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005340}
5341
5342PyDoc_STRVAR(posix_makedev__doc__,
5343"makedev(major, minor) -> device number\n\
5344Composes a raw device number from the major and minor device numbers.");
5345
5346static PyObject *
5347posix_makedev(PyObject *self, PyObject *args)
5348{
5349 int major, minor;
5350 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5351 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005352 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005353}
5354#endif /* device macros */
5355
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005356
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005357#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005358PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005359"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005360Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005361
Barry Warsaw53699e91996-12-10 23:23:01 +00005362static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005363posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005364{
5365 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005366 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005367 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005368 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005369
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005370 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005371 return NULL;
5372
5373#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005374 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005375#else
5376 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005377 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005378#endif
5379 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005380 return NULL;
5381
Barry Warsaw53699e91996-12-10 23:23:01 +00005382 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005383 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005384 Py_END_ALLOW_THREADS
Benjamin Peterson9053d752009-01-19 17:53:36 +00005385 if (res < 0)
5386 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005387 Py_INCREF(Py_None);
5388 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005389}
5390#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005391
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005392#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005393PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005394"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005395Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005396
Fred Drake762e2061999-08-26 17:23:54 +00005397/* Save putenv() parameters as values here, so we can collect them when they
5398 * get re-set with another call for the same key. */
5399static PyObject *posix_putenv_garbage;
5400
Tim Peters5aa91602002-01-30 05:46:57 +00005401static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005402posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005403{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005404#ifdef MS_WINDOWS
5405 wchar_t *s1, *s2;
5406 wchar_t *newenv;
5407#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00005408 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005409 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005410 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005411#endif
Fred Drake762e2061999-08-26 17:23:54 +00005412 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005413 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005414
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005415#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00005416 if (!PyArg_ParseTuple(args,
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005417 "uu:putenv",
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005418 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005419 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005420#else
5421 if (!PyArg_ParseTuple(args,
5422 "O&O&:putenv",
5423 PyUnicode_FSConverter, &os1,
5424 PyUnicode_FSConverter, &os2))
5425 return NULL;
5426 s1 = bytes2str(os1, 1);
5427 s2 = bytes2str(os2, 1);
5428#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005429
5430#if defined(PYOS_OS2)
5431 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5432 APIRET rc;
5433
Guido van Rossumd48f2521997-12-05 22:19:34 +00005434 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5435 if (rc != NO_ERROR)
5436 return os2_error(rc);
5437
5438 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5439 APIRET rc;
5440
Guido van Rossumd48f2521997-12-05 22:19:34 +00005441 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5442 if (rc != NO_ERROR)
5443 return os2_error(rc);
5444 } else {
5445#endif
Fred Drake762e2061999-08-26 17:23:54 +00005446 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005447 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005448 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005449#ifdef MS_WINDOWS
5450 len = wcslen(s1) + wcslen(s2) + 2;
5451 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5452#else
5453 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005454 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005455#endif
Fred Drake762e2061999-08-26 17:23:54 +00005456 if (newstr == NULL)
5457 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005458#ifdef MS_WINDOWS
5459 newenv = PyUnicode_AsUnicode(newstr);
5460 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5461 if (_wputenv(newenv)) {
5462 Py_DECREF(newstr);
5463 posix_error();
5464 return NULL;
5465 }
5466#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005467 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005468 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5469 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005470 Py_DECREF(newstr);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005471 release_bytes(os1);
5472 release_bytes(os2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005473 posix_error();
5474 return NULL;
5475 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005476#endif
Fred Drake762e2061999-08-26 17:23:54 +00005477 /* Install the first arg and newstr in posix_putenv_garbage;
5478 * this will cause previous value to be collected. This has to
5479 * happen after the real putenv() call because the old value
5480 * was still accessible until then. */
5481 if (PyDict_SetItem(posix_putenv_garbage,
5482 PyTuple_GET_ITEM(args, 0), newstr)) {
5483 /* really not much we can do; just leak */
5484 PyErr_Clear();
5485 }
5486 else {
5487 Py_DECREF(newstr);
5488 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005489
5490#if defined(PYOS_OS2)
5491 }
5492#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00005493#ifndef MS_WINDOWS
5494 release_bytes(os1);
5495 release_bytes(os2);
5496#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005497 Py_INCREF(Py_None);
5498 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005499}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005500#endif /* putenv */
5501
Guido van Rossumc524d952001-10-19 01:31:59 +00005502#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005503PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005504"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005505Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005506
5507static PyObject *
5508posix_unsetenv(PyObject *self, PyObject *args)
5509{
5510 char *s1;
5511
5512 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5513 return NULL;
5514
5515 unsetenv(s1);
5516
5517 /* Remove the key from posix_putenv_garbage;
5518 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005519 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005520 * old value was still accessible until then.
5521 */
5522 if (PyDict_DelItem(posix_putenv_garbage,
5523 PyTuple_GET_ITEM(args, 0))) {
5524 /* really not much we can do; just leak */
5525 PyErr_Clear();
5526 }
5527
5528 Py_INCREF(Py_None);
5529 return Py_None;
5530}
5531#endif /* unsetenv */
5532
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005533PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005534"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005535Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005536
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005537static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005538posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005539{
5540 int code;
5541 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005542 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005543 return NULL;
5544 message = strerror(code);
5545 if (message == NULL) {
5546 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005547 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005548 return NULL;
5549 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005550 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005551}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005552
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005553
Guido van Rossumc9641791998-08-04 15:26:23 +00005554#ifdef HAVE_SYS_WAIT_H
5555
Fred Drake106c1a02002-04-23 15:58:02 +00005556#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005557PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005558"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005559Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005560
5561static PyObject *
5562posix_WCOREDUMP(PyObject *self, PyObject *args)
5563{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005564 WAIT_TYPE status;
5565 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005566
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005567 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005568 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005569
5570 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005571}
5572#endif /* WCOREDUMP */
5573
5574#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005575PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005576"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005577Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005578job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005579
5580static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005581posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005582{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005583 WAIT_TYPE status;
5584 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005585
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005586 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005587 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005588
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005589 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005590}
5591#endif /* WIFCONTINUED */
5592
Guido van Rossumc9641791998-08-04 15:26:23 +00005593#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005594PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005595"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005596Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005597
5598static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005599posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005600{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005601 WAIT_TYPE status;
5602 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005603
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005604 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005605 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005606
Fred Drake106c1a02002-04-23 15:58:02 +00005607 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005608}
5609#endif /* WIFSTOPPED */
5610
5611#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005612PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005613"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005614Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005615
5616static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005617posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005618{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005619 WAIT_TYPE status;
5620 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005621
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005622 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005623 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005624
Fred Drake106c1a02002-04-23 15:58:02 +00005625 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005626}
5627#endif /* WIFSIGNALED */
5628
5629#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005630PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005631"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005632Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005633system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005634
5635static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005636posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005637{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005638 WAIT_TYPE status;
5639 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005640
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005641 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005642 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005643
Fred Drake106c1a02002-04-23 15:58:02 +00005644 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005645}
5646#endif /* WIFEXITED */
5647
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005648#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005649PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005650"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005651Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005652
5653static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005654posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005655{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005656 WAIT_TYPE status;
5657 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005658
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005659 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005660 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005661
Guido van Rossumc9641791998-08-04 15:26:23 +00005662 return Py_BuildValue("i", WEXITSTATUS(status));
5663}
5664#endif /* WEXITSTATUS */
5665
5666#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005667PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005668"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005669Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005670value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005671
5672static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005673posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005674{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005675 WAIT_TYPE status;
5676 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005677
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005678 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005679 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005680
Guido van Rossumc9641791998-08-04 15:26:23 +00005681 return Py_BuildValue("i", WTERMSIG(status));
5682}
5683#endif /* WTERMSIG */
5684
5685#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005686PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005687"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005688Return the signal that stopped the process that provided\n\
5689the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005690
5691static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005692posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005693{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005694 WAIT_TYPE status;
5695 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005696
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005697 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005698 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005699
Guido van Rossumc9641791998-08-04 15:26:23 +00005700 return Py_BuildValue("i", WSTOPSIG(status));
5701}
5702#endif /* WSTOPSIG */
5703
5704#endif /* HAVE_SYS_WAIT_H */
5705
5706
Thomas Wouters477c8d52006-05-27 19:21:47 +00005707#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005708#ifdef _SCO_DS
5709/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5710 needed definitions in sys/statvfs.h */
5711#define _SVID3
5712#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005713#include <sys/statvfs.h>
5714
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005715static PyObject*
5716_pystatvfs_fromstructstatvfs(struct statvfs st) {
5717 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5718 if (v == NULL)
5719 return NULL;
5720
5721#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005722 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5723 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5724 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5725 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5726 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5727 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5728 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5729 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5730 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5731 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005732#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005733 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5734 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005735 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005736 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005737 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005738 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005739 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005740 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005741 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005742 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005743 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005744 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005745 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005746 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005747 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5748 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005749#endif
5750
5751 return v;
5752}
5753
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005754PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005755"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005756Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005757
5758static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005759posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005760{
5761 int fd, res;
5762 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005763
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005764 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005765 return NULL;
5766 Py_BEGIN_ALLOW_THREADS
5767 res = fstatvfs(fd, &st);
5768 Py_END_ALLOW_THREADS
5769 if (res != 0)
5770 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005771
5772 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005773}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005774#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005775
5776
Thomas Wouters477c8d52006-05-27 19:21:47 +00005777#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005778#include <sys/statvfs.h>
5779
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005780PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005781"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005782Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005783
5784static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005785posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005786{
5787 char *path;
5788 int res;
5789 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005790 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005791 return NULL;
5792 Py_BEGIN_ALLOW_THREADS
5793 res = statvfs(path, &st);
5794 Py_END_ALLOW_THREADS
5795 if (res != 0)
5796 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005797
5798 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005799}
5800#endif /* HAVE_STATVFS */
5801
Fred Drakec9680921999-12-13 16:37:25 +00005802/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5803 * It maps strings representing configuration variable names to
5804 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005805 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005806 * rarely-used constants. There are three separate tables that use
5807 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005808 *
5809 * This code is always included, even if none of the interfaces that
5810 * need it are included. The #if hackery needed to avoid it would be
5811 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005812 */
5813struct constdef {
5814 char *name;
5815 long value;
5816};
5817
Fred Drake12c6e2d1999-12-14 21:25:03 +00005818static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005819conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005820 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005821{
Christian Heimes217cfd12007-12-02 14:31:20 +00005822 if (PyLong_Check(arg)) {
5823 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005824 return 1;
5825 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005826 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005827 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005828 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005829 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005830 size_t hi = tablesize;
5831 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005832 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005833 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005834 PyErr_SetString(PyExc_TypeError,
5835 "configuration names must be strings or integers");
5836 return 0;
5837 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005838 confname = _PyUnicode_AsString(arg);
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005839 if (confname == NULL)
5840 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005841 while (lo < hi) {
5842 mid = (lo + hi) / 2;
5843 cmp = strcmp(confname, table[mid].name);
5844 if (cmp < 0)
5845 hi = mid;
5846 else if (cmp > 0)
5847 lo = mid + 1;
5848 else {
5849 *valuep = table[mid].value;
5850 return 1;
5851 }
5852 }
5853 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005854 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005855 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005856}
5857
5858
5859#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5860static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005861#ifdef _PC_ABI_AIO_XFER_MAX
5862 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5863#endif
5864#ifdef _PC_ABI_ASYNC_IO
5865 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5866#endif
Fred Drakec9680921999-12-13 16:37:25 +00005867#ifdef _PC_ASYNC_IO
5868 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5869#endif
5870#ifdef _PC_CHOWN_RESTRICTED
5871 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5872#endif
5873#ifdef _PC_FILESIZEBITS
5874 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5875#endif
5876#ifdef _PC_LAST
5877 {"PC_LAST", _PC_LAST},
5878#endif
5879#ifdef _PC_LINK_MAX
5880 {"PC_LINK_MAX", _PC_LINK_MAX},
5881#endif
5882#ifdef _PC_MAX_CANON
5883 {"PC_MAX_CANON", _PC_MAX_CANON},
5884#endif
5885#ifdef _PC_MAX_INPUT
5886 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5887#endif
5888#ifdef _PC_NAME_MAX
5889 {"PC_NAME_MAX", _PC_NAME_MAX},
5890#endif
5891#ifdef _PC_NO_TRUNC
5892 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5893#endif
5894#ifdef _PC_PATH_MAX
5895 {"PC_PATH_MAX", _PC_PATH_MAX},
5896#endif
5897#ifdef _PC_PIPE_BUF
5898 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5899#endif
5900#ifdef _PC_PRIO_IO
5901 {"PC_PRIO_IO", _PC_PRIO_IO},
5902#endif
5903#ifdef _PC_SOCK_MAXBUF
5904 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5905#endif
5906#ifdef _PC_SYNC_IO
5907 {"PC_SYNC_IO", _PC_SYNC_IO},
5908#endif
5909#ifdef _PC_VDISABLE
5910 {"PC_VDISABLE", _PC_VDISABLE},
5911#endif
5912};
5913
Fred Drakec9680921999-12-13 16:37:25 +00005914static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005915conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005916{
5917 return conv_confname(arg, valuep, posix_constants_pathconf,
5918 sizeof(posix_constants_pathconf)
5919 / sizeof(struct constdef));
5920}
5921#endif
5922
5923#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005924PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005925"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005926Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005927If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005928
5929static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005930posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005931{
5932 PyObject *result = NULL;
5933 int name, fd;
5934
Fred Drake12c6e2d1999-12-14 21:25:03 +00005935 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5936 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005937 long limit;
5938
5939 errno = 0;
5940 limit = fpathconf(fd, name);
5941 if (limit == -1 && errno != 0)
5942 posix_error();
5943 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005944 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005945 }
5946 return result;
5947}
5948#endif
5949
5950
5951#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005952PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005953"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005954Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005955If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005956
5957static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005958posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005959{
5960 PyObject *result = NULL;
5961 int name;
5962 char *path;
5963
5964 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5965 conv_path_confname, &name)) {
5966 long limit;
5967
5968 errno = 0;
5969 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005970 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005971 if (errno == EINVAL)
5972 /* could be a path or name problem */
5973 posix_error();
5974 else
5975 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005976 }
Fred Drakec9680921999-12-13 16:37:25 +00005977 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005978 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005979 }
5980 return result;
5981}
5982#endif
5983
5984#ifdef HAVE_CONFSTR
5985static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005986#ifdef _CS_ARCHITECTURE
5987 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5988#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00005989#ifdef _CS_GNU_LIBC_VERSION
5990 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
5991#endif
5992#ifdef _CS_GNU_LIBPTHREAD_VERSION
5993 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
5994#endif
Fred Draked86ed291999-12-15 15:34:33 +00005995#ifdef _CS_HOSTNAME
5996 {"CS_HOSTNAME", _CS_HOSTNAME},
5997#endif
5998#ifdef _CS_HW_PROVIDER
5999 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6000#endif
6001#ifdef _CS_HW_SERIAL
6002 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6003#endif
6004#ifdef _CS_INITTAB_NAME
6005 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6006#endif
Fred Drakec9680921999-12-13 16:37:25 +00006007#ifdef _CS_LFS64_CFLAGS
6008 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6009#endif
6010#ifdef _CS_LFS64_LDFLAGS
6011 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6012#endif
6013#ifdef _CS_LFS64_LIBS
6014 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6015#endif
6016#ifdef _CS_LFS64_LINTFLAGS
6017 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6018#endif
6019#ifdef _CS_LFS_CFLAGS
6020 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6021#endif
6022#ifdef _CS_LFS_LDFLAGS
6023 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6024#endif
6025#ifdef _CS_LFS_LIBS
6026 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6027#endif
6028#ifdef _CS_LFS_LINTFLAGS
6029 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6030#endif
Fred Draked86ed291999-12-15 15:34:33 +00006031#ifdef _CS_MACHINE
6032 {"CS_MACHINE", _CS_MACHINE},
6033#endif
Fred Drakec9680921999-12-13 16:37:25 +00006034#ifdef _CS_PATH
6035 {"CS_PATH", _CS_PATH},
6036#endif
Fred Draked86ed291999-12-15 15:34:33 +00006037#ifdef _CS_RELEASE
6038 {"CS_RELEASE", _CS_RELEASE},
6039#endif
6040#ifdef _CS_SRPC_DOMAIN
6041 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6042#endif
6043#ifdef _CS_SYSNAME
6044 {"CS_SYSNAME", _CS_SYSNAME},
6045#endif
6046#ifdef _CS_VERSION
6047 {"CS_VERSION", _CS_VERSION},
6048#endif
Fred Drakec9680921999-12-13 16:37:25 +00006049#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6050 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6051#endif
6052#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6053 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6054#endif
6055#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6056 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6057#endif
6058#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6059 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6060#endif
6061#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6062 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6063#endif
6064#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6065 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6066#endif
6067#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6068 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6069#endif
6070#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6071 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6072#endif
6073#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6074 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6075#endif
6076#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6077 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6078#endif
6079#ifdef _CS_XBS5_LP64_OFF64_LIBS
6080 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6081#endif
6082#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6083 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6084#endif
6085#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6086 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6087#endif
6088#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6089 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6090#endif
6091#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6092 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6093#endif
6094#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6095 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6096#endif
Fred Draked86ed291999-12-15 15:34:33 +00006097#ifdef _MIPS_CS_AVAIL_PROCESSORS
6098 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6099#endif
6100#ifdef _MIPS_CS_BASE
6101 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6102#endif
6103#ifdef _MIPS_CS_HOSTID
6104 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6105#endif
6106#ifdef _MIPS_CS_HW_NAME
6107 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6108#endif
6109#ifdef _MIPS_CS_NUM_PROCESSORS
6110 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6111#endif
6112#ifdef _MIPS_CS_OSREL_MAJ
6113 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6114#endif
6115#ifdef _MIPS_CS_OSREL_MIN
6116 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6117#endif
6118#ifdef _MIPS_CS_OSREL_PATCH
6119 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6120#endif
6121#ifdef _MIPS_CS_OS_NAME
6122 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6123#endif
6124#ifdef _MIPS_CS_OS_PROVIDER
6125 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6126#endif
6127#ifdef _MIPS_CS_PROCESSORS
6128 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6129#endif
6130#ifdef _MIPS_CS_SERIAL
6131 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6132#endif
6133#ifdef _MIPS_CS_VENDOR
6134 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6135#endif
Fred Drakec9680921999-12-13 16:37:25 +00006136};
6137
6138static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006139conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006140{
6141 return conv_confname(arg, valuep, posix_constants_confstr,
6142 sizeof(posix_constants_confstr)
6143 / sizeof(struct constdef));
6144}
6145
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006146PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006147"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006148Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006149
6150static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006151posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006152{
6153 PyObject *result = NULL;
6154 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006155 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006156
6157 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006158 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006159
Fred Drakec9680921999-12-13 16:37:25 +00006160 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006161 len = confstr(name, buffer, sizeof(buffer));
6162 if (len == 0) {
6163 if (errno) {
6164 posix_error();
6165 }
6166 else {
6167 result = Py_None;
6168 Py_INCREF(Py_None);
6169 }
Fred Drakec9680921999-12-13 16:37:25 +00006170 }
6171 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006172 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006173 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006174 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006175 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006176 }
6177 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006178 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006179 }
6180 }
6181 return result;
6182}
6183#endif
6184
6185
6186#ifdef HAVE_SYSCONF
6187static struct constdef posix_constants_sysconf[] = {
6188#ifdef _SC_2_CHAR_TERM
6189 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6190#endif
6191#ifdef _SC_2_C_BIND
6192 {"SC_2_C_BIND", _SC_2_C_BIND},
6193#endif
6194#ifdef _SC_2_C_DEV
6195 {"SC_2_C_DEV", _SC_2_C_DEV},
6196#endif
6197#ifdef _SC_2_C_VERSION
6198 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6199#endif
6200#ifdef _SC_2_FORT_DEV
6201 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6202#endif
6203#ifdef _SC_2_FORT_RUN
6204 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6205#endif
6206#ifdef _SC_2_LOCALEDEF
6207 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6208#endif
6209#ifdef _SC_2_SW_DEV
6210 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6211#endif
6212#ifdef _SC_2_UPE
6213 {"SC_2_UPE", _SC_2_UPE},
6214#endif
6215#ifdef _SC_2_VERSION
6216 {"SC_2_VERSION", _SC_2_VERSION},
6217#endif
Fred Draked86ed291999-12-15 15:34:33 +00006218#ifdef _SC_ABI_ASYNCHRONOUS_IO
6219 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6220#endif
6221#ifdef _SC_ACL
6222 {"SC_ACL", _SC_ACL},
6223#endif
Fred Drakec9680921999-12-13 16:37:25 +00006224#ifdef _SC_AIO_LISTIO_MAX
6225 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6226#endif
Fred Drakec9680921999-12-13 16:37:25 +00006227#ifdef _SC_AIO_MAX
6228 {"SC_AIO_MAX", _SC_AIO_MAX},
6229#endif
6230#ifdef _SC_AIO_PRIO_DELTA_MAX
6231 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6232#endif
6233#ifdef _SC_ARG_MAX
6234 {"SC_ARG_MAX", _SC_ARG_MAX},
6235#endif
6236#ifdef _SC_ASYNCHRONOUS_IO
6237 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6238#endif
6239#ifdef _SC_ATEXIT_MAX
6240 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6241#endif
Fred Draked86ed291999-12-15 15:34:33 +00006242#ifdef _SC_AUDIT
6243 {"SC_AUDIT", _SC_AUDIT},
6244#endif
Fred Drakec9680921999-12-13 16:37:25 +00006245#ifdef _SC_AVPHYS_PAGES
6246 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6247#endif
6248#ifdef _SC_BC_BASE_MAX
6249 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6250#endif
6251#ifdef _SC_BC_DIM_MAX
6252 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6253#endif
6254#ifdef _SC_BC_SCALE_MAX
6255 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6256#endif
6257#ifdef _SC_BC_STRING_MAX
6258 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6259#endif
Fred Draked86ed291999-12-15 15:34:33 +00006260#ifdef _SC_CAP
6261 {"SC_CAP", _SC_CAP},
6262#endif
Fred Drakec9680921999-12-13 16:37:25 +00006263#ifdef _SC_CHARCLASS_NAME_MAX
6264 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6265#endif
6266#ifdef _SC_CHAR_BIT
6267 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6268#endif
6269#ifdef _SC_CHAR_MAX
6270 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6271#endif
6272#ifdef _SC_CHAR_MIN
6273 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6274#endif
6275#ifdef _SC_CHILD_MAX
6276 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6277#endif
6278#ifdef _SC_CLK_TCK
6279 {"SC_CLK_TCK", _SC_CLK_TCK},
6280#endif
6281#ifdef _SC_COHER_BLKSZ
6282 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6283#endif
6284#ifdef _SC_COLL_WEIGHTS_MAX
6285 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6286#endif
6287#ifdef _SC_DCACHE_ASSOC
6288 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6289#endif
6290#ifdef _SC_DCACHE_BLKSZ
6291 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6292#endif
6293#ifdef _SC_DCACHE_LINESZ
6294 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6295#endif
6296#ifdef _SC_DCACHE_SZ
6297 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6298#endif
6299#ifdef _SC_DCACHE_TBLKSZ
6300 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6301#endif
6302#ifdef _SC_DELAYTIMER_MAX
6303 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6304#endif
6305#ifdef _SC_EQUIV_CLASS_MAX
6306 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6307#endif
6308#ifdef _SC_EXPR_NEST_MAX
6309 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6310#endif
6311#ifdef _SC_FSYNC
6312 {"SC_FSYNC", _SC_FSYNC},
6313#endif
6314#ifdef _SC_GETGR_R_SIZE_MAX
6315 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6316#endif
6317#ifdef _SC_GETPW_R_SIZE_MAX
6318 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6319#endif
6320#ifdef _SC_ICACHE_ASSOC
6321 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6322#endif
6323#ifdef _SC_ICACHE_BLKSZ
6324 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6325#endif
6326#ifdef _SC_ICACHE_LINESZ
6327 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6328#endif
6329#ifdef _SC_ICACHE_SZ
6330 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6331#endif
Fred Draked86ed291999-12-15 15:34:33 +00006332#ifdef _SC_INF
6333 {"SC_INF", _SC_INF},
6334#endif
Fred Drakec9680921999-12-13 16:37:25 +00006335#ifdef _SC_INT_MAX
6336 {"SC_INT_MAX", _SC_INT_MAX},
6337#endif
6338#ifdef _SC_INT_MIN
6339 {"SC_INT_MIN", _SC_INT_MIN},
6340#endif
6341#ifdef _SC_IOV_MAX
6342 {"SC_IOV_MAX", _SC_IOV_MAX},
6343#endif
Fred Draked86ed291999-12-15 15:34:33 +00006344#ifdef _SC_IP_SECOPTS
6345 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6346#endif
Fred Drakec9680921999-12-13 16:37:25 +00006347#ifdef _SC_JOB_CONTROL
6348 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6349#endif
Fred Draked86ed291999-12-15 15:34:33 +00006350#ifdef _SC_KERN_POINTERS
6351 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6352#endif
6353#ifdef _SC_KERN_SIM
6354 {"SC_KERN_SIM", _SC_KERN_SIM},
6355#endif
Fred Drakec9680921999-12-13 16:37:25 +00006356#ifdef _SC_LINE_MAX
6357 {"SC_LINE_MAX", _SC_LINE_MAX},
6358#endif
6359#ifdef _SC_LOGIN_NAME_MAX
6360 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6361#endif
6362#ifdef _SC_LOGNAME_MAX
6363 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6364#endif
6365#ifdef _SC_LONG_BIT
6366 {"SC_LONG_BIT", _SC_LONG_BIT},
6367#endif
Fred Draked86ed291999-12-15 15:34:33 +00006368#ifdef _SC_MAC
6369 {"SC_MAC", _SC_MAC},
6370#endif
Fred Drakec9680921999-12-13 16:37:25 +00006371#ifdef _SC_MAPPED_FILES
6372 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6373#endif
6374#ifdef _SC_MAXPID
6375 {"SC_MAXPID", _SC_MAXPID},
6376#endif
6377#ifdef _SC_MB_LEN_MAX
6378 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6379#endif
6380#ifdef _SC_MEMLOCK
6381 {"SC_MEMLOCK", _SC_MEMLOCK},
6382#endif
6383#ifdef _SC_MEMLOCK_RANGE
6384 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6385#endif
6386#ifdef _SC_MEMORY_PROTECTION
6387 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6388#endif
6389#ifdef _SC_MESSAGE_PASSING
6390 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6391#endif
Fred Draked86ed291999-12-15 15:34:33 +00006392#ifdef _SC_MMAP_FIXED_ALIGNMENT
6393 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6394#endif
Fred Drakec9680921999-12-13 16:37:25 +00006395#ifdef _SC_MQ_OPEN_MAX
6396 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6397#endif
6398#ifdef _SC_MQ_PRIO_MAX
6399 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6400#endif
Fred Draked86ed291999-12-15 15:34:33 +00006401#ifdef _SC_NACLS_MAX
6402 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6403#endif
Fred Drakec9680921999-12-13 16:37:25 +00006404#ifdef _SC_NGROUPS_MAX
6405 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6406#endif
6407#ifdef _SC_NL_ARGMAX
6408 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6409#endif
6410#ifdef _SC_NL_LANGMAX
6411 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6412#endif
6413#ifdef _SC_NL_MSGMAX
6414 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6415#endif
6416#ifdef _SC_NL_NMAX
6417 {"SC_NL_NMAX", _SC_NL_NMAX},
6418#endif
6419#ifdef _SC_NL_SETMAX
6420 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6421#endif
6422#ifdef _SC_NL_TEXTMAX
6423 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6424#endif
6425#ifdef _SC_NPROCESSORS_CONF
6426 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6427#endif
6428#ifdef _SC_NPROCESSORS_ONLN
6429 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6430#endif
Fred Draked86ed291999-12-15 15:34:33 +00006431#ifdef _SC_NPROC_CONF
6432 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6433#endif
6434#ifdef _SC_NPROC_ONLN
6435 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6436#endif
Fred Drakec9680921999-12-13 16:37:25 +00006437#ifdef _SC_NZERO
6438 {"SC_NZERO", _SC_NZERO},
6439#endif
6440#ifdef _SC_OPEN_MAX
6441 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6442#endif
6443#ifdef _SC_PAGESIZE
6444 {"SC_PAGESIZE", _SC_PAGESIZE},
6445#endif
6446#ifdef _SC_PAGE_SIZE
6447 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6448#endif
6449#ifdef _SC_PASS_MAX
6450 {"SC_PASS_MAX", _SC_PASS_MAX},
6451#endif
6452#ifdef _SC_PHYS_PAGES
6453 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6454#endif
6455#ifdef _SC_PII
6456 {"SC_PII", _SC_PII},
6457#endif
6458#ifdef _SC_PII_INTERNET
6459 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6460#endif
6461#ifdef _SC_PII_INTERNET_DGRAM
6462 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6463#endif
6464#ifdef _SC_PII_INTERNET_STREAM
6465 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6466#endif
6467#ifdef _SC_PII_OSI
6468 {"SC_PII_OSI", _SC_PII_OSI},
6469#endif
6470#ifdef _SC_PII_OSI_CLTS
6471 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6472#endif
6473#ifdef _SC_PII_OSI_COTS
6474 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6475#endif
6476#ifdef _SC_PII_OSI_M
6477 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6478#endif
6479#ifdef _SC_PII_SOCKET
6480 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6481#endif
6482#ifdef _SC_PII_XTI
6483 {"SC_PII_XTI", _SC_PII_XTI},
6484#endif
6485#ifdef _SC_POLL
6486 {"SC_POLL", _SC_POLL},
6487#endif
6488#ifdef _SC_PRIORITIZED_IO
6489 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6490#endif
6491#ifdef _SC_PRIORITY_SCHEDULING
6492 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6493#endif
6494#ifdef _SC_REALTIME_SIGNALS
6495 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6496#endif
6497#ifdef _SC_RE_DUP_MAX
6498 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6499#endif
6500#ifdef _SC_RTSIG_MAX
6501 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6502#endif
6503#ifdef _SC_SAVED_IDS
6504 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6505#endif
6506#ifdef _SC_SCHAR_MAX
6507 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6508#endif
6509#ifdef _SC_SCHAR_MIN
6510 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6511#endif
6512#ifdef _SC_SELECT
6513 {"SC_SELECT", _SC_SELECT},
6514#endif
6515#ifdef _SC_SEMAPHORES
6516 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6517#endif
6518#ifdef _SC_SEM_NSEMS_MAX
6519 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6520#endif
6521#ifdef _SC_SEM_VALUE_MAX
6522 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6523#endif
6524#ifdef _SC_SHARED_MEMORY_OBJECTS
6525 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6526#endif
6527#ifdef _SC_SHRT_MAX
6528 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6529#endif
6530#ifdef _SC_SHRT_MIN
6531 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6532#endif
6533#ifdef _SC_SIGQUEUE_MAX
6534 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6535#endif
6536#ifdef _SC_SIGRT_MAX
6537 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6538#endif
6539#ifdef _SC_SIGRT_MIN
6540 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6541#endif
Fred Draked86ed291999-12-15 15:34:33 +00006542#ifdef _SC_SOFTPOWER
6543 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6544#endif
Fred Drakec9680921999-12-13 16:37:25 +00006545#ifdef _SC_SPLIT_CACHE
6546 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6547#endif
6548#ifdef _SC_SSIZE_MAX
6549 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6550#endif
6551#ifdef _SC_STACK_PROT
6552 {"SC_STACK_PROT", _SC_STACK_PROT},
6553#endif
6554#ifdef _SC_STREAM_MAX
6555 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6556#endif
6557#ifdef _SC_SYNCHRONIZED_IO
6558 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6559#endif
6560#ifdef _SC_THREADS
6561 {"SC_THREADS", _SC_THREADS},
6562#endif
6563#ifdef _SC_THREAD_ATTR_STACKADDR
6564 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6565#endif
6566#ifdef _SC_THREAD_ATTR_STACKSIZE
6567 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6568#endif
6569#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6570 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6571#endif
6572#ifdef _SC_THREAD_KEYS_MAX
6573 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6574#endif
6575#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6576 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6577#endif
6578#ifdef _SC_THREAD_PRIO_INHERIT
6579 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6580#endif
6581#ifdef _SC_THREAD_PRIO_PROTECT
6582 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6583#endif
6584#ifdef _SC_THREAD_PROCESS_SHARED
6585 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6586#endif
6587#ifdef _SC_THREAD_SAFE_FUNCTIONS
6588 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6589#endif
6590#ifdef _SC_THREAD_STACK_MIN
6591 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6592#endif
6593#ifdef _SC_THREAD_THREADS_MAX
6594 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6595#endif
6596#ifdef _SC_TIMERS
6597 {"SC_TIMERS", _SC_TIMERS},
6598#endif
6599#ifdef _SC_TIMER_MAX
6600 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6601#endif
6602#ifdef _SC_TTY_NAME_MAX
6603 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6604#endif
6605#ifdef _SC_TZNAME_MAX
6606 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6607#endif
6608#ifdef _SC_T_IOV_MAX
6609 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6610#endif
6611#ifdef _SC_UCHAR_MAX
6612 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6613#endif
6614#ifdef _SC_UINT_MAX
6615 {"SC_UINT_MAX", _SC_UINT_MAX},
6616#endif
6617#ifdef _SC_UIO_MAXIOV
6618 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6619#endif
6620#ifdef _SC_ULONG_MAX
6621 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6622#endif
6623#ifdef _SC_USHRT_MAX
6624 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6625#endif
6626#ifdef _SC_VERSION
6627 {"SC_VERSION", _SC_VERSION},
6628#endif
6629#ifdef _SC_WORD_BIT
6630 {"SC_WORD_BIT", _SC_WORD_BIT},
6631#endif
6632#ifdef _SC_XBS5_ILP32_OFF32
6633 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6634#endif
6635#ifdef _SC_XBS5_ILP32_OFFBIG
6636 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6637#endif
6638#ifdef _SC_XBS5_LP64_OFF64
6639 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6640#endif
6641#ifdef _SC_XBS5_LPBIG_OFFBIG
6642 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6643#endif
6644#ifdef _SC_XOPEN_CRYPT
6645 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6646#endif
6647#ifdef _SC_XOPEN_ENH_I18N
6648 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6649#endif
6650#ifdef _SC_XOPEN_LEGACY
6651 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6652#endif
6653#ifdef _SC_XOPEN_REALTIME
6654 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6655#endif
6656#ifdef _SC_XOPEN_REALTIME_THREADS
6657 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6658#endif
6659#ifdef _SC_XOPEN_SHM
6660 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6661#endif
6662#ifdef _SC_XOPEN_UNIX
6663 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6664#endif
6665#ifdef _SC_XOPEN_VERSION
6666 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6667#endif
6668#ifdef _SC_XOPEN_XCU_VERSION
6669 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6670#endif
6671#ifdef _SC_XOPEN_XPG2
6672 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6673#endif
6674#ifdef _SC_XOPEN_XPG3
6675 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6676#endif
6677#ifdef _SC_XOPEN_XPG4
6678 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6679#endif
6680};
6681
6682static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006683conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006684{
6685 return conv_confname(arg, valuep, posix_constants_sysconf,
6686 sizeof(posix_constants_sysconf)
6687 / sizeof(struct constdef));
6688}
6689
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006690PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006691"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006692Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006693
6694static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006695posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006696{
6697 PyObject *result = NULL;
6698 int name;
6699
6700 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6701 int value;
6702
6703 errno = 0;
6704 value = sysconf(name);
6705 if (value == -1 && errno != 0)
6706 posix_error();
6707 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006708 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006709 }
6710 return result;
6711}
6712#endif
6713
6714
Fred Drakebec628d1999-12-15 18:31:10 +00006715/* This code is used to ensure that the tables of configuration value names
6716 * are in sorted order as required by conv_confname(), and also to build the
6717 * the exported dictionaries that are used to publish information about the
6718 * names available on the host platform.
6719 *
6720 * Sorting the table at runtime ensures that the table is properly ordered
6721 * when used, even for platforms we're not able to test on. It also makes
6722 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006723 */
Fred Drakebec628d1999-12-15 18:31:10 +00006724
6725static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006726cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006727{
6728 const struct constdef *c1 =
6729 (const struct constdef *) v1;
6730 const struct constdef *c2 =
6731 (const struct constdef *) v2;
6732
6733 return strcmp(c1->name, c2->name);
6734}
6735
6736static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006737setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006738 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006739{
Fred Drakebec628d1999-12-15 18:31:10 +00006740 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006741 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006742
6743 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6744 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006745 if (d == NULL)
6746 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006747
Barry Warsaw3155db32000-04-13 15:20:40 +00006748 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006749 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006750 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6751 Py_XDECREF(o);
6752 Py_DECREF(d);
6753 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006754 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006755 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006756 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006757 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006758}
6759
Fred Drakebec628d1999-12-15 18:31:10 +00006760/* Return -1 on failure, 0 on success. */
6761static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006762setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006763{
6764#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006765 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006766 sizeof(posix_constants_pathconf)
6767 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006768 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006769 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006770#endif
6771#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006772 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006773 sizeof(posix_constants_confstr)
6774 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006775 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006776 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006777#endif
6778#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006779 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006780 sizeof(posix_constants_sysconf)
6781 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006782 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006783 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006784#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006785 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006786}
Fred Draked86ed291999-12-15 15:34:33 +00006787
6788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006789PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006790"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006791Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006792in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006793
6794static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006795posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006796{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006797 abort();
6798 /*NOTREACHED*/
6799 Py_FatalError("abort() called from Python code didn't abort!");
6800 return NULL;
6801}
Fred Drakebec628d1999-12-15 18:31:10 +00006802
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006803#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006804PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006805"startfile(filepath [, operation]) - Start a file with its associated\n\
6806application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006807\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006808When \"operation\" is not specified or \"open\", this acts like\n\
6809double-clicking the file in Explorer, or giving the file name as an\n\
6810argument to the DOS \"start\" command: the file is opened with whatever\n\
6811application (if any) its extension is associated.\n\
6812When another \"operation\" is given, it specifies what should be done with\n\
6813the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006814\n\
6815startfile returns as soon as the associated application is launched.\n\
6816There is no option to wait for the application to close, and no way\n\
6817to retrieve the application's exit status.\n\
6818\n\
6819The filepath is relative to the current directory. If you want to use\n\
6820an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006821the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006822
6823static PyObject *
6824win32_startfile(PyObject *self, PyObject *args)
6825{
Martin v. Löwis011e8422009-05-05 04:43:17 +00006826 PyObject *ofilepath;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006827 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006828 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006829 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006830
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006831 PyObject *unipath, *woperation = NULL;
6832 if (!PyArg_ParseTuple(args, "U|s:startfile",
6833 &unipath, &operation)) {
6834 PyErr_Clear();
6835 goto normal;
6836 }
6837
6838 if (operation) {
6839 woperation = PyUnicode_DecodeASCII(operation,
6840 strlen(operation), NULL);
6841 if (!woperation) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006842 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006843 operation = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006844 goto normal;
6845 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006846 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006847
6848 Py_BEGIN_ALLOW_THREADS
6849 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6850 PyUnicode_AS_UNICODE(unipath),
6851 NULL, NULL, SW_SHOWNORMAL);
6852 Py_END_ALLOW_THREADS
6853
6854 Py_XDECREF(woperation);
6855 if (rc <= (HINSTANCE)32) {
6856 PyObject *errval = win32_error_unicode("startfile",
6857 PyUnicode_AS_UNICODE(unipath));
6858 return errval;
6859 }
6860 Py_INCREF(Py_None);
6861 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006862
6863normal:
Martin v. Löwis011e8422009-05-05 04:43:17 +00006864 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6865 PyUnicode_FSConverter, &ofilepath,
Georg Brandlf4f44152006-02-18 22:29:33 +00006866 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006867 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006868 filepath = bytes2str(ofilepath, 1);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006869 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006870 rc = ShellExecute((HWND)0, operation, filepath,
6871 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006872 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006873 if (rc <= (HINSTANCE)32) {
6874 PyObject *errval = win32_error("startfile", filepath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006875 release_bytes(ofilepath);
Georg Brandle9f8ec92005-09-25 06:16:40 +00006876 return errval;
6877 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00006878 release_bytes(ofilepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006879 Py_INCREF(Py_None);
6880 return Py_None;
6881}
6882#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006883
Martin v. Löwis438b5342002-12-27 10:16:42 +00006884#ifdef HAVE_GETLOADAVG
6885PyDoc_STRVAR(posix_getloadavg__doc__,
6886"getloadavg() -> (float, float, float)\n\n\
6887Return the number of processes in the system run queue averaged over\n\
6888the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6889was unobtainable");
6890
6891static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006892posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006893{
6894 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006895 if (getloadavg(loadavg, 3)!=3) {
6896 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6897 return NULL;
6898 } else
6899 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6900}
6901#endif
6902
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006903#ifdef MS_WINDOWS
6904
6905PyDoc_STRVAR(win32_urandom__doc__,
6906"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006907Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006908
6909typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6910 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6911 DWORD dwFlags );
6912typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6913 BYTE *pbBuffer );
6914
6915static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006916/* This handle is never explicitly released. Instead, the operating
6917 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006918static HCRYPTPROV hCryptProv = 0;
6919
Tim Peters4ad82172004-08-30 17:02:04 +00006920static PyObject*
6921win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006922{
Tim Petersd3115382004-08-30 17:36:46 +00006923 int howMany;
6924 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006925
Tim Peters4ad82172004-08-30 17:02:04 +00006926 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006927 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006928 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006929 if (howMany < 0)
6930 return PyErr_Format(PyExc_ValueError,
6931 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006932
Tim Peters4ad82172004-08-30 17:02:04 +00006933 if (hCryptProv == 0) {
6934 HINSTANCE hAdvAPI32 = NULL;
6935 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006936
Tim Peters4ad82172004-08-30 17:02:04 +00006937 /* Obtain handle to the DLL containing CryptoAPI
6938 This should not fail */
6939 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6940 if(hAdvAPI32 == NULL)
6941 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006942
Tim Peters4ad82172004-08-30 17:02:04 +00006943 /* Obtain pointers to the CryptoAPI functions
6944 This will fail on some early versions of Win95 */
6945 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6946 hAdvAPI32,
6947 "CryptAcquireContextA");
6948 if (pCryptAcquireContext == NULL)
6949 return PyErr_Format(PyExc_NotImplementedError,
6950 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006951
Tim Peters4ad82172004-08-30 17:02:04 +00006952 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6953 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006954 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006955 return PyErr_Format(PyExc_NotImplementedError,
6956 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006957
Tim Peters4ad82172004-08-30 17:02:04 +00006958 /* Acquire context */
6959 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6960 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6961 return win32_error("CryptAcquireContext", NULL);
6962 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006963
Tim Peters4ad82172004-08-30 17:02:04 +00006964 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006965 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006966 if (result != NULL) {
6967 /* Get random data */
Amaury Forgeot d'Arca05ada32008-07-21 21:13:14 +00006968 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00006969 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006970 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006971 Py_DECREF(result);
6972 return win32_error("CryptGenRandom", NULL);
6973 }
Tim Peters4ad82172004-08-30 17:02:04 +00006974 }
Tim Petersd3115382004-08-30 17:36:46 +00006975 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006976}
6977#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006978
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006979PyDoc_STRVAR(device_encoding__doc__,
6980"device_encoding(fd) -> str\n\n\
6981Return a string describing the encoding of the device\n\
6982if the output is a terminal; else return None.");
6983
6984static PyObject *
6985device_encoding(PyObject *self, PyObject *args)
6986{
6987 int fd;
6988 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6989 return NULL;
Kristján Valur Jónsson649170b2009-03-24 14:15:49 +00006990 if (!_PyVerify_fd(fd) || !isatty(fd)) {
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006991 Py_INCREF(Py_None);
6992 return Py_None;
6993 }
6994#if defined(MS_WINDOWS) || defined(MS_WIN64)
6995 if (fd == 0) {
6996 char buf[100];
6997 sprintf(buf, "cp%d", GetConsoleCP());
6998 return PyUnicode_FromString(buf);
6999 }
7000 if (fd == 1 || fd == 2) {
7001 char buf[100];
7002 sprintf(buf, "cp%d", GetConsoleOutputCP());
7003 return PyUnicode_FromString(buf);
7004 }
7005#elif defined(CODESET)
7006 {
7007 char *codeset = nl_langinfo(CODESET);
Mark Dickinsonda2706b2008-12-11 18:03:03 +00007008 if (codeset != NULL && codeset[0] != 0)
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007009 return PyUnicode_FromString(codeset);
7010 }
7011#endif
7012 Py_INCREF(Py_None);
7013 return Py_None;
7014}
7015
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007016#ifdef __VMS
7017/* Use openssl random routine */
7018#include <openssl/rand.h>
7019PyDoc_STRVAR(vms_urandom__doc__,
7020"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007021Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007022
7023static PyObject*
7024vms_urandom(PyObject *self, PyObject *args)
7025{
7026 int howMany;
7027 PyObject* result;
7028
7029 /* Read arguments */
7030 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7031 return NULL;
7032 if (howMany < 0)
7033 return PyErr_Format(PyExc_ValueError,
7034 "negative argument not allowed");
7035
7036 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00007037 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007038 if (result != NULL) {
7039 /* Get random data */
7040 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00007041 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007042 howMany) < 0) {
7043 Py_DECREF(result);
7044 return PyErr_Format(PyExc_ValueError,
7045 "RAND_pseudo_bytes");
7046 }
7047 }
7048 return result;
7049}
7050#endif
7051
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007052#ifdef HAVE_SETRESUID
7053PyDoc_STRVAR(posix_setresuid__doc__,
7054"setresuid(ruid, euid, suid)\n\n\
7055Set the current process's real, effective, and saved user ids.");
7056
7057static PyObject*
7058posix_setresuid (PyObject *self, PyObject *args)
7059{
7060 /* We assume uid_t is no larger than a long. */
7061 long ruid, euid, suid;
7062 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7063 return NULL;
7064 if (setresuid(ruid, euid, suid) < 0)
7065 return posix_error();
7066 Py_RETURN_NONE;
7067}
7068#endif
7069
7070#ifdef HAVE_SETRESGID
7071PyDoc_STRVAR(posix_setresgid__doc__,
7072"setresgid(rgid, egid, sgid)\n\n\
7073Set the current process's real, effective, and saved group ids.");
7074
7075static PyObject*
7076posix_setresgid (PyObject *self, PyObject *args)
7077{
7078 /* We assume uid_t is no larger than a long. */
7079 long rgid, egid, sgid;
7080 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7081 return NULL;
7082 if (setresgid(rgid, egid, sgid) < 0)
7083 return posix_error();
7084 Py_RETURN_NONE;
7085}
7086#endif
7087
7088#ifdef HAVE_GETRESUID
7089PyDoc_STRVAR(posix_getresuid__doc__,
7090"getresuid() -> (ruid, euid, suid)\n\n\
7091Get tuple of the current process's real, effective, and saved user ids.");
7092
7093static PyObject*
7094posix_getresuid (PyObject *self, PyObject *noargs)
7095{
7096 uid_t ruid, euid, suid;
7097 long l_ruid, l_euid, l_suid;
7098 if (getresuid(&ruid, &euid, &suid) < 0)
7099 return posix_error();
7100 /* Force the values into long's as we don't know the size of uid_t. */
7101 l_ruid = ruid;
7102 l_euid = euid;
7103 l_suid = suid;
7104 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
7105}
7106#endif
7107
7108#ifdef HAVE_GETRESGID
7109PyDoc_STRVAR(posix_getresgid__doc__,
7110"getresgid() -> (rgid, egid, sgid)\n\n\
7111Get tuple of the current process's real, effective, and saved user ids.");
7112
7113static PyObject*
7114posix_getresgid (PyObject *self, PyObject *noargs)
7115{
7116 uid_t rgid, egid, sgid;
7117 long l_rgid, l_egid, l_sgid;
7118 if (getresgid(&rgid, &egid, &sgid) < 0)
7119 return posix_error();
7120 /* Force the values into long's as we don't know the size of uid_t. */
7121 l_rgid = rgid;
7122 l_egid = egid;
7123 l_sgid = sgid;
7124 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
7125}
7126#endif
7127
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007128static PyMethodDef posix_methods[] = {
7129 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7130#ifdef HAVE_TTYNAME
7131 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7132#endif
7133 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007134#ifdef HAVE_CHFLAGS
7135 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
7136#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007137 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007138#ifdef HAVE_FCHMOD
7139 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
7140#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007141#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007142 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007143#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007144#ifdef HAVE_LCHMOD
7145 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
7146#endif /* HAVE_LCHMOD */
7147#ifdef HAVE_FCHOWN
7148 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
7149#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007150#ifdef HAVE_LCHFLAGS
7151 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
7152#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007153#ifdef HAVE_LCHOWN
7154 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7155#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007156#ifdef HAVE_CHROOT
7157 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7158#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007159#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007160 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007161#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007162#ifdef HAVE_GETCWD
Guido van Rossumf0af3e32008-10-02 18:55:37 +00007163 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7164 METH_NOARGS, posix_getcwd__doc__},
7165 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7166 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007167#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007168#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007169 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007170#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007171 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7172 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7173 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007174#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007175 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007176#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007177#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007178 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007179#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007180 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7181 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7182 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007183 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007184#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007185 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007186#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007187#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007188 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007189#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007190 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007191#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007192 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007193#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007194 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7195 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7196 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007197#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007198 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007199#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007200 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007201#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007202 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7203 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007204#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007205#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007206 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7207 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007208#if defined(PYOS_OS2)
7209 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7210 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7211#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007212#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007213#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007214 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007215#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007216#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007217 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007218#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007219#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007220 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007221#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007222#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007223 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007224#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007225#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007226 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007227#endif /* HAVE_GETEGID */
7228#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007229 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007230#endif /* HAVE_GETEUID */
7231#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007232 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007233#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007234#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007235 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007236#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007237 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007238#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007239 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007240#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007241#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007242 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007243#endif /* HAVE_GETPPID */
7244#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007245 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007246#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007247#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007248 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007249#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007250#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007251 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007252#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007253#ifdef HAVE_KILLPG
7254 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7255#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007256#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007257 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007258#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007259#ifdef MS_WINDOWS
7260 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Brian Curtineb24d742010-04-12 17:16:38 +00007261 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007262#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007263#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007264 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007265#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007266#ifdef HAVE_SETEUID
7267 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7268#endif /* HAVE_SETEUID */
7269#ifdef HAVE_SETEGID
7270 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7271#endif /* HAVE_SETEGID */
7272#ifdef HAVE_SETREUID
7273 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7274#endif /* HAVE_SETREUID */
7275#ifdef HAVE_SETREGID
7276 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7277#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007278#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007279 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007280#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007281#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00007282 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007283#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007284#ifdef HAVE_INITGROUPS
7285 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
7286#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007287#ifdef HAVE_GETPGID
7288 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7289#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007290#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007291 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007292#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007293#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007294 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007295#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007296#ifdef HAVE_WAIT3
7297 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7298#endif /* HAVE_WAIT3 */
7299#ifdef HAVE_WAIT4
7300 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7301#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007302#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007303 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007304#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007305#ifdef HAVE_GETSID
7306 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7307#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007308#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007309 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007310#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007311#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007312 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007313#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007314#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007315 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007316#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007317#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007318 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007319#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007320 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7321 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00007322 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007323 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007324 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7325 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7326 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7327 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7328 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7329 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007330 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007331#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007332 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007333#endif
7334#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007335 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007336#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007337#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007338 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7339#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007340#ifdef HAVE_DEVICE_MACROS
7341 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7342 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7343 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7344#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007345#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007346 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007347#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007348#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007349 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007350#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007351#ifdef HAVE_UNSETENV
7352 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7353#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007354 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007355#ifdef HAVE_FCHDIR
7356 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7357#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007358#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007359 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007360#endif
7361#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007362 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007363#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007364#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007365#ifdef WCOREDUMP
7366 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7367#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007368#ifdef WIFCONTINUED
7369 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7370#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007371#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007372 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007373#endif /* WIFSTOPPED */
7374#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007375 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007376#endif /* WIFSIGNALED */
7377#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007378 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007379#endif /* WIFEXITED */
7380#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007381 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007382#endif /* WEXITSTATUS */
7383#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007384 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007385#endif /* WTERMSIG */
7386#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007387 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007388#endif /* WSTOPSIG */
7389#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007390#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007391 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007392#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007393#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007394 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007395#endif
Fred Drakec9680921999-12-13 16:37:25 +00007396#ifdef HAVE_CONFSTR
7397 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7398#endif
7399#ifdef HAVE_SYSCONF
7400 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7401#endif
7402#ifdef HAVE_FPATHCONF
7403 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7404#endif
7405#ifdef HAVE_PATHCONF
7406 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7407#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007408 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007409#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007410 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7411#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007412#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007413 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007414#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007415 #ifdef MS_WINDOWS
7416 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7417 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007418 #ifdef __VMS
7419 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7420 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007421#ifdef HAVE_SETRESUID
7422 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
7423#endif
7424#ifdef HAVE_SETRESGID
7425 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
7426#endif
7427#ifdef HAVE_GETRESUID
7428 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
7429#endif
7430#ifdef HAVE_GETRESGID
7431 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
7432#endif
7433
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007434 {NULL, NULL} /* Sentinel */
7435};
7436
7437
Barry Warsaw4a342091996-12-19 23:50:02 +00007438static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007439ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007440{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007441 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007442}
7443
Guido van Rossumd48f2521997-12-05 22:19:34 +00007444#if defined(PYOS_OS2)
7445/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007446static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007447{
7448 APIRET rc;
7449 ULONG values[QSV_MAX+1];
7450 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007451 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007452
7453 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007454 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007455 Py_END_ALLOW_THREADS
7456
7457 if (rc != NO_ERROR) {
7458 os2_error(rc);
7459 return -1;
7460 }
7461
Fred Drake4d1e64b2002-04-15 19:40:07 +00007462 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7463 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7464 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7465 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7466 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7467 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7468 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007469
7470 switch (values[QSV_VERSION_MINOR]) {
7471 case 0: ver = "2.00"; break;
7472 case 10: ver = "2.10"; break;
7473 case 11: ver = "2.11"; break;
7474 case 30: ver = "3.00"; break;
7475 case 40: ver = "4.00"; break;
7476 case 50: ver = "5.00"; break;
7477 default:
Tim Peters885d4572001-11-28 20:27:42 +00007478 PyOS_snprintf(tmp, sizeof(tmp),
7479 "%d-%d", values[QSV_VERSION_MAJOR],
7480 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007481 ver = &tmp[0];
7482 }
7483
7484 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007485 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007486 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007487
7488 /* Add Indicator of Which Drive was Used to Boot the System */
7489 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7490 tmp[1] = ':';
7491 tmp[2] = '\0';
7492
Fred Drake4d1e64b2002-04-15 19:40:07 +00007493 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007494}
7495#endif
7496
Barry Warsaw4a342091996-12-19 23:50:02 +00007497static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007498all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007499{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007500#ifdef F_OK
7501 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007502#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007503#ifdef R_OK
7504 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007505#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007506#ifdef W_OK
7507 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007508#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007509#ifdef X_OK
7510 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007511#endif
Fred Drakec9680921999-12-13 16:37:25 +00007512#ifdef NGROUPS_MAX
7513 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7514#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007515#ifdef TMP_MAX
7516 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7517#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007518#ifdef WCONTINUED
7519 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7520#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007521#ifdef WNOHANG
7522 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007523#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007524#ifdef WUNTRACED
7525 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7526#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007527#ifdef O_RDONLY
7528 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7529#endif
7530#ifdef O_WRONLY
7531 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7532#endif
7533#ifdef O_RDWR
7534 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7535#endif
7536#ifdef O_NDELAY
7537 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7538#endif
7539#ifdef O_NONBLOCK
7540 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7541#endif
7542#ifdef O_APPEND
7543 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7544#endif
7545#ifdef O_DSYNC
7546 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7547#endif
7548#ifdef O_RSYNC
7549 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7550#endif
7551#ifdef O_SYNC
7552 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7553#endif
7554#ifdef O_NOCTTY
7555 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7556#endif
7557#ifdef O_CREAT
7558 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7559#endif
7560#ifdef O_EXCL
7561 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7562#endif
7563#ifdef O_TRUNC
7564 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7565#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007566#ifdef O_BINARY
7567 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7568#endif
7569#ifdef O_TEXT
7570 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7571#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007572#ifdef O_LARGEFILE
7573 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7574#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007575#ifdef O_SHLOCK
7576 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7577#endif
7578#ifdef O_EXLOCK
7579 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7580#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007581
Tim Peters5aa91602002-01-30 05:46:57 +00007582/* MS Windows */
7583#ifdef O_NOINHERIT
7584 /* Don't inherit in child processes. */
7585 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7586#endif
7587#ifdef _O_SHORT_LIVED
7588 /* Optimize for short life (keep in memory). */
7589 /* MS forgot to define this one with a non-underscore form too. */
7590 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7591#endif
7592#ifdef O_TEMPORARY
7593 /* Automatically delete when last handle is closed. */
7594 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7595#endif
7596#ifdef O_RANDOM
7597 /* Optimize for random access. */
7598 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7599#endif
7600#ifdef O_SEQUENTIAL
7601 /* Optimize for sequential access. */
7602 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7603#endif
7604
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007605/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007606#ifdef O_ASYNC
7607 /* Send a SIGIO signal whenever input or output
7608 becomes available on file descriptor */
7609 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7610#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007611#ifdef O_DIRECT
7612 /* Direct disk access. */
7613 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7614#endif
7615#ifdef O_DIRECTORY
7616 /* Must be a directory. */
7617 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7618#endif
7619#ifdef O_NOFOLLOW
7620 /* Do not follow links. */
7621 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7622#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007623#ifdef O_NOATIME
7624 /* Do not update the access time. */
7625 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7626#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007627
Barry Warsaw5676bd12003-01-07 20:57:09 +00007628 /* These come from sysexits.h */
7629#ifdef EX_OK
7630 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007631#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007632#ifdef EX_USAGE
7633 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007634#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007635#ifdef EX_DATAERR
7636 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007637#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007638#ifdef EX_NOINPUT
7639 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007640#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007641#ifdef EX_NOUSER
7642 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007643#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007644#ifdef EX_NOHOST
7645 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007646#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007647#ifdef EX_UNAVAILABLE
7648 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007649#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007650#ifdef EX_SOFTWARE
7651 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007652#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007653#ifdef EX_OSERR
7654 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007655#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007656#ifdef EX_OSFILE
7657 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007658#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007659#ifdef EX_CANTCREAT
7660 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007661#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007662#ifdef EX_IOERR
7663 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007664#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007665#ifdef EX_TEMPFAIL
7666 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007667#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007668#ifdef EX_PROTOCOL
7669 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007670#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007671#ifdef EX_NOPERM
7672 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007673#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007674#ifdef EX_CONFIG
7675 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007676#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007677#ifdef EX_NOTFOUND
7678 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007679#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007680
Guido van Rossum246bc171999-02-01 23:54:31 +00007681#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007682#if defined(PYOS_OS2) && defined(PYCC_GCC)
7683 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7684 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7685 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7686 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7687 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7688 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7689 if (ins(d, "P_PM", (long)P_PM)) return -1;
7690 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7691 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7692 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7693 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7694 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7695 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7696 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7697 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7698 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7699 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7700 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7701 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7702 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7703#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007704 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7705 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7706 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7707 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7708 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007709#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007710#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007711
Guido van Rossumd48f2521997-12-05 22:19:34 +00007712#if defined(PYOS_OS2)
7713 if (insertvalues(d)) return -1;
7714#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007715 return 0;
7716}
7717
7718
Tim Peters5aa91602002-01-30 05:46:57 +00007719#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007720#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007721#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007722
7723#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007724#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007725#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007726
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007727#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007728#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007729#define MODNAME "posix"
7730#endif
7731
Martin v. Löwis1a214512008-06-11 05:26:20 +00007732static struct PyModuleDef posixmodule = {
7733 PyModuleDef_HEAD_INIT,
7734 MODNAME,
7735 posix__doc__,
7736 -1,
7737 posix_methods,
7738 NULL,
7739 NULL,
7740 NULL,
7741 NULL
7742};
7743
7744
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007745PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007746INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007747{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007748 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007749
Martin v. Löwis1a214512008-06-11 05:26:20 +00007750 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007751 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007752 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007753
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007754 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007755 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007756 Py_XINCREF(v);
7757 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007758 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007759 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007760
Fred Drake4d1e64b2002-04-15 19:40:07 +00007761 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007762 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007763
Fred Drake4d1e64b2002-04-15 19:40:07 +00007764 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007765 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007766
Fred Drake4d1e64b2002-04-15 19:40:07 +00007767 Py_INCREF(PyExc_OSError);
7768 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007769
Guido van Rossumb3d39562000-01-31 18:41:26 +00007770#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007771 if (posix_putenv_garbage == NULL)
7772 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007773#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007774
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007775 if (!initialized) {
7776 stat_result_desc.name = MODNAME ".stat_result";
7777 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7778 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7779 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7780 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7781 structseq_new = StatResultType.tp_new;
7782 StatResultType.tp_new = statresult_new;
7783
7784 statvfs_result_desc.name = MODNAME ".statvfs_result";
7785 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007786#ifdef NEED_TICKS_PER_SECOND
7787# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7788 ticks_per_second = sysconf(_SC_CLK_TCK);
7789# elif defined(HZ)
7790 ticks_per_second = HZ;
7791# else
7792 ticks_per_second = 60; /* magic fallback value; may be bogus */
7793# endif
7794#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007795 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007796 Py_INCREF((PyObject*) &StatResultType);
7797 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007798 Py_INCREF((PyObject*) &StatVFSResultType);
7799 PyModule_AddObject(m, "statvfs_result",
7800 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007801 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007802
7803#ifdef __APPLE__
7804 /*
7805 * Step 2 of weak-linking support on Mac OS X.
7806 *
7807 * The code below removes functions that are not available on the
7808 * currently active platform.
7809 *
7810 * This block allow one to use a python binary that was build on
7811 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7812 * OSX 10.4.
7813 */
7814#ifdef HAVE_FSTATVFS
7815 if (fstatvfs == NULL) {
7816 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007817 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007818 }
7819 }
7820#endif /* HAVE_FSTATVFS */
7821
7822#ifdef HAVE_STATVFS
7823 if (statvfs == NULL) {
7824 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007825 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007826 }
7827 }
7828#endif /* HAVE_STATVFS */
7829
7830# ifdef HAVE_LCHOWN
7831 if (lchown == NULL) {
7832 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007833 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007834 }
7835 }
7836#endif /* HAVE_LCHOWN */
7837
7838
7839#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007840 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007841
Guido van Rossumb6775db1994-08-01 11:34:53 +00007842}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007843
7844#ifdef __cplusplus
7845}
7846#endif