blob: 533f5bb5b85143670d092c33ce42a9bbbcf9297a [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
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000539/* Set a POSIX-specific error from errno, and return NULL */
540
Barry Warsawd58d7641998-07-23 16:14:40 +0000541static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000542posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000543{
Barry Warsawca74da41999-02-09 19:31:45 +0000544 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545}
Barry Warsawd58d7641998-07-23 16:14:40 +0000546static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000547posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000548{
Barry Warsawca74da41999-02-09 19:31:45 +0000549 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000550}
551
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000552#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000553static PyObject *
554posix_error_with_unicode_filename(Py_UNICODE* name)
555{
556 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
557}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000558#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000559
560
Mark Hammondef8b6542001-05-13 08:04:26 +0000561static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000562posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000563{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000564 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError,
Victor Stinnerdcb24032010-04-22 12:08:36 +0000565 PyBytes_AsString(name));
566 Py_DECREF(name);
Mark Hammondef8b6542001-05-13 08:04:26 +0000567 return rc;
568}
569
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000570#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000571static PyObject *
572win32_error(char* function, char* filename)
573{
Mark Hammond33a6da92000-08-15 00:46:38 +0000574 /* XXX We should pass the function name along in the future.
Georg Brandl38feaf02008-05-25 07:45:51 +0000575 (winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000576 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000577 Windows error object, which is non-trivial.
578 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000579 errno = GetLastError();
580 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000581 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000582 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000583 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000584}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000585
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000586static PyObject *
587win32_error_unicode(char* function, Py_UNICODE* filename)
588{
589 /* XXX - see win32_error for comments on 'function' */
590 errno = GetLastError();
591 if (filename)
592 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
593 else
594 return PyErr_SetFromWindowsErr(errno);
595}
596
Thomas Wouters477c8d52006-05-27 19:21:47 +0000597static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000598convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000599{
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000600 if (PyUnicode_CheckExact(*param))
601 Py_INCREF(*param);
602 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000603 /* For a Unicode subtype that's not a Unicode object,
604 return a true Unicode object with the same data. */
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000605 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
606 PyUnicode_GET_SIZE(*param));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000607 else
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000608 *param = PyUnicode_FromEncodedObject(*param,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000609 Py_FileSystemDefaultEncoding,
610 "strict");
611 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000612}
613
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000614#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000615
Guido van Rossumd48f2521997-12-05 22:19:34 +0000616#if defined(PYOS_OS2)
617/**********************************************************************
618 * Helper Function to Trim and Format OS/2 Messages
619 **********************************************************************/
620 static void
621os2_formatmsg(char *msgbuf, int msglen, char *reason)
622{
623 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
624
625 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
626 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
627
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000628 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000629 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
630 }
631
632 /* Add Optional Reason Text */
633 if (reason) {
634 strcat(msgbuf, " : ");
635 strcat(msgbuf, reason);
636 }
637}
638
639/**********************************************************************
640 * Decode an OS/2 Operating System Error Code
641 *
642 * A convenience function to lookup an OS/2 error code and return a
643 * text message we can use to raise a Python exception.
644 *
645 * Notes:
646 * The messages for errors returned from the OS/2 kernel reside in
647 * the file OSO001.MSG in the \OS2 directory hierarchy.
648 *
649 **********************************************************************/
650 static char *
651os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
652{
653 APIRET rc;
654 ULONG msglen;
655
656 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
657 Py_BEGIN_ALLOW_THREADS
658 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
659 errorcode, "oso001.msg", &msglen);
660 Py_END_ALLOW_THREADS
661
662 if (rc == NO_ERROR)
663 os2_formatmsg(msgbuf, msglen, reason);
664 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000665 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000666 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000667
668 return msgbuf;
669}
670
671/* Set an OS/2-specific error and return NULL. OS/2 kernel
672 errors are not in a global variable e.g. 'errno' nor are
673 they congruent with posix error numbers. */
674
675static PyObject * os2_error(int code)
676{
677 char text[1024];
678 PyObject *v;
679
680 os2_strerror(text, sizeof(text), code, "");
681
682 v = Py_BuildValue("(is)", code, text);
683 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000684 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000685 Py_DECREF(v);
686 }
687 return NULL; /* Signal to Python that an Exception is Pending */
688}
689
690#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000691
692/* POSIX generic methods */
693
Barry Warsaw53699e91996-12-10 23:23:01 +0000694static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000695posix_fildes(PyObject *fdobj, int (*func)(int))
696{
697 int fd;
698 int res;
699 fd = PyObject_AsFileDescriptor(fdobj);
700 if (fd < 0)
701 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000702 if (!_PyVerify_fd(fd))
703 return posix_error();
Fred Drake4d1e64b2002-04-15 19:40:07 +0000704 Py_BEGIN_ALLOW_THREADS
705 res = (*func)(fd);
706 Py_END_ALLOW_THREADS
707 if (res < 0)
708 return posix_error();
709 Py_INCREF(Py_None);
710 return Py_None;
711}
Guido van Rossum21142a01999-01-08 21:05:37 +0000712
713static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000714posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000716 PyObject *opath1 = NULL;
717 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000718 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000719 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000720 PyUnicode_FSConverter, &opath1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000721 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000722 path1 = PyBytes_AsString(opath1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000723 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000724 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000725 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000726 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +0000727 return posix_error_with_allocated_filename(opath1);
Victor Stinnerdcb24032010-04-22 12:08:36 +0000728 Py_DECREF(opath1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000729 Py_INCREF(Py_None);
730 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000731}
732
Barry Warsaw53699e91996-12-10 23:23:01 +0000733static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000734posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000735 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000736 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000737{
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000738 PyObject *opath1 = NULL, *opath2 = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000739 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000740 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000741 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000742 PyUnicode_FSConverter, &opath1,
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000743 PyUnicode_FSConverter, &opath2)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000744 return NULL;
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000745 }
Victor Stinnerdcb24032010-04-22 12:08:36 +0000746 path1 = PyBytes_AsString(opath1);
747 path2 = PyBytes_AsString(opath2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000748 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000749 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000750 Py_END_ALLOW_THREADS
Victor Stinnerdcb24032010-04-22 12:08:36 +0000751 Py_DECREF(opath1);
752 Py_DECREF(opath2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000753 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000754 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000756 Py_INCREF(Py_None);
757 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000758}
759
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000760#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000761static PyObject*
762win32_1str(PyObject* args, char* func,
763 char* format, BOOL (__stdcall *funcA)(LPCSTR),
764 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
765{
766 PyObject *uni;
767 char *ansi;
768 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000769
770 if (!PyArg_ParseTuple(args, wformat, &uni))
771 PyErr_Clear();
772 else {
773 Py_BEGIN_ALLOW_THREADS
774 result = funcW(PyUnicode_AsUnicode(uni));
775 Py_END_ALLOW_THREADS
776 if (!result)
777 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
778 Py_INCREF(Py_None);
779 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000780 }
781 if (!PyArg_ParseTuple(args, format, &ansi))
782 return NULL;
783 Py_BEGIN_ALLOW_THREADS
784 result = funcA(ansi);
785 Py_END_ALLOW_THREADS
786 if (!result)
787 return win32_error(func, ansi);
788 Py_INCREF(Py_None);
789 return Py_None;
790
791}
792
793/* This is a reimplementation of the C library's chdir function,
794 but one that produces Win32 errors instead of DOS error codes.
795 chdir is essentially a wrapper around SetCurrentDirectory; however,
796 it also needs to set "magic" environment variables indicating
797 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000798static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000799win32_chdir(LPCSTR path)
800{
801 char new_path[MAX_PATH+1];
802 int result;
803 char env[4] = "=x:";
804
805 if(!SetCurrentDirectoryA(path))
806 return FALSE;
807 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
808 if (!result)
809 return FALSE;
810 /* In the ANSI API, there should not be any paths longer
811 than MAX_PATH. */
812 assert(result <= MAX_PATH+1);
813 if (strncmp(new_path, "\\\\", 2) == 0 ||
814 strncmp(new_path, "//", 2) == 0)
815 /* UNC path, nothing to do. */
816 return TRUE;
817 env[1] = new_path[0];
818 return SetEnvironmentVariableA(env, new_path);
819}
820
821/* The Unicode version differs from the ANSI version
822 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000823static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000824win32_wchdir(LPCWSTR path)
825{
826 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
827 int result;
828 wchar_t env[4] = L"=x:";
829
830 if(!SetCurrentDirectoryW(path))
831 return FALSE;
832 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
833 if (!result)
834 return FALSE;
835 if (result > MAX_PATH+1) {
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000836 new_path = malloc(result * sizeof(wchar_t));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000837 if (!new_path) {
838 SetLastError(ERROR_OUTOFMEMORY);
839 return FALSE;
840 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000841 result = GetCurrentDirectoryW(result, new_path);
842 if (!result) {
843 free(new_path);
844 return FALSE;
845 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000846 }
847 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
848 wcsncmp(new_path, L"//", 2) == 0)
849 /* UNC path, nothing to do. */
850 return TRUE;
851 env[1] = new_path[0];
852 result = SetEnvironmentVariableW(env, new_path);
853 if (new_path != _new_path)
854 free(new_path);
855 return result;
856}
857#endif
858
Martin v. Löwis14694662006-02-03 12:54:16 +0000859#ifdef MS_WINDOWS
860/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
861 - time stamps are restricted to second resolution
862 - file modification times suffer from forth-and-back conversions between
863 UTC and local time
864 Therefore, we implement our own stat, based on the Win32 API directly.
865*/
866#define HAVE_STAT_NSEC 1
867
868struct win32_stat{
869 int st_dev;
870 __int64 st_ino;
871 unsigned short st_mode;
872 int st_nlink;
873 int st_uid;
874 int st_gid;
875 int st_rdev;
876 __int64 st_size;
877 int st_atime;
878 int st_atime_nsec;
879 int st_mtime;
880 int st_mtime_nsec;
881 int st_ctime;
882 int st_ctime_nsec;
883};
884
885static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
886
887static void
888FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
889{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000890 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
891 /* Cannot simply cast and dereference in_ptr,
892 since it might not be aligned properly */
893 __int64 in;
894 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000895 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
896 /* XXX Win32 supports time stamps past 2038; we currently don't */
897 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
898}
899
Thomas Wouters477c8d52006-05-27 19:21:47 +0000900static void
901time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
902{
903 /* XXX endianness */
904 __int64 out;
905 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000906 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000907 memcpy(out_ptr, &out, sizeof(out));
908}
909
Martin v. Löwis14694662006-02-03 12:54:16 +0000910/* Below, we *know* that ugo+r is 0444 */
911#if _S_IREAD != 0400
912#error Unsupported C library
913#endif
914static int
915attributes_to_mode(DWORD attr)
916{
917 int m = 0;
918 if (attr & FILE_ATTRIBUTE_DIRECTORY)
919 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
920 else
921 m |= _S_IFREG;
922 if (attr & FILE_ATTRIBUTE_READONLY)
923 m |= 0444;
924 else
925 m |= 0666;
926 return m;
927}
928
929static int
930attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
931{
932 memset(result, 0, sizeof(*result));
933 result->st_mode = attributes_to_mode(info->dwFileAttributes);
934 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
935 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
936 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
937 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
938
939 return 0;
940}
941
Guido van Rossumd8faa362007-04-27 19:54:29 +0000942static BOOL
943attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
944{
945 HANDLE hFindFile;
946 WIN32_FIND_DATAA FileData;
947 hFindFile = FindFirstFileA(pszFile, &FileData);
948 if (hFindFile == INVALID_HANDLE_VALUE)
949 return FALSE;
950 FindClose(hFindFile);
951 pfad->dwFileAttributes = FileData.dwFileAttributes;
952 pfad->ftCreationTime = FileData.ftCreationTime;
953 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
954 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
955 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
956 pfad->nFileSizeLow = FileData.nFileSizeLow;
957 return TRUE;
958}
959
960static BOOL
961attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
962{
963 HANDLE hFindFile;
964 WIN32_FIND_DATAW FileData;
965 hFindFile = FindFirstFileW(pszFile, &FileData);
966 if (hFindFile == INVALID_HANDLE_VALUE)
967 return FALSE;
968 FindClose(hFindFile);
969 pfad->dwFileAttributes = FileData.dwFileAttributes;
970 pfad->ftCreationTime = FileData.ftCreationTime;
971 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
972 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
973 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
974 pfad->nFileSizeLow = FileData.nFileSizeLow;
975 return TRUE;
976}
977
Martin v. Löwis14694662006-02-03 12:54:16 +0000978static int
979win32_stat(const char* path, struct win32_stat *result)
980{
981 WIN32_FILE_ATTRIBUTE_DATA info;
982 int code;
983 char *dot;
Hirokazu Yamamoto6fbdfda2009-06-29 11:37:19 +0000984 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000985 if (GetLastError() != ERROR_SHARING_VIOLATION) {
986 /* Protocol violation: we explicitly clear errno, instead of
987 setting it to a POSIX error. Callers should use GetLastError. */
988 errno = 0;
989 return -1;
990 } else {
991 /* Could not get attributes on open file. Fall back to
992 reading the directory. */
993 if (!attributes_from_dir(path, &info)) {
994 /* Very strange. This should not fail now */
995 errno = 0;
996 return -1;
997 }
998 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000999 }
1000 code = attribute_data_to_stat(&info, result);
1001 if (code != 0)
1002 return code;
1003 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1004 dot = strrchr(path, '.');
1005 if (dot) {
1006 if (stricmp(dot, ".bat") == 0 ||
1007 stricmp(dot, ".cmd") == 0 ||
1008 stricmp(dot, ".exe") == 0 ||
1009 stricmp(dot, ".com") == 0)
1010 result->st_mode |= 0111;
1011 }
1012 return code;
1013}
1014
1015static int
1016win32_wstat(const wchar_t* path, struct win32_stat *result)
1017{
1018 int code;
1019 const wchar_t *dot;
1020 WIN32_FILE_ATTRIBUTE_DATA info;
Hirokazu Yamamoto6fbdfda2009-06-29 11:37:19 +00001021 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001022 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1023 /* Protocol violation: we explicitly clear errno, instead of
1024 setting it to a POSIX error. Callers should use GetLastError. */
1025 errno = 0;
1026 return -1;
1027 } else {
1028 /* Could not get attributes on open file. Fall back to
1029 reading the directory. */
1030 if (!attributes_from_dir_w(path, &info)) {
1031 /* Very strange. This should not fail now */
1032 errno = 0;
1033 return -1;
1034 }
1035 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001036 }
1037 code = attribute_data_to_stat(&info, result);
1038 if (code < 0)
1039 return code;
1040 /* Set IFEXEC if it is an .exe, .bat, ... */
1041 dot = wcsrchr(path, '.');
1042 if (dot) {
1043 if (_wcsicmp(dot, L".bat") == 0 ||
1044 _wcsicmp(dot, L".cmd") == 0 ||
1045 _wcsicmp(dot, L".exe") == 0 ||
1046 _wcsicmp(dot, L".com") == 0)
1047 result->st_mode |= 0111;
1048 }
1049 return code;
1050}
1051
1052static int
1053win32_fstat(int file_number, struct win32_stat *result)
1054{
1055 BY_HANDLE_FILE_INFORMATION info;
1056 HANDLE h;
1057 int type;
1058
1059 h = (HANDLE)_get_osfhandle(file_number);
1060
1061 /* Protocol violation: we explicitly clear errno, instead of
1062 setting it to a POSIX error. Callers should use GetLastError. */
1063 errno = 0;
1064
1065 if (h == INVALID_HANDLE_VALUE) {
1066 /* This is really a C library error (invalid file handle).
1067 We set the Win32 error to the closes one matching. */
1068 SetLastError(ERROR_INVALID_HANDLE);
1069 return -1;
1070 }
1071 memset(result, 0, sizeof(*result));
1072
1073 type = GetFileType(h);
1074 if (type == FILE_TYPE_UNKNOWN) {
1075 DWORD error = GetLastError();
1076 if (error != 0) {
1077 return -1;
1078 }
1079 /* else: valid but unknown file */
1080 }
1081
1082 if (type != FILE_TYPE_DISK) {
1083 if (type == FILE_TYPE_CHAR)
1084 result->st_mode = _S_IFCHR;
1085 else if (type == FILE_TYPE_PIPE)
1086 result->st_mode = _S_IFIFO;
1087 return 0;
1088 }
1089
1090 if (!GetFileInformationByHandle(h, &info)) {
1091 return -1;
1092 }
1093
1094 /* similar to stat() */
1095 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1096 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1097 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1098 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1099 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1100 /* specific to fstat() */
1101 result->st_nlink = info.nNumberOfLinks;
1102 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1103 return 0;
1104}
1105
1106#endif /* MS_WINDOWS */
1107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001108PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001109"stat_result: Result from stat or lstat.\n\n\
1110This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001111 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001112or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1113\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001114Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1115or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001116\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001117See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001118
1119static PyStructSequence_Field stat_result_fields[] = {
1120 {"st_mode", "protection bits"},
1121 {"st_ino", "inode"},
1122 {"st_dev", "device"},
1123 {"st_nlink", "number of hard links"},
1124 {"st_uid", "user ID of owner"},
1125 {"st_gid", "group ID of owner"},
1126 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001127 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1128 {NULL, "integer time of last access"},
1129 {NULL, "integer time of last modification"},
1130 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001131 {"st_atime", "time of last access"},
1132 {"st_mtime", "time of last modification"},
1133 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001134#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001135 {"st_blksize", "blocksize for filesystem I/O"},
1136#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001137#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001138 {"st_blocks", "number of blocks allocated"},
1139#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001140#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001141 {"st_rdev", "device type (if inode device)"},
1142#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001143#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1144 {"st_flags", "user defined flags for file"},
1145#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001146#ifdef HAVE_STRUCT_STAT_ST_GEN
1147 {"st_gen", "generation number"},
1148#endif
1149#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1150 {"st_birthtime", "time of creation"},
1151#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001152 {0}
1153};
1154
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001155#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001156#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001157#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001158#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001159#endif
1160
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001161#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001162#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1163#else
1164#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1165#endif
1166
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001167#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001168#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1169#else
1170#define ST_RDEV_IDX ST_BLOCKS_IDX
1171#endif
1172
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001173#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1174#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1175#else
1176#define ST_FLAGS_IDX ST_RDEV_IDX
1177#endif
1178
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001179#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001180#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001181#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001182#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001183#endif
1184
1185#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1186#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1187#else
1188#define ST_BIRTHTIME_IDX ST_GEN_IDX
1189#endif
1190
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001191static PyStructSequence_Desc stat_result_desc = {
1192 "stat_result", /* name */
1193 stat_result__doc__, /* doc */
1194 stat_result_fields,
1195 10
1196};
1197
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001198PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001199"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1200This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001201 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001202or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001203\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001204See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001205
1206static PyStructSequence_Field statvfs_result_fields[] = {
1207 {"f_bsize", },
1208 {"f_frsize", },
1209 {"f_blocks", },
1210 {"f_bfree", },
1211 {"f_bavail", },
1212 {"f_files", },
1213 {"f_ffree", },
1214 {"f_favail", },
1215 {"f_flag", },
1216 {"f_namemax",},
1217 {0}
1218};
1219
1220static PyStructSequence_Desc statvfs_result_desc = {
1221 "statvfs_result", /* name */
1222 statvfs_result__doc__, /* doc */
1223 statvfs_result_fields,
1224 10
1225};
1226
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001227static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001228static PyTypeObject StatResultType;
1229static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001230static newfunc structseq_new;
1231
1232static PyObject *
1233statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1234{
1235 PyStructSequence *result;
1236 int i;
1237
1238 result = (PyStructSequence*)structseq_new(type, args, kwds);
1239 if (!result)
1240 return NULL;
1241 /* If we have been initialized from a tuple,
1242 st_?time might be set to None. Initialize it
1243 from the int slots. */
1244 for (i = 7; i <= 9; i++) {
1245 if (result->ob_item[i+3] == Py_None) {
1246 Py_DECREF(Py_None);
1247 Py_INCREF(result->ob_item[i]);
1248 result->ob_item[i+3] = result->ob_item[i];
1249 }
1250 }
1251 return (PyObject*)result;
1252}
1253
1254
1255
1256/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001257static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001258
1259PyDoc_STRVAR(stat_float_times__doc__,
1260"stat_float_times([newval]) -> oldval\n\n\
1261Determine whether os.[lf]stat represents time stamps as float objects.\n\
1262If newval is True, future calls to stat() return floats, if it is False,\n\
1263future calls return ints. \n\
1264If newval is omitted, return the current setting.\n");
1265
1266static PyObject*
1267stat_float_times(PyObject* self, PyObject *args)
1268{
1269 int newval = -1;
1270 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1271 return NULL;
1272 if (newval == -1)
1273 /* Return old value */
1274 return PyBool_FromLong(_stat_float_times);
1275 _stat_float_times = newval;
1276 Py_INCREF(Py_None);
1277 return Py_None;
1278}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001279
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001280static void
1281fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1282{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001283 PyObject *fval,*ival;
1284#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001285 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001286#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001287 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001288#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001289 if (!ival)
1290 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001291 if (_stat_float_times) {
1292 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1293 } else {
1294 fval = ival;
1295 Py_INCREF(fval);
1296 }
1297 PyStructSequence_SET_ITEM(v, index, ival);
1298 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001299}
1300
Tim Peters5aa91602002-01-30 05:46:57 +00001301/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001302 (used by posix_stat() and posix_fstat()) */
1303static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001304_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001305{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001306 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001307 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001308 if (v == NULL)
1309 return NULL;
1310
Christian Heimes217cfd12007-12-02 14:31:20 +00001311 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001312#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001313 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001314 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001315#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001316 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001317#endif
1318#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001319 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001320 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001321#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001322 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001323#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00001324 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1325 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1326 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001327#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001328 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001329 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001330#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001331 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001332#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001333
Martin v. Löwis14694662006-02-03 12:54:16 +00001334#if defined(HAVE_STAT_TV_NSEC)
1335 ansec = st->st_atim.tv_nsec;
1336 mnsec = st->st_mtim.tv_nsec;
1337 cnsec = st->st_ctim.tv_nsec;
1338#elif defined(HAVE_STAT_TV_NSEC2)
1339 ansec = st->st_atimespec.tv_nsec;
1340 mnsec = st->st_mtimespec.tv_nsec;
1341 cnsec = st->st_ctimespec.tv_nsec;
1342#elif defined(HAVE_STAT_NSEC)
1343 ansec = st->st_atime_nsec;
1344 mnsec = st->st_mtime_nsec;
1345 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001346#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001347 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001348#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001349 fill_time(v, 7, st->st_atime, ansec);
1350 fill_time(v, 8, st->st_mtime, mnsec);
1351 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001352
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001353#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001354 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001355 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001356#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001357#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001358 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001359 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001360#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001361#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001362 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001363 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001364#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001365#ifdef HAVE_STRUCT_STAT_ST_GEN
1366 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001367 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001368#endif
1369#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1370 {
1371 PyObject *val;
1372 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001373 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001374#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001375 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001376#else
1377 bnsec = 0;
1378#endif
1379 if (_stat_float_times) {
1380 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1381 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001382 val = PyLong_FromLong((long)bsec);
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001383 }
1384 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1385 val);
1386 }
1387#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001388#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1389 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001390 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001391#endif
Fred Drake699f3522000-06-29 21:12:41 +00001392
1393 if (PyErr_Occurred()) {
1394 Py_DECREF(v);
1395 return NULL;
1396 }
1397
1398 return v;
1399}
1400
Martin v. Löwisd8948722004-06-02 09:57:56 +00001401#ifdef MS_WINDOWS
1402
1403/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1404 where / can be used in place of \ and the trailing slash is optional.
1405 Both SERVER and SHARE must have at least one character.
1406*/
1407
1408#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1409#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001410#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001411#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001412#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001413
Tim Peters4ad82172004-08-30 17:02:04 +00001414static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001415IsUNCRootA(char *path, int pathlen)
1416{
1417 #define ISSLASH ISSLASHA
1418
1419 int i, share;
1420
1421 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1422 /* minimum UNCRoot is \\x\y */
1423 return FALSE;
1424 for (i = 2; i < pathlen ; i++)
1425 if (ISSLASH(path[i])) break;
1426 if (i == 2 || i == pathlen)
1427 /* do not allow \\\SHARE or \\SERVER */
1428 return FALSE;
1429 share = i+1;
1430 for (i = share; i < pathlen; i++)
1431 if (ISSLASH(path[i])) break;
1432 return (i != share && (i == pathlen || i == pathlen-1));
1433
1434 #undef ISSLASH
1435}
1436
Tim Peters4ad82172004-08-30 17:02:04 +00001437static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001438IsUNCRootW(Py_UNICODE *path, int pathlen)
1439{
1440 #define ISSLASH ISSLASHW
1441
1442 int i, share;
1443
1444 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1445 /* minimum UNCRoot is \\x\y */
1446 return FALSE;
1447 for (i = 2; i < pathlen ; i++)
1448 if (ISSLASH(path[i])) break;
1449 if (i == 2 || i == pathlen)
1450 /* do not allow \\\SHARE or \\SERVER */
1451 return FALSE;
1452 share = i+1;
1453 for (i = share; i < pathlen; i++)
1454 if (ISSLASH(path[i])) break;
1455 return (i != share && (i == pathlen || i == pathlen-1));
1456
1457 #undef ISSLASH
1458}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001459#endif /* MS_WINDOWS */
1460
Barry Warsaw53699e91996-12-10 23:23:01 +00001461static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001462posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001463 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001464#ifdef __VMS
1465 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1466#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001467 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001468#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001469 char *wformat,
1470 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001471{
Fred Drake699f3522000-06-29 21:12:41 +00001472 STRUCT_STAT st;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001473 PyObject *opath;
1474 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001475 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001476 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001477
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001478#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001479 PyUnicodeObject *po;
1480 if (PyArg_ParseTuple(args, wformat, &po)) {
1481 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001482
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001483 Py_BEGIN_ALLOW_THREADS
1484 /* PyUnicode_AS_UNICODE result OK without
1485 thread lock as it is a simple dereference. */
1486 res = wstatfunc(wpath, &st);
1487 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001488
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001489 if (res != 0)
1490 return win32_error_unicode("stat", wpath);
1491 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001492 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001493 /* Drop the argument parsing error as narrow strings
1494 are also valid. */
1495 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001496#endif
1497
Tim Peters5aa91602002-01-30 05:46:57 +00001498 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +00001499 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001500 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001501 path = PyBytes_AsString(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00001502 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001503 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001504 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001505
1506 if (res != 0) {
1507#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001508 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001509#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001510 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001511#endif
1512 }
1513 else
1514 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001515
Victor Stinnerdcb24032010-04-22 12:08:36 +00001516 Py_DECREF(opath);
Martin v. Löwis14694662006-02-03 12:54:16 +00001517 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001518}
1519
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001520/* POSIX methods */
1521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001522PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001523"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001524Use the real uid/gid to test for access to a path. Note that most\n\
1525operations will use the effective uid/gid, therefore this routine can\n\
1526be used in a suid/sgid environment to test if the invoking user has the\n\
1527specified access to the path. The mode argument can be F_OK to test\n\
1528existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001529
1530static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001531posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001532{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001533 PyObject *opath;
Guido van Rossum015f22a1999-01-06 22:52:38 +00001534 char *path;
1535 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001536
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001537#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001538 DWORD attr;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001539 PyUnicodeObject *po;
1540 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1541 Py_BEGIN_ALLOW_THREADS
1542 /* PyUnicode_AS_UNICODE OK without thread lock as
1543 it is a simple dereference. */
1544 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1545 Py_END_ALLOW_THREADS
1546 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001547 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001548 /* Drop the argument parsing error as narrow strings
1549 are also valid. */
1550 PyErr_Clear();
Martin v. Löwis011e8422009-05-05 04:43:17 +00001551 if (!PyArg_ParseTuple(args, "O&i:access",
1552 PyUnicode_FSConverter, &opath, &mode))
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001553 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001554 path = PyBytes_AsString(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001555 Py_BEGIN_ALLOW_THREADS
1556 attr = GetFileAttributesA(path);
1557 Py_END_ALLOW_THREADS
Victor Stinnerdcb24032010-04-22 12:08:36 +00001558 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001559finish:
1560 if (attr == 0xFFFFFFFF)
1561 /* File does not exist, or cannot read attributes */
1562 return PyBool_FromLong(0);
1563 /* Access is possible if either write access wasn't requested, or
Guido van Rossumb00324f2007-12-04 01:13:14 +00001564 the file isn't read-only, or if it's a directory, as there are
1565 no read-only directories on Windows. */
1566 return PyBool_FromLong(!(mode & 2)
1567 || !(attr & FILE_ATTRIBUTE_READONLY)
1568 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001569#else
1570 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001571 if (!PyArg_ParseTuple(args, "O&i:access",
1572 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001573 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001574 path = PyBytes_AsString(opath);
Guido van Rossum015f22a1999-01-06 22:52:38 +00001575 Py_BEGIN_ALLOW_THREADS
1576 res = access(path, mode);
1577 Py_END_ALLOW_THREADS
Victor Stinnerdcb24032010-04-22 12:08:36 +00001578 Py_DECREF(opath);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001579 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001580#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001581}
1582
Guido van Rossumd371ff11999-01-25 16:12:23 +00001583#ifndef F_OK
1584#define F_OK 0
1585#endif
1586#ifndef R_OK
1587#define R_OK 4
1588#endif
1589#ifndef W_OK
1590#define W_OK 2
1591#endif
1592#ifndef X_OK
1593#define X_OK 1
1594#endif
1595
1596#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001597PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001598"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001599Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001600
1601static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001602posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001603{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001604 int id;
1605 char *ret;
1606
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001607 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001608 return NULL;
1609
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001610#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001611 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001612 if (id == 0) {
1613 ret = ttyname();
1614 }
1615 else {
1616 ret = NULL;
1617 }
1618#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001619 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001620#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001621 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001622 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001623 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001624}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001625#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001626
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001627#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001628PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001629"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001630Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001631
1632static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001633posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001634{
1635 char *ret;
1636 char buffer[L_ctermid];
1637
Greg Wardb48bc172000-03-01 21:51:56 +00001638#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001639 ret = ctermid_r(buffer);
1640#else
1641 ret = ctermid(buffer);
1642#endif
1643 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001644 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001645 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001646}
1647#endif
1648
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001649PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001650"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001651Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001652
Barry Warsaw53699e91996-12-10 23:23:01 +00001653static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001654posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001655{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001656#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00001657 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001658#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001659 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001660#elif defined(__VMS)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001661 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001662#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001663 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001664#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001665}
1666
Fred Drake4d1e64b2002-04-15 19:40:07 +00001667#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001668PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001669"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001670Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001671opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001672
1673static PyObject *
1674posix_fchdir(PyObject *self, PyObject *fdobj)
1675{
1676 return posix_fildes(fdobj, fchdir);
1677}
1678#endif /* HAVE_FCHDIR */
1679
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001681PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001682"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001683Change the access permissions of a file.");
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_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001687{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001688 PyObject *opath = NULL;
Mark Hammondef8b6542001-05-13 08:04:26 +00001689 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001690 int i;
1691 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001692#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001693 DWORD attr;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001694 PyUnicodeObject *po;
1695 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1696 Py_BEGIN_ALLOW_THREADS
1697 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1698 if (attr != 0xFFFFFFFF) {
1699 if (i & _S_IWRITE)
1700 attr &= ~FILE_ATTRIBUTE_READONLY;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001701 else
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001702 attr |= FILE_ATTRIBUTE_READONLY;
1703 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
Mark Hammond817c9292003-12-03 01:22:38 +00001704 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001705 else
1706 res = 0;
1707 Py_END_ALLOW_THREADS
1708 if (!res)
1709 return win32_error_unicode("chmod",
1710 PyUnicode_AS_UNICODE(po));
1711 Py_INCREF(Py_None);
1712 return Py_None;
Mark Hammond817c9292003-12-03 01:22:38 +00001713 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001714 /* Drop the argument parsing error as narrow strings
1715 are also valid. */
1716 PyErr_Clear();
1717
Martin v. Löwis011e8422009-05-05 04:43:17 +00001718 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1719 &opath, &i))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001720 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001721 path = PyBytes_AsString(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001722 Py_BEGIN_ALLOW_THREADS
1723 attr = GetFileAttributesA(path);
1724 if (attr != 0xFFFFFFFF) {
1725 if (i & _S_IWRITE)
1726 attr &= ~FILE_ATTRIBUTE_READONLY;
1727 else
1728 attr |= FILE_ATTRIBUTE_READONLY;
1729 res = SetFileAttributesA(path, attr);
1730 }
1731 else
1732 res = 0;
1733 Py_END_ALLOW_THREADS
1734 if (!res) {
1735 win32_error("chmod", path);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001736 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001737 return NULL;
1738 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00001739 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001740 Py_INCREF(Py_None);
1741 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001742#else /* MS_WINDOWS */
Martin v. Löwis011e8422009-05-05 04:43:17 +00001743 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1744 &opath, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001745 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001746 path = PyBytes_AsString(opath);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001747 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001748 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001749 Py_END_ALLOW_THREADS
1750 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001751 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001752 Py_DECREF(opath);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001753 Py_INCREF(Py_None);
1754 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001755#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001756}
1757
Christian Heimes4e30a842007-11-30 22:12:06 +00001758#ifdef HAVE_FCHMOD
1759PyDoc_STRVAR(posix_fchmod__doc__,
1760"fchmod(fd, mode)\n\n\
1761Change the access permissions of the file given by file\n\
1762descriptor fd.");
1763
1764static PyObject *
1765posix_fchmod(PyObject *self, PyObject *args)
1766{
1767 int fd, mode, res;
1768 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1769 return NULL;
1770 Py_BEGIN_ALLOW_THREADS
1771 res = fchmod(fd, mode);
1772 Py_END_ALLOW_THREADS
1773 if (res < 0)
1774 return posix_error();
1775 Py_RETURN_NONE;
1776}
1777#endif /* HAVE_FCHMOD */
1778
1779#ifdef HAVE_LCHMOD
1780PyDoc_STRVAR(posix_lchmod__doc__,
1781"lchmod(path, mode)\n\n\
1782Change the access permissions of a file. If path is a symlink, this\n\
1783affects the link itself rather than the target.");
1784
1785static PyObject *
1786posix_lchmod(PyObject *self, PyObject *args)
1787{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001788 PyObject *opath;
1789 char *path;
Christian Heimes4e30a842007-11-30 22:12:06 +00001790 int i;
1791 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001792 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1793 &opath, &i))
Christian Heimes4e30a842007-11-30 22:12:06 +00001794 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001795 path = PyBytes_AsString(opath);
Christian Heimes4e30a842007-11-30 22:12:06 +00001796 Py_BEGIN_ALLOW_THREADS
1797 res = lchmod(path, i);
1798 Py_END_ALLOW_THREADS
1799 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001800 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001801 Py_DECREF(opath);
Christian Heimes4e30a842007-11-30 22:12:06 +00001802 Py_RETURN_NONE;
1803}
1804#endif /* HAVE_LCHMOD */
1805
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001806
Thomas Wouterscf297e42007-02-23 15:07:44 +00001807#ifdef HAVE_CHFLAGS
1808PyDoc_STRVAR(posix_chflags__doc__,
1809"chflags(path, flags)\n\n\
1810Set file flags.");
1811
1812static PyObject *
1813posix_chflags(PyObject *self, PyObject *args)
1814{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001815 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001816 char *path;
1817 unsigned long flags;
1818 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001819 if (!PyArg_ParseTuple(args, "O&k:chflags",
1820 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00001821 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001822 path = PyBytes_AsString(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001823 Py_BEGIN_ALLOW_THREADS
1824 res = chflags(path, flags);
1825 Py_END_ALLOW_THREADS
1826 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001827 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001828 Py_DECREF(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001829 Py_INCREF(Py_None);
1830 return Py_None;
1831}
1832#endif /* HAVE_CHFLAGS */
1833
1834#ifdef HAVE_LCHFLAGS
1835PyDoc_STRVAR(posix_lchflags__doc__,
1836"lchflags(path, flags)\n\n\
1837Set file flags.\n\
1838This function will not follow symbolic links.");
1839
1840static PyObject *
1841posix_lchflags(PyObject *self, PyObject *args)
1842{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001843 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001844 char *path;
1845 unsigned long flags;
1846 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001847 if (!PyArg_ParseTuple(args, "O&k:lchflags",
Martin v. Löwis4adbc342009-05-05 17:17:55 +00001848 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00001849 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001850 path = PyBytes_AsString(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001851 Py_BEGIN_ALLOW_THREADS
1852 res = lchflags(path, flags);
1853 Py_END_ALLOW_THREADS
1854 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001855 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001856 Py_DECREF(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001857 Py_INCREF(Py_None);
1858 return Py_None;
1859}
1860#endif /* HAVE_LCHFLAGS */
1861
Martin v. Löwis244edc82001-10-04 22:44:26 +00001862#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001863PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001864"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001865Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001866
1867static PyObject *
1868posix_chroot(PyObject *self, PyObject *args)
1869{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001870 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001871}
1872#endif
1873
Guido van Rossum21142a01999-01-08 21:05:37 +00001874#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001875PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001876"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001877force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001878
1879static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001880posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001881{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001882 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001883}
1884#endif /* HAVE_FSYNC */
1885
1886#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001887
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001888#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001889extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1890#endif
1891
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001892PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001893"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001894force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001895 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001896
1897static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001898posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001899{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001900 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001901}
1902#endif /* HAVE_FDATASYNC */
1903
1904
Fredrik Lundh10723342000-07-10 16:38:09 +00001905#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001906PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001907"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001908Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001909
Barry Warsaw53699e91996-12-10 23:23:01 +00001910static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001911posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001912{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001913 PyObject *opath;
1914 char *path;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00001915 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001916 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001917 if (!PyArg_ParseTuple(args, "O&ll:chown",
1918 PyUnicode_FSConverter, &opath,
Mark Hammondef8b6542001-05-13 08:04:26 +00001919 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001920 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001921 path = PyBytes_AsString(opath);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001922 Py_BEGIN_ALLOW_THREADS
1923 res = chown(path, (uid_t) uid, (gid_t) gid);
1924 Py_END_ALLOW_THREADS
1925 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001926 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001927 Py_DECREF(opath);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001928 Py_INCREF(Py_None);
1929 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001930}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001931#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001932
Christian Heimes4e30a842007-11-30 22:12:06 +00001933#ifdef HAVE_FCHOWN
1934PyDoc_STRVAR(posix_fchown__doc__,
1935"fchown(fd, uid, gid)\n\n\
1936Change the owner and group id of the file given by file descriptor\n\
1937fd to the numeric uid and gid.");
1938
1939static PyObject *
1940posix_fchown(PyObject *self, PyObject *args)
1941{
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001942 int fd;
1943 long uid, gid;
Christian Heimes4e30a842007-11-30 22:12:06 +00001944 int res;
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001945 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
Christian Heimes4e30a842007-11-30 22:12:06 +00001946 return NULL;
1947 Py_BEGIN_ALLOW_THREADS
1948 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1949 Py_END_ALLOW_THREADS
1950 if (res < 0)
1951 return posix_error();
1952 Py_RETURN_NONE;
1953}
1954#endif /* HAVE_FCHOWN */
1955
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001956#ifdef HAVE_LCHOWN
1957PyDoc_STRVAR(posix_lchown__doc__,
1958"lchown(path, uid, gid)\n\n\
1959Change the owner and group id of path to the numeric uid and gid.\n\
1960This function will not follow symbolic links.");
1961
1962static PyObject *
1963posix_lchown(PyObject *self, PyObject *args)
1964{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001965 PyObject *opath;
1966 char *path;
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001967 long uid, gid;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001968 int res;
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001969 if (!PyArg_ParseTuple(args, "O&ll:lchown",
Martin v. Löwis011e8422009-05-05 04:43:17 +00001970 PyUnicode_FSConverter, &opath,
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001971 &uid, &gid))
1972 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001973 path = PyBytes_AsString(opath);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001974 Py_BEGIN_ALLOW_THREADS
1975 res = lchown(path, (uid_t) uid, (gid_t) gid);
1976 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001977 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001978 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001979 Py_DECREF(opath);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001980 Py_INCREF(Py_None);
1981 return Py_None;
1982}
1983#endif /* HAVE_LCHOWN */
1984
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001985
Guido van Rossum36bc6801995-06-14 22:54:23 +00001986#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00001987static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001988posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001989{
1990 char buf[1026];
1991 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001992
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001993#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001994 if (!use_bytes) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001995 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001996 wchar_t *wbuf2 = wbuf;
1997 PyObject *resobj;
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001998 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001999 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002000 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2001 /* If the buffer is large enough, len does not include the
2002 terminating \0. If the buffer is too small, len includes
2003 the space needed for the terminator. */
2004 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2005 wbuf2 = malloc(len * sizeof(wchar_t));
2006 if (wbuf2)
2007 len = GetCurrentDirectoryW(len, wbuf2);
2008 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002009 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002010 if (!wbuf2) {
2011 PyErr_NoMemory();
2012 return NULL;
2013 }
2014 if (!len) {
2015 if (wbuf2 != wbuf) free(wbuf2);
2016 return win32_error("getcwdu", NULL);
2017 }
2018 resobj = PyUnicode_FromWideChar(wbuf2, len);
2019 if (wbuf2 != wbuf) free(wbuf2);
2020 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002021 }
2022#endif
2023
2024 Py_BEGIN_ALLOW_THREADS
2025#if defined(PYOS_OS2) && defined(PYCC_GCC)
2026 res = _getcwd2(buf, sizeof buf);
2027#else
2028 res = getcwd(buf, sizeof buf);
2029#endif
2030 Py_END_ALLOW_THREADS
2031 if (res == NULL)
2032 return posix_error();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002033 if (use_bytes)
2034 return PyBytes_FromStringAndSize(buf, strlen(buf));
Martin v. Löwis43c57782009-05-10 08:15:24 +00002035 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002036}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002037
2038PyDoc_STRVAR(posix_getcwd__doc__,
2039"getcwd() -> path\n\n\
2040Return a unicode string representing the current working directory.");
2041
2042static PyObject *
2043posix_getcwd_unicode(PyObject *self)
2044{
2045 return posix_getcwd(0);
2046}
2047
2048PyDoc_STRVAR(posix_getcwdb__doc__,
2049"getcwdb() -> path\n\n\
2050Return a bytes string representing the current working directory.");
2051
2052static PyObject *
2053posix_getcwd_bytes(PyObject *self)
2054{
2055 return posix_getcwd(1);
2056}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002057#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002058
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002059
Guido van Rossumb6775db1994-08-01 11:34:53 +00002060#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002061PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002062"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002063Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002064
Barry Warsaw53699e91996-12-10 23:23:01 +00002065static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002066posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002067{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002068 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002069}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002070#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002071
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002072
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002073PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002074"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002075Return a list containing the names of the entries in the directory.\n\
2076\n\
2077 path: path of directory to list\n\
2078\n\
2079The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002080entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002081
Barry Warsaw53699e91996-12-10 23:23:01 +00002082static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002083posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002084{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002085 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002086 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002087#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002088
Barry Warsaw53699e91996-12-10 23:23:01 +00002089 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002090 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002091 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002092 WIN32_FIND_DATA FileData;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002093 PyObject *opath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002094 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002095 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002096 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002097
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002098 PyObject *po;
2099 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2100 WIN32_FIND_DATAW wFileData;
2101 Py_UNICODE *wnamebuf;
2102 /* Overallocate for \\*.*\0 */
2103 len = PyUnicode_GET_SIZE(po);
2104 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2105 if (!wnamebuf) {
2106 PyErr_NoMemory();
2107 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002108 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002109 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2110 if (len > 0) {
2111 Py_UNICODE wch = wnamebuf[len-1];
2112 if (wch != L'/' && wch != L'\\' && wch != L':')
2113 wnamebuf[len++] = L'\\';
2114 wcscpy(wnamebuf + len, L"*.*");
2115 }
2116 if ((d = PyList_New(0)) == NULL) {
2117 free(wnamebuf);
2118 return NULL;
2119 }
2120 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2121 if (hFindFile == INVALID_HANDLE_VALUE) {
2122 int error = GetLastError();
2123 if (error == ERROR_FILE_NOT_FOUND) {
2124 free(wnamebuf);
2125 return d;
2126 }
2127 Py_DECREF(d);
2128 win32_error_unicode("FindFirstFileW", wnamebuf);
2129 free(wnamebuf);
2130 return NULL;
2131 }
2132 do {
2133 /* Skip over . and .. */
2134 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2135 wcscmp(wFileData.cFileName, L"..") != 0) {
2136 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2137 if (v == NULL) {
2138 Py_DECREF(d);
2139 d = NULL;
2140 break;
2141 }
2142 if (PyList_Append(d, v) != 0) {
2143 Py_DECREF(v);
2144 Py_DECREF(d);
2145 d = NULL;
2146 break;
2147 }
2148 Py_DECREF(v);
2149 }
2150 Py_BEGIN_ALLOW_THREADS
2151 result = FindNextFileW(hFindFile, &wFileData);
2152 Py_END_ALLOW_THREADS
2153 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2154 it got to the end of the directory. */
2155 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2156 Py_DECREF(d);
2157 win32_error_unicode("FindNextFileW", wnamebuf);
2158 FindClose(hFindFile);
2159 free(wnamebuf);
2160 return NULL;
2161 }
2162 } while (result == TRUE);
2163
2164 if (FindClose(hFindFile) == FALSE) {
2165 Py_DECREF(d);
2166 win32_error_unicode("FindClose", wnamebuf);
2167 free(wnamebuf);
2168 return NULL;
2169 }
2170 free(wnamebuf);
2171 return d;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002172 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002173 /* Drop the argument parsing error as narrow strings
2174 are also valid. */
2175 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002176
Martin v. Löwis011e8422009-05-05 04:43:17 +00002177 if (!PyArg_ParseTuple(args, "O&:listdir",
2178 PyUnicode_FSConverter, &opath))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002179 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002180 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002181 PyErr_SetString(PyExc_ValueError, "path too long");
2182 Py_DECREF(opath);
2183 return NULL;
2184 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00002185 strcpy(namebuf, PyBytes_AsString(opath));
Martin v. Löwis011e8422009-05-05 04:43:17 +00002186 len = PyObject_Size(opath);
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002187 if (len > 0) {
2188 char ch = namebuf[len-1];
2189 if (ch != SEP && ch != ALTSEP && ch != ':')
2190 namebuf[len++] = '/';
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002191 strcpy(namebuf + len, "*.*");
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002192 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002193
Barry Warsaw53699e91996-12-10 23:23:01 +00002194 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002195 return NULL;
2196
2197 hFindFile = FindFirstFile(namebuf, &FileData);
2198 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002199 int error = GetLastError();
2200 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002201 return d;
2202 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002203 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002204 }
2205 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002206 /* Skip over . and .. */
2207 if (strcmp(FileData.cFileName, ".") != 0 &&
2208 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002209 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002210 if (v == NULL) {
2211 Py_DECREF(d);
2212 d = NULL;
2213 break;
2214 }
2215 if (PyList_Append(d, v) != 0) {
2216 Py_DECREF(v);
2217 Py_DECREF(d);
2218 d = NULL;
2219 break;
2220 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002221 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002222 }
Georg Brandl622927b2006-03-07 12:48:03 +00002223 Py_BEGIN_ALLOW_THREADS
2224 result = FindNextFile(hFindFile, &FileData);
2225 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002226 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2227 it got to the end of the directory. */
2228 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2229 Py_DECREF(d);
2230 win32_error("FindNextFile", namebuf);
2231 FindClose(hFindFile);
2232 return NULL;
2233 }
Georg Brandl622927b2006-03-07 12:48:03 +00002234 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002235
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002236 if (FindClose(hFindFile) == FALSE) {
2237 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002238 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002239 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002240
2241 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002242
Tim Peters0bb44a42000-09-15 07:44:49 +00002243#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002244
2245#ifndef MAX_PATH
2246#define MAX_PATH CCHMAXPATH
2247#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002248 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002249 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002250 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002251 PyObject *d, *v;
2252 char namebuf[MAX_PATH+5];
2253 HDIR hdir = 1;
2254 ULONG srchcnt = 1;
2255 FILEFINDBUF3 ep;
2256 APIRET rc;
2257
Martin v. Löwis011e8422009-05-05 04:43:17 +00002258 if (!PyArg_ParseTuple(args, "O&:listdir",
2259 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002260 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002261 name = PyBytes_AsString(oname);
2262 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002263 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002264 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002265 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002266 return NULL;
2267 }
2268 strcpy(namebuf, name);
2269 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002270 if (*pt == ALTSEP)
2271 *pt = SEP;
2272 if (namebuf[len-1] != SEP)
2273 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002274 strcpy(namebuf + len, "*.*");
2275
Neal Norwitz6c913782007-10-14 03:23:09 +00002276 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002277 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002278 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002279 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002280
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002281 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2282 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002283 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002284 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2285 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2286 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002287
2288 if (rc != NO_ERROR) {
2289 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002290 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002291 }
2292
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002293 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002294 do {
2295 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002296 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002297 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002298
2299 strcpy(namebuf, ep.achName);
2300
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002301 /* Leave Case of Name Alone -- In Native Form */
2302 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002303
Christian Heimes72b710a2008-05-26 13:28:38 +00002304 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002305 if (v == NULL) {
2306 Py_DECREF(d);
2307 d = NULL;
2308 break;
2309 }
2310 if (PyList_Append(d, v) != 0) {
2311 Py_DECREF(v);
2312 Py_DECREF(d);
2313 d = NULL;
2314 break;
2315 }
2316 Py_DECREF(v);
2317 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2318 }
2319
Victor Stinnerdcb24032010-04-22 12:08:36 +00002320 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002321 return d;
2322#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002323 PyObject *oname;
2324 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +00002325 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002326 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002327 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002328 int arg_is_unicode = 1;
2329
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002330 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002331 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2332 arg_is_unicode = 0;
2333 PyErr_Clear();
2334 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002335 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002336 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002337 name = PyBytes_AsString(oname);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002338 if ((dirp = opendir(name)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002339 return posix_error_with_allocated_filename(oname);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002340 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002341 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002342 closedir(dirp);
Victor Stinnerdcb24032010-04-22 12:08:36 +00002343 Py_DECREF(oname);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002344 return NULL;
2345 }
Georg Brandl622927b2006-03-07 12:48:03 +00002346 for (;;) {
Georg Brandl3dbca812008-07-23 16:10:53 +00002347 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002348 Py_BEGIN_ALLOW_THREADS
2349 ep = readdir(dirp);
2350 Py_END_ALLOW_THREADS
Georg Brandl3dbca812008-07-23 16:10:53 +00002351 if (ep == NULL) {
2352 if (errno == 0) {
2353 break;
2354 } else {
2355 closedir(dirp);
2356 Py_DECREF(d);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002357 return posix_error_with_allocated_filename(oname);
Georg Brandl3dbca812008-07-23 16:10:53 +00002358 }
2359 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002360 if (ep->d_name[0] == '.' &&
2361 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002362 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002363 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002364 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002365 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002366 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002367 d = NULL;
2368 break;
2369 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002370 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002371 PyObject *w;
2372
2373 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002374 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00002375 "surrogateescape");
Martin v. Löwis011e8422009-05-05 04:43:17 +00002376 Py_DECREF(v);
2377 if (w != NULL)
Just van Rossum6a421832003-03-04 19:30:44 +00002378 v = w;
Just van Rossum6a421832003-03-04 19:30:44 +00002379 else {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002380 /* Encoding failed to decode ASCII bytes.
2381 Raise exception. */
2382 Py_DECREF(d);
2383 d = NULL;
2384 break;
Just van Rossum46c97842003-02-25 21:42:15 +00002385 }
Just van Rossum46c97842003-02-25 21:42:15 +00002386 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002387 if (PyList_Append(d, v) != 0) {
2388 Py_DECREF(v);
2389 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002390 d = NULL;
2391 break;
2392 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002393 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002394 }
2395 closedir(dirp);
Victor Stinnerdcb24032010-04-22 12:08:36 +00002396 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002397
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002398 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002399
Tim Peters0bb44a42000-09-15 07:44:49 +00002400#endif /* which OS */
2401} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002402
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002403#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002404/* A helper function for abspath on win32 */
2405static PyObject *
2406posix__getfullpathname(PyObject *self, PyObject *args)
2407{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002408 PyObject *opath;
2409 char *path;
Mark Hammondef8b6542001-05-13 08:04:26 +00002410 char outbuf[MAX_PATH*2];
2411 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002412#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002413 PyUnicodeObject *po;
2414 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2415 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2416 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2417 Py_UNICODE *wtemp;
2418 DWORD result;
2419 PyObject *v;
2420 result = GetFullPathNameW(wpath,
2421 sizeof(woutbuf)/sizeof(woutbuf[0]),
2422 woutbuf, &wtemp);
2423 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2424 woutbufp = malloc(result * sizeof(Py_UNICODE));
2425 if (!woutbufp)
2426 return PyErr_NoMemory();
2427 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002428 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002429 if (result)
2430 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2431 else
2432 v = win32_error_unicode("GetFullPathNameW", wpath);
2433 if (woutbufp != woutbuf)
2434 free(woutbufp);
2435 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002436 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002437 /* Drop the argument parsing error as narrow strings
2438 are also valid. */
2439 PyErr_Clear();
2440
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002441#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002442 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2443 PyUnicode_FSConverter, &opath))
Mark Hammondef8b6542001-05-13 08:04:26 +00002444 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002445 path = PyBytes_AsString(opath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002446 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2447 outbuf, &temp)) {
2448 win32_error("GetFullPathName", path);
Victor Stinnerdcb24032010-04-22 12:08:36 +00002449 Py_DECREF(opath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002450 return NULL;
2451 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00002452 Py_DECREF(opath);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002453 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2454 return PyUnicode_Decode(outbuf, strlen(outbuf),
2455 Py_FileSystemDefaultEncoding, NULL);
2456 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002457 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002458} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002459#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002460
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002461PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002462"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002463Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002464
Barry Warsaw53699e91996-12-10 23:23:01 +00002465static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002466posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002467{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002468 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002469 PyObject *opath;
2470 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002471 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002472
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002473#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002474 PyUnicodeObject *po;
2475 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2476 Py_BEGIN_ALLOW_THREADS
2477 /* PyUnicode_AS_UNICODE OK without thread lock as
2478 it is a simple dereference. */
2479 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2480 Py_END_ALLOW_THREADS
2481 if (!res)
2482 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2483 Py_INCREF(Py_None);
2484 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002485 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002486 /* Drop the argument parsing error as narrow strings
2487 are also valid. */
2488 PyErr_Clear();
Martin v. Löwis011e8422009-05-05 04:43:17 +00002489 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2490 PyUnicode_FSConverter, &opath, &mode))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002491 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002492 path = PyBytes_AsString(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002493 Py_BEGIN_ALLOW_THREADS
2494 /* PyUnicode_AS_UNICODE OK without thread lock as
2495 it is a simple dereference. */
2496 res = CreateDirectoryA(path, NULL);
2497 Py_END_ALLOW_THREADS
2498 if (!res) {
2499 win32_error("mkdir", path);
Victor Stinnerdcb24032010-04-22 12:08:36 +00002500 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002501 return NULL;
2502 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00002503 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002504 Py_INCREF(Py_None);
2505 return Py_None;
2506#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002507
Martin v. Löwis011e8422009-05-05 04:43:17 +00002508 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2509 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002510 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002511 path = PyBytes_AsString(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002512 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002513#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002514 res = mkdir(path);
2515#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002516 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002517#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002518 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002519 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002520 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00002521 Py_DECREF(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002522 Py_INCREF(Py_None);
2523 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002524#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002525}
2526
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002527
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002528/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2529#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002530#include <sys/resource.h>
2531#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002532
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002533
2534#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002535PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002536"nice(inc) -> new_priority\n\n\
2537Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002538
Barry Warsaw53699e91996-12-10 23:23:01 +00002539static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002540posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002541{
2542 int increment, value;
2543
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002544 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002545 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002546
2547 /* There are two flavours of 'nice': one that returns the new
2548 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002549 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2550 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002551
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002552 If we are of the nice family that returns the new priority, we
2553 need to clear errno before the call, and check if errno is filled
2554 before calling posix_error() on a returnvalue of -1, because the
2555 -1 may be the actual new priority! */
2556
2557 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002558 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002559#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002560 if (value == 0)
2561 value = getpriority(PRIO_PROCESS, 0);
2562#endif
2563 if (value == -1 && errno != 0)
2564 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002565 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002566 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002567}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002568#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002569
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002570PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002571"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002572Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002573
Barry Warsaw53699e91996-12-10 23:23:01 +00002574static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002575posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002576{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002577#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002578 PyObject *o1, *o2;
2579 char *p1, *p2;
2580 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002581 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002582 goto error;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002583 if (!convert_to_unicode(&o1))
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002584 goto error;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002585 if (!convert_to_unicode(&o2)) {
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002586 Py_DECREF(o1);
2587 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002588 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002589 Py_BEGIN_ALLOW_THREADS
2590 result = MoveFileW(PyUnicode_AsUnicode(o1),
2591 PyUnicode_AsUnicode(o2));
2592 Py_END_ALLOW_THREADS
2593 Py_DECREF(o1);
2594 Py_DECREF(o2);
2595 if (!result)
2596 return win32_error("rename", NULL);
2597 Py_INCREF(Py_None);
2598 return Py_None;
2599error:
2600 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002601 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2602 return NULL;
2603 Py_BEGIN_ALLOW_THREADS
2604 result = MoveFileA(p1, p2);
2605 Py_END_ALLOW_THREADS
2606 if (!result)
2607 return win32_error("rename", NULL);
2608 Py_INCREF(Py_None);
2609 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002610#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002611 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002612#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002613}
2614
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002616PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002617"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002618Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002619
Barry Warsaw53699e91996-12-10 23:23:01 +00002620static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002621posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002622{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002623#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002624 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002625#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002626 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002627#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002628}
2629
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002631PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002632"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002633Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002634
Barry Warsaw53699e91996-12-10 23:23:01 +00002635static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002636posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002637{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002638#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00002639 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002640#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002641 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002642#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002643}
2644
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002645
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002646#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002647PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002648"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002649Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002650
Barry Warsaw53699e91996-12-10 23:23:01 +00002651static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002652posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002653{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002654 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002655#ifdef MS_WINDOWS
2656 wchar_t *command;
2657 if (!PyArg_ParseTuple(args, "u:system", &command))
2658 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002659
Barry Warsaw53699e91996-12-10 23:23:01 +00002660 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002661 sts = _wsystem(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002662 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00002663#else
2664 PyObject *command_obj;
2665 char *command;
2666 if (!PyArg_ParseTuple(args, "O&:system",
2667 PyUnicode_FSConverter, &command_obj))
2668 return NULL;
2669
Victor Stinnerdcb24032010-04-22 12:08:36 +00002670 command = PyBytes_AsString(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00002671 Py_BEGIN_ALLOW_THREADS
2672 sts = system(command);
2673 Py_END_ALLOW_THREADS
Victor Stinnerdcb24032010-04-22 12:08:36 +00002674 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00002675#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00002676 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002677}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002678#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002679
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002681PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002682"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002683Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002684
Barry Warsaw53699e91996-12-10 23:23:01 +00002685static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002686posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002687{
2688 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002689 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002690 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002691 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002692 if (i < 0)
2693 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002694 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002695}
2696
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002697
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002698PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002699"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002700Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002701
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002702PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002703"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002704Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002705
Barry Warsaw53699e91996-12-10 23:23:01 +00002706static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002707posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002708{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002709#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002710 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002711#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002712 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002713#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002714}
2715
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002716
Guido van Rossumb6775db1994-08-01 11:34:53 +00002717#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002718PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002719"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002720Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002721
Barry Warsaw53699e91996-12-10 23:23:01 +00002722static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002723posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002724{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002725 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002726 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002727
Barry Warsaw53699e91996-12-10 23:23:01 +00002728 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002729 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002730 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002731 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002732 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002733 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002734 u.sysname,
2735 u.nodename,
2736 u.release,
2737 u.version,
2738 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002739}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002740#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002741
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002742static int
2743extract_time(PyObject *t, long* sec, long* usec)
2744{
2745 long intval;
2746 if (PyFloat_Check(t)) {
2747 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002748 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002749 if (!intobj)
2750 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002751 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002752 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002753 if (intval == -1 && PyErr_Occurred())
2754 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002755 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002756 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002757 if (*usec < 0)
2758 /* If rounding gave us a negative number,
2759 truncate. */
2760 *usec = 0;
2761 return 0;
2762 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002763 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002764 if (intval == -1 && PyErr_Occurred())
2765 return -1;
2766 *sec = intval;
2767 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002768 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002769}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002770
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002771PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002772"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002773utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002774Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002775second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002776
Barry Warsaw53699e91996-12-10 23:23:01 +00002777static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002778posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002779{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002780#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002781 PyObject *arg;
2782 PyUnicodeObject *obwpath;
2783 wchar_t *wpath = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002784 PyObject *oapath;
2785 char *apath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002786 HANDLE hFile;
2787 long atimesec, mtimesec, ausec, musec;
2788 FILETIME atime, mtime;
2789 PyObject *result = NULL;
2790
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002791 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2792 wpath = PyUnicode_AS_UNICODE(obwpath);
2793 Py_BEGIN_ALLOW_THREADS
2794 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2795 NULL, OPEN_EXISTING,
2796 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2797 Py_END_ALLOW_THREADS
2798 if (hFile == INVALID_HANDLE_VALUE)
2799 return win32_error_unicode("utime", wpath);
2800 } else
2801 /* Drop the argument parsing error as narrow strings
2802 are also valid. */
2803 PyErr_Clear();
2804
Thomas Wouters477c8d52006-05-27 19:21:47 +00002805 if (!wpath) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002806 if (!PyArg_ParseTuple(args, "O&O:utime",
2807 PyUnicode_FSConverter, &oapath, &arg))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002808 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002809 apath = PyBytes_AsString(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002810 Py_BEGIN_ALLOW_THREADS
2811 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002812 NULL, OPEN_EXISTING,
2813 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002814 Py_END_ALLOW_THREADS
2815 if (hFile == INVALID_HANDLE_VALUE) {
2816 win32_error("utime", apath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00002817 Py_DECREF(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002818 return NULL;
2819 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00002820 Py_DECREF(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002821 }
2822
2823 if (arg == Py_None) {
2824 SYSTEMTIME now;
2825 GetSystemTime(&now);
2826 if (!SystemTimeToFileTime(&now, &mtime) ||
2827 !SystemTimeToFileTime(&now, &atime)) {
2828 win32_error("utime", NULL);
2829 goto done;
2830 }
2831 }
2832 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2833 PyErr_SetString(PyExc_TypeError,
2834 "utime() arg 2 must be a tuple (atime, mtime)");
2835 goto done;
2836 }
2837 else {
2838 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2839 &atimesec, &ausec) == -1)
2840 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002841 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002842 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2843 &mtimesec, &musec) == -1)
2844 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002845 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002846 }
2847 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2848 /* Avoid putting the file name into the error here,
2849 as that may confuse the user into believing that
2850 something is wrong with the file, when it also
2851 could be the time stamp that gives a problem. */
2852 win32_error("utime", NULL);
2853 }
2854 Py_INCREF(Py_None);
2855 result = Py_None;
2856done:
2857 CloseHandle(hFile);
2858 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002859#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002860
Martin v. Löwis011e8422009-05-05 04:43:17 +00002861 PyObject *opath;
2862 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002863 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002864 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002865 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002866
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002867#if defined(HAVE_UTIMES)
2868 struct timeval buf[2];
2869#define ATIME buf[0].tv_sec
2870#define MTIME buf[1].tv_sec
2871#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002872/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002873 struct utimbuf buf;
2874#define ATIME buf.actime
2875#define MTIME buf.modtime
2876#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002877#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002878 time_t buf[2];
2879#define ATIME buf[0]
2880#define MTIME buf[1]
2881#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002882#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002883
Mark Hammond817c9292003-12-03 01:22:38 +00002884
Martin v. Löwis011e8422009-05-05 04:43:17 +00002885 if (!PyArg_ParseTuple(args, "O&O:utime",
2886 PyUnicode_FSConverter, &opath, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002887 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002888 path = PyBytes_AsString(opath);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002889 if (arg == Py_None) {
2890 /* optional time values not given */
2891 Py_BEGIN_ALLOW_THREADS
2892 res = utime(path, NULL);
2893 Py_END_ALLOW_THREADS
2894 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002895 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002896 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002897 "utime() arg 2 must be a tuple (atime, mtime)");
Victor Stinnerdcb24032010-04-22 12:08:36 +00002898 Py_DECREF(opath);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002899 return NULL;
2900 }
2901 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002902 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002903 &atime, &ausec) == -1) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002904 Py_DECREF(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002905 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002906 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002907 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002908 &mtime, &musec) == -1) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002909 Py_DECREF(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002910 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002911 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002912 ATIME = atime;
2913 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002914#ifdef HAVE_UTIMES
2915 buf[0].tv_usec = ausec;
2916 buf[1].tv_usec = musec;
2917 Py_BEGIN_ALLOW_THREADS
2918 res = utimes(path, buf);
2919 Py_END_ALLOW_THREADS
2920#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002921 Py_BEGIN_ALLOW_THREADS
2922 res = utime(path, UTIME_ARG);
2923 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002924#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002925 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002926 if (res < 0) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002927 return posix_error_with_allocated_filename(opath);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002928 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00002929 Py_DECREF(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002930 Py_INCREF(Py_None);
2931 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002932#undef UTIME_ARG
2933#undef ATIME
2934#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002935#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002936}
2937
Guido van Rossum85e3b011991-06-03 12:42:10 +00002938
Guido van Rossum3b066191991-06-04 19:40:25 +00002939/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002940
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002941PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002942"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002943Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002944
Barry Warsaw53699e91996-12-10 23:23:01 +00002945static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002946posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002947{
2948 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002949 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002950 return NULL;
2951 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002952 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002953}
2954
Martin v. Löwis114619e2002-10-07 06:44:21 +00002955#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2956static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002957free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002958{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002959 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002960 for (i = 0; i < count; i++)
2961 PyMem_Free(array[i]);
2962 PyMem_DEL(array);
2963}
Martin v. Löwis011e8422009-05-05 04:43:17 +00002964
Antoine Pitrou69f71142009-05-24 21:25:49 +00002965static
Martin v. Löwis011e8422009-05-05 04:43:17 +00002966int fsconvert_strdup(PyObject *o, char**out)
2967{
2968 PyObject *bytes;
2969 Py_ssize_t size;
2970 if (!PyUnicode_FSConverter(o, &bytes))
2971 return 0;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002972 size = PyBytes_GET_SIZE(bytes);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002973 *out = PyMem_Malloc(size+1);
2974 if (!*out)
2975 return 0;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002976 memcpy(*out, PyBytes_AsString(bytes), size+1);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002977 Py_DECREF(bytes);
2978 return 1;
2979}
Martin v. Löwis114619e2002-10-07 06:44:21 +00002980#endif
2981
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002982
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002983#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002984PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002985"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002986Execute an executable path with arguments, replacing current process.\n\
2987\n\
2988 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002989 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002990
Barry Warsaw53699e91996-12-10 23:23:01 +00002991static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002992posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002993{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002994 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002995 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002996 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002997 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002998 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002999 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003000
Guido van Rossum89b33251993-10-22 14:26:06 +00003001 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00003002 argv is a list or tuple of strings. */
3003
Martin v. Löwis011e8422009-05-05 04:43:17 +00003004 if (!PyArg_ParseTuple(args, "O&O:execv",
3005 PyUnicode_FSConverter,
3006 &opath, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003007 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00003008 path = PyBytes_AsString(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00003009 if (PyList_Check(argv)) {
3010 argc = PyList_Size(argv);
3011 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003012 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003013 else if (PyTuple_Check(argv)) {
3014 argc = PyTuple_Size(argv);
3015 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003016 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003017 else {
Fred Drake661ea262000-10-24 19:57:45 +00003018 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003019 Py_DECREF(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003020 return NULL;
3021 }
Thomas Heller6790d602007-08-30 17:15:14 +00003022 if (argc < 1) {
3023 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003024 Py_DECREF(opath);
Thomas Heller6790d602007-08-30 17:15:14 +00003025 return NULL;
3026 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003027
Barry Warsaw53699e91996-12-10 23:23:01 +00003028 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003029 if (argvlist == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00003030 Py_DECREF(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003031 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003032 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003033 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003034 if (!fsconvert_strdup((*getitem)(argv, i),
3035 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003036 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00003037 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003038 "execv() arg 2 must contain only strings");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003039 Py_DECREF(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003040 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003041
Guido van Rossum85e3b011991-06-03 12:42:10 +00003042 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003043 }
3044 argvlist[argc] = NULL;
3045
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003046 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003047
Guido van Rossum85e3b011991-06-03 12:42:10 +00003048 /* If we get here it's definitely an error */
3049
Martin v. Löwis114619e2002-10-07 06:44:21 +00003050 free_string_array(argvlist, argc);
Victor Stinnerdcb24032010-04-22 12:08:36 +00003051 Py_DECREF(opath);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003052 return posix_error();
3053}
3054
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003055
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003056PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003057"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003058Execute a path with arguments and environment, replacing current process.\n\
3059\n\
3060 path: path of executable file\n\
3061 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003062 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003063
Barry Warsaw53699e91996-12-10 23:23:01 +00003064static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003065posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003066{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003067 PyObject *opath;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003068 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003069 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003070 char **argvlist;
3071 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003072 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003073 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003074 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003075 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003076
3077 /* execve has three arguments: (path, argv, env), where
3078 argv is a list or tuple of strings and env is a dictionary
3079 like posix.environ. */
3080
Martin v. Löwis011e8422009-05-05 04:43:17 +00003081 if (!PyArg_ParseTuple(args, "O&OO:execve",
3082 PyUnicode_FSConverter,
3083 &opath, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003084 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00003085 path = PyBytes_AsString(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00003086 if (PyList_Check(argv)) {
3087 argc = PyList_Size(argv);
3088 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003089 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003090 else if (PyTuple_Check(argv)) {
3091 argc = PyTuple_Size(argv);
3092 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003093 }
3094 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003095 PyErr_SetString(PyExc_TypeError,
3096 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003097 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003098 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003099 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003100 PyErr_SetString(PyExc_TypeError,
3101 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003102 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003103 }
3104
Barry Warsaw53699e91996-12-10 23:23:01 +00003105 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003106 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003107 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003108 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003109 }
3110 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003111 if (!fsconvert_strdup((*getitem)(argv, i),
3112 &argvlist[i]))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003113 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003114 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003115 goto fail_1;
3116 }
3117 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003118 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003119 argvlist[argc] = NULL;
3120
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003121 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003122 if (i < 0)
3123 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003124 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003125 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003126 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003127 goto fail_1;
3128 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003129 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003130 keys = PyMapping_Keys(env);
3131 vals = PyMapping_Values(env);
3132 if (!keys || !vals)
3133 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003134 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3135 PyErr_SetString(PyExc_TypeError,
3136 "execve(): env.keys() or env.values() is not a list");
3137 goto fail_2;
3138 }
Tim Peters5aa91602002-01-30 05:46:57 +00003139
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003140 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003141 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003142 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003143
3144 key = PyList_GetItem(keys, pos);
3145 val = PyList_GetItem(vals, pos);
3146 if (!key || !val)
3147 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003148
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003149 if (!PyArg_Parse(
3150 key,
3151 "s;execve() arg 3 contains a non-string key",
3152 &k) ||
3153 !PyArg_Parse(
3154 val,
3155 "s;execve() arg 3 contains a non-string value",
3156 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003157 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003158 goto fail_2;
3159 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003160
3161#if defined(PYOS_OS2)
3162 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3163 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3164#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003165 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003166 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003167 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003168 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003169 goto fail_2;
3170 }
Tim Petersc8996f52001-12-03 20:41:00 +00003171 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003172 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003173#if defined(PYOS_OS2)
3174 }
3175#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003176 }
3177 envlist[envc] = 0;
3178
3179 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003180
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003181 /* If we get here it's definitely an error */
3182
3183 (void) posix_error();
3184
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003185 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003186 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003187 PyMem_DEL(envlist[envc]);
3188 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003189 fail_1:
3190 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003191 Py_XDECREF(vals);
3192 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003193 fail_0:
Victor Stinnerdcb24032010-04-22 12:08:36 +00003194 Py_DECREF(opath);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003195 return NULL;
3196}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003197#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003198
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003199
Guido van Rossuma1065681999-01-25 23:20:23 +00003200#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003201PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003202"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003203Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003204\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003205 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003206 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003207 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003208
3209static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003210posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003211{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003212 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003213 char *path;
3214 PyObject *argv;
3215 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003216 int mode, i;
3217 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003218 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003219 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003220
3221 /* spawnv has three arguments: (mode, path, argv), where
3222 argv is a list or tuple of strings. */
3223
Martin v. Löwis011e8422009-05-05 04:43:17 +00003224 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3225 PyUnicode_FSConverter,
3226 &opath, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003227 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00003228 path = PyBytes_AsString(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003229 if (PyList_Check(argv)) {
3230 argc = PyList_Size(argv);
3231 getitem = PyList_GetItem;
3232 }
3233 else if (PyTuple_Check(argv)) {
3234 argc = PyTuple_Size(argv);
3235 getitem = PyTuple_GetItem;
3236 }
3237 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003238 PyErr_SetString(PyExc_TypeError,
3239 "spawnv() arg 2 must be a tuple or list");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003240 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003241 return NULL;
3242 }
3243
3244 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003245 if (argvlist == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00003246 Py_DECREF(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003247 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003248 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003249 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003250 if (!fsconvert_strdup((*getitem)(argv, i),
3251 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003252 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003253 PyErr_SetString(
3254 PyExc_TypeError,
3255 "spawnv() arg 2 must contain only strings");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003256 Py_DECREF(opath);
Fred Drake137507e2000-06-01 02:02:46 +00003257 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003258 }
3259 }
3260 argvlist[argc] = NULL;
3261
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003262#if defined(PYOS_OS2) && defined(PYCC_GCC)
3263 Py_BEGIN_ALLOW_THREADS
3264 spawnval = spawnv(mode, path, argvlist);
3265 Py_END_ALLOW_THREADS
3266#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003267 if (mode == _OLD_P_OVERLAY)
3268 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003269
Tim Peters25059d32001-12-07 20:35:43 +00003270 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003271 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003272 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003273#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003274
Martin v. Löwis114619e2002-10-07 06:44:21 +00003275 free_string_array(argvlist, argc);
Victor Stinnerdcb24032010-04-22 12:08:36 +00003276 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003277
Fred Drake699f3522000-06-29 21:12:41 +00003278 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003279 return posix_error();
3280 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003281#if SIZEOF_LONG == SIZEOF_VOID_P
3282 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003283#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003284 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003285#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003286}
3287
3288
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003289PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003290"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003291Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003292\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003293 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003294 path: path of executable file\n\
3295 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003296 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003297
3298static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003299posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003300{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003301 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003302 char *path;
3303 PyObject *argv, *env;
3304 char **argvlist;
3305 char **envlist;
3306 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003307 int mode, pos, envc;
3308 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003309 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003310 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003311 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003312
3313 /* spawnve has four arguments: (mode, path, argv, env), where
3314 argv is a list or tuple of strings and env is a dictionary
3315 like posix.environ. */
3316
Martin v. Löwis011e8422009-05-05 04:43:17 +00003317 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3318 PyUnicode_FSConverter,
3319 &opath, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003320 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00003321 path = PyBytes_AsString(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003322 if (PyList_Check(argv)) {
3323 argc = PyList_Size(argv);
3324 getitem = PyList_GetItem;
3325 }
3326 else if (PyTuple_Check(argv)) {
3327 argc = PyTuple_Size(argv);
3328 getitem = PyTuple_GetItem;
3329 }
3330 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003331 PyErr_SetString(PyExc_TypeError,
3332 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003333 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003334 }
3335 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003336 PyErr_SetString(PyExc_TypeError,
3337 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003338 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003339 }
3340
3341 argvlist = PyMem_NEW(char *, argc+1);
3342 if (argvlist == NULL) {
3343 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003344 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003345 }
3346 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003347 if (!fsconvert_strdup((*getitem)(argv, i),
3348 &argvlist[i]))
Guido van Rossuma1065681999-01-25 23:20:23 +00003349 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003350 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003351 goto fail_1;
3352 }
3353 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003354 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003355 argvlist[argc] = NULL;
3356
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003357 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003358 if (i < 0)
3359 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003360 envlist = PyMem_NEW(char *, i + 1);
3361 if (envlist == NULL) {
3362 PyErr_NoMemory();
3363 goto fail_1;
3364 }
3365 envc = 0;
3366 keys = PyMapping_Keys(env);
3367 vals = PyMapping_Values(env);
3368 if (!keys || !vals)
3369 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003370 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3371 PyErr_SetString(PyExc_TypeError,
3372 "spawnve(): env.keys() or env.values() is not a list");
3373 goto fail_2;
3374 }
Tim Peters5aa91602002-01-30 05:46:57 +00003375
Guido van Rossuma1065681999-01-25 23:20:23 +00003376 for (pos = 0; pos < i; pos++) {
3377 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003378 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003379
3380 key = PyList_GetItem(keys, pos);
3381 val = PyList_GetItem(vals, pos);
3382 if (!key || !val)
3383 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003384
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003385 if (!PyArg_Parse(
3386 key,
3387 "s;spawnve() arg 3 contains a non-string key",
3388 &k) ||
3389 !PyArg_Parse(
3390 val,
3391 "s;spawnve() arg 3 contains a non-string value",
3392 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003393 {
3394 goto fail_2;
3395 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003396 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003397 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003398 if (p == NULL) {
3399 PyErr_NoMemory();
3400 goto fail_2;
3401 }
Tim Petersc8996f52001-12-03 20:41:00 +00003402 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003403 envlist[envc++] = p;
3404 }
3405 envlist[envc] = 0;
3406
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003407#if defined(PYOS_OS2) && defined(PYCC_GCC)
3408 Py_BEGIN_ALLOW_THREADS
3409 spawnval = spawnve(mode, path, argvlist, envlist);
3410 Py_END_ALLOW_THREADS
3411#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003412 if (mode == _OLD_P_OVERLAY)
3413 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003414
3415 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003416 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003417 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003418#endif
Tim Peters25059d32001-12-07 20:35:43 +00003419
Fred Drake699f3522000-06-29 21:12:41 +00003420 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003421 (void) posix_error();
3422 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003423#if SIZEOF_LONG == SIZEOF_VOID_P
3424 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003425#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003426 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003427#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003428
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003429 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003430 while (--envc >= 0)
3431 PyMem_DEL(envlist[envc]);
3432 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003433 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003434 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003435 Py_XDECREF(vals);
3436 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003437 fail_0:
Victor Stinnerdcb24032010-04-22 12:08:36 +00003438 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003439 return res;
3440}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003441
3442/* OS/2 supports spawnvp & spawnvpe natively */
3443#if defined(PYOS_OS2)
3444PyDoc_STRVAR(posix_spawnvp__doc__,
3445"spawnvp(mode, file, args)\n\n\
3446Execute the program 'file' in a new process, using the environment\n\
3447search path to find the file.\n\
3448\n\
3449 mode: mode of process creation\n\
3450 file: executable file name\n\
3451 args: tuple or list of strings");
3452
3453static PyObject *
3454posix_spawnvp(PyObject *self, PyObject *args)
3455{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003456 PyObject *opath;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003457 char *path;
3458 PyObject *argv;
3459 char **argvlist;
3460 int mode, i, argc;
3461 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003462 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003463
3464 /* spawnvp has three arguments: (mode, path, argv), where
3465 argv is a list or tuple of strings. */
3466
Martin v. Löwis011e8422009-05-05 04:43:17 +00003467 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3468 PyUnicode_FSConverter,
3469 &opath, &argv))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003470 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00003471 path = PyBytes_AsString(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003472 if (PyList_Check(argv)) {
3473 argc = PyList_Size(argv);
3474 getitem = PyList_GetItem;
3475 }
3476 else if (PyTuple_Check(argv)) {
3477 argc = PyTuple_Size(argv);
3478 getitem = PyTuple_GetItem;
3479 }
3480 else {
3481 PyErr_SetString(PyExc_TypeError,
3482 "spawnvp() arg 2 must be a tuple or list");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003483 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003484 return NULL;
3485 }
3486
3487 argvlist = PyMem_NEW(char *, argc+1);
3488 if (argvlist == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00003489 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003490 return PyErr_NoMemory();
3491 }
3492 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003493 if (!fsconvert_strdup((*getitem)(argv, i),
3494 &argvlist[i])) {
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003495 free_string_array(argvlist, i);
3496 PyErr_SetString(
3497 PyExc_TypeError,
3498 "spawnvp() arg 2 must contain only strings");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003499 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003500 return NULL;
3501 }
3502 }
3503 argvlist[argc] = NULL;
3504
3505 Py_BEGIN_ALLOW_THREADS
3506#if defined(PYCC_GCC)
3507 spawnval = spawnvp(mode, path, argvlist);
3508#else
3509 spawnval = _spawnvp(mode, path, argvlist);
3510#endif
3511 Py_END_ALLOW_THREADS
3512
3513 free_string_array(argvlist, argc);
Victor Stinnerdcb24032010-04-22 12:08:36 +00003514 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003515
3516 if (spawnval == -1)
3517 return posix_error();
3518 else
3519 return Py_BuildValue("l", (long) spawnval);
3520}
3521
3522
3523PyDoc_STRVAR(posix_spawnvpe__doc__,
3524"spawnvpe(mode, file, args, env)\n\n\
3525Execute the program 'file' in a new process, using the environment\n\
3526search path to find the file.\n\
3527\n\
3528 mode: mode of process creation\n\
3529 file: executable file name\n\
3530 args: tuple or list of arguments\n\
3531 env: dictionary of strings mapping to strings");
3532
3533static PyObject *
3534posix_spawnvpe(PyObject *self, PyObject *args)
3535{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003536 PyObject *opath
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003537 char *path;
3538 PyObject *argv, *env;
3539 char **argvlist;
3540 char **envlist;
3541 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3542 int mode, i, pos, argc, envc;
3543 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003544 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003545 int lastarg = 0;
3546
3547 /* spawnvpe has four arguments: (mode, path, argv, env), where
3548 argv is a list or tuple of strings and env is a dictionary
3549 like posix.environ. */
3550
3551 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
Martin v. Löwis011e8422009-05-05 04:43:17 +00003552 PyUnicode_FSConverter,
3553 &opath, &argv, &env))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003554 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00003555 path = PyBytes_AsString(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003556 if (PyList_Check(argv)) {
3557 argc = PyList_Size(argv);
3558 getitem = PyList_GetItem;
3559 }
3560 else if (PyTuple_Check(argv)) {
3561 argc = PyTuple_Size(argv);
3562 getitem = PyTuple_GetItem;
3563 }
3564 else {
3565 PyErr_SetString(PyExc_TypeError,
3566 "spawnvpe() arg 2 must be a tuple or list");
3567 goto fail_0;
3568 }
3569 if (!PyMapping_Check(env)) {
3570 PyErr_SetString(PyExc_TypeError,
3571 "spawnvpe() arg 3 must be a mapping object");
3572 goto fail_0;
3573 }
3574
3575 argvlist = PyMem_NEW(char *, argc+1);
3576 if (argvlist == NULL) {
3577 PyErr_NoMemory();
3578 goto fail_0;
3579 }
3580 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003581 if (!fsconvert_strdup((*getitem)(argv, i),
3582 &argvlist[i]))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003583 {
3584 lastarg = i;
3585 goto fail_1;
3586 }
3587 }
3588 lastarg = argc;
3589 argvlist[argc] = NULL;
3590
3591 i = PyMapping_Size(env);
3592 if (i < 0)
3593 goto fail_1;
3594 envlist = PyMem_NEW(char *, i + 1);
3595 if (envlist == NULL) {
3596 PyErr_NoMemory();
3597 goto fail_1;
3598 }
3599 envc = 0;
3600 keys = PyMapping_Keys(env);
3601 vals = PyMapping_Values(env);
3602 if (!keys || !vals)
3603 goto fail_2;
3604 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3605 PyErr_SetString(PyExc_TypeError,
3606 "spawnvpe(): env.keys() or env.values() is not a list");
3607 goto fail_2;
3608 }
3609
3610 for (pos = 0; pos < i; pos++) {
3611 char *p, *k, *v;
3612 size_t len;
3613
3614 key = PyList_GetItem(keys, pos);
3615 val = PyList_GetItem(vals, pos);
3616 if (!key || !val)
3617 goto fail_2;
3618
3619 if (!PyArg_Parse(
3620 key,
3621 "s;spawnvpe() arg 3 contains a non-string key",
3622 &k) ||
3623 !PyArg_Parse(
3624 val,
3625 "s;spawnvpe() arg 3 contains a non-string value",
3626 &v))
3627 {
3628 goto fail_2;
3629 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003630 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003631 p = PyMem_NEW(char, len);
3632 if (p == NULL) {
3633 PyErr_NoMemory();
3634 goto fail_2;
3635 }
3636 PyOS_snprintf(p, len, "%s=%s", k, v);
3637 envlist[envc++] = p;
3638 }
3639 envlist[envc] = 0;
3640
3641 Py_BEGIN_ALLOW_THREADS
3642#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003643 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003644#else
Christian Heimes292d3512008-02-03 16:51:08 +00003645 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003646#endif
3647 Py_END_ALLOW_THREADS
3648
3649 if (spawnval == -1)
3650 (void) posix_error();
3651 else
3652 res = Py_BuildValue("l", (long) spawnval);
3653
3654 fail_2:
3655 while (--envc >= 0)
3656 PyMem_DEL(envlist[envc]);
3657 PyMem_DEL(envlist);
3658 fail_1:
3659 free_string_array(argvlist, lastarg);
3660 Py_XDECREF(vals);
3661 Py_XDECREF(keys);
3662 fail_0:
Victor Stinnerdcb24032010-04-22 12:08:36 +00003663 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003664 return res;
3665}
3666#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003667#endif /* HAVE_SPAWNV */
3668
3669
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003670#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003671PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003672"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003673Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3674\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003675Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003676
3677static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003678posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003679{
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003680 pid_t pid;
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003681 int result = 0;
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003682 _PyImport_AcquireLock();
3683 pid = fork1();
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003684 if (pid == 0) {
3685 /* child: this clobbers and resets the import lock. */
3686 PyOS_AfterFork();
3687 } else {
3688 /* parent: release the import lock. */
3689 result = _PyImport_ReleaseLock();
3690 }
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003691 if (pid == -1)
3692 return posix_error();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003693 if (result < 0) {
3694 /* Don't clobber the OSError if the fork failed. */
3695 PyErr_SetString(PyExc_RuntimeError,
3696 "not holding the import lock");
3697 return NULL;
3698 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003699 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003700}
3701#endif
3702
3703
Guido van Rossumad0ee831995-03-01 10:34:45 +00003704#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003705PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003706"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003707Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003708Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003709
Barry Warsaw53699e91996-12-10 23:23:01 +00003710static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003711posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +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 = fork();
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 Rossum85e3b011991-06-03 12:42:10 +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 Rossum85e3b011991-06-03 12:42:10 +00003733}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003734#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003735
Neal Norwitzb59798b2003-03-21 01:43:31 +00003736/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003737/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3738#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003739#define DEV_PTY_FILE "/dev/ptc"
3740#define HAVE_DEV_PTMX
3741#else
3742#define DEV_PTY_FILE "/dev/ptmx"
3743#endif
3744
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003745#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003746#ifdef HAVE_PTY_H
3747#include <pty.h>
3748#else
3749#ifdef HAVE_LIBUTIL_H
3750#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00003751#else
3752#ifdef HAVE_UTIL_H
3753#include <util.h>
3754#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003755#endif /* HAVE_LIBUTIL_H */
3756#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003757#ifdef HAVE_STROPTS_H
3758#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003759#endif
3760#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003761
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003762#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003763PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003764"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003765Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003766
3767static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003768posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003769{
3770 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003771#ifndef HAVE_OPENPTY
3772 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003773#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003774#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003775 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003776#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003777 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003778#endif
3779#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003780
Thomas Wouters70c21a12000-07-14 14:28:33 +00003781#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003782 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3783 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003784#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003785 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3786 if (slave_name == NULL)
3787 return posix_error();
3788
3789 slave_fd = open(slave_name, O_RDWR);
3790 if (slave_fd < 0)
3791 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003792#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003793 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003794 if (master_fd < 0)
3795 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003796 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003797 /* change permission of slave */
3798 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003799 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003800 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003801 }
3802 /* unlock slave */
3803 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003804 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003805 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003806 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003807 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003808 slave_name = ptsname(master_fd); /* get name of slave */
3809 if (slave_name == NULL)
3810 return posix_error();
3811 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3812 if (slave_fd < 0)
3813 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003814#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003815 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3816 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003817#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003818 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003819#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003820#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003821#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003822
Fred Drake8cef4cf2000-06-28 16:40:38 +00003823 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003824
Fred Drake8cef4cf2000-06-28 16:40:38 +00003825}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003826#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003827
3828#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003829PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003830"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003831Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3832Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003833To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003834
3835static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003836posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003837{
Gregory P. Smith2a1c0272010-03-01 17:04:45 +00003838 int master_fd = -1, result = 0;
Christian Heimes400adb02008-02-01 08:12:03 +00003839 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003840
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003841 _PyImport_AcquireLock();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003842 pid = forkpty(&master_fd, NULL, NULL, NULL);
Gregory P. Smith2a1c0272010-03-01 17:04:45 +00003843 if (pid == 0) {
3844 /* child: this clobbers and resets the import lock. */
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003845 PyOS_AfterFork();
Gregory P. Smith2a1c0272010-03-01 17:04:45 +00003846 } else {
3847 /* parent: release the import lock. */
3848 result = _PyImport_ReleaseLock();
3849 }
Fred Drake8cef4cf2000-06-28 16:40:38 +00003850 if (pid == -1)
3851 return posix_error();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003852 if (result < 0) {
3853 /* Don't clobber the OSError if the fork failed. */
3854 PyErr_SetString(PyExc_RuntimeError,
3855 "not holding the import lock");
3856 return NULL;
3857 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003858 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003859}
3860#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003861
Guido van Rossumad0ee831995-03-01 10:34:45 +00003862#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003863PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003864"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003865Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003866
Barry Warsaw53699e91996-12-10 23:23:01 +00003867static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003868posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003869{
Christian Heimes217cfd12007-12-02 14:31:20 +00003870 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003871}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003872#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003873
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003874
Guido van Rossumad0ee831995-03-01 10:34:45 +00003875#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003876PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003877"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003878Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003879
Barry Warsaw53699e91996-12-10 23:23:01 +00003880static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003881posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003882{
Christian Heimes217cfd12007-12-02 14:31:20 +00003883 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003884}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003885#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003886
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003887
Guido van Rossumad0ee831995-03-01 10:34:45 +00003888#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003889PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003890"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003891Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003892
Barry Warsaw53699e91996-12-10 23:23:01 +00003893static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003894posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003895{
Christian Heimes217cfd12007-12-02 14:31:20 +00003896 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003897}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003898#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003899
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003900
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003901PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003902"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003903Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003904
Barry Warsaw53699e91996-12-10 23:23:01 +00003905static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003906posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003907{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003908 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003909}
3910
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003911
Fred Drakec9680921999-12-13 16:37:25 +00003912#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003913PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003914"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003915Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003916
3917static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003918posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003919{
3920 PyObject *result = NULL;
3921
Fred Drakec9680921999-12-13 16:37:25 +00003922#ifdef NGROUPS_MAX
3923#define MAX_GROUPS NGROUPS_MAX
3924#else
3925 /* defined to be 16 on Solaris7, so this should be a small number */
3926#define MAX_GROUPS 64
3927#endif
3928 gid_t grouplist[MAX_GROUPS];
3929 int n;
3930
3931 n = getgroups(MAX_GROUPS, grouplist);
3932 if (n < 0)
3933 posix_error();
3934 else {
3935 result = PyList_New(n);
3936 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003937 int i;
3938 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003939 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003940 if (o == NULL) {
3941 Py_DECREF(result);
3942 result = NULL;
3943 break;
3944 }
3945 PyList_SET_ITEM(result, i, o);
3946 }
3947 }
3948 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003949
Fred Drakec9680921999-12-13 16:37:25 +00003950 return result;
3951}
3952#endif
3953
Antoine Pitroub7572f02009-12-02 20:46:48 +00003954#ifdef HAVE_INITGROUPS
3955PyDoc_STRVAR(posix_initgroups__doc__,
3956"initgroups(username, gid) -> None\n\n\
3957Call the system initgroups() to initialize the group access list with all of\n\
3958the groups of which the specified username is a member, plus the specified\n\
3959group id.");
3960
3961static PyObject *
3962posix_initgroups(PyObject *self, PyObject *args)
3963{
3964 char *username;
3965 long gid;
3966
3967 if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid))
3968 return NULL;
3969
3970 if (initgroups(username, (gid_t) gid) == -1)
3971 return PyErr_SetFromErrno(PyExc_OSError);
3972
3973 Py_INCREF(Py_None);
3974 return Py_None;
3975}
3976#endif
3977
Martin v. Löwis606edc12002-06-13 21:09:11 +00003978#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003979PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003980"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003981Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003982
3983static PyObject *
3984posix_getpgid(PyObject *self, PyObject *args)
3985{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003986 pid_t pid, pgid;
Gregory P. Smith84508572010-03-14 18:56:11 +00003987 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
Martin v. Löwis606edc12002-06-13 21:09:11 +00003988 return NULL;
3989 pgid = getpgid(pid);
3990 if (pgid < 0)
3991 return posix_error();
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003992 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003993}
3994#endif /* HAVE_GETPGID */
3995
3996
Guido van Rossumb6775db1994-08-01 11:34:53 +00003997#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003998PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003999"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004000Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004001
Barry Warsaw53699e91996-12-10 23:23:01 +00004002static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004003posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004004{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004005#ifdef GETPGRP_HAVE_ARG
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004006 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004007#else /* GETPGRP_HAVE_ARG */
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004008 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004009#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004010}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004011#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004012
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004013
Guido van Rossumb6775db1994-08-01 11:34:53 +00004014#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004015PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004016"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004017Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004018
Barry Warsaw53699e91996-12-10 23:23:01 +00004019static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004020posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004021{
Guido van Rossum64933891994-10-20 21:56:42 +00004022#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00004023 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004024#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004025 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004026#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00004027 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004028 Py_INCREF(Py_None);
4029 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004030}
4031
Guido van Rossumb6775db1994-08-01 11:34:53 +00004032#endif /* HAVE_SETPGRP */
4033
Guido van Rossumad0ee831995-03-01 10:34:45 +00004034#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004035PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004036"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004037Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004038
Barry Warsaw53699e91996-12-10 23:23:01 +00004039static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004040posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004041{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004042 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004043}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004044#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004045
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004046
Fred Drake12c6e2d1999-12-14 21:25:03 +00004047#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004048PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004049"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004050Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004051
4052static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004053posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004054{
Neal Norwitze241ce82003-02-17 18:17:05 +00004055 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00004056 char *name;
4057 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004058
Fred Drakea30680b2000-12-06 21:24:28 +00004059 errno = 0;
4060 name = getlogin();
4061 if (name == NULL) {
4062 if (errno)
4063 posix_error();
4064 else
4065 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00004066 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00004067 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00004068 else
Neal Norwitz93c56822007-08-26 07:10:06 +00004069 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00004070 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004071
Fred Drake12c6e2d1999-12-14 21:25:03 +00004072 return result;
4073}
4074#endif
4075
Guido van Rossumad0ee831995-03-01 10:34:45 +00004076#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004077PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004078"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004079Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004080
Barry Warsaw53699e91996-12-10 23:23:01 +00004081static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004082posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004083{
Christian Heimes217cfd12007-12-02 14:31:20 +00004084 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004085}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004086#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004087
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004088
Guido van Rossumad0ee831995-03-01 10:34:45 +00004089#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004090PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004091"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004092Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004093
Barry Warsaw53699e91996-12-10 23:23:01 +00004094static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004095posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004096{
Christian Heimes292d3512008-02-03 16:51:08 +00004097 pid_t pid;
4098 int sig;
Gregory P. Smith84508572010-03-14 18:56:11 +00004099 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00004100 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004101#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004102 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4103 APIRET rc;
4104 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004105 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004106
4107 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4108 APIRET rc;
4109 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004110 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004111
4112 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004113 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004114#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004115 if (kill(pid, sig) == -1)
4116 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004117#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004118 Py_INCREF(Py_None);
4119 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004120}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004121#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004122
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004123#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004124PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004125"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004126Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004127
4128static PyObject *
4129posix_killpg(PyObject *self, PyObject *args)
4130{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004131 int sig;
4132 pid_t pgid;
4133 /* XXX some man pages make the `pgid` parameter an int, others
4134 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4135 take the same type. Moreover, pid_t is always at least as wide as
4136 int (else compilation of this module fails), which is safe. */
Gregory P. Smith84508572010-03-14 18:56:11 +00004137 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004138 return NULL;
4139 if (killpg(pgid, sig) == -1)
4140 return posix_error();
4141 Py_INCREF(Py_None);
4142 return Py_None;
4143}
4144#endif
4145
Brian Curtineb24d742010-04-12 17:16:38 +00004146#ifdef MS_WINDOWS
4147PyDoc_STRVAR(win32_kill__doc__,
4148"kill(pid, sig)\n\n\
4149Kill a process with a signal.");
4150
4151static PyObject *
4152win32_kill(PyObject *self, PyObject *args)
4153{
4154 PyObject *result, handle_obj;
4155 DWORD pid, sig, err;
4156 HANDLE handle;
4157
4158 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4159 return NULL;
4160
4161 /* Console processes which share a common console can be sent CTRL+C or
4162 CTRL+BREAK events, provided they handle said events. */
4163 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4164 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4165 err = GetLastError();
4166 PyErr_SetFromWindowsErr(err);
4167 }
4168 else
4169 Py_RETURN_NONE;
4170 }
4171
4172 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4173 attempt to open and terminate the process. */
4174 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4175 if (handle == NULL) {
4176 err = GetLastError();
4177 return PyErr_SetFromWindowsErr(err);
4178 }
4179
4180 if (TerminateProcess(handle, sig) == 0) {
4181 err = GetLastError();
4182 result = PyErr_SetFromWindowsErr(err);
4183 } else {
4184 Py_INCREF(Py_None);
4185 result = Py_None;
4186 }
4187
4188 CloseHandle(handle);
4189 return result;
4190}
4191#endif /* MS_WINDOWS */
4192
Guido van Rossumc0125471996-06-28 18:55:32 +00004193#ifdef HAVE_PLOCK
4194
4195#ifdef HAVE_SYS_LOCK_H
4196#include <sys/lock.h>
4197#endif
4198
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004199PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004200"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004201Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004202
Barry Warsaw53699e91996-12-10 23:23:01 +00004203static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004204posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004205{
4206 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004207 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004208 return NULL;
4209 if (plock(op) == -1)
4210 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004211 Py_INCREF(Py_None);
4212 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004213}
4214#endif
4215
Guido van Rossumb6775db1994-08-01 11:34:53 +00004216#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004217PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004218"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004219Set the current process's user id.");
4220
Barry Warsaw53699e91996-12-10 23:23:01 +00004221static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004222posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004223{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004224 long uid_arg;
4225 uid_t uid;
4226 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004227 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004228 uid = uid_arg;
4229 if (uid != uid_arg) {
4230 PyErr_SetString(PyExc_OverflowError, "user id too big");
4231 return NULL;
4232 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004233 if (setuid(uid) < 0)
4234 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004235 Py_INCREF(Py_None);
4236 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004237}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004238#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004239
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004240
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004241#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004242PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004243"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004244Set the current process's effective user id.");
4245
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004246static PyObject *
4247posix_seteuid (PyObject *self, PyObject *args)
4248{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004249 long euid_arg;
4250 uid_t euid;
4251 if (!PyArg_ParseTuple(args, "l", &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004252 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004253 euid = euid_arg;
4254 if (euid != euid_arg) {
4255 PyErr_SetString(PyExc_OverflowError, "user id too big");
4256 return NULL;
4257 }
4258 if (seteuid(euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004259 return posix_error();
4260 } else {
4261 Py_INCREF(Py_None);
4262 return Py_None;
4263 }
4264}
4265#endif /* HAVE_SETEUID */
4266
4267#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004268PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004269"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004270Set the current process's effective group id.");
4271
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004272static PyObject *
4273posix_setegid (PyObject *self, PyObject *args)
4274{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004275 long egid_arg;
4276 gid_t egid;
4277 if (!PyArg_ParseTuple(args, "l", &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004278 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004279 egid = egid_arg;
4280 if (egid != egid_arg) {
4281 PyErr_SetString(PyExc_OverflowError, "group id too big");
4282 return NULL;
4283 }
4284 if (setegid(egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004285 return posix_error();
4286 } else {
4287 Py_INCREF(Py_None);
4288 return Py_None;
4289 }
4290}
4291#endif /* HAVE_SETEGID */
4292
4293#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004294PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004295"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004296Set the current process's real and effective user ids.");
4297
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004298static PyObject *
4299posix_setreuid (PyObject *self, PyObject *args)
4300{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004301 long ruid_arg, euid_arg;
4302 uid_t ruid, euid;
4303 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004304 return NULL;
Gregory P. Smithc78d79c2010-03-01 05:54:14 +00004305 if (ruid_arg == -1)
4306 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4307 else
4308 ruid = ruid_arg; /* otherwise, assign from our long */
4309 if (euid_arg == -1)
4310 euid = (uid_t)-1;
4311 else
4312 euid = euid_arg;
4313 if ((euid_arg != -1 && euid != euid_arg) ||
4314 (ruid_arg != -1 && ruid != ruid_arg)) {
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004315 PyErr_SetString(PyExc_OverflowError, "user id too big");
4316 return NULL;
4317 }
4318 if (setreuid(ruid, euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004319 return posix_error();
4320 } else {
4321 Py_INCREF(Py_None);
4322 return Py_None;
4323 }
4324}
4325#endif /* HAVE_SETREUID */
4326
4327#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004328PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004329"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004330Set the current process's real and effective group ids.");
4331
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004332static PyObject *
4333posix_setregid (PyObject *self, PyObject *args)
4334{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004335 long rgid_arg, egid_arg;
4336 gid_t rgid, egid;
4337 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004338 return NULL;
Gregory P. Smithc78d79c2010-03-01 05:54:14 +00004339 if (rgid_arg == -1)
4340 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4341 else
4342 rgid = rgid_arg; /* otherwise, assign from our long */
4343 if (egid_arg == -1)
4344 egid = (gid_t)-1;
4345 else
4346 egid = egid_arg;
4347 if ((egid_arg != -1 && egid != egid_arg) ||
4348 (rgid_arg != -1 && rgid != rgid_arg)) {
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004349 PyErr_SetString(PyExc_OverflowError, "group id too big");
4350 return NULL;
4351 }
4352 if (setregid(rgid, egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004353 return posix_error();
4354 } else {
4355 Py_INCREF(Py_None);
4356 return Py_None;
4357 }
4358}
4359#endif /* HAVE_SETREGID */
4360
Guido van Rossumb6775db1994-08-01 11:34:53 +00004361#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004362PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004363"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004364Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004365
Barry Warsaw53699e91996-12-10 23:23:01 +00004366static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004367posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004368{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004369 long gid_arg;
4370 gid_t gid;
4371 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004372 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004373 gid = gid_arg;
4374 if (gid != gid_arg) {
4375 PyErr_SetString(PyExc_OverflowError, "group id too big");
4376 return NULL;
4377 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004378 if (setgid(gid) < 0)
4379 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004380 Py_INCREF(Py_None);
4381 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004382}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004383#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004384
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004385#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004386PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004387"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004388Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004389
4390static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004391posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004392{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004393 int i, len;
4394 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004395
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004396 if (!PySequence_Check(groups)) {
4397 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4398 return NULL;
4399 }
4400 len = PySequence_Size(groups);
4401 if (len > MAX_GROUPS) {
4402 PyErr_SetString(PyExc_ValueError, "too many groups");
4403 return NULL;
4404 }
4405 for(i = 0; i < len; i++) {
4406 PyObject *elem;
4407 elem = PySequence_GetItem(groups, i);
4408 if (!elem)
4409 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004410 if (!PyLong_Check(elem)) {
4411 PyErr_SetString(PyExc_TypeError,
4412 "groups must be integers");
4413 Py_DECREF(elem);
4414 return NULL;
4415 } else {
4416 unsigned long x = PyLong_AsUnsignedLong(elem);
4417 if (PyErr_Occurred()) {
4418 PyErr_SetString(PyExc_TypeError,
4419 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004420 Py_DECREF(elem);
4421 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004422 }
Georg Brandla13c2442005-11-22 19:30:31 +00004423 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004424 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004425 if (grouplist[i] != x) {
4426 PyErr_SetString(PyExc_TypeError,
4427 "group id too big");
4428 Py_DECREF(elem);
4429 return NULL;
4430 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004431 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004432 Py_DECREF(elem);
4433 }
4434
4435 if (setgroups(len, grouplist) < 0)
4436 return posix_error();
4437 Py_INCREF(Py_None);
4438 return Py_None;
4439}
4440#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004441
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004442#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4443static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004444wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004445{
4446 PyObject *result;
4447 static PyObject *struct_rusage;
4448
4449 if (pid == -1)
4450 return posix_error();
4451
4452 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004453 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004454 if (m == NULL)
4455 return NULL;
4456 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4457 Py_DECREF(m);
4458 if (struct_rusage == NULL)
4459 return NULL;
4460 }
4461
4462 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4463 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4464 if (!result)
4465 return NULL;
4466
4467#ifndef doubletime
4468#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4469#endif
4470
4471 PyStructSequence_SET_ITEM(result, 0,
4472 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4473 PyStructSequence_SET_ITEM(result, 1,
4474 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4475#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004476 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004477 SET_INT(result, 2, ru->ru_maxrss);
4478 SET_INT(result, 3, ru->ru_ixrss);
4479 SET_INT(result, 4, ru->ru_idrss);
4480 SET_INT(result, 5, ru->ru_isrss);
4481 SET_INT(result, 6, ru->ru_minflt);
4482 SET_INT(result, 7, ru->ru_majflt);
4483 SET_INT(result, 8, ru->ru_nswap);
4484 SET_INT(result, 9, ru->ru_inblock);
4485 SET_INT(result, 10, ru->ru_oublock);
4486 SET_INT(result, 11, ru->ru_msgsnd);
4487 SET_INT(result, 12, ru->ru_msgrcv);
4488 SET_INT(result, 13, ru->ru_nsignals);
4489 SET_INT(result, 14, ru->ru_nvcsw);
4490 SET_INT(result, 15, ru->ru_nivcsw);
4491#undef SET_INT
4492
4493 if (PyErr_Occurred()) {
4494 Py_DECREF(result);
4495 return NULL;
4496 }
4497
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004498 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004499}
4500#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4501
4502#ifdef HAVE_WAIT3
4503PyDoc_STRVAR(posix_wait3__doc__,
4504"wait3(options) -> (pid, status, rusage)\n\n\
4505Wait for completion of a child process.");
4506
4507static PyObject *
4508posix_wait3(PyObject *self, PyObject *args)
4509{
Christian Heimes292d3512008-02-03 16:51:08 +00004510 pid_t pid;
4511 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004512 struct rusage ru;
4513 WAIT_TYPE status;
4514 WAIT_STATUS_INT(status) = 0;
4515
4516 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4517 return NULL;
4518
4519 Py_BEGIN_ALLOW_THREADS
4520 pid = wait3(&status, options, &ru);
4521 Py_END_ALLOW_THREADS
4522
4523 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4524}
4525#endif /* HAVE_WAIT3 */
4526
4527#ifdef HAVE_WAIT4
4528PyDoc_STRVAR(posix_wait4__doc__,
4529"wait4(pid, options) -> (pid, status, rusage)\n\n\
4530Wait for completion of a given child process.");
4531
4532static PyObject *
4533posix_wait4(PyObject *self, PyObject *args)
4534{
Christian Heimes292d3512008-02-03 16:51:08 +00004535 pid_t pid;
4536 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004537 struct rusage ru;
4538 WAIT_TYPE status;
4539 WAIT_STATUS_INT(status) = 0;
4540
Gregory P. Smith84508572010-03-14 18:56:11 +00004541 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004542 return NULL;
4543
4544 Py_BEGIN_ALLOW_THREADS
4545 pid = wait4(pid, &status, options, &ru);
4546 Py_END_ALLOW_THREADS
4547
4548 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4549}
4550#endif /* HAVE_WAIT4 */
4551
Guido van Rossumb6775db1994-08-01 11:34:53 +00004552#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004553PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004554"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004555Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004556
Barry Warsaw53699e91996-12-10 23:23:01 +00004557static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004558posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004559{
Christian Heimes292d3512008-02-03 16:51:08 +00004560 pid_t pid;
4561 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004562 WAIT_TYPE status;
4563 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004564
Gregory P. Smith84508572010-03-14 18:56:11 +00004565 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004566 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004567 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004568 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004569 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004570 if (pid == -1)
4571 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004572
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004573 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004574}
4575
Tim Petersab034fa2002-02-01 11:27:43 +00004576#elif defined(HAVE_CWAIT)
4577
4578/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004579PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004580"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004581"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004582
4583static PyObject *
4584posix_waitpid(PyObject *self, PyObject *args)
4585{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004586 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004587 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004588
Gregory P. Smith84508572010-03-14 18:56:11 +00004589 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
Tim Petersab034fa2002-02-01 11:27:43 +00004590 return NULL;
4591 Py_BEGIN_ALLOW_THREADS
4592 pid = _cwait(&status, pid, options);
4593 Py_END_ALLOW_THREADS
4594 if (pid == -1)
4595 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004596
4597 /* shift the status left a byte so this is more like the POSIX waitpid */
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004598 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004599}
4600#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004601
Guido van Rossumad0ee831995-03-01 10:34:45 +00004602#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004603PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004604"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004605Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004606
Barry Warsaw53699e91996-12-10 23:23:01 +00004607static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004608posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004609{
Christian Heimes292d3512008-02-03 16:51:08 +00004610 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004611 WAIT_TYPE status;
4612 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004613
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004614 Py_BEGIN_ALLOW_THREADS
4615 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004616 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004617 if (pid == -1)
4618 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004619
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004620 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004621}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004622#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004623
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004625PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004626"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004627Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004628
Barry Warsaw53699e91996-12-10 23:23:01 +00004629static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004630posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004631{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004632#ifdef HAVE_LSTAT
Martin v. Löwis011e8422009-05-05 04:43:17 +00004633 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004634#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004635#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00004636 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004637#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00004638 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004639#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004640#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004641}
4642
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004643
Guido van Rossumb6775db1994-08-01 11:34:53 +00004644#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004645PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004646"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004647Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004648
Barry Warsaw53699e91996-12-10 23:23:01 +00004649static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004650posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004651{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004652 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004653 char buf[MAXPATHLEN];
Martin v. Löwis011e8422009-05-05 04:43:17 +00004654 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004655 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004656 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004657 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004658
Martin v. Löwis011e8422009-05-05 04:43:17 +00004659 if (!PyArg_ParseTuple(args, "O&:readlink",
4660 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004661 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00004662 path = PyBytes_AsString(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004663 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004664 if (v == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00004665 Py_DECREF(opath);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004666 return NULL;
4667 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004668
4669 if (PyUnicode_Check(v)) {
4670 arg_is_unicode = 1;
4671 }
4672 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004673
Barry Warsaw53699e91996-12-10 23:23:01 +00004674 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004675 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004676 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004677 if (n < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004678 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004679
Victor Stinnerdcb24032010-04-22 12:08:36 +00004680 Py_DECREF(opath);
Christian Heimes72b710a2008-05-26 13:28:38 +00004681 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004682 if (arg_is_unicode) {
4683 PyObject *w;
4684
4685 w = PyUnicode_FromEncodedObject(v,
4686 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00004687 "surrogateescape");
Thomas Wouters89f507f2006-12-13 04:49:30 +00004688 if (w != NULL) {
4689 Py_DECREF(v);
4690 v = w;
4691 }
4692 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00004693 v = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004694 }
4695 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004696 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004697}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004698#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004699
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004700
Guido van Rossumb6775db1994-08-01 11:34:53 +00004701#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004702PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004703"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004704Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004705
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004706static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004707posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004708{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004709 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004710}
4711#endif /* HAVE_SYMLINK */
4712
4713
4714#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004715#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4716static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004717system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004718{
4719 ULONG value = 0;
4720
4721 Py_BEGIN_ALLOW_THREADS
4722 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4723 Py_END_ALLOW_THREADS
4724
4725 return value;
4726}
4727
4728static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004729posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004730{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004731 /* Currently Only Uptime is Provided -- Others Later */
4732 return Py_BuildValue("ddddd",
4733 (double)0 /* t.tms_utime / HZ */,
4734 (double)0 /* t.tms_stime / HZ */,
4735 (double)0 /* t.tms_cutime / HZ */,
4736 (double)0 /* t.tms_cstime / HZ */,
4737 (double)system_uptime() / 1000);
4738}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004739#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004740#define NEED_TICKS_PER_SECOND
4741static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004742static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004743posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004744{
4745 struct tms t;
4746 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004747 errno = 0;
4748 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004749 if (c == (clock_t) -1)
4750 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004751 return Py_BuildValue("ddddd",
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004752 (double)t.tms_utime / ticks_per_second,
4753 (double)t.tms_stime / ticks_per_second,
4754 (double)t.tms_cutime / ticks_per_second,
4755 (double)t.tms_cstime / ticks_per_second,
4756 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004757}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004758#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004759#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004760
4761
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004762#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004763#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004764static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004765posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004766{
4767 FILETIME create, exit, kernel, user;
4768 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004769 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004770 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4771 /* The fields of a FILETIME structure are the hi and lo part
4772 of a 64-bit value expressed in 100 nanosecond units.
4773 1e7 is one second in such units; 1e-7 the inverse.
4774 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4775 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004776 return Py_BuildValue(
4777 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004778 (double)(user.dwHighDateTime*429.4967296 +
4779 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004780 (double)(kernel.dwHighDateTime*429.4967296 +
4781 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004782 (double)0,
4783 (double)0,
4784 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004785}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004786#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004787
4788#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004789PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004790"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004791Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004792#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004793
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004794
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004795#ifdef HAVE_GETSID
4796PyDoc_STRVAR(posix_getsid__doc__,
4797"getsid(pid) -> sid\n\n\
4798Call the system call getsid().");
4799
4800static PyObject *
4801posix_getsid(PyObject *self, PyObject *args)
4802{
Christian Heimes292d3512008-02-03 16:51:08 +00004803 pid_t pid;
4804 int sid;
Gregory P. Smith84508572010-03-14 18:56:11 +00004805 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004806 return NULL;
4807 sid = getsid(pid);
4808 if (sid < 0)
4809 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004810 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004811}
4812#endif /* HAVE_GETSID */
4813
4814
Guido van Rossumb6775db1994-08-01 11:34:53 +00004815#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004816PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004817"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004818Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004819
Barry Warsaw53699e91996-12-10 23:23:01 +00004820static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004821posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004822{
Guido van Rossum687dd131993-05-17 08:34:16 +00004823 if (setsid() < 0)
4824 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004825 Py_INCREF(Py_None);
4826 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004827}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004828#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004829
Guido van Rossumb6775db1994-08-01 11:34:53 +00004830#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004831PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004832"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004833Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004834
Barry Warsaw53699e91996-12-10 23:23:01 +00004835static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004836posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004837{
Christian Heimes292d3512008-02-03 16:51:08 +00004838 pid_t pid;
4839 int pgrp;
Gregory P. Smith84508572010-03-14 18:56:11 +00004840 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004841 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004842 if (setpgid(pid, pgrp) < 0)
4843 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004844 Py_INCREF(Py_None);
4845 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004846}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004847#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004848
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004849
Guido van Rossumb6775db1994-08-01 11:34:53 +00004850#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004851PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004852"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004853Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004854
Barry Warsaw53699e91996-12-10 23:23:01 +00004855static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004856posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004857{
Christian Heimes15ebc882008-02-04 18:48:49 +00004858 int fd;
4859 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004860 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004861 return NULL;
4862 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004863 if (pgid < 0)
4864 return posix_error();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004865 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004866}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004867#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004868
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004869
Guido van Rossumb6775db1994-08-01 11:34:53 +00004870#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004871PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004872"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004873Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004874
Barry Warsaw53699e91996-12-10 23:23:01 +00004875static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004876posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004877{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004878 int fd;
4879 pid_t pgid;
Gregory P. Smith84508572010-03-14 18:56:11 +00004880 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004881 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004882 if (tcsetpgrp(fd, pgid) < 0)
4883 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004884 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004885 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004886}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004887#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004888
Guido van Rossum687dd131993-05-17 08:34:16 +00004889/* Functions acting on file descriptors */
4890
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004891PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004892"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004893Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004894
Barry Warsaw53699e91996-12-10 23:23:01 +00004895static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004896posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004897{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004898 PyObject *ofile;
4899 char *file;
Guido van Rossum687dd131993-05-17 08:34:16 +00004900 int flag;
4901 int mode = 0777;
4902 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004903
4904#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00004905 PyUnicodeObject *po;
4906 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4907 Py_BEGIN_ALLOW_THREADS
4908 /* PyUnicode_AS_UNICODE OK without thread
4909 lock as it is a simple dereference. */
4910 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4911 Py_END_ALLOW_THREADS
4912 if (fd < 0)
4913 return posix_error();
4914 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004915 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00004916 /* Drop the argument parsing error as narrow strings
4917 are also valid. */
4918 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004919#endif
4920
Martin v. Löwis011e8422009-05-05 04:43:17 +00004921 if (!PyArg_ParseTuple(args, "O&i|i",
4922 PyUnicode_FSConverter, &ofile,
Mark Hammondef8b6542001-05-13 08:04:26 +00004923 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004924 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00004925 file = PyBytes_AsString(ofile);
Barry Warsaw53699e91996-12-10 23:23:01 +00004926 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004927 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004928 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004929 if (fd < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004930 return posix_error_with_allocated_filename(ofile);
Victor Stinnerdcb24032010-04-22 12:08:36 +00004931 Py_DECREF(ofile);
Christian Heimes217cfd12007-12-02 14:31:20 +00004932 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004933}
4934
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004935
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004936PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004937"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004938Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004939
Barry Warsaw53699e91996-12-10 23:23:01 +00004940static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004941posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004942{
4943 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004944 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004945 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004946 if (!_PyVerify_fd(fd))
4947 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004948 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004949 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004950 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004951 if (res < 0)
4952 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004953 Py_INCREF(Py_None);
4954 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004955}
4956
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004957
Christian Heimesfdab48e2008-01-20 09:06:41 +00004958PyDoc_STRVAR(posix_closerange__doc__,
4959"closerange(fd_low, fd_high)\n\n\
4960Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4961
4962static PyObject *
4963posix_closerange(PyObject *self, PyObject *args)
4964{
4965 int fd_from, fd_to, i;
4966 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4967 return NULL;
4968 Py_BEGIN_ALLOW_THREADS
4969 for (i = fd_from; i < fd_to; i++)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004970 if (_PyVerify_fd(i))
4971 close(i);
Christian Heimesfdab48e2008-01-20 09:06:41 +00004972 Py_END_ALLOW_THREADS
4973 Py_RETURN_NONE;
4974}
4975
4976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004977PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004978"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004979Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004980
Barry Warsaw53699e91996-12-10 23:23:01 +00004981static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004982posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004983{
4984 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004985 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004986 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004987 if (!_PyVerify_fd(fd))
4988 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004989 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004990 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004991 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004992 if (fd < 0)
4993 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004994 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004995}
4996
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004997
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004998PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004999"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005000Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005001
Barry Warsaw53699e91996-12-10 23:23:01 +00005002static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005003posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005004{
5005 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005006 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005007 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005008 if (!_PyVerify_fd_dup2(fd, fd2))
5009 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005010 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005011 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005012 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005013 if (res < 0)
5014 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005015 Py_INCREF(Py_None);
5016 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005017}
5018
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005019
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005020PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005021"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005022Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005023
Barry Warsaw53699e91996-12-10 23:23:01 +00005024static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005025posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005026{
5027 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005028#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005029 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005030#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005031 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005032#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005033 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005034 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005035 return NULL;
5036#ifdef SEEK_SET
5037 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5038 switch (how) {
5039 case 0: how = SEEK_SET; break;
5040 case 1: how = SEEK_CUR; break;
5041 case 2: how = SEEK_END; break;
5042 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005043#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005044
5045#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005046 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005047#else
5048 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005049 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005050#endif
5051 if (PyErr_Occurred())
5052 return NULL;
5053
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005054 if (!_PyVerify_fd(fd))
5055 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005056 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005057#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005058 res = _lseeki64(fd, pos, how);
5059#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005060 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005061#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005062 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005063 if (res < 0)
5064 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005065
5066#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005067 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005068#else
5069 return PyLong_FromLongLong(res);
5070#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005071}
5072
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005073
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005074PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005075"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005076Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005077
Barry Warsaw53699e91996-12-10 23:23:01 +00005078static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005079posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005080{
Guido van Rossum572dbf82007-04-27 23:53:51 +00005081 int fd, size;
5082 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005083 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005084 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005085 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005086 if (size < 0) {
5087 errno = EINVAL;
5088 return posix_error();
5089 }
Christian Heimes72b710a2008-05-26 13:28:38 +00005090 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005091 if (buffer == NULL)
5092 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005093 if (!_PyVerify_fd(fd))
5094 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005095 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00005096 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005097 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005098 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005099 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005100 return posix_error();
5101 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005102 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00005103 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005104 return buffer;
5105}
5106
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005108PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005109"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005110Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005111
Barry Warsaw53699e91996-12-10 23:23:01 +00005112static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005113posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005114{
Martin v. Löwis423be952008-08-13 15:53:07 +00005115 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005116 int fd;
5117 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005118
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +00005119 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00005120 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005121 if (!_PyVerify_fd(fd))
5122 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005123 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005124 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00005125 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005126 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00005127 if (size < 0)
5128 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005129 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005130}
5131
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005133PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005134"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005135Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005136
Barry Warsaw53699e91996-12-10 23:23:01 +00005137static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005138posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005139{
5140 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005141 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005142 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005143 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005144 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005145#ifdef __VMS
5146 /* on OpenVMS we must ensure that all bytes are written to the file */
5147 fsync(fd);
5148#endif
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005149 if (!_PyVerify_fd(fd))
5150 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005151 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005152 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005153 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005154 if (res != 0) {
5155#ifdef MS_WINDOWS
5156 return win32_error("fstat", NULL);
5157#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005158 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005159#endif
5160 }
Tim Peters5aa91602002-01-30 05:46:57 +00005161
Martin v. Löwis14694662006-02-03 12:54:16 +00005162 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005163}
5164
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005165PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005166"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005167Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005168connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005169
5170static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005171posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005172{
5173 int fd;
5174 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5175 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005176 if (!_PyVerify_fd(fd))
5177 return PyBool_FromLong(0);
Fred Drake106c1a02002-04-23 15:58:02 +00005178 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005179}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005180
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005181#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005182PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005183"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005184Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005185
Barry Warsaw53699e91996-12-10 23:23:01 +00005186static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005187posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005188{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005189#if defined(PYOS_OS2)
5190 HFILE read, write;
5191 APIRET rc;
5192
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005193 Py_BEGIN_ALLOW_THREADS
5194 rc = DosCreatePipe( &read, &write, 4096);
5195 Py_END_ALLOW_THREADS
5196 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005197 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005198
5199 return Py_BuildValue("(ii)", read, write);
5200#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005201#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005202 int fds[2];
5203 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005204 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005205 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005206 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005207 if (res != 0)
5208 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005209 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005210#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005211 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005212 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005213 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005214 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005215 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005216 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005217 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005218 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005219 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5220 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005221 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005222#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005223#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005224}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005225#endif /* HAVE_PIPE */
5226
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005227
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005228#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005229PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005230"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005231Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005232
Barry Warsaw53699e91996-12-10 23:23:01 +00005233static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005234posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005235{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005236 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005237 int mode = 0666;
5238 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005239 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005240 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005241 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005242 res = mkfifo(filename, mode);
5243 Py_END_ALLOW_THREADS
5244 if (res < 0)
5245 return posix_error();
5246 Py_INCREF(Py_None);
5247 return Py_None;
5248}
5249#endif
5250
5251
Neal Norwitz11690112002-07-30 01:08:28 +00005252#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005253PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005254"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005255Create a filesystem node (file, device special file or named pipe)\n\
5256named filename. mode specifies both the permissions to use and the\n\
5257type of node to be created, being combined (bitwise OR) with one of\n\
5258S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005259device defines the newly created device special file (probably using\n\
5260os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005261
5262
5263static PyObject *
5264posix_mknod(PyObject *self, PyObject *args)
5265{
5266 char *filename;
5267 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005268 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005269 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005270 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005271 return NULL;
5272 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005273 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005274 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005275 if (res < 0)
5276 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005277 Py_INCREF(Py_None);
5278 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005279}
5280#endif
5281
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005282#ifdef HAVE_DEVICE_MACROS
5283PyDoc_STRVAR(posix_major__doc__,
5284"major(device) -> major number\n\
5285Extracts a device major number from a raw device number.");
5286
5287static PyObject *
5288posix_major(PyObject *self, PyObject *args)
5289{
5290 int device;
5291 if (!PyArg_ParseTuple(args, "i:major", &device))
5292 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005293 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005294}
5295
5296PyDoc_STRVAR(posix_minor__doc__,
5297"minor(device) -> minor number\n\
5298Extracts a device minor number from a raw device number.");
5299
5300static PyObject *
5301posix_minor(PyObject *self, PyObject *args)
5302{
5303 int device;
5304 if (!PyArg_ParseTuple(args, "i:minor", &device))
5305 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005306 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005307}
5308
5309PyDoc_STRVAR(posix_makedev__doc__,
5310"makedev(major, minor) -> device number\n\
5311Composes a raw device number from the major and minor device numbers.");
5312
5313static PyObject *
5314posix_makedev(PyObject *self, PyObject *args)
5315{
5316 int major, minor;
5317 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5318 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005319 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005320}
5321#endif /* device macros */
5322
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005323
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005324#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005325PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005326"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005327Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005328
Barry Warsaw53699e91996-12-10 23:23:01 +00005329static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005330posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005331{
5332 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005333 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005334 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005335 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005336
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005337 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005338 return NULL;
5339
5340#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005341 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005342#else
5343 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005344 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005345#endif
5346 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005347 return NULL;
5348
Barry Warsaw53699e91996-12-10 23:23:01 +00005349 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005350 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005351 Py_END_ALLOW_THREADS
Benjamin Peterson9053d752009-01-19 17:53:36 +00005352 if (res < 0)
5353 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005354 Py_INCREF(Py_None);
5355 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005356}
5357#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005358
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005359#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005360PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005361"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005362Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005363
Fred Drake762e2061999-08-26 17:23:54 +00005364/* Save putenv() parameters as values here, so we can collect them when they
5365 * get re-set with another call for the same key. */
5366static PyObject *posix_putenv_garbage;
5367
Tim Peters5aa91602002-01-30 05:46:57 +00005368static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005369posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005370{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005371#ifdef MS_WINDOWS
5372 wchar_t *s1, *s2;
5373 wchar_t *newenv;
5374#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00005375 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005376 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005377 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005378#endif
Fred Drake762e2061999-08-26 17:23:54 +00005379 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005380 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005381
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005382#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00005383 if (!PyArg_ParseTuple(args,
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005384 "uu:putenv",
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005385 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005386 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005387#else
5388 if (!PyArg_ParseTuple(args,
5389 "O&O&:putenv",
5390 PyUnicode_FSConverter, &os1,
5391 PyUnicode_FSConverter, &os2))
5392 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00005393 s1 = PyBytes_AsString(os1);
5394 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005395#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005396
5397#if defined(PYOS_OS2)
5398 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5399 APIRET rc;
5400
Guido van Rossumd48f2521997-12-05 22:19:34 +00005401 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5402 if (rc != NO_ERROR)
5403 return os2_error(rc);
5404
5405 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5406 APIRET rc;
5407
Guido van Rossumd48f2521997-12-05 22:19:34 +00005408 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5409 if (rc != NO_ERROR)
5410 return os2_error(rc);
5411 } else {
5412#endif
Fred Drake762e2061999-08-26 17:23:54 +00005413 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005414 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005415 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005416#ifdef MS_WINDOWS
5417 len = wcslen(s1) + wcslen(s2) + 2;
5418 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5419#else
5420 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005421 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005422#endif
Fred Drake762e2061999-08-26 17:23:54 +00005423 if (newstr == NULL)
5424 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005425#ifdef MS_WINDOWS
5426 newenv = PyUnicode_AsUnicode(newstr);
5427 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5428 if (_wputenv(newenv)) {
5429 Py_DECREF(newstr);
5430 posix_error();
5431 return NULL;
5432 }
5433#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005434 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005435 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5436 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005437 Py_DECREF(newstr);
Victor Stinnerdcb24032010-04-22 12:08:36 +00005438 Py_DECREF(os1);
5439 Py_DECREF(os2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005440 posix_error();
5441 return NULL;
5442 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005443#endif
Fred Drake762e2061999-08-26 17:23:54 +00005444 /* Install the first arg and newstr in posix_putenv_garbage;
5445 * this will cause previous value to be collected. This has to
5446 * happen after the real putenv() call because the old value
5447 * was still accessible until then. */
5448 if (PyDict_SetItem(posix_putenv_garbage,
5449 PyTuple_GET_ITEM(args, 0), newstr)) {
5450 /* really not much we can do; just leak */
5451 PyErr_Clear();
5452 }
5453 else {
5454 Py_DECREF(newstr);
5455 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005456
5457#if defined(PYOS_OS2)
5458 }
5459#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00005460#ifndef MS_WINDOWS
Victor Stinnerdcb24032010-04-22 12:08:36 +00005461 Py_DECREF(os1);
5462 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005463#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005464 Py_INCREF(Py_None);
5465 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005466}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005467#endif /* putenv */
5468
Guido van Rossumc524d952001-10-19 01:31:59 +00005469#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005470PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005471"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005472Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005473
5474static PyObject *
5475posix_unsetenv(PyObject *self, PyObject *args)
5476{
5477 char *s1;
5478
5479 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5480 return NULL;
5481
5482 unsetenv(s1);
5483
5484 /* Remove the key from posix_putenv_garbage;
5485 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005486 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005487 * old value was still accessible until then.
5488 */
5489 if (PyDict_DelItem(posix_putenv_garbage,
5490 PyTuple_GET_ITEM(args, 0))) {
5491 /* really not much we can do; just leak */
5492 PyErr_Clear();
5493 }
5494
5495 Py_INCREF(Py_None);
5496 return Py_None;
5497}
5498#endif /* unsetenv */
5499
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005500PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005501"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005502Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005503
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005504static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005505posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005506{
5507 int code;
5508 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005509 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005510 return NULL;
5511 message = strerror(code);
5512 if (message == NULL) {
5513 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005514 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005515 return NULL;
5516 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005517 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005518}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005519
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005520
Guido van Rossumc9641791998-08-04 15:26:23 +00005521#ifdef HAVE_SYS_WAIT_H
5522
Fred Drake106c1a02002-04-23 15:58:02 +00005523#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005524PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005525"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005526Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005527
5528static PyObject *
5529posix_WCOREDUMP(PyObject *self, PyObject *args)
5530{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005531 WAIT_TYPE status;
5532 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005533
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005534 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005535 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005536
5537 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005538}
5539#endif /* WCOREDUMP */
5540
5541#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005542PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005543"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005544Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005545job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005546
5547static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005548posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005549{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005550 WAIT_TYPE status;
5551 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005552
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005553 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005554 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005555
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005556 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005557}
5558#endif /* WIFCONTINUED */
5559
Guido van Rossumc9641791998-08-04 15:26:23 +00005560#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005561PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005562"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005563Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005564
5565static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005566posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005567{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005568 WAIT_TYPE status;
5569 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005570
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005571 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005572 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005573
Fred Drake106c1a02002-04-23 15:58:02 +00005574 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005575}
5576#endif /* WIFSTOPPED */
5577
5578#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005579PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005580"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005581Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005582
5583static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005584posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005585{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005586 WAIT_TYPE status;
5587 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005588
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005589 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005590 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005591
Fred Drake106c1a02002-04-23 15:58:02 +00005592 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005593}
5594#endif /* WIFSIGNALED */
5595
5596#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005597PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005598"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005599Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005600system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005601
5602static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005603posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005604{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005605 WAIT_TYPE status;
5606 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005607
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005608 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005609 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005610
Fred Drake106c1a02002-04-23 15:58:02 +00005611 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005612}
5613#endif /* WIFEXITED */
5614
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005615#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005616PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005617"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005618Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005619
5620static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005621posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005622{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005623 WAIT_TYPE status;
5624 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005625
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005626 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005627 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005628
Guido van Rossumc9641791998-08-04 15:26:23 +00005629 return Py_BuildValue("i", WEXITSTATUS(status));
5630}
5631#endif /* WEXITSTATUS */
5632
5633#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005634PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005635"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005636Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005637value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005638
5639static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005640posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005641{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005642 WAIT_TYPE status;
5643 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005644
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005645 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005646 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005647
Guido van Rossumc9641791998-08-04 15:26:23 +00005648 return Py_BuildValue("i", WTERMSIG(status));
5649}
5650#endif /* WTERMSIG */
5651
5652#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005653PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005654"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005655Return the signal that stopped the process that provided\n\
5656the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005657
5658static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005659posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005660{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005661 WAIT_TYPE status;
5662 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005663
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005664 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005665 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005666
Guido van Rossumc9641791998-08-04 15:26:23 +00005667 return Py_BuildValue("i", WSTOPSIG(status));
5668}
5669#endif /* WSTOPSIG */
5670
5671#endif /* HAVE_SYS_WAIT_H */
5672
5673
Thomas Wouters477c8d52006-05-27 19:21:47 +00005674#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005675#ifdef _SCO_DS
5676/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5677 needed definitions in sys/statvfs.h */
5678#define _SVID3
5679#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005680#include <sys/statvfs.h>
5681
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005682static PyObject*
5683_pystatvfs_fromstructstatvfs(struct statvfs st) {
5684 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5685 if (v == NULL)
5686 return NULL;
5687
5688#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005689 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5690 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5691 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5692 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5693 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5694 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5695 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5696 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5697 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5698 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005699#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005700 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5701 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005702 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005703 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005704 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005705 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005706 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005707 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005708 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005709 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005710 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005711 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005712 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005713 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005714 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5715 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005716#endif
5717
5718 return v;
5719}
5720
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005721PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005722"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005723Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005724
5725static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005726posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005727{
5728 int fd, res;
5729 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005730
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005731 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005732 return NULL;
5733 Py_BEGIN_ALLOW_THREADS
5734 res = fstatvfs(fd, &st);
5735 Py_END_ALLOW_THREADS
5736 if (res != 0)
5737 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005738
5739 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005740}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005741#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005742
5743
Thomas Wouters477c8d52006-05-27 19:21:47 +00005744#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005745#include <sys/statvfs.h>
5746
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005747PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005748"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005749Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005750
5751static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005752posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005753{
5754 char *path;
5755 int res;
5756 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005757 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005758 return NULL;
5759 Py_BEGIN_ALLOW_THREADS
5760 res = statvfs(path, &st);
5761 Py_END_ALLOW_THREADS
5762 if (res != 0)
5763 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005764
5765 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005766}
5767#endif /* HAVE_STATVFS */
5768
Fred Drakec9680921999-12-13 16:37:25 +00005769/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5770 * It maps strings representing configuration variable names to
5771 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005772 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005773 * rarely-used constants. There are three separate tables that use
5774 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005775 *
5776 * This code is always included, even if none of the interfaces that
5777 * need it are included. The #if hackery needed to avoid it would be
5778 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005779 */
5780struct constdef {
5781 char *name;
5782 long value;
5783};
5784
Fred Drake12c6e2d1999-12-14 21:25:03 +00005785static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005786conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005787 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005788{
Christian Heimes217cfd12007-12-02 14:31:20 +00005789 if (PyLong_Check(arg)) {
5790 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005791 return 1;
5792 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005793 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005794 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005795 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005796 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005797 size_t hi = tablesize;
5798 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005799 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005800 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005801 PyErr_SetString(PyExc_TypeError,
5802 "configuration names must be strings or integers");
5803 return 0;
5804 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005805 confname = _PyUnicode_AsString(arg);
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005806 if (confname == NULL)
5807 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005808 while (lo < hi) {
5809 mid = (lo + hi) / 2;
5810 cmp = strcmp(confname, table[mid].name);
5811 if (cmp < 0)
5812 hi = mid;
5813 else if (cmp > 0)
5814 lo = mid + 1;
5815 else {
5816 *valuep = table[mid].value;
5817 return 1;
5818 }
5819 }
5820 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005821 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005822 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005823}
5824
5825
5826#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5827static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005828#ifdef _PC_ABI_AIO_XFER_MAX
5829 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5830#endif
5831#ifdef _PC_ABI_ASYNC_IO
5832 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5833#endif
Fred Drakec9680921999-12-13 16:37:25 +00005834#ifdef _PC_ASYNC_IO
5835 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5836#endif
5837#ifdef _PC_CHOWN_RESTRICTED
5838 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5839#endif
5840#ifdef _PC_FILESIZEBITS
5841 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5842#endif
5843#ifdef _PC_LAST
5844 {"PC_LAST", _PC_LAST},
5845#endif
5846#ifdef _PC_LINK_MAX
5847 {"PC_LINK_MAX", _PC_LINK_MAX},
5848#endif
5849#ifdef _PC_MAX_CANON
5850 {"PC_MAX_CANON", _PC_MAX_CANON},
5851#endif
5852#ifdef _PC_MAX_INPUT
5853 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5854#endif
5855#ifdef _PC_NAME_MAX
5856 {"PC_NAME_MAX", _PC_NAME_MAX},
5857#endif
5858#ifdef _PC_NO_TRUNC
5859 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5860#endif
5861#ifdef _PC_PATH_MAX
5862 {"PC_PATH_MAX", _PC_PATH_MAX},
5863#endif
5864#ifdef _PC_PIPE_BUF
5865 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5866#endif
5867#ifdef _PC_PRIO_IO
5868 {"PC_PRIO_IO", _PC_PRIO_IO},
5869#endif
5870#ifdef _PC_SOCK_MAXBUF
5871 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5872#endif
5873#ifdef _PC_SYNC_IO
5874 {"PC_SYNC_IO", _PC_SYNC_IO},
5875#endif
5876#ifdef _PC_VDISABLE
5877 {"PC_VDISABLE", _PC_VDISABLE},
5878#endif
5879};
5880
Fred Drakec9680921999-12-13 16:37:25 +00005881static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005882conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005883{
5884 return conv_confname(arg, valuep, posix_constants_pathconf,
5885 sizeof(posix_constants_pathconf)
5886 / sizeof(struct constdef));
5887}
5888#endif
5889
5890#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005891PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005892"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005893Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005894If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005895
5896static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005897posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005898{
5899 PyObject *result = NULL;
5900 int name, fd;
5901
Fred Drake12c6e2d1999-12-14 21:25:03 +00005902 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5903 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005904 long limit;
5905
5906 errno = 0;
5907 limit = fpathconf(fd, name);
5908 if (limit == -1 && errno != 0)
5909 posix_error();
5910 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005911 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005912 }
5913 return result;
5914}
5915#endif
5916
5917
5918#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005919PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005920"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005921Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005922If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005923
5924static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005925posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005926{
5927 PyObject *result = NULL;
5928 int name;
5929 char *path;
5930
5931 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5932 conv_path_confname, &name)) {
5933 long limit;
5934
5935 errno = 0;
5936 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005937 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005938 if (errno == EINVAL)
5939 /* could be a path or name problem */
5940 posix_error();
5941 else
5942 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005943 }
Fred Drakec9680921999-12-13 16:37:25 +00005944 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005945 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005946 }
5947 return result;
5948}
5949#endif
5950
5951#ifdef HAVE_CONFSTR
5952static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005953#ifdef _CS_ARCHITECTURE
5954 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5955#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00005956#ifdef _CS_GNU_LIBC_VERSION
5957 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
5958#endif
5959#ifdef _CS_GNU_LIBPTHREAD_VERSION
5960 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
5961#endif
Fred Draked86ed291999-12-15 15:34:33 +00005962#ifdef _CS_HOSTNAME
5963 {"CS_HOSTNAME", _CS_HOSTNAME},
5964#endif
5965#ifdef _CS_HW_PROVIDER
5966 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5967#endif
5968#ifdef _CS_HW_SERIAL
5969 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5970#endif
5971#ifdef _CS_INITTAB_NAME
5972 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5973#endif
Fred Drakec9680921999-12-13 16:37:25 +00005974#ifdef _CS_LFS64_CFLAGS
5975 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5976#endif
5977#ifdef _CS_LFS64_LDFLAGS
5978 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5979#endif
5980#ifdef _CS_LFS64_LIBS
5981 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5982#endif
5983#ifdef _CS_LFS64_LINTFLAGS
5984 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5985#endif
5986#ifdef _CS_LFS_CFLAGS
5987 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5988#endif
5989#ifdef _CS_LFS_LDFLAGS
5990 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5991#endif
5992#ifdef _CS_LFS_LIBS
5993 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5994#endif
5995#ifdef _CS_LFS_LINTFLAGS
5996 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5997#endif
Fred Draked86ed291999-12-15 15:34:33 +00005998#ifdef _CS_MACHINE
5999 {"CS_MACHINE", _CS_MACHINE},
6000#endif
Fred Drakec9680921999-12-13 16:37:25 +00006001#ifdef _CS_PATH
6002 {"CS_PATH", _CS_PATH},
6003#endif
Fred Draked86ed291999-12-15 15:34:33 +00006004#ifdef _CS_RELEASE
6005 {"CS_RELEASE", _CS_RELEASE},
6006#endif
6007#ifdef _CS_SRPC_DOMAIN
6008 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6009#endif
6010#ifdef _CS_SYSNAME
6011 {"CS_SYSNAME", _CS_SYSNAME},
6012#endif
6013#ifdef _CS_VERSION
6014 {"CS_VERSION", _CS_VERSION},
6015#endif
Fred Drakec9680921999-12-13 16:37:25 +00006016#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6017 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6018#endif
6019#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6020 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6021#endif
6022#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6023 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6024#endif
6025#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6026 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6027#endif
6028#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6029 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6030#endif
6031#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6032 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6033#endif
6034#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6035 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6036#endif
6037#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6038 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6039#endif
6040#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6041 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6042#endif
6043#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6044 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6045#endif
6046#ifdef _CS_XBS5_LP64_OFF64_LIBS
6047 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6048#endif
6049#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6050 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6051#endif
6052#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6053 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6054#endif
6055#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6056 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6057#endif
6058#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6059 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6060#endif
6061#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6062 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6063#endif
Fred Draked86ed291999-12-15 15:34:33 +00006064#ifdef _MIPS_CS_AVAIL_PROCESSORS
6065 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6066#endif
6067#ifdef _MIPS_CS_BASE
6068 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6069#endif
6070#ifdef _MIPS_CS_HOSTID
6071 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6072#endif
6073#ifdef _MIPS_CS_HW_NAME
6074 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6075#endif
6076#ifdef _MIPS_CS_NUM_PROCESSORS
6077 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6078#endif
6079#ifdef _MIPS_CS_OSREL_MAJ
6080 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6081#endif
6082#ifdef _MIPS_CS_OSREL_MIN
6083 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6084#endif
6085#ifdef _MIPS_CS_OSREL_PATCH
6086 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6087#endif
6088#ifdef _MIPS_CS_OS_NAME
6089 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6090#endif
6091#ifdef _MIPS_CS_OS_PROVIDER
6092 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6093#endif
6094#ifdef _MIPS_CS_PROCESSORS
6095 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6096#endif
6097#ifdef _MIPS_CS_SERIAL
6098 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6099#endif
6100#ifdef _MIPS_CS_VENDOR
6101 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6102#endif
Fred Drakec9680921999-12-13 16:37:25 +00006103};
6104
6105static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006106conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006107{
6108 return conv_confname(arg, valuep, posix_constants_confstr,
6109 sizeof(posix_constants_confstr)
6110 / sizeof(struct constdef));
6111}
6112
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006113PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006114"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006115Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006116
6117static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006118posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006119{
6120 PyObject *result = NULL;
6121 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006122 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006123
6124 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006125 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006126
Fred Drakec9680921999-12-13 16:37:25 +00006127 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006128 len = confstr(name, buffer, sizeof(buffer));
6129 if (len == 0) {
6130 if (errno) {
6131 posix_error();
6132 }
6133 else {
6134 result = Py_None;
6135 Py_INCREF(Py_None);
6136 }
Fred Drakec9680921999-12-13 16:37:25 +00006137 }
6138 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006139 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006140 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006141 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006142 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006143 }
6144 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006145 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006146 }
6147 }
6148 return result;
6149}
6150#endif
6151
6152
6153#ifdef HAVE_SYSCONF
6154static struct constdef posix_constants_sysconf[] = {
6155#ifdef _SC_2_CHAR_TERM
6156 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6157#endif
6158#ifdef _SC_2_C_BIND
6159 {"SC_2_C_BIND", _SC_2_C_BIND},
6160#endif
6161#ifdef _SC_2_C_DEV
6162 {"SC_2_C_DEV", _SC_2_C_DEV},
6163#endif
6164#ifdef _SC_2_C_VERSION
6165 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6166#endif
6167#ifdef _SC_2_FORT_DEV
6168 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6169#endif
6170#ifdef _SC_2_FORT_RUN
6171 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6172#endif
6173#ifdef _SC_2_LOCALEDEF
6174 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6175#endif
6176#ifdef _SC_2_SW_DEV
6177 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6178#endif
6179#ifdef _SC_2_UPE
6180 {"SC_2_UPE", _SC_2_UPE},
6181#endif
6182#ifdef _SC_2_VERSION
6183 {"SC_2_VERSION", _SC_2_VERSION},
6184#endif
Fred Draked86ed291999-12-15 15:34:33 +00006185#ifdef _SC_ABI_ASYNCHRONOUS_IO
6186 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6187#endif
6188#ifdef _SC_ACL
6189 {"SC_ACL", _SC_ACL},
6190#endif
Fred Drakec9680921999-12-13 16:37:25 +00006191#ifdef _SC_AIO_LISTIO_MAX
6192 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6193#endif
Fred Drakec9680921999-12-13 16:37:25 +00006194#ifdef _SC_AIO_MAX
6195 {"SC_AIO_MAX", _SC_AIO_MAX},
6196#endif
6197#ifdef _SC_AIO_PRIO_DELTA_MAX
6198 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6199#endif
6200#ifdef _SC_ARG_MAX
6201 {"SC_ARG_MAX", _SC_ARG_MAX},
6202#endif
6203#ifdef _SC_ASYNCHRONOUS_IO
6204 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6205#endif
6206#ifdef _SC_ATEXIT_MAX
6207 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6208#endif
Fred Draked86ed291999-12-15 15:34:33 +00006209#ifdef _SC_AUDIT
6210 {"SC_AUDIT", _SC_AUDIT},
6211#endif
Fred Drakec9680921999-12-13 16:37:25 +00006212#ifdef _SC_AVPHYS_PAGES
6213 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6214#endif
6215#ifdef _SC_BC_BASE_MAX
6216 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6217#endif
6218#ifdef _SC_BC_DIM_MAX
6219 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6220#endif
6221#ifdef _SC_BC_SCALE_MAX
6222 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6223#endif
6224#ifdef _SC_BC_STRING_MAX
6225 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6226#endif
Fred Draked86ed291999-12-15 15:34:33 +00006227#ifdef _SC_CAP
6228 {"SC_CAP", _SC_CAP},
6229#endif
Fred Drakec9680921999-12-13 16:37:25 +00006230#ifdef _SC_CHARCLASS_NAME_MAX
6231 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6232#endif
6233#ifdef _SC_CHAR_BIT
6234 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6235#endif
6236#ifdef _SC_CHAR_MAX
6237 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6238#endif
6239#ifdef _SC_CHAR_MIN
6240 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6241#endif
6242#ifdef _SC_CHILD_MAX
6243 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6244#endif
6245#ifdef _SC_CLK_TCK
6246 {"SC_CLK_TCK", _SC_CLK_TCK},
6247#endif
6248#ifdef _SC_COHER_BLKSZ
6249 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6250#endif
6251#ifdef _SC_COLL_WEIGHTS_MAX
6252 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6253#endif
6254#ifdef _SC_DCACHE_ASSOC
6255 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6256#endif
6257#ifdef _SC_DCACHE_BLKSZ
6258 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6259#endif
6260#ifdef _SC_DCACHE_LINESZ
6261 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6262#endif
6263#ifdef _SC_DCACHE_SZ
6264 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6265#endif
6266#ifdef _SC_DCACHE_TBLKSZ
6267 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6268#endif
6269#ifdef _SC_DELAYTIMER_MAX
6270 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6271#endif
6272#ifdef _SC_EQUIV_CLASS_MAX
6273 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6274#endif
6275#ifdef _SC_EXPR_NEST_MAX
6276 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6277#endif
6278#ifdef _SC_FSYNC
6279 {"SC_FSYNC", _SC_FSYNC},
6280#endif
6281#ifdef _SC_GETGR_R_SIZE_MAX
6282 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6283#endif
6284#ifdef _SC_GETPW_R_SIZE_MAX
6285 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6286#endif
6287#ifdef _SC_ICACHE_ASSOC
6288 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6289#endif
6290#ifdef _SC_ICACHE_BLKSZ
6291 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6292#endif
6293#ifdef _SC_ICACHE_LINESZ
6294 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6295#endif
6296#ifdef _SC_ICACHE_SZ
6297 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6298#endif
Fred Draked86ed291999-12-15 15:34:33 +00006299#ifdef _SC_INF
6300 {"SC_INF", _SC_INF},
6301#endif
Fred Drakec9680921999-12-13 16:37:25 +00006302#ifdef _SC_INT_MAX
6303 {"SC_INT_MAX", _SC_INT_MAX},
6304#endif
6305#ifdef _SC_INT_MIN
6306 {"SC_INT_MIN", _SC_INT_MIN},
6307#endif
6308#ifdef _SC_IOV_MAX
6309 {"SC_IOV_MAX", _SC_IOV_MAX},
6310#endif
Fred Draked86ed291999-12-15 15:34:33 +00006311#ifdef _SC_IP_SECOPTS
6312 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6313#endif
Fred Drakec9680921999-12-13 16:37:25 +00006314#ifdef _SC_JOB_CONTROL
6315 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6316#endif
Fred Draked86ed291999-12-15 15:34:33 +00006317#ifdef _SC_KERN_POINTERS
6318 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6319#endif
6320#ifdef _SC_KERN_SIM
6321 {"SC_KERN_SIM", _SC_KERN_SIM},
6322#endif
Fred Drakec9680921999-12-13 16:37:25 +00006323#ifdef _SC_LINE_MAX
6324 {"SC_LINE_MAX", _SC_LINE_MAX},
6325#endif
6326#ifdef _SC_LOGIN_NAME_MAX
6327 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6328#endif
6329#ifdef _SC_LOGNAME_MAX
6330 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6331#endif
6332#ifdef _SC_LONG_BIT
6333 {"SC_LONG_BIT", _SC_LONG_BIT},
6334#endif
Fred Draked86ed291999-12-15 15:34:33 +00006335#ifdef _SC_MAC
6336 {"SC_MAC", _SC_MAC},
6337#endif
Fred Drakec9680921999-12-13 16:37:25 +00006338#ifdef _SC_MAPPED_FILES
6339 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6340#endif
6341#ifdef _SC_MAXPID
6342 {"SC_MAXPID", _SC_MAXPID},
6343#endif
6344#ifdef _SC_MB_LEN_MAX
6345 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6346#endif
6347#ifdef _SC_MEMLOCK
6348 {"SC_MEMLOCK", _SC_MEMLOCK},
6349#endif
6350#ifdef _SC_MEMLOCK_RANGE
6351 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6352#endif
6353#ifdef _SC_MEMORY_PROTECTION
6354 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6355#endif
6356#ifdef _SC_MESSAGE_PASSING
6357 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6358#endif
Fred Draked86ed291999-12-15 15:34:33 +00006359#ifdef _SC_MMAP_FIXED_ALIGNMENT
6360 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6361#endif
Fred Drakec9680921999-12-13 16:37:25 +00006362#ifdef _SC_MQ_OPEN_MAX
6363 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6364#endif
6365#ifdef _SC_MQ_PRIO_MAX
6366 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6367#endif
Fred Draked86ed291999-12-15 15:34:33 +00006368#ifdef _SC_NACLS_MAX
6369 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6370#endif
Fred Drakec9680921999-12-13 16:37:25 +00006371#ifdef _SC_NGROUPS_MAX
6372 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6373#endif
6374#ifdef _SC_NL_ARGMAX
6375 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6376#endif
6377#ifdef _SC_NL_LANGMAX
6378 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6379#endif
6380#ifdef _SC_NL_MSGMAX
6381 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6382#endif
6383#ifdef _SC_NL_NMAX
6384 {"SC_NL_NMAX", _SC_NL_NMAX},
6385#endif
6386#ifdef _SC_NL_SETMAX
6387 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6388#endif
6389#ifdef _SC_NL_TEXTMAX
6390 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6391#endif
6392#ifdef _SC_NPROCESSORS_CONF
6393 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6394#endif
6395#ifdef _SC_NPROCESSORS_ONLN
6396 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6397#endif
Fred Draked86ed291999-12-15 15:34:33 +00006398#ifdef _SC_NPROC_CONF
6399 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6400#endif
6401#ifdef _SC_NPROC_ONLN
6402 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6403#endif
Fred Drakec9680921999-12-13 16:37:25 +00006404#ifdef _SC_NZERO
6405 {"SC_NZERO", _SC_NZERO},
6406#endif
6407#ifdef _SC_OPEN_MAX
6408 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6409#endif
6410#ifdef _SC_PAGESIZE
6411 {"SC_PAGESIZE", _SC_PAGESIZE},
6412#endif
6413#ifdef _SC_PAGE_SIZE
6414 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6415#endif
6416#ifdef _SC_PASS_MAX
6417 {"SC_PASS_MAX", _SC_PASS_MAX},
6418#endif
6419#ifdef _SC_PHYS_PAGES
6420 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6421#endif
6422#ifdef _SC_PII
6423 {"SC_PII", _SC_PII},
6424#endif
6425#ifdef _SC_PII_INTERNET
6426 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6427#endif
6428#ifdef _SC_PII_INTERNET_DGRAM
6429 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6430#endif
6431#ifdef _SC_PII_INTERNET_STREAM
6432 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6433#endif
6434#ifdef _SC_PII_OSI
6435 {"SC_PII_OSI", _SC_PII_OSI},
6436#endif
6437#ifdef _SC_PII_OSI_CLTS
6438 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6439#endif
6440#ifdef _SC_PII_OSI_COTS
6441 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6442#endif
6443#ifdef _SC_PII_OSI_M
6444 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6445#endif
6446#ifdef _SC_PII_SOCKET
6447 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6448#endif
6449#ifdef _SC_PII_XTI
6450 {"SC_PII_XTI", _SC_PII_XTI},
6451#endif
6452#ifdef _SC_POLL
6453 {"SC_POLL", _SC_POLL},
6454#endif
6455#ifdef _SC_PRIORITIZED_IO
6456 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6457#endif
6458#ifdef _SC_PRIORITY_SCHEDULING
6459 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6460#endif
6461#ifdef _SC_REALTIME_SIGNALS
6462 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6463#endif
6464#ifdef _SC_RE_DUP_MAX
6465 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6466#endif
6467#ifdef _SC_RTSIG_MAX
6468 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6469#endif
6470#ifdef _SC_SAVED_IDS
6471 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6472#endif
6473#ifdef _SC_SCHAR_MAX
6474 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6475#endif
6476#ifdef _SC_SCHAR_MIN
6477 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6478#endif
6479#ifdef _SC_SELECT
6480 {"SC_SELECT", _SC_SELECT},
6481#endif
6482#ifdef _SC_SEMAPHORES
6483 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6484#endif
6485#ifdef _SC_SEM_NSEMS_MAX
6486 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6487#endif
6488#ifdef _SC_SEM_VALUE_MAX
6489 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6490#endif
6491#ifdef _SC_SHARED_MEMORY_OBJECTS
6492 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6493#endif
6494#ifdef _SC_SHRT_MAX
6495 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6496#endif
6497#ifdef _SC_SHRT_MIN
6498 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6499#endif
6500#ifdef _SC_SIGQUEUE_MAX
6501 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6502#endif
6503#ifdef _SC_SIGRT_MAX
6504 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6505#endif
6506#ifdef _SC_SIGRT_MIN
6507 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6508#endif
Fred Draked86ed291999-12-15 15:34:33 +00006509#ifdef _SC_SOFTPOWER
6510 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6511#endif
Fred Drakec9680921999-12-13 16:37:25 +00006512#ifdef _SC_SPLIT_CACHE
6513 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6514#endif
6515#ifdef _SC_SSIZE_MAX
6516 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6517#endif
6518#ifdef _SC_STACK_PROT
6519 {"SC_STACK_PROT", _SC_STACK_PROT},
6520#endif
6521#ifdef _SC_STREAM_MAX
6522 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6523#endif
6524#ifdef _SC_SYNCHRONIZED_IO
6525 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6526#endif
6527#ifdef _SC_THREADS
6528 {"SC_THREADS", _SC_THREADS},
6529#endif
6530#ifdef _SC_THREAD_ATTR_STACKADDR
6531 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6532#endif
6533#ifdef _SC_THREAD_ATTR_STACKSIZE
6534 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6535#endif
6536#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6537 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6538#endif
6539#ifdef _SC_THREAD_KEYS_MAX
6540 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6541#endif
6542#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6543 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6544#endif
6545#ifdef _SC_THREAD_PRIO_INHERIT
6546 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6547#endif
6548#ifdef _SC_THREAD_PRIO_PROTECT
6549 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6550#endif
6551#ifdef _SC_THREAD_PROCESS_SHARED
6552 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6553#endif
6554#ifdef _SC_THREAD_SAFE_FUNCTIONS
6555 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6556#endif
6557#ifdef _SC_THREAD_STACK_MIN
6558 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6559#endif
6560#ifdef _SC_THREAD_THREADS_MAX
6561 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6562#endif
6563#ifdef _SC_TIMERS
6564 {"SC_TIMERS", _SC_TIMERS},
6565#endif
6566#ifdef _SC_TIMER_MAX
6567 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6568#endif
6569#ifdef _SC_TTY_NAME_MAX
6570 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6571#endif
6572#ifdef _SC_TZNAME_MAX
6573 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6574#endif
6575#ifdef _SC_T_IOV_MAX
6576 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6577#endif
6578#ifdef _SC_UCHAR_MAX
6579 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6580#endif
6581#ifdef _SC_UINT_MAX
6582 {"SC_UINT_MAX", _SC_UINT_MAX},
6583#endif
6584#ifdef _SC_UIO_MAXIOV
6585 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6586#endif
6587#ifdef _SC_ULONG_MAX
6588 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6589#endif
6590#ifdef _SC_USHRT_MAX
6591 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6592#endif
6593#ifdef _SC_VERSION
6594 {"SC_VERSION", _SC_VERSION},
6595#endif
6596#ifdef _SC_WORD_BIT
6597 {"SC_WORD_BIT", _SC_WORD_BIT},
6598#endif
6599#ifdef _SC_XBS5_ILP32_OFF32
6600 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6601#endif
6602#ifdef _SC_XBS5_ILP32_OFFBIG
6603 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6604#endif
6605#ifdef _SC_XBS5_LP64_OFF64
6606 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6607#endif
6608#ifdef _SC_XBS5_LPBIG_OFFBIG
6609 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6610#endif
6611#ifdef _SC_XOPEN_CRYPT
6612 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6613#endif
6614#ifdef _SC_XOPEN_ENH_I18N
6615 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6616#endif
6617#ifdef _SC_XOPEN_LEGACY
6618 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6619#endif
6620#ifdef _SC_XOPEN_REALTIME
6621 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6622#endif
6623#ifdef _SC_XOPEN_REALTIME_THREADS
6624 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6625#endif
6626#ifdef _SC_XOPEN_SHM
6627 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6628#endif
6629#ifdef _SC_XOPEN_UNIX
6630 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6631#endif
6632#ifdef _SC_XOPEN_VERSION
6633 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6634#endif
6635#ifdef _SC_XOPEN_XCU_VERSION
6636 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6637#endif
6638#ifdef _SC_XOPEN_XPG2
6639 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6640#endif
6641#ifdef _SC_XOPEN_XPG3
6642 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6643#endif
6644#ifdef _SC_XOPEN_XPG4
6645 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6646#endif
6647};
6648
6649static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006650conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006651{
6652 return conv_confname(arg, valuep, posix_constants_sysconf,
6653 sizeof(posix_constants_sysconf)
6654 / sizeof(struct constdef));
6655}
6656
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006657PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006658"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006659Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006660
6661static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006662posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006663{
6664 PyObject *result = NULL;
6665 int name;
6666
6667 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6668 int value;
6669
6670 errno = 0;
6671 value = sysconf(name);
6672 if (value == -1 && errno != 0)
6673 posix_error();
6674 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006675 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006676 }
6677 return result;
6678}
6679#endif
6680
6681
Fred Drakebec628d1999-12-15 18:31:10 +00006682/* This code is used to ensure that the tables of configuration value names
6683 * are in sorted order as required by conv_confname(), and also to build the
6684 * the exported dictionaries that are used to publish information about the
6685 * names available on the host platform.
6686 *
6687 * Sorting the table at runtime ensures that the table is properly ordered
6688 * when used, even for platforms we're not able to test on. It also makes
6689 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006690 */
Fred Drakebec628d1999-12-15 18:31:10 +00006691
6692static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006693cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006694{
6695 const struct constdef *c1 =
6696 (const struct constdef *) v1;
6697 const struct constdef *c2 =
6698 (const struct constdef *) v2;
6699
6700 return strcmp(c1->name, c2->name);
6701}
6702
6703static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006704setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006705 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006706{
Fred Drakebec628d1999-12-15 18:31:10 +00006707 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006708 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006709
6710 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6711 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006712 if (d == NULL)
6713 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006714
Barry Warsaw3155db32000-04-13 15:20:40 +00006715 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006716 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006717 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6718 Py_XDECREF(o);
6719 Py_DECREF(d);
6720 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006721 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006722 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006723 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006724 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006725}
6726
Fred Drakebec628d1999-12-15 18:31:10 +00006727/* Return -1 on failure, 0 on success. */
6728static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006729setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006730{
6731#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006732 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006733 sizeof(posix_constants_pathconf)
6734 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006735 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006736 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006737#endif
6738#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006739 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006740 sizeof(posix_constants_confstr)
6741 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006742 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006743 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006744#endif
6745#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006746 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006747 sizeof(posix_constants_sysconf)
6748 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006749 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006750 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006751#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006752 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006753}
Fred Draked86ed291999-12-15 15:34:33 +00006754
6755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006756PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006757"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006758Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006759in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006760
6761static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006762posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006763{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006764 abort();
6765 /*NOTREACHED*/
6766 Py_FatalError("abort() called from Python code didn't abort!");
6767 return NULL;
6768}
Fred Drakebec628d1999-12-15 18:31:10 +00006769
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006770#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006771PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006772"startfile(filepath [, operation]) - Start a file with its associated\n\
6773application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006774\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006775When \"operation\" is not specified or \"open\", this acts like\n\
6776double-clicking the file in Explorer, or giving the file name as an\n\
6777argument to the DOS \"start\" command: the file is opened with whatever\n\
6778application (if any) its extension is associated.\n\
6779When another \"operation\" is given, it specifies what should be done with\n\
6780the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006781\n\
6782startfile returns as soon as the associated application is launched.\n\
6783There is no option to wait for the application to close, and no way\n\
6784to retrieve the application's exit status.\n\
6785\n\
6786The filepath is relative to the current directory. If you want to use\n\
6787an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006788the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006789
6790static PyObject *
6791win32_startfile(PyObject *self, PyObject *args)
6792{
Martin v. Löwis011e8422009-05-05 04:43:17 +00006793 PyObject *ofilepath;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006794 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006795 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006796 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006797
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006798 PyObject *unipath, *woperation = NULL;
6799 if (!PyArg_ParseTuple(args, "U|s:startfile",
6800 &unipath, &operation)) {
6801 PyErr_Clear();
6802 goto normal;
6803 }
6804
6805 if (operation) {
6806 woperation = PyUnicode_DecodeASCII(operation,
6807 strlen(operation), NULL);
6808 if (!woperation) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006809 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006810 operation = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006811 goto normal;
6812 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006813 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006814
6815 Py_BEGIN_ALLOW_THREADS
6816 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6817 PyUnicode_AS_UNICODE(unipath),
6818 NULL, NULL, SW_SHOWNORMAL);
6819 Py_END_ALLOW_THREADS
6820
6821 Py_XDECREF(woperation);
6822 if (rc <= (HINSTANCE)32) {
6823 PyObject *errval = win32_error_unicode("startfile",
6824 PyUnicode_AS_UNICODE(unipath));
6825 return errval;
6826 }
6827 Py_INCREF(Py_None);
6828 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006829
6830normal:
Martin v. Löwis011e8422009-05-05 04:43:17 +00006831 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6832 PyUnicode_FSConverter, &ofilepath,
Georg Brandlf4f44152006-02-18 22:29:33 +00006833 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006834 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00006835 filepath = PyBytes_AsString(ofilepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006836 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006837 rc = ShellExecute((HWND)0, operation, filepath,
6838 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006839 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006840 if (rc <= (HINSTANCE)32) {
6841 PyObject *errval = win32_error("startfile", filepath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00006842 Py_DECREF(ofilepath);
Georg Brandle9f8ec92005-09-25 06:16:40 +00006843 return errval;
6844 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00006845 Py_DECREF(ofilepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006846 Py_INCREF(Py_None);
6847 return Py_None;
6848}
6849#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006850
Martin v. Löwis438b5342002-12-27 10:16:42 +00006851#ifdef HAVE_GETLOADAVG
6852PyDoc_STRVAR(posix_getloadavg__doc__,
6853"getloadavg() -> (float, float, float)\n\n\
6854Return the number of processes in the system run queue averaged over\n\
6855the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6856was unobtainable");
6857
6858static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006859posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006860{
6861 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006862 if (getloadavg(loadavg, 3)!=3) {
6863 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6864 return NULL;
6865 } else
6866 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6867}
6868#endif
6869
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006870#ifdef MS_WINDOWS
6871
6872PyDoc_STRVAR(win32_urandom__doc__,
6873"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006874Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006875
6876typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6877 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6878 DWORD dwFlags );
6879typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6880 BYTE *pbBuffer );
6881
6882static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006883/* This handle is never explicitly released. Instead, the operating
6884 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006885static HCRYPTPROV hCryptProv = 0;
6886
Tim Peters4ad82172004-08-30 17:02:04 +00006887static PyObject*
6888win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006889{
Tim Petersd3115382004-08-30 17:36:46 +00006890 int howMany;
6891 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006892
Tim Peters4ad82172004-08-30 17:02:04 +00006893 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006894 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006895 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006896 if (howMany < 0)
6897 return PyErr_Format(PyExc_ValueError,
6898 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006899
Tim Peters4ad82172004-08-30 17:02:04 +00006900 if (hCryptProv == 0) {
6901 HINSTANCE hAdvAPI32 = NULL;
6902 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006903
Tim Peters4ad82172004-08-30 17:02:04 +00006904 /* Obtain handle to the DLL containing CryptoAPI
6905 This should not fail */
6906 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6907 if(hAdvAPI32 == NULL)
6908 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006909
Tim Peters4ad82172004-08-30 17:02:04 +00006910 /* Obtain pointers to the CryptoAPI functions
6911 This will fail on some early versions of Win95 */
6912 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6913 hAdvAPI32,
6914 "CryptAcquireContextA");
6915 if (pCryptAcquireContext == NULL)
6916 return PyErr_Format(PyExc_NotImplementedError,
6917 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006918
Tim Peters4ad82172004-08-30 17:02:04 +00006919 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6920 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006921 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006922 return PyErr_Format(PyExc_NotImplementedError,
6923 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006924
Tim Peters4ad82172004-08-30 17:02:04 +00006925 /* Acquire context */
6926 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6927 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6928 return win32_error("CryptAcquireContext", NULL);
6929 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006930
Tim Peters4ad82172004-08-30 17:02:04 +00006931 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006932 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006933 if (result != NULL) {
6934 /* Get random data */
Amaury Forgeot d'Arca05ada32008-07-21 21:13:14 +00006935 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00006936 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006937 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006938 Py_DECREF(result);
6939 return win32_error("CryptGenRandom", NULL);
6940 }
Tim Peters4ad82172004-08-30 17:02:04 +00006941 }
Tim Petersd3115382004-08-30 17:36:46 +00006942 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006943}
6944#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006945
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006946PyDoc_STRVAR(device_encoding__doc__,
6947"device_encoding(fd) -> str\n\n\
6948Return a string describing the encoding of the device\n\
6949if the output is a terminal; else return None.");
6950
6951static PyObject *
6952device_encoding(PyObject *self, PyObject *args)
6953{
6954 int fd;
6955 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6956 return NULL;
Kristján Valur Jónsson649170b2009-03-24 14:15:49 +00006957 if (!_PyVerify_fd(fd) || !isatty(fd)) {
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006958 Py_INCREF(Py_None);
6959 return Py_None;
6960 }
6961#if defined(MS_WINDOWS) || defined(MS_WIN64)
6962 if (fd == 0) {
6963 char buf[100];
6964 sprintf(buf, "cp%d", GetConsoleCP());
6965 return PyUnicode_FromString(buf);
6966 }
6967 if (fd == 1 || fd == 2) {
6968 char buf[100];
6969 sprintf(buf, "cp%d", GetConsoleOutputCP());
6970 return PyUnicode_FromString(buf);
6971 }
6972#elif defined(CODESET)
6973 {
6974 char *codeset = nl_langinfo(CODESET);
Mark Dickinsonda2706b2008-12-11 18:03:03 +00006975 if (codeset != NULL && codeset[0] != 0)
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006976 return PyUnicode_FromString(codeset);
6977 }
6978#endif
6979 Py_INCREF(Py_None);
6980 return Py_None;
6981}
6982
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006983#ifdef __VMS
6984/* Use openssl random routine */
6985#include <openssl/rand.h>
6986PyDoc_STRVAR(vms_urandom__doc__,
6987"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006988Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006989
6990static PyObject*
6991vms_urandom(PyObject *self, PyObject *args)
6992{
6993 int howMany;
6994 PyObject* result;
6995
6996 /* Read arguments */
6997 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6998 return NULL;
6999 if (howMany < 0)
7000 return PyErr_Format(PyExc_ValueError,
7001 "negative argument not allowed");
7002
7003 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00007004 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007005 if (result != NULL) {
7006 /* Get random data */
7007 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00007008 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007009 howMany) < 0) {
7010 Py_DECREF(result);
7011 return PyErr_Format(PyExc_ValueError,
7012 "RAND_pseudo_bytes");
7013 }
7014 }
7015 return result;
7016}
7017#endif
7018
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007019#ifdef HAVE_SETRESUID
7020PyDoc_STRVAR(posix_setresuid__doc__,
7021"setresuid(ruid, euid, suid)\n\n\
7022Set the current process's real, effective, and saved user ids.");
7023
7024static PyObject*
7025posix_setresuid (PyObject *self, PyObject *args)
7026{
7027 /* We assume uid_t is no larger than a long. */
7028 long ruid, euid, suid;
7029 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7030 return NULL;
7031 if (setresuid(ruid, euid, suid) < 0)
7032 return posix_error();
7033 Py_RETURN_NONE;
7034}
7035#endif
7036
7037#ifdef HAVE_SETRESGID
7038PyDoc_STRVAR(posix_setresgid__doc__,
7039"setresgid(rgid, egid, sgid)\n\n\
7040Set the current process's real, effective, and saved group ids.");
7041
7042static PyObject*
7043posix_setresgid (PyObject *self, PyObject *args)
7044{
7045 /* We assume uid_t is no larger than a long. */
7046 long rgid, egid, sgid;
7047 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7048 return NULL;
7049 if (setresgid(rgid, egid, sgid) < 0)
7050 return posix_error();
7051 Py_RETURN_NONE;
7052}
7053#endif
7054
7055#ifdef HAVE_GETRESUID
7056PyDoc_STRVAR(posix_getresuid__doc__,
7057"getresuid() -> (ruid, euid, suid)\n\n\
7058Get tuple of the current process's real, effective, and saved user ids.");
7059
7060static PyObject*
7061posix_getresuid (PyObject *self, PyObject *noargs)
7062{
7063 uid_t ruid, euid, suid;
7064 long l_ruid, l_euid, l_suid;
7065 if (getresuid(&ruid, &euid, &suid) < 0)
7066 return posix_error();
7067 /* Force the values into long's as we don't know the size of uid_t. */
7068 l_ruid = ruid;
7069 l_euid = euid;
7070 l_suid = suid;
7071 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
7072}
7073#endif
7074
7075#ifdef HAVE_GETRESGID
7076PyDoc_STRVAR(posix_getresgid__doc__,
7077"getresgid() -> (rgid, egid, sgid)\n\n\
7078Get tuple of the current process's real, effective, and saved user ids.");
7079
7080static PyObject*
7081posix_getresgid (PyObject *self, PyObject *noargs)
7082{
7083 uid_t rgid, egid, sgid;
7084 long l_rgid, l_egid, l_sgid;
7085 if (getresgid(&rgid, &egid, &sgid) < 0)
7086 return posix_error();
7087 /* Force the values into long's as we don't know the size of uid_t. */
7088 l_rgid = rgid;
7089 l_egid = egid;
7090 l_sgid = sgid;
7091 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
7092}
7093#endif
7094
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007095static PyMethodDef posix_methods[] = {
7096 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7097#ifdef HAVE_TTYNAME
7098 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7099#endif
7100 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007101#ifdef HAVE_CHFLAGS
7102 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
7103#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007104 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007105#ifdef HAVE_FCHMOD
7106 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
7107#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007108#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007109 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007110#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007111#ifdef HAVE_LCHMOD
7112 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
7113#endif /* HAVE_LCHMOD */
7114#ifdef HAVE_FCHOWN
7115 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
7116#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007117#ifdef HAVE_LCHFLAGS
7118 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
7119#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007120#ifdef HAVE_LCHOWN
7121 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7122#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007123#ifdef HAVE_CHROOT
7124 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7125#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007126#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007127 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007128#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007129#ifdef HAVE_GETCWD
Guido van Rossumf0af3e32008-10-02 18:55:37 +00007130 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7131 METH_NOARGS, posix_getcwd__doc__},
7132 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7133 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007134#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007135#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007136 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007137#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007138 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7139 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7140 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007141#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007142 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007143#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007144#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007145 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007146#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007147 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7148 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7149 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007150 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007151#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007152 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007153#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007154#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007155 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007156#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007157 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007158#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007159 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007160#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007161 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7162 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7163 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007164#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007165 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007166#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007167 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007168#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007169 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7170 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007171#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007172#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007173 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7174 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007175#if defined(PYOS_OS2)
7176 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7177 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7178#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007179#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007180#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007181 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007182#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007183#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007184 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007185#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007186#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007187 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007188#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007189#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007190 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007191#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007192#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007193 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007194#endif /* HAVE_GETEGID */
7195#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007196 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007197#endif /* HAVE_GETEUID */
7198#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007199 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007200#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007201#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007202 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007203#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007204 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007205#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007206 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007207#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007208#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007209 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007210#endif /* HAVE_GETPPID */
7211#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007212 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007213#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007214#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007215 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007216#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007217#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007218 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007219#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007220#ifdef HAVE_KILLPG
7221 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7222#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007223#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007224 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007225#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007226#ifdef MS_WINDOWS
7227 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Brian Curtineb24d742010-04-12 17:16:38 +00007228 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007229#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007230#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007231 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007232#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007233#ifdef HAVE_SETEUID
7234 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7235#endif /* HAVE_SETEUID */
7236#ifdef HAVE_SETEGID
7237 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7238#endif /* HAVE_SETEGID */
7239#ifdef HAVE_SETREUID
7240 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7241#endif /* HAVE_SETREUID */
7242#ifdef HAVE_SETREGID
7243 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7244#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007245#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007246 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007247#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007248#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00007249 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007250#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007251#ifdef HAVE_INITGROUPS
7252 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
7253#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007254#ifdef HAVE_GETPGID
7255 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7256#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007257#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007258 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007259#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007260#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007261 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007262#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007263#ifdef HAVE_WAIT3
7264 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7265#endif /* HAVE_WAIT3 */
7266#ifdef HAVE_WAIT4
7267 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7268#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007269#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007270 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007271#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007272#ifdef HAVE_GETSID
7273 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7274#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007275#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007276 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007277#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007278#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007279 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007280#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007281#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007282 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007283#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007284#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007285 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007286#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007287 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7288 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00007289 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007290 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007291 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7292 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7293 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7294 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7295 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7296 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007297 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007298#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007299 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007300#endif
7301#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007302 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007303#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007304#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007305 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7306#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007307#ifdef HAVE_DEVICE_MACROS
7308 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7309 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7310 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7311#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007312#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007313 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007314#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007315#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007316 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007317#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007318#ifdef HAVE_UNSETENV
7319 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7320#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007321 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007322#ifdef HAVE_FCHDIR
7323 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7324#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007325#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007326 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007327#endif
7328#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007329 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007330#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007331#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007332#ifdef WCOREDUMP
7333 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7334#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007335#ifdef WIFCONTINUED
7336 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7337#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007338#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007339 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007340#endif /* WIFSTOPPED */
7341#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007342 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007343#endif /* WIFSIGNALED */
7344#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007345 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007346#endif /* WIFEXITED */
7347#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007348 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007349#endif /* WEXITSTATUS */
7350#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007351 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007352#endif /* WTERMSIG */
7353#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007354 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007355#endif /* WSTOPSIG */
7356#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007357#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007358 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007359#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007360#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007361 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007362#endif
Fred Drakec9680921999-12-13 16:37:25 +00007363#ifdef HAVE_CONFSTR
7364 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7365#endif
7366#ifdef HAVE_SYSCONF
7367 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7368#endif
7369#ifdef HAVE_FPATHCONF
7370 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7371#endif
7372#ifdef HAVE_PATHCONF
7373 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7374#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007375 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007376#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007377 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7378#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007379#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007380 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007381#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007382 #ifdef MS_WINDOWS
7383 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7384 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007385 #ifdef __VMS
7386 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7387 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007388#ifdef HAVE_SETRESUID
7389 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
7390#endif
7391#ifdef HAVE_SETRESGID
7392 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
7393#endif
7394#ifdef HAVE_GETRESUID
7395 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
7396#endif
7397#ifdef HAVE_GETRESGID
7398 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
7399#endif
7400
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007401 {NULL, NULL} /* Sentinel */
7402};
7403
7404
Barry Warsaw4a342091996-12-19 23:50:02 +00007405static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007406ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007407{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007408 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007409}
7410
Guido van Rossumd48f2521997-12-05 22:19:34 +00007411#if defined(PYOS_OS2)
7412/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007413static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007414{
7415 APIRET rc;
7416 ULONG values[QSV_MAX+1];
7417 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007418 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007419
7420 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007421 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007422 Py_END_ALLOW_THREADS
7423
7424 if (rc != NO_ERROR) {
7425 os2_error(rc);
7426 return -1;
7427 }
7428
Fred Drake4d1e64b2002-04-15 19:40:07 +00007429 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7430 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7431 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7432 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7433 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7434 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7435 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007436
7437 switch (values[QSV_VERSION_MINOR]) {
7438 case 0: ver = "2.00"; break;
7439 case 10: ver = "2.10"; break;
7440 case 11: ver = "2.11"; break;
7441 case 30: ver = "3.00"; break;
7442 case 40: ver = "4.00"; break;
7443 case 50: ver = "5.00"; break;
7444 default:
Tim Peters885d4572001-11-28 20:27:42 +00007445 PyOS_snprintf(tmp, sizeof(tmp),
7446 "%d-%d", values[QSV_VERSION_MAJOR],
7447 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007448 ver = &tmp[0];
7449 }
7450
7451 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007452 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007453 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007454
7455 /* Add Indicator of Which Drive was Used to Boot the System */
7456 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7457 tmp[1] = ':';
7458 tmp[2] = '\0';
7459
Fred Drake4d1e64b2002-04-15 19:40:07 +00007460 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007461}
7462#endif
7463
Barry Warsaw4a342091996-12-19 23:50:02 +00007464static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007465all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007466{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007467#ifdef F_OK
7468 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007469#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007470#ifdef R_OK
7471 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007472#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007473#ifdef W_OK
7474 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007475#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007476#ifdef X_OK
7477 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007478#endif
Fred Drakec9680921999-12-13 16:37:25 +00007479#ifdef NGROUPS_MAX
7480 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7481#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007482#ifdef TMP_MAX
7483 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7484#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007485#ifdef WCONTINUED
7486 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7487#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007488#ifdef WNOHANG
7489 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007490#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007491#ifdef WUNTRACED
7492 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7493#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007494#ifdef O_RDONLY
7495 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7496#endif
7497#ifdef O_WRONLY
7498 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7499#endif
7500#ifdef O_RDWR
7501 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7502#endif
7503#ifdef O_NDELAY
7504 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7505#endif
7506#ifdef O_NONBLOCK
7507 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7508#endif
7509#ifdef O_APPEND
7510 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7511#endif
7512#ifdef O_DSYNC
7513 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7514#endif
7515#ifdef O_RSYNC
7516 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7517#endif
7518#ifdef O_SYNC
7519 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7520#endif
7521#ifdef O_NOCTTY
7522 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7523#endif
7524#ifdef O_CREAT
7525 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7526#endif
7527#ifdef O_EXCL
7528 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7529#endif
7530#ifdef O_TRUNC
7531 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7532#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007533#ifdef O_BINARY
7534 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7535#endif
7536#ifdef O_TEXT
7537 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7538#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007539#ifdef O_LARGEFILE
7540 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7541#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007542#ifdef O_SHLOCK
7543 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7544#endif
7545#ifdef O_EXLOCK
7546 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7547#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007548
Tim Peters5aa91602002-01-30 05:46:57 +00007549/* MS Windows */
7550#ifdef O_NOINHERIT
7551 /* Don't inherit in child processes. */
7552 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7553#endif
7554#ifdef _O_SHORT_LIVED
7555 /* Optimize for short life (keep in memory). */
7556 /* MS forgot to define this one with a non-underscore form too. */
7557 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7558#endif
7559#ifdef O_TEMPORARY
7560 /* Automatically delete when last handle is closed. */
7561 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7562#endif
7563#ifdef O_RANDOM
7564 /* Optimize for random access. */
7565 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7566#endif
7567#ifdef O_SEQUENTIAL
7568 /* Optimize for sequential access. */
7569 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7570#endif
7571
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007572/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007573#ifdef O_ASYNC
7574 /* Send a SIGIO signal whenever input or output
7575 becomes available on file descriptor */
7576 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7577#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007578#ifdef O_DIRECT
7579 /* Direct disk access. */
7580 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7581#endif
7582#ifdef O_DIRECTORY
7583 /* Must be a directory. */
7584 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7585#endif
7586#ifdef O_NOFOLLOW
7587 /* Do not follow links. */
7588 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7589#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007590#ifdef O_NOATIME
7591 /* Do not update the access time. */
7592 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7593#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007594
Barry Warsaw5676bd12003-01-07 20:57:09 +00007595 /* These come from sysexits.h */
7596#ifdef EX_OK
7597 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007598#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007599#ifdef EX_USAGE
7600 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007601#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007602#ifdef EX_DATAERR
7603 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007604#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007605#ifdef EX_NOINPUT
7606 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007607#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007608#ifdef EX_NOUSER
7609 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007610#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007611#ifdef EX_NOHOST
7612 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007613#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007614#ifdef EX_UNAVAILABLE
7615 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007616#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007617#ifdef EX_SOFTWARE
7618 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007619#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007620#ifdef EX_OSERR
7621 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007622#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007623#ifdef EX_OSFILE
7624 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007625#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007626#ifdef EX_CANTCREAT
7627 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007628#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007629#ifdef EX_IOERR
7630 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007631#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007632#ifdef EX_TEMPFAIL
7633 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007634#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007635#ifdef EX_PROTOCOL
7636 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007637#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007638#ifdef EX_NOPERM
7639 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007640#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007641#ifdef EX_CONFIG
7642 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007643#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007644#ifdef EX_NOTFOUND
7645 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007646#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007647
Guido van Rossum246bc171999-02-01 23:54:31 +00007648#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007649#if defined(PYOS_OS2) && defined(PYCC_GCC)
7650 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7651 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7652 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7653 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7654 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7655 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7656 if (ins(d, "P_PM", (long)P_PM)) return -1;
7657 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7658 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7659 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7660 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7661 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7662 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7663 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7664 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7665 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7666 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7667 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7668 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7669 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7670#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007671 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7672 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7673 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7674 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7675 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007676#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007677#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007678
Guido van Rossumd48f2521997-12-05 22:19:34 +00007679#if defined(PYOS_OS2)
7680 if (insertvalues(d)) return -1;
7681#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007682 return 0;
7683}
7684
7685
Tim Peters5aa91602002-01-30 05:46:57 +00007686#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007687#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007688#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007689
7690#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007691#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007692#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007693
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007694#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007695#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007696#define MODNAME "posix"
7697#endif
7698
Martin v. Löwis1a214512008-06-11 05:26:20 +00007699static struct PyModuleDef posixmodule = {
7700 PyModuleDef_HEAD_INIT,
7701 MODNAME,
7702 posix__doc__,
7703 -1,
7704 posix_methods,
7705 NULL,
7706 NULL,
7707 NULL,
7708 NULL
7709};
7710
7711
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007712PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007713INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007714{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007715 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007716
Martin v. Löwis1a214512008-06-11 05:26:20 +00007717 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007718 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007719 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007720
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007721 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007722 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007723 Py_XINCREF(v);
7724 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007725 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007726 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007727
Fred Drake4d1e64b2002-04-15 19:40:07 +00007728 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007729 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007730
Fred Drake4d1e64b2002-04-15 19:40:07 +00007731 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007732 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007733
Fred Drake4d1e64b2002-04-15 19:40:07 +00007734 Py_INCREF(PyExc_OSError);
7735 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007736
Guido van Rossumb3d39562000-01-31 18:41:26 +00007737#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007738 if (posix_putenv_garbage == NULL)
7739 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007740#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007741
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007742 if (!initialized) {
7743 stat_result_desc.name = MODNAME ".stat_result";
7744 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7745 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7746 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7747 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7748 structseq_new = StatResultType.tp_new;
7749 StatResultType.tp_new = statresult_new;
7750
7751 statvfs_result_desc.name = MODNAME ".statvfs_result";
7752 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007753#ifdef NEED_TICKS_PER_SECOND
7754# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7755 ticks_per_second = sysconf(_SC_CLK_TCK);
7756# elif defined(HZ)
7757 ticks_per_second = HZ;
7758# else
7759 ticks_per_second = 60; /* magic fallback value; may be bogus */
7760# endif
7761#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007762 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007763 Py_INCREF((PyObject*) &StatResultType);
7764 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007765 Py_INCREF((PyObject*) &StatVFSResultType);
7766 PyModule_AddObject(m, "statvfs_result",
7767 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007768 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007769
7770#ifdef __APPLE__
7771 /*
7772 * Step 2 of weak-linking support on Mac OS X.
7773 *
7774 * The code below removes functions that are not available on the
7775 * currently active platform.
7776 *
7777 * This block allow one to use a python binary that was build on
7778 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7779 * OSX 10.4.
7780 */
7781#ifdef HAVE_FSTATVFS
7782 if (fstatvfs == NULL) {
7783 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007784 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007785 }
7786 }
7787#endif /* HAVE_FSTATVFS */
7788
7789#ifdef HAVE_STATVFS
7790 if (statvfs == NULL) {
7791 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007792 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007793 }
7794 }
7795#endif /* HAVE_STATVFS */
7796
7797# ifdef HAVE_LCHOWN
7798 if (lchown == NULL) {
7799 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007800 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007801 }
7802 }
7803#endif /* HAVE_LCHOWN */
7804
7805
7806#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007807 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007808
Guido van Rossumb6775db1994-08-01 11:34:53 +00007809}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007810
7811#ifdef __cplusplus
7812}
7813#endif