blob: 0575be23bba1e5afb45bad6b635f10891103551a [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;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000496 k = PyUnicode_FromStringAndSize(*e, (int)(p-*e));
Guido van Rossum6a619f41999-08-03 19:41:10 +0000497 if (k == NULL) {
498 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000499 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000500 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000501 v = PyUnicode_FromString(p+1);
Guido van Rossum6a619f41999-08-03 19:41:10 +0000502 if (v == NULL) {
503 PyErr_Clear();
504 Py_DECREF(k);
505 continue;
506 }
507 if (PyDict_GetItem(d, k) == NULL) {
508 if (PyDict_SetItem(d, k, v) != 0)
509 PyErr_Clear();
510 }
511 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000512 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000513 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000514#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000515#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000516 {
517 APIRET rc;
518 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
519
520 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000521 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000522 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000523 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000524 Py_DECREF(v);
525 }
526 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
527 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000528 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000529 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000530 Py_DECREF(v);
531 }
532 }
533#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000534 return d;
535}
536
537
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000538/* Set a POSIX-specific error from errno, and return NULL */
539
Barry Warsawd58d7641998-07-23 16:14:40 +0000540static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000541posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000542{
Barry Warsawca74da41999-02-09 19:31:45 +0000543 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544}
Barry Warsawd58d7641998-07-23 16:14:40 +0000545static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000546posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000547{
Barry Warsawca74da41999-02-09 19:31:45 +0000548 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000549}
550
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000551#ifdef Py_WIN_WIDE_FILENAMES
552static PyObject *
553posix_error_with_unicode_filename(Py_UNICODE* name)
554{
555 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
556}
557#endif /* Py_WIN_WIDE_FILENAMES */
558
559
Mark Hammondef8b6542001-05-13 08:04:26 +0000560static PyObject *
561posix_error_with_allocated_filename(char* name)
562{
563 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
564 PyMem_Free(name);
565 return rc;
566}
567
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000568#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000569static PyObject *
570win32_error(char* function, char* filename)
571{
Mark Hammond33a6da92000-08-15 00:46:38 +0000572 /* XXX We should pass the function name along in the future.
Georg Brandl38feaf02008-05-25 07:45:51 +0000573 (winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000574 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000575 Windows error object, which is non-trivial.
576 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000577 errno = GetLastError();
578 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000579 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000580 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000581 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000582}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000583
584#ifdef Py_WIN_WIDE_FILENAMES
585static PyObject *
586win32_error_unicode(char* function, Py_UNICODE* filename)
587{
588 /* XXX - see win32_error for comments on 'function' */
589 errno = GetLastError();
590 if (filename)
591 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
592 else
593 return PyErr_SetFromWindowsErr(errno);
594}
595
Thomas Wouters477c8d52006-05-27 19:21:47 +0000596static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000597convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000598{
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000599 if (PyUnicode_CheckExact(*param))
600 Py_INCREF(*param);
601 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000602 /* For a Unicode subtype that's not a Unicode object,
603 return a true Unicode object with the same data. */
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000604 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
605 PyUnicode_GET_SIZE(*param));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000606 else
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000607 *param = PyUnicode_FromEncodedObject(*param,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000608 Py_FileSystemDefaultEncoding,
609 "strict");
610 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000611}
612
613#endif /* Py_WIN_WIDE_FILENAMES */
614
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000615#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000616
Guido van Rossumd48f2521997-12-05 22:19:34 +0000617#if defined(PYOS_OS2)
618/**********************************************************************
619 * Helper Function to Trim and Format OS/2 Messages
620 **********************************************************************/
621 static void
622os2_formatmsg(char *msgbuf, int msglen, char *reason)
623{
624 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
625
626 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
627 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
628
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000629 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000630 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
631 }
632
633 /* Add Optional Reason Text */
634 if (reason) {
635 strcat(msgbuf, " : ");
636 strcat(msgbuf, reason);
637 }
638}
639
640/**********************************************************************
641 * Decode an OS/2 Operating System Error Code
642 *
643 * A convenience function to lookup an OS/2 error code and return a
644 * text message we can use to raise a Python exception.
645 *
646 * Notes:
647 * The messages for errors returned from the OS/2 kernel reside in
648 * the file OSO001.MSG in the \OS2 directory hierarchy.
649 *
650 **********************************************************************/
651 static char *
652os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
653{
654 APIRET rc;
655 ULONG msglen;
656
657 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
658 Py_BEGIN_ALLOW_THREADS
659 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
660 errorcode, "oso001.msg", &msglen);
661 Py_END_ALLOW_THREADS
662
663 if (rc == NO_ERROR)
664 os2_formatmsg(msgbuf, msglen, reason);
665 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000666 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000667 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000668
669 return msgbuf;
670}
671
672/* Set an OS/2-specific error and return NULL. OS/2 kernel
673 errors are not in a global variable e.g. 'errno' nor are
674 they congruent with posix error numbers. */
675
676static PyObject * os2_error(int code)
677{
678 char text[1024];
679 PyObject *v;
680
681 os2_strerror(text, sizeof(text), code, "");
682
683 v = Py_BuildValue("(is)", code, text);
684 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000685 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000686 Py_DECREF(v);
687 }
688 return NULL; /* Signal to Python that an Exception is Pending */
689}
690
691#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000692
693/* POSIX generic methods */
694
Barry Warsaw53699e91996-12-10 23:23:01 +0000695static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000696posix_fildes(PyObject *fdobj, int (*func)(int))
697{
698 int fd;
699 int res;
700 fd = PyObject_AsFileDescriptor(fdobj);
701 if (fd < 0)
702 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000703 if (!_PyVerify_fd(fd))
704 return posix_error();
Fred Drake4d1e64b2002-04-15 19:40:07 +0000705 Py_BEGIN_ALLOW_THREADS
706 res = (*func)(fd);
707 Py_END_ALLOW_THREADS
708 if (res < 0)
709 return posix_error();
710 Py_INCREF(Py_None);
711 return Py_None;
712}
Guido van Rossum21142a01999-01-08 21:05:37 +0000713
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000714#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000715static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000716unicode_file_names(void)
717{
718 static int canusewide = -1;
719 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000720 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000721 the Windows NT family. */
722 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
723 }
724 return canusewide;
725}
726#endif
Tim Peters11b23062003-04-23 02:39:17 +0000727
Guido van Rossum21142a01999-01-08 21:05:37 +0000728static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000729posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000730{
Mark Hammondef8b6542001-05-13 08:04:26 +0000731 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000732 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000733 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000734 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000735 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000736 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000737 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000738 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000739 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000740 return posix_error_with_allocated_filename(path1);
741 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000742 Py_INCREF(Py_None);
743 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000744}
745
Barry Warsaw53699e91996-12-10 23:23:01 +0000746static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000747posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000748 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000749 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000750{
Mark Hammondef8b6542001-05-13 08:04:26 +0000751 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000752 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000753 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000754 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000755 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000756 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000757 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000758 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000759 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000760 PyMem_Free(path1);
761 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000762 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000763 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000764 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000765 Py_INCREF(Py_None);
766 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000767}
768
Thomas Wouters477c8d52006-05-27 19:21:47 +0000769#ifdef Py_WIN_WIDE_FILENAMES
770static PyObject*
771win32_1str(PyObject* args, char* func,
772 char* format, BOOL (__stdcall *funcA)(LPCSTR),
773 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
774{
775 PyObject *uni;
776 char *ansi;
777 BOOL result;
778 if (unicode_file_names()) {
779 if (!PyArg_ParseTuple(args, wformat, &uni))
780 PyErr_Clear();
781 else {
782 Py_BEGIN_ALLOW_THREADS
783 result = funcW(PyUnicode_AsUnicode(uni));
784 Py_END_ALLOW_THREADS
785 if (!result)
786 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
787 Py_INCREF(Py_None);
788 return Py_None;
789 }
790 }
791 if (!PyArg_ParseTuple(args, format, &ansi))
792 return NULL;
793 Py_BEGIN_ALLOW_THREADS
794 result = funcA(ansi);
795 Py_END_ALLOW_THREADS
796 if (!result)
797 return win32_error(func, ansi);
798 Py_INCREF(Py_None);
799 return Py_None;
800
801}
802
803/* This is a reimplementation of the C library's chdir function,
804 but one that produces Win32 errors instead of DOS error codes.
805 chdir is essentially a wrapper around SetCurrentDirectory; however,
806 it also needs to set "magic" environment variables indicating
807 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000808static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000809win32_chdir(LPCSTR path)
810{
811 char new_path[MAX_PATH+1];
812 int result;
813 char env[4] = "=x:";
814
815 if(!SetCurrentDirectoryA(path))
816 return FALSE;
817 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
818 if (!result)
819 return FALSE;
820 /* In the ANSI API, there should not be any paths longer
821 than MAX_PATH. */
822 assert(result <= MAX_PATH+1);
823 if (strncmp(new_path, "\\\\", 2) == 0 ||
824 strncmp(new_path, "//", 2) == 0)
825 /* UNC path, nothing to do. */
826 return TRUE;
827 env[1] = new_path[0];
828 return SetEnvironmentVariableA(env, new_path);
829}
830
831/* The Unicode version differs from the ANSI version
832 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000833static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000834win32_wchdir(LPCWSTR path)
835{
836 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
837 int result;
838 wchar_t env[4] = L"=x:";
839
840 if(!SetCurrentDirectoryW(path))
841 return FALSE;
842 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
843 if (!result)
844 return FALSE;
845 if (result > MAX_PATH+1) {
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000846 new_path = malloc(result * sizeof(wchar_t));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000847 if (!new_path) {
848 SetLastError(ERROR_OUTOFMEMORY);
849 return FALSE;
850 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000851 result = GetCurrentDirectoryW(result, new_path);
852 if (!result) {
853 free(new_path);
854 return FALSE;
855 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000856 }
857 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
858 wcsncmp(new_path, L"//", 2) == 0)
859 /* UNC path, nothing to do. */
860 return TRUE;
861 env[1] = new_path[0];
862 result = SetEnvironmentVariableW(env, new_path);
863 if (new_path != _new_path)
864 free(new_path);
865 return result;
866}
867#endif
868
Martin v. Löwis14694662006-02-03 12:54:16 +0000869#ifdef MS_WINDOWS
870/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
871 - time stamps are restricted to second resolution
872 - file modification times suffer from forth-and-back conversions between
873 UTC and local time
874 Therefore, we implement our own stat, based on the Win32 API directly.
875*/
876#define HAVE_STAT_NSEC 1
877
878struct win32_stat{
879 int st_dev;
880 __int64 st_ino;
881 unsigned short st_mode;
882 int st_nlink;
883 int st_uid;
884 int st_gid;
885 int st_rdev;
886 __int64 st_size;
887 int st_atime;
888 int st_atime_nsec;
889 int st_mtime;
890 int st_mtime_nsec;
891 int st_ctime;
892 int st_ctime_nsec;
893};
894
895static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
896
897static void
898FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
899{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000900 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
901 /* Cannot simply cast and dereference in_ptr,
902 since it might not be aligned properly */
903 __int64 in;
904 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000905 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
906 /* XXX Win32 supports time stamps past 2038; we currently don't */
907 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
908}
909
Thomas Wouters477c8d52006-05-27 19:21:47 +0000910static void
911time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
912{
913 /* XXX endianness */
914 __int64 out;
915 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000916 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000917 memcpy(out_ptr, &out, sizeof(out));
918}
919
Martin v. Löwis14694662006-02-03 12:54:16 +0000920/* Below, we *know* that ugo+r is 0444 */
921#if _S_IREAD != 0400
922#error Unsupported C library
923#endif
924static int
925attributes_to_mode(DWORD attr)
926{
927 int m = 0;
928 if (attr & FILE_ATTRIBUTE_DIRECTORY)
929 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
930 else
931 m |= _S_IFREG;
932 if (attr & FILE_ATTRIBUTE_READONLY)
933 m |= 0444;
934 else
935 m |= 0666;
936 return m;
937}
938
939static int
940attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
941{
942 memset(result, 0, sizeof(*result));
943 result->st_mode = attributes_to_mode(info->dwFileAttributes);
944 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
945 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
946 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
947 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
948
949 return 0;
950}
951
Thomas Wouters89f507f2006-12-13 04:49:30 +0000952/* Emulate GetFileAttributesEx[AW] on Windows 95 */
953static int checked = 0;
954static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
955static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
956static void
957check_gfax()
958{
959 HINSTANCE hKernel32;
960 if (checked)
961 return;
962 checked = 1;
963 hKernel32 = GetModuleHandle("KERNEL32");
964 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
965 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
966}
967
Guido van Rossumd8faa362007-04-27 19:54:29 +0000968static BOOL
969attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
970{
971 HANDLE hFindFile;
972 WIN32_FIND_DATAA FileData;
973 hFindFile = FindFirstFileA(pszFile, &FileData);
974 if (hFindFile == INVALID_HANDLE_VALUE)
975 return FALSE;
976 FindClose(hFindFile);
977 pfad->dwFileAttributes = FileData.dwFileAttributes;
978 pfad->ftCreationTime = FileData.ftCreationTime;
979 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
980 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
981 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
982 pfad->nFileSizeLow = FileData.nFileSizeLow;
983 return TRUE;
984}
985
986static BOOL
987attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
988{
989 HANDLE hFindFile;
990 WIN32_FIND_DATAW FileData;
991 hFindFile = FindFirstFileW(pszFile, &FileData);
992 if (hFindFile == INVALID_HANDLE_VALUE)
993 return FALSE;
994 FindClose(hFindFile);
995 pfad->dwFileAttributes = FileData.dwFileAttributes;
996 pfad->ftCreationTime = FileData.ftCreationTime;
997 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
998 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
999 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1000 pfad->nFileSizeLow = FileData.nFileSizeLow;
1001 return TRUE;
1002}
1003
Thomas Wouters89f507f2006-12-13 04:49:30 +00001004static BOOL WINAPI
1005Py_GetFileAttributesExA(LPCSTR pszFile,
1006 GET_FILEEX_INFO_LEVELS level,
1007 LPVOID pv)
1008{
1009 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001010 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1011 /* First try to use the system's implementation, if that is
1012 available and either succeeds to gives an error other than
1013 that it isn't implemented. */
1014 check_gfax();
1015 if (gfaxa) {
1016 result = gfaxa(pszFile, level, pv);
1017 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1018 return result;
1019 }
1020 /* It's either not present, or not implemented.
1021 Emulate using FindFirstFile. */
1022 if (level != GetFileExInfoStandard) {
1023 SetLastError(ERROR_INVALID_PARAMETER);
1024 return FALSE;
1025 }
1026 /* Use GetFileAttributes to validate that the file name
1027 does not contain wildcards (which FindFirstFile would
1028 accept). */
1029 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
1030 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001031 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001032}
1033
1034static BOOL WINAPI
1035Py_GetFileAttributesExW(LPCWSTR pszFile,
1036 GET_FILEEX_INFO_LEVELS level,
1037 LPVOID pv)
1038{
1039 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001040 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1041 /* First try to use the system's implementation, if that is
1042 available and either succeeds to gives an error other than
1043 that it isn't implemented. */
1044 check_gfax();
1045 if (gfaxa) {
1046 result = gfaxw(pszFile, level, pv);
1047 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1048 return result;
1049 }
1050 /* It's either not present, or not implemented.
1051 Emulate using FindFirstFile. */
1052 if (level != GetFileExInfoStandard) {
1053 SetLastError(ERROR_INVALID_PARAMETER);
1054 return FALSE;
1055 }
1056 /* Use GetFileAttributes to validate that the file name
1057 does not contain wildcards (which FindFirstFile would
1058 accept). */
1059 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
1060 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001061 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001062}
1063
Martin v. Löwis14694662006-02-03 12:54:16 +00001064static int
1065win32_stat(const char* path, struct win32_stat *result)
1066{
1067 WIN32_FILE_ATTRIBUTE_DATA info;
1068 int code;
1069 char *dot;
1070 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001071 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001072 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1073 /* Protocol violation: we explicitly clear errno, instead of
1074 setting it to a POSIX error. Callers should use GetLastError. */
1075 errno = 0;
1076 return -1;
1077 } else {
1078 /* Could not get attributes on open file. Fall back to
1079 reading the directory. */
1080 if (!attributes_from_dir(path, &info)) {
1081 /* Very strange. This should not fail now */
1082 errno = 0;
1083 return -1;
1084 }
1085 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001086 }
1087 code = attribute_data_to_stat(&info, result);
1088 if (code != 0)
1089 return code;
1090 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1091 dot = strrchr(path, '.');
1092 if (dot) {
1093 if (stricmp(dot, ".bat") == 0 ||
1094 stricmp(dot, ".cmd") == 0 ||
1095 stricmp(dot, ".exe") == 0 ||
1096 stricmp(dot, ".com") == 0)
1097 result->st_mode |= 0111;
1098 }
1099 return code;
1100}
1101
1102static int
1103win32_wstat(const wchar_t* path, struct win32_stat *result)
1104{
1105 int code;
1106 const wchar_t *dot;
1107 WIN32_FILE_ATTRIBUTE_DATA info;
1108 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001109 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001110 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1111 /* Protocol violation: we explicitly clear errno, instead of
1112 setting it to a POSIX error. Callers should use GetLastError. */
1113 errno = 0;
1114 return -1;
1115 } else {
1116 /* Could not get attributes on open file. Fall back to
1117 reading the directory. */
1118 if (!attributes_from_dir_w(path, &info)) {
1119 /* Very strange. This should not fail now */
1120 errno = 0;
1121 return -1;
1122 }
1123 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001124 }
1125 code = attribute_data_to_stat(&info, result);
1126 if (code < 0)
1127 return code;
1128 /* Set IFEXEC if it is an .exe, .bat, ... */
1129 dot = wcsrchr(path, '.');
1130 if (dot) {
1131 if (_wcsicmp(dot, L".bat") == 0 ||
1132 _wcsicmp(dot, L".cmd") == 0 ||
1133 _wcsicmp(dot, L".exe") == 0 ||
1134 _wcsicmp(dot, L".com") == 0)
1135 result->st_mode |= 0111;
1136 }
1137 return code;
1138}
1139
1140static int
1141win32_fstat(int file_number, struct win32_stat *result)
1142{
1143 BY_HANDLE_FILE_INFORMATION info;
1144 HANDLE h;
1145 int type;
1146
1147 h = (HANDLE)_get_osfhandle(file_number);
1148
1149 /* Protocol violation: we explicitly clear errno, instead of
1150 setting it to a POSIX error. Callers should use GetLastError. */
1151 errno = 0;
1152
1153 if (h == INVALID_HANDLE_VALUE) {
1154 /* This is really a C library error (invalid file handle).
1155 We set the Win32 error to the closes one matching. */
1156 SetLastError(ERROR_INVALID_HANDLE);
1157 return -1;
1158 }
1159 memset(result, 0, sizeof(*result));
1160
1161 type = GetFileType(h);
1162 if (type == FILE_TYPE_UNKNOWN) {
1163 DWORD error = GetLastError();
1164 if (error != 0) {
1165 return -1;
1166 }
1167 /* else: valid but unknown file */
1168 }
1169
1170 if (type != FILE_TYPE_DISK) {
1171 if (type == FILE_TYPE_CHAR)
1172 result->st_mode = _S_IFCHR;
1173 else if (type == FILE_TYPE_PIPE)
1174 result->st_mode = _S_IFIFO;
1175 return 0;
1176 }
1177
1178 if (!GetFileInformationByHandle(h, &info)) {
1179 return -1;
1180 }
1181
1182 /* similar to stat() */
1183 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1184 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1185 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1186 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1187 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1188 /* specific to fstat() */
1189 result->st_nlink = info.nNumberOfLinks;
1190 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1191 return 0;
1192}
1193
1194#endif /* MS_WINDOWS */
1195
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001196PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001197"stat_result: Result from stat or lstat.\n\n\
1198This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001199 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001200or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1201\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001202Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1203or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001204\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001205See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001206
1207static PyStructSequence_Field stat_result_fields[] = {
1208 {"st_mode", "protection bits"},
1209 {"st_ino", "inode"},
1210 {"st_dev", "device"},
1211 {"st_nlink", "number of hard links"},
1212 {"st_uid", "user ID of owner"},
1213 {"st_gid", "group ID of owner"},
1214 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001215 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1216 {NULL, "integer time of last access"},
1217 {NULL, "integer time of last modification"},
1218 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001219 {"st_atime", "time of last access"},
1220 {"st_mtime", "time of last modification"},
1221 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001222#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001223 {"st_blksize", "blocksize for filesystem I/O"},
1224#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001225#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001226 {"st_blocks", "number of blocks allocated"},
1227#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001228#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001229 {"st_rdev", "device type (if inode device)"},
1230#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001231#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1232 {"st_flags", "user defined flags for file"},
1233#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001234#ifdef HAVE_STRUCT_STAT_ST_GEN
1235 {"st_gen", "generation number"},
1236#endif
1237#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1238 {"st_birthtime", "time of creation"},
1239#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001240 {0}
1241};
1242
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001243#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001244#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001245#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001246#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001247#endif
1248
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001249#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001250#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1251#else
1252#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1253#endif
1254
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001255#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001256#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1257#else
1258#define ST_RDEV_IDX ST_BLOCKS_IDX
1259#endif
1260
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001261#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1262#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1263#else
1264#define ST_FLAGS_IDX ST_RDEV_IDX
1265#endif
1266
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001267#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001268#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001269#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001270#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001271#endif
1272
1273#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1274#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1275#else
1276#define ST_BIRTHTIME_IDX ST_GEN_IDX
1277#endif
1278
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001279static PyStructSequence_Desc stat_result_desc = {
1280 "stat_result", /* name */
1281 stat_result__doc__, /* doc */
1282 stat_result_fields,
1283 10
1284};
1285
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001286PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001287"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1288This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001289 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001290or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001291\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001292See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001293
1294static PyStructSequence_Field statvfs_result_fields[] = {
1295 {"f_bsize", },
1296 {"f_frsize", },
1297 {"f_blocks", },
1298 {"f_bfree", },
1299 {"f_bavail", },
1300 {"f_files", },
1301 {"f_ffree", },
1302 {"f_favail", },
1303 {"f_flag", },
1304 {"f_namemax",},
1305 {0}
1306};
1307
1308static PyStructSequence_Desc statvfs_result_desc = {
1309 "statvfs_result", /* name */
1310 statvfs_result__doc__, /* doc */
1311 statvfs_result_fields,
1312 10
1313};
1314
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001315static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001316static PyTypeObject StatResultType;
1317static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001318static newfunc structseq_new;
1319
1320static PyObject *
1321statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1322{
1323 PyStructSequence *result;
1324 int i;
1325
1326 result = (PyStructSequence*)structseq_new(type, args, kwds);
1327 if (!result)
1328 return NULL;
1329 /* If we have been initialized from a tuple,
1330 st_?time might be set to None. Initialize it
1331 from the int slots. */
1332 for (i = 7; i <= 9; i++) {
1333 if (result->ob_item[i+3] == Py_None) {
1334 Py_DECREF(Py_None);
1335 Py_INCREF(result->ob_item[i]);
1336 result->ob_item[i+3] = result->ob_item[i];
1337 }
1338 }
1339 return (PyObject*)result;
1340}
1341
1342
1343
1344/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001345static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001346
1347PyDoc_STRVAR(stat_float_times__doc__,
1348"stat_float_times([newval]) -> oldval\n\n\
1349Determine whether os.[lf]stat represents time stamps as float objects.\n\
1350If newval is True, future calls to stat() return floats, if it is False,\n\
1351future calls return ints. \n\
1352If newval is omitted, return the current setting.\n");
1353
1354static PyObject*
1355stat_float_times(PyObject* self, PyObject *args)
1356{
1357 int newval = -1;
1358 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1359 return NULL;
1360 if (newval == -1)
1361 /* Return old value */
1362 return PyBool_FromLong(_stat_float_times);
1363 _stat_float_times = newval;
1364 Py_INCREF(Py_None);
1365 return Py_None;
1366}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001367
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001368static void
1369fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1370{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001371 PyObject *fval,*ival;
1372#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001373 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001374#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001375 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001376#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001377 if (!ival)
1378 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001379 if (_stat_float_times) {
1380 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1381 } else {
1382 fval = ival;
1383 Py_INCREF(fval);
1384 }
1385 PyStructSequence_SET_ITEM(v, index, ival);
1386 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001387}
1388
Tim Peters5aa91602002-01-30 05:46:57 +00001389/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001390 (used by posix_stat() and posix_fstat()) */
1391static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001392_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001393{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001394 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001395 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001396 if (v == NULL)
1397 return NULL;
1398
Christian Heimes217cfd12007-12-02 14:31:20 +00001399 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001400#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001401 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001402 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001403#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001404 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001405#endif
1406#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001407 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001408 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001409#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001410 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001411#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00001412 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1413 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1414 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001415#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001416 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001417 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001418#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001419 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001420#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001421
Martin v. Löwis14694662006-02-03 12:54:16 +00001422#if defined(HAVE_STAT_TV_NSEC)
1423 ansec = st->st_atim.tv_nsec;
1424 mnsec = st->st_mtim.tv_nsec;
1425 cnsec = st->st_ctim.tv_nsec;
1426#elif defined(HAVE_STAT_TV_NSEC2)
1427 ansec = st->st_atimespec.tv_nsec;
1428 mnsec = st->st_mtimespec.tv_nsec;
1429 cnsec = st->st_ctimespec.tv_nsec;
1430#elif defined(HAVE_STAT_NSEC)
1431 ansec = st->st_atime_nsec;
1432 mnsec = st->st_mtime_nsec;
1433 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001434#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001435 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001436#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001437 fill_time(v, 7, st->st_atime, ansec);
1438 fill_time(v, 8, st->st_mtime, mnsec);
1439 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001440
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001441#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001442 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001443 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001444#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001445#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001446 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001447 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001448#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001449#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001450 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001451 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001452#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001453#ifdef HAVE_STRUCT_STAT_ST_GEN
1454 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001455 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001456#endif
1457#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1458 {
1459 PyObject *val;
1460 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001461 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001462#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001463 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001464#else
1465 bnsec = 0;
1466#endif
1467 if (_stat_float_times) {
1468 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1469 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001470 val = PyLong_FromLong((long)bsec);
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001471 }
1472 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1473 val);
1474 }
1475#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001476#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1477 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001478 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001479#endif
Fred Drake699f3522000-06-29 21:12:41 +00001480
1481 if (PyErr_Occurred()) {
1482 Py_DECREF(v);
1483 return NULL;
1484 }
1485
1486 return v;
1487}
1488
Martin v. Löwisd8948722004-06-02 09:57:56 +00001489#ifdef MS_WINDOWS
1490
1491/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1492 where / can be used in place of \ and the trailing slash is optional.
1493 Both SERVER and SHARE must have at least one character.
1494*/
1495
1496#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1497#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001498#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001499#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001500#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001501
Tim Peters4ad82172004-08-30 17:02:04 +00001502static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001503IsUNCRootA(char *path, int pathlen)
1504{
1505 #define ISSLASH ISSLASHA
1506
1507 int i, share;
1508
1509 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1510 /* minimum UNCRoot is \\x\y */
1511 return FALSE;
1512 for (i = 2; i < pathlen ; i++)
1513 if (ISSLASH(path[i])) break;
1514 if (i == 2 || i == pathlen)
1515 /* do not allow \\\SHARE or \\SERVER */
1516 return FALSE;
1517 share = i+1;
1518 for (i = share; i < pathlen; i++)
1519 if (ISSLASH(path[i])) break;
1520 return (i != share && (i == pathlen || i == pathlen-1));
1521
1522 #undef ISSLASH
1523}
1524
1525#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001526static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001527IsUNCRootW(Py_UNICODE *path, int pathlen)
1528{
1529 #define ISSLASH ISSLASHW
1530
1531 int i, share;
1532
1533 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1534 /* minimum UNCRoot is \\x\y */
1535 return FALSE;
1536 for (i = 2; i < pathlen ; i++)
1537 if (ISSLASH(path[i])) break;
1538 if (i == 2 || i == pathlen)
1539 /* do not allow \\\SHARE or \\SERVER */
1540 return FALSE;
1541 share = i+1;
1542 for (i = share; i < pathlen; i++)
1543 if (ISSLASH(path[i])) break;
1544 return (i != share && (i == pathlen || i == pathlen-1));
1545
1546 #undef ISSLASH
1547}
1548#endif /* Py_WIN_WIDE_FILENAMES */
1549#endif /* MS_WINDOWS */
1550
Barry Warsaw53699e91996-12-10 23:23:01 +00001551static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001552posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001553 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001554#ifdef __VMS
1555 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1556#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001557 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001558#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001559 char *wformat,
1560 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001561{
Fred Drake699f3522000-06-29 21:12:41 +00001562 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001563 char *path = NULL; /* pass this to stat; do not free() it */
1564 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001565 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001566 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001567
1568#ifdef Py_WIN_WIDE_FILENAMES
1569 /* If on wide-character-capable OS see if argument
1570 is Unicode and if so use wide API. */
1571 if (unicode_file_names()) {
1572 PyUnicodeObject *po;
1573 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001574 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1575
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001576 Py_BEGIN_ALLOW_THREADS
1577 /* PyUnicode_AS_UNICODE result OK without
1578 thread lock as it is a simple dereference. */
1579 res = wstatfunc(wpath, &st);
1580 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001581
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001582 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001583 return win32_error_unicode("stat", wpath);
1584 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001585 }
1586 /* Drop the argument parsing error as narrow strings
1587 are also valid. */
1588 PyErr_Clear();
1589 }
1590#endif
1591
Tim Peters5aa91602002-01-30 05:46:57 +00001592 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001593 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001594 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001595 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001596
Barry Warsaw53699e91996-12-10 23:23:01 +00001597 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001598 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001599 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001600
1601 if (res != 0) {
1602#ifdef MS_WINDOWS
1603 result = win32_error("stat", pathfree);
1604#else
1605 result = posix_error_with_filename(pathfree);
1606#endif
1607 }
1608 else
1609 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001610
Tim Peters500bd032001-12-19 19:05:01 +00001611 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001612 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001613}
1614
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001615/* POSIX methods */
1616
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001617PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001618"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001619Use the real uid/gid to test for access to a path. Note that most\n\
1620operations will use the effective uid/gid, therefore this routine can\n\
1621be used in a suid/sgid environment to test if the invoking user has the\n\
1622specified access to the path. The mode argument can be F_OK to test\n\
1623existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001624
1625static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001626posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001627{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001628 char *path;
1629 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001630
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001631#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001632 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001633 if (unicode_file_names()) {
1634 PyUnicodeObject *po;
1635 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1636 Py_BEGIN_ALLOW_THREADS
1637 /* PyUnicode_AS_UNICODE OK without thread lock as
1638 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001639 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001640 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001641 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001642 }
1643 /* Drop the argument parsing error as narrow strings
1644 are also valid. */
1645 PyErr_Clear();
1646 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001647 if (!PyArg_ParseTuple(args, "eti:access",
1648 Py_FileSystemDefaultEncoding, &path, &mode))
1649 return 0;
1650 Py_BEGIN_ALLOW_THREADS
1651 attr = GetFileAttributesA(path);
1652 Py_END_ALLOW_THREADS
1653 PyMem_Free(path);
1654finish:
1655 if (attr == 0xFFFFFFFF)
1656 /* File does not exist, or cannot read attributes */
1657 return PyBool_FromLong(0);
1658 /* Access is possible if either write access wasn't requested, or
Guido van Rossumb00324f2007-12-04 01:13:14 +00001659 the file isn't read-only, or if it's a directory, as there are
1660 no read-only directories on Windows. */
1661 return PyBool_FromLong(!(mode & 2)
1662 || !(attr & FILE_ATTRIBUTE_READONLY)
1663 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001664#else
1665 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001666 if (!PyArg_ParseTuple(args, "eti:access",
1667 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001668 return NULL;
1669 Py_BEGIN_ALLOW_THREADS
1670 res = access(path, mode);
1671 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001672 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001673 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001674#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001675}
1676
Guido van Rossumd371ff11999-01-25 16:12:23 +00001677#ifndef F_OK
1678#define F_OK 0
1679#endif
1680#ifndef R_OK
1681#define R_OK 4
1682#endif
1683#ifndef W_OK
1684#define W_OK 2
1685#endif
1686#ifndef X_OK
1687#define X_OK 1
1688#endif
1689
1690#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001691PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001692"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001693Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001694
1695static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001696posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001697{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001698 int id;
1699 char *ret;
1700
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001701 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001702 return NULL;
1703
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001704#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001705 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001706 if (id == 0) {
1707 ret = ttyname();
1708 }
1709 else {
1710 ret = NULL;
1711 }
1712#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001713 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001714#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001715 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001716 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001717 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001718}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001719#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001720
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001721#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001722PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001723"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001724Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001725
1726static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001727posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001728{
1729 char *ret;
1730 char buffer[L_ctermid];
1731
Greg Wardb48bc172000-03-01 21:51:56 +00001732#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001733 ret = ctermid_r(buffer);
1734#else
1735 ret = ctermid(buffer);
1736#endif
1737 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001738 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001739 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001740}
1741#endif
1742
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001743PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001744"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001745Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001746
Barry Warsaw53699e91996-12-10 23:23:01 +00001747static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001748posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001749{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001750#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00001751 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001752#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001753 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001754#elif defined(__VMS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001755 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001756#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00001757 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001758#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001759}
1760
Fred Drake4d1e64b2002-04-15 19:40:07 +00001761#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001762PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001763"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001764Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001765opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001766
1767static PyObject *
1768posix_fchdir(PyObject *self, PyObject *fdobj)
1769{
1770 return posix_fildes(fdobj, fchdir);
1771}
1772#endif /* HAVE_FCHDIR */
1773
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001774
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001775PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001776"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001777Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001778
Barry Warsaw53699e91996-12-10 23:23:01 +00001779static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001780posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001781{
Mark Hammondef8b6542001-05-13 08:04:26 +00001782 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001783 int i;
1784 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001785#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001786 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001787 if (unicode_file_names()) {
1788 PyUnicodeObject *po;
1789 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1790 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001791 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1792 if (attr != 0xFFFFFFFF) {
1793 if (i & _S_IWRITE)
1794 attr &= ~FILE_ATTRIBUTE_READONLY;
1795 else
1796 attr |= FILE_ATTRIBUTE_READONLY;
1797 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1798 }
1799 else
1800 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001801 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001802 if (!res)
1803 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001804 PyUnicode_AS_UNICODE(po));
1805 Py_INCREF(Py_None);
1806 return Py_None;
1807 }
1808 /* Drop the argument parsing error as narrow strings
1809 are also valid. */
1810 PyErr_Clear();
1811 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001812 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1813 &path, &i))
1814 return NULL;
1815 Py_BEGIN_ALLOW_THREADS
1816 attr = GetFileAttributesA(path);
1817 if (attr != 0xFFFFFFFF) {
1818 if (i & _S_IWRITE)
1819 attr &= ~FILE_ATTRIBUTE_READONLY;
1820 else
1821 attr |= FILE_ATTRIBUTE_READONLY;
1822 res = SetFileAttributesA(path, attr);
1823 }
1824 else
1825 res = 0;
1826 Py_END_ALLOW_THREADS
1827 if (!res) {
1828 win32_error("chmod", path);
1829 PyMem_Free(path);
1830 return NULL;
1831 }
1832 PyMem_Free(path);
1833 Py_INCREF(Py_None);
1834 return Py_None;
1835#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001836 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001837 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001838 return NULL;
1839 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001840 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001841 Py_END_ALLOW_THREADS
1842 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001843 return posix_error_with_allocated_filename(path);
1844 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001845 Py_INCREF(Py_None);
1846 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001847#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001848}
1849
Christian Heimes4e30a842007-11-30 22:12:06 +00001850#ifdef HAVE_FCHMOD
1851PyDoc_STRVAR(posix_fchmod__doc__,
1852"fchmod(fd, mode)\n\n\
1853Change the access permissions of the file given by file\n\
1854descriptor fd.");
1855
1856static PyObject *
1857posix_fchmod(PyObject *self, PyObject *args)
1858{
1859 int fd, mode, res;
1860 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1861 return NULL;
1862 Py_BEGIN_ALLOW_THREADS
1863 res = fchmod(fd, mode);
1864 Py_END_ALLOW_THREADS
1865 if (res < 0)
1866 return posix_error();
1867 Py_RETURN_NONE;
1868}
1869#endif /* HAVE_FCHMOD */
1870
1871#ifdef HAVE_LCHMOD
1872PyDoc_STRVAR(posix_lchmod__doc__,
1873"lchmod(path, mode)\n\n\
1874Change the access permissions of a file. If path is a symlink, this\n\
1875affects the link itself rather than the target.");
1876
1877static PyObject *
1878posix_lchmod(PyObject *self, PyObject *args)
1879{
1880 char *path = NULL;
1881 int i;
1882 int res;
1883 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1884 &path, &i))
1885 return NULL;
1886 Py_BEGIN_ALLOW_THREADS
1887 res = lchmod(path, i);
1888 Py_END_ALLOW_THREADS
1889 if (res < 0)
1890 return posix_error_with_allocated_filename(path);
1891 PyMem_Free(path);
1892 Py_RETURN_NONE;
1893}
1894#endif /* HAVE_LCHMOD */
1895
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001896
Thomas Wouterscf297e42007-02-23 15:07:44 +00001897#ifdef HAVE_CHFLAGS
1898PyDoc_STRVAR(posix_chflags__doc__,
1899"chflags(path, flags)\n\n\
1900Set file flags.");
1901
1902static PyObject *
1903posix_chflags(PyObject *self, PyObject *args)
1904{
1905 char *path;
1906 unsigned long flags;
1907 int res;
1908 if (!PyArg_ParseTuple(args, "etk:chflags",
1909 Py_FileSystemDefaultEncoding, &path, &flags))
1910 return NULL;
1911 Py_BEGIN_ALLOW_THREADS
1912 res = chflags(path, flags);
1913 Py_END_ALLOW_THREADS
1914 if (res < 0)
1915 return posix_error_with_allocated_filename(path);
1916 PyMem_Free(path);
1917 Py_INCREF(Py_None);
1918 return Py_None;
1919}
1920#endif /* HAVE_CHFLAGS */
1921
1922#ifdef HAVE_LCHFLAGS
1923PyDoc_STRVAR(posix_lchflags__doc__,
1924"lchflags(path, flags)\n\n\
1925Set file flags.\n\
1926This function will not follow symbolic links.");
1927
1928static PyObject *
1929posix_lchflags(PyObject *self, PyObject *args)
1930{
1931 char *path;
1932 unsigned long flags;
1933 int res;
1934 if (!PyArg_ParseTuple(args, "etk:lchflags",
1935 Py_FileSystemDefaultEncoding, &path, &flags))
1936 return NULL;
1937 Py_BEGIN_ALLOW_THREADS
1938 res = lchflags(path, flags);
1939 Py_END_ALLOW_THREADS
1940 if (res < 0)
1941 return posix_error_with_allocated_filename(path);
1942 PyMem_Free(path);
1943 Py_INCREF(Py_None);
1944 return Py_None;
1945}
1946#endif /* HAVE_LCHFLAGS */
1947
Martin v. Löwis244edc82001-10-04 22:44:26 +00001948#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001949PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001950"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001951Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001952
1953static PyObject *
1954posix_chroot(PyObject *self, PyObject *args)
1955{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001956 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001957}
1958#endif
1959
Guido van Rossum21142a01999-01-08 21:05:37 +00001960#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001961PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001962"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001963force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001964
1965static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001966posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001967{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001968 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001969}
1970#endif /* HAVE_FSYNC */
1971
1972#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001973
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001974#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001975extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1976#endif
1977
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001978PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001979"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001980force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001981 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001982
1983static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001984posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001985{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001986 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001987}
1988#endif /* HAVE_FDATASYNC */
1989
1990
Fredrik Lundh10723342000-07-10 16:38:09 +00001991#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001992PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001993"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001994Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001995
Barry Warsaw53699e91996-12-10 23:23:01 +00001996static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001997posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001998{
Mark Hammondef8b6542001-05-13 08:04:26 +00001999 char *path = NULL;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002000 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00002001 int res;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002002 if (!PyArg_ParseTuple(args, "etll:chown",
Tim Peters5aa91602002-01-30 05:46:57 +00002003 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00002004 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00002005 return NULL;
2006 Py_BEGIN_ALLOW_THREADS
2007 res = chown(path, (uid_t) uid, (gid_t) gid);
2008 Py_END_ALLOW_THREADS
2009 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002010 return posix_error_with_allocated_filename(path);
2011 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00002012 Py_INCREF(Py_None);
2013 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002014}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002015#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002016
Christian Heimes4e30a842007-11-30 22:12:06 +00002017#ifdef HAVE_FCHOWN
2018PyDoc_STRVAR(posix_fchown__doc__,
2019"fchown(fd, uid, gid)\n\n\
2020Change the owner and group id of the file given by file descriptor\n\
2021fd to the numeric uid and gid.");
2022
2023static PyObject *
2024posix_fchown(PyObject *self, PyObject *args)
2025{
2026 int fd, uid, gid;
2027 int res;
2028 if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid))
2029 return NULL;
2030 Py_BEGIN_ALLOW_THREADS
2031 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2032 Py_END_ALLOW_THREADS
2033 if (res < 0)
2034 return posix_error();
2035 Py_RETURN_NONE;
2036}
2037#endif /* HAVE_FCHOWN */
2038
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002039#ifdef HAVE_LCHOWN
2040PyDoc_STRVAR(posix_lchown__doc__,
2041"lchown(path, uid, gid)\n\n\
2042Change the owner and group id of path to the numeric uid and gid.\n\
2043This function will not follow symbolic links.");
2044
2045static PyObject *
2046posix_lchown(PyObject *self, PyObject *args)
2047{
2048 char *path = NULL;
2049 int uid, gid;
2050 int res;
2051 if (!PyArg_ParseTuple(args, "etii:lchown",
2052 Py_FileSystemDefaultEncoding, &path,
2053 &uid, &gid))
2054 return NULL;
2055 Py_BEGIN_ALLOW_THREADS
2056 res = lchown(path, (uid_t) uid, (gid_t) gid);
2057 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00002058 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002059 return posix_error_with_allocated_filename(path);
2060 PyMem_Free(path);
2061 Py_INCREF(Py_None);
2062 return Py_None;
2063}
2064#endif /* HAVE_LCHOWN */
2065
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002066
Guido van Rossum36bc6801995-06-14 22:54:23 +00002067#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002068static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002069posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002070{
2071 char buf[1026];
2072 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002073
2074#ifdef Py_WIN_WIDE_FILENAMES
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002075 if (!use_bytes && unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002076 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00002077 wchar_t *wbuf2 = wbuf;
2078 PyObject *resobj;
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002079 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002080 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002081 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2082 /* If the buffer is large enough, len does not include the
2083 terminating \0. If the buffer is too small, len includes
2084 the space needed for the terminator. */
2085 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2086 wbuf2 = malloc(len * sizeof(wchar_t));
2087 if (wbuf2)
2088 len = GetCurrentDirectoryW(len, wbuf2);
2089 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002090 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002091 if (!wbuf2) {
2092 PyErr_NoMemory();
2093 return NULL;
2094 }
2095 if (!len) {
2096 if (wbuf2 != wbuf) free(wbuf2);
2097 return win32_error("getcwdu", NULL);
2098 }
2099 resobj = PyUnicode_FromWideChar(wbuf2, len);
2100 if (wbuf2 != wbuf) free(wbuf2);
2101 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002102 }
2103#endif
2104
2105 Py_BEGIN_ALLOW_THREADS
2106#if defined(PYOS_OS2) && defined(PYCC_GCC)
2107 res = _getcwd2(buf, sizeof buf);
2108#else
2109 res = getcwd(buf, sizeof buf);
2110#endif
2111 Py_END_ALLOW_THREADS
2112 if (res == NULL)
2113 return posix_error();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002114 if (use_bytes)
2115 return PyBytes_FromStringAndSize(buf, strlen(buf));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002116 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
2117}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002118
2119PyDoc_STRVAR(posix_getcwd__doc__,
2120"getcwd() -> path\n\n\
2121Return a unicode string representing the current working directory.");
2122
2123static PyObject *
2124posix_getcwd_unicode(PyObject *self)
2125{
2126 return posix_getcwd(0);
2127}
2128
2129PyDoc_STRVAR(posix_getcwdb__doc__,
2130"getcwdb() -> path\n\n\
2131Return a bytes string representing the current working directory.");
2132
2133static PyObject *
2134posix_getcwd_bytes(PyObject *self)
2135{
2136 return posix_getcwd(1);
2137}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002138#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002139
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002140
Guido van Rossumb6775db1994-08-01 11:34:53 +00002141#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002142PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002143"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002144Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002145
Barry Warsaw53699e91996-12-10 23:23:01 +00002146static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002147posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002148{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002149 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002150}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002151#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002152
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002153
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002154PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002155"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002156Return a list containing the names of the entries in the directory.\n\
2157\n\
2158 path: path of directory to list\n\
2159\n\
2160The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002161entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002162
Barry Warsaw53699e91996-12-10 23:23:01 +00002163static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002164posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002165{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002166 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002167 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002168#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002169
Barry Warsaw53699e91996-12-10 23:23:01 +00002170 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002171 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002172 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002173 WIN32_FIND_DATA FileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002174 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002175 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002176 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002177
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002178#ifdef Py_WIN_WIDE_FILENAMES
2179 /* If on wide-character-capable OS see if argument
2180 is Unicode and if so use wide API. */
2181 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002182 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002183 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2184 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002185 Py_UNICODE *wnamebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002186 /* Overallocate for \\*.*\0 */
2187 len = PyUnicode_GET_SIZE(po);
2188 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2189 if (!wnamebuf) {
2190 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002191 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002192 }
2193 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002194 if (len > 0) {
2195 Py_UNICODE wch = wnamebuf[len-1];
2196 if (wch != L'/' && wch != L'\\' && wch != L':')
2197 wnamebuf[len++] = L'\\';
2198 wcscpy(wnamebuf + len, L"*.*");
2199 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002200 if ((d = PyList_New(0)) == NULL) {
2201 free(wnamebuf);
2202 return NULL;
2203 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002204 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2205 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002206 int error = GetLastError();
2207 if (error == ERROR_FILE_NOT_FOUND) {
2208 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002209 return d;
2210 }
2211 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002212 win32_error_unicode("FindFirstFileW", wnamebuf);
2213 free(wnamebuf);
2214 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002215 }
2216 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002217 /* Skip over . and .. */
2218 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2219 wcscmp(wFileData.cFileName, L"..") != 0) {
2220 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2221 if (v == NULL) {
2222 Py_DECREF(d);
2223 d = NULL;
2224 break;
2225 }
2226 if (PyList_Append(d, v) != 0) {
2227 Py_DECREF(v);
2228 Py_DECREF(d);
2229 d = NULL;
2230 break;
2231 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002232 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002233 }
Georg Brandl622927b2006-03-07 12:48:03 +00002234 Py_BEGIN_ALLOW_THREADS
2235 result = FindNextFileW(hFindFile, &wFileData);
2236 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002237 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2238 it got to the end of the directory. */
2239 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2240 Py_DECREF(d);
2241 win32_error_unicode("FindNextFileW", wnamebuf);
2242 FindClose(hFindFile);
2243 free(wnamebuf);
2244 return NULL;
2245 }
Georg Brandl622927b2006-03-07 12:48:03 +00002246 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002247
2248 if (FindClose(hFindFile) == FALSE) {
2249 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002250 win32_error_unicode("FindClose", wnamebuf);
2251 free(wnamebuf);
2252 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002253 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002254 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002255 return d;
2256 }
2257 /* Drop the argument parsing error as narrow strings
2258 are also valid. */
2259 PyErr_Clear();
2260 }
2261#endif
2262
Tim Peters5aa91602002-01-30 05:46:57 +00002263 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002264 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002265 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002266 if (len > 0) {
2267 char ch = namebuf[len-1];
2268 if (ch != SEP && ch != ALTSEP && ch != ':')
2269 namebuf[len++] = '/';
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002270 strcpy(namebuf + len, "*.*");
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002271 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002272
Barry Warsaw53699e91996-12-10 23:23:01 +00002273 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002274 return NULL;
2275
2276 hFindFile = FindFirstFile(namebuf, &FileData);
2277 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002278 int error = GetLastError();
2279 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002280 return d;
2281 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002282 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002283 }
2284 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002285 /* Skip over . and .. */
2286 if (strcmp(FileData.cFileName, ".") != 0 &&
2287 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002288 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002289 if (v == NULL) {
2290 Py_DECREF(d);
2291 d = NULL;
2292 break;
2293 }
2294 if (PyList_Append(d, v) != 0) {
2295 Py_DECREF(v);
2296 Py_DECREF(d);
2297 d = NULL;
2298 break;
2299 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002300 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002301 }
Georg Brandl622927b2006-03-07 12:48:03 +00002302 Py_BEGIN_ALLOW_THREADS
2303 result = FindNextFile(hFindFile, &FileData);
2304 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002305 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2306 it got to the end of the directory. */
2307 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2308 Py_DECREF(d);
2309 win32_error("FindNextFile", namebuf);
2310 FindClose(hFindFile);
2311 return NULL;
2312 }
Georg Brandl622927b2006-03-07 12:48:03 +00002313 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002314
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002315 if (FindClose(hFindFile) == FALSE) {
2316 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002317 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002318 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002319
2320 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002321
Tim Peters0bb44a42000-09-15 07:44:49 +00002322#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002323
2324#ifndef MAX_PATH
2325#define MAX_PATH CCHMAXPATH
2326#endif
2327 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002328 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002329 PyObject *d, *v;
2330 char namebuf[MAX_PATH+5];
2331 HDIR hdir = 1;
2332 ULONG srchcnt = 1;
2333 FILEFINDBUF3 ep;
2334 APIRET rc;
2335
Alexandre Vassalotti70a23712007-10-14 02:05:51 +00002336 if (!PyArg_ParseTuple(args, "et#:listdir",
2337 Py_FileSystemDefaultEncoding, &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002338 return NULL;
2339 if (len >= MAX_PATH) {
Neal Norwitz6c913782007-10-14 03:23:09 +00002340 PyMem_Free(name);
2341 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002342 return NULL;
2343 }
2344 strcpy(namebuf, name);
2345 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002346 if (*pt == ALTSEP)
2347 *pt = SEP;
2348 if (namebuf[len-1] != SEP)
2349 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002350 strcpy(namebuf + len, "*.*");
2351
Neal Norwitz6c913782007-10-14 03:23:09 +00002352 if ((d = PyList_New(0)) == NULL) {
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002353 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002354 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002355 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002356
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002357 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2358 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002359 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002360 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2361 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2362 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002363
2364 if (rc != NO_ERROR) {
2365 errno = ENOENT;
Neal Norwitz6c913782007-10-14 03:23:09 +00002366 return posix_error_with_allocated_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002367 }
2368
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002369 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002370 do {
2371 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002372 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002373 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002374
2375 strcpy(namebuf, ep.achName);
2376
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002377 /* Leave Case of Name Alone -- In Native Form */
2378 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002379
Christian Heimes72b710a2008-05-26 13:28:38 +00002380 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002381 if (v == NULL) {
2382 Py_DECREF(d);
2383 d = NULL;
2384 break;
2385 }
2386 if (PyList_Append(d, v) != 0) {
2387 Py_DECREF(v);
2388 Py_DECREF(d);
2389 d = NULL;
2390 break;
2391 }
2392 Py_DECREF(v);
2393 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2394 }
2395
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002396 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002397 return d;
2398#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002399
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002400 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002401 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002402 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002403 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002404 int arg_is_unicode = 1;
2405
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002406 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002407 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2408 arg_is_unicode = 0;
2409 PyErr_Clear();
2410 }
2411 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002412 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002413 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002414 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002415 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002416 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002417 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002418 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002419 return NULL;
2420 }
Georg Brandl622927b2006-03-07 12:48:03 +00002421 for (;;) {
Georg Brandl3dbca812008-07-23 16:10:53 +00002422 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002423 Py_BEGIN_ALLOW_THREADS
2424 ep = readdir(dirp);
2425 Py_END_ALLOW_THREADS
Georg Brandl3dbca812008-07-23 16:10:53 +00002426 if (ep == NULL) {
2427 if (errno == 0) {
2428 break;
2429 } else {
2430 closedir(dirp);
2431 Py_DECREF(d);
2432 return posix_error_with_allocated_filename(name);
2433 }
2434 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002435 if (ep->d_name[0] == '.' &&
2436 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002437 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002438 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002439 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002440 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002441 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002442 d = NULL;
2443 break;
2444 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002445 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002446 PyObject *w;
2447
2448 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002449 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002450 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002451 if (w != NULL) {
2452 Py_DECREF(v);
2453 v = w;
2454 }
2455 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002456 /* Ignore undecodable filenames, as discussed
2457 * in issue 3187. To include these,
2458 * use getcwdb(). */
Just van Rossum6a421832003-03-04 19:30:44 +00002459 PyErr_Clear();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002460 Py_DECREF(v);
2461 continue;
Just van Rossum46c97842003-02-25 21:42:15 +00002462 }
Just van Rossum46c97842003-02-25 21:42:15 +00002463 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002464 if (PyList_Append(d, v) != 0) {
2465 Py_DECREF(v);
2466 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002467 d = NULL;
2468 break;
2469 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002470 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002471 }
2472 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002473 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002474
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002475 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002476
Tim Peters0bb44a42000-09-15 07:44:49 +00002477#endif /* which OS */
2478} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002479
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002480#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002481/* A helper function for abspath on win32 */
2482static PyObject *
2483posix__getfullpathname(PyObject *self, PyObject *args)
2484{
Mark Dickinson934896d2009-02-21 20:59:32 +00002485 /* assume encoded strings won't more than double no of chars */
Mark Hammondef8b6542001-05-13 08:04:26 +00002486 char inbuf[MAX_PATH*2];
2487 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002488 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002489 char outbuf[MAX_PATH*2];
2490 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002491#ifdef Py_WIN_WIDE_FILENAMES
2492 if (unicode_file_names()) {
2493 PyUnicodeObject *po;
2494 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
Benjamin Petersonf608c612008-11-16 18:33:53 +00002495 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2496 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002497 Py_UNICODE *wtemp;
Benjamin Petersonf608c612008-11-16 18:33:53 +00002498 DWORD result;
2499 PyObject *v;
2500 result = GetFullPathNameW(wpath,
2501 sizeof(woutbuf)/sizeof(woutbuf[0]),
2502 woutbuf, &wtemp);
2503 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2504 woutbufp = malloc(result * sizeof(Py_UNICODE));
2505 if (!woutbufp)
2506 return PyErr_NoMemory();
2507 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2508 }
2509 if (result)
2510 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2511 else
2512 v = win32_error_unicode("GetFullPathNameW", wpath);
2513 if (woutbufp != woutbuf)
2514 free(woutbufp);
2515 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002516 }
2517 /* Drop the argument parsing error as narrow strings
2518 are also valid. */
2519 PyErr_Clear();
2520 }
2521#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002522 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2523 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002524 &insize))
2525 return NULL;
2526 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2527 outbuf, &temp))
2528 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002529 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2530 return PyUnicode_Decode(outbuf, strlen(outbuf),
2531 Py_FileSystemDefaultEncoding, NULL);
2532 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002533 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002534} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002535#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002536
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002537PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002538"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002539Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002540
Barry Warsaw53699e91996-12-10 23:23:01 +00002541static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002542posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002543{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002544 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002545 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002546 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002547
2548#ifdef Py_WIN_WIDE_FILENAMES
2549 if (unicode_file_names()) {
2550 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002551 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002552 Py_BEGIN_ALLOW_THREADS
2553 /* PyUnicode_AS_UNICODE OK without thread lock as
2554 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002555 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002556 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002557 if (!res)
2558 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002559 Py_INCREF(Py_None);
2560 return Py_None;
2561 }
2562 /* Drop the argument parsing error as narrow strings
2563 are also valid. */
2564 PyErr_Clear();
2565 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002566 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2567 Py_FileSystemDefaultEncoding, &path, &mode))
2568 return NULL;
2569 Py_BEGIN_ALLOW_THREADS
2570 /* PyUnicode_AS_UNICODE OK without thread lock as
2571 it is a simple dereference. */
2572 res = CreateDirectoryA(path, NULL);
2573 Py_END_ALLOW_THREADS
2574 if (!res) {
2575 win32_error("mkdir", path);
2576 PyMem_Free(path);
2577 return NULL;
2578 }
2579 PyMem_Free(path);
2580 Py_INCREF(Py_None);
2581 return Py_None;
2582#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002583
Tim Peters5aa91602002-01-30 05:46:57 +00002584 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002585 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002586 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002587 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002588#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002589 res = mkdir(path);
2590#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002591 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002592#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002593 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002594 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002595 return posix_error_with_allocated_filename(path);
2596 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002597 Py_INCREF(Py_None);
2598 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002599#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002600}
2601
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002602
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002603/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2604#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002605#include <sys/resource.h>
2606#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002607
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002608
2609#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002610PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002611"nice(inc) -> new_priority\n\n\
2612Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002613
Barry Warsaw53699e91996-12-10 23:23:01 +00002614static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002615posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002616{
2617 int increment, value;
2618
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002619 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002620 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002621
2622 /* There are two flavours of 'nice': one that returns the new
2623 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002624 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2625 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002626
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002627 If we are of the nice family that returns the new priority, we
2628 need to clear errno before the call, and check if errno is filled
2629 before calling posix_error() on a returnvalue of -1, because the
2630 -1 may be the actual new priority! */
2631
2632 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002633 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002634#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002635 if (value == 0)
2636 value = getpriority(PRIO_PROCESS, 0);
2637#endif
2638 if (value == -1 && errno != 0)
2639 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002640 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002641 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002642}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002643#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002644
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002645PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002646"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002647Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002648
Barry Warsaw53699e91996-12-10 23:23:01 +00002649static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002650posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002651{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002652#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002653 PyObject *o1, *o2;
2654 char *p1, *p2;
2655 BOOL result;
2656 if (unicode_file_names()) {
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002657 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2658 goto error;
2659 if (!convert_to_unicode(&o1))
2660 goto error;
2661 if (!convert_to_unicode(&o2)) {
2662 Py_DECREF(o1);
2663 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002664 }
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002665 Py_BEGIN_ALLOW_THREADS
2666 result = MoveFileW(PyUnicode_AsUnicode(o1),
2667 PyUnicode_AsUnicode(o2));
2668 Py_END_ALLOW_THREADS
2669 Py_DECREF(o1);
2670 Py_DECREF(o2);
2671 if (!result)
2672 return win32_error("rename", NULL);
2673 Py_INCREF(Py_None);
2674 return Py_None;
2675error:
2676 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002677 }
2678 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2679 return NULL;
2680 Py_BEGIN_ALLOW_THREADS
2681 result = MoveFileA(p1, p2);
2682 Py_END_ALLOW_THREADS
2683 if (!result)
2684 return win32_error("rename", NULL);
2685 Py_INCREF(Py_None);
2686 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002687#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002688 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002689#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002690}
2691
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002692
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002693PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002694"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002695Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002696
Barry Warsaw53699e91996-12-10 23:23:01 +00002697static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002698posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002699{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002700#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002701 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002702#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002703 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002704#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002705}
2706
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002707
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002708PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002709"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002710Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002711
Barry Warsaw53699e91996-12-10 23:23:01 +00002712static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002713posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002714{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002715#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002716 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002717#else
2718 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2719#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002720}
2721
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002722
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002723#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002724PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002725"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002726Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002727
Barry Warsaw53699e91996-12-10 23:23:01 +00002728static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002729posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002730{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002731 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002732#ifdef MS_WINDOWS
2733 wchar_t *command;
2734 if (!PyArg_ParseTuple(args, "u:system", &command))
2735 return NULL;
2736#else
2737 char *command;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002738 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002739 return NULL;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002740#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002741 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002742#ifdef MS_WINDOWS
2743 sts = _wsystem(command);
2744#else
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002745 sts = system(command);
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002746#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002747 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +00002748 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002749}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002750#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002751
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002752
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002753PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002754"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002755Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002756
Barry Warsaw53699e91996-12-10 23:23:01 +00002757static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002758posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002759{
2760 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002761 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002762 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002763 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002764 if (i < 0)
2765 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002766 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002767}
2768
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002769
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002770PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002771"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002772Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002773
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002774PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002775"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002776Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002777
Barry Warsaw53699e91996-12-10 23:23:01 +00002778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002779posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002780{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002781#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002782 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002783#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002784 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002785#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002786}
2787
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002788
Guido van Rossumb6775db1994-08-01 11:34:53 +00002789#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002790PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002791"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002792Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002793
Barry Warsaw53699e91996-12-10 23:23:01 +00002794static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002795posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002796{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002797 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002798 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002799
Barry Warsaw53699e91996-12-10 23:23:01 +00002800 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002801 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002802 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002803 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002804 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002805 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002806 u.sysname,
2807 u.nodename,
2808 u.release,
2809 u.version,
2810 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002811}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002812#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002813
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002814static int
2815extract_time(PyObject *t, long* sec, long* usec)
2816{
2817 long intval;
2818 if (PyFloat_Check(t)) {
2819 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002820 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002821 if (!intobj)
2822 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002823 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002824 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002825 if (intval == -1 && PyErr_Occurred())
2826 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002827 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002828 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002829 if (*usec < 0)
2830 /* If rounding gave us a negative number,
2831 truncate. */
2832 *usec = 0;
2833 return 0;
2834 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002835 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002836 if (intval == -1 && PyErr_Occurred())
2837 return -1;
2838 *sec = intval;
2839 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002840 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002841}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002842
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002843PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002844"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002845utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002846Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002847second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002848
Barry Warsaw53699e91996-12-10 23:23:01 +00002849static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002850posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002851{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002852#ifdef Py_WIN_WIDE_FILENAMES
2853 PyObject *arg;
2854 PyUnicodeObject *obwpath;
2855 wchar_t *wpath = NULL;
2856 char *apath = NULL;
2857 HANDLE hFile;
2858 long atimesec, mtimesec, ausec, musec;
2859 FILETIME atime, mtime;
2860 PyObject *result = NULL;
2861
2862 if (unicode_file_names()) {
2863 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2864 wpath = PyUnicode_AS_UNICODE(obwpath);
2865 Py_BEGIN_ALLOW_THREADS
2866 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002867 NULL, OPEN_EXISTING,
2868 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002869 Py_END_ALLOW_THREADS
2870 if (hFile == INVALID_HANDLE_VALUE)
2871 return win32_error_unicode("utime", wpath);
2872 } else
2873 /* Drop the argument parsing error as narrow strings
2874 are also valid. */
2875 PyErr_Clear();
2876 }
2877 if (!wpath) {
2878 if (!PyArg_ParseTuple(args, "etO:utime",
2879 Py_FileSystemDefaultEncoding, &apath, &arg))
2880 return NULL;
2881 Py_BEGIN_ALLOW_THREADS
2882 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002883 NULL, OPEN_EXISTING,
2884 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002885 Py_END_ALLOW_THREADS
2886 if (hFile == INVALID_HANDLE_VALUE) {
2887 win32_error("utime", apath);
2888 PyMem_Free(apath);
2889 return NULL;
2890 }
2891 PyMem_Free(apath);
2892 }
2893
2894 if (arg == Py_None) {
2895 SYSTEMTIME now;
2896 GetSystemTime(&now);
2897 if (!SystemTimeToFileTime(&now, &mtime) ||
2898 !SystemTimeToFileTime(&now, &atime)) {
2899 win32_error("utime", NULL);
2900 goto done;
2901 }
2902 }
2903 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2904 PyErr_SetString(PyExc_TypeError,
2905 "utime() arg 2 must be a tuple (atime, mtime)");
2906 goto done;
2907 }
2908 else {
2909 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2910 &atimesec, &ausec) == -1)
2911 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002912 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002913 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2914 &mtimesec, &musec) == -1)
2915 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002916 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002917 }
2918 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2919 /* Avoid putting the file name into the error here,
2920 as that may confuse the user into believing that
2921 something is wrong with the file, when it also
2922 could be the time stamp that gives a problem. */
2923 win32_error("utime", NULL);
2924 }
2925 Py_INCREF(Py_None);
2926 result = Py_None;
2927done:
2928 CloseHandle(hFile);
2929 return result;
2930#else /* Py_WIN_WIDE_FILENAMES */
2931
Neal Norwitz2adf2102004-06-09 01:46:02 +00002932 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002933 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002934 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002935 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002936
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002937#if defined(HAVE_UTIMES)
2938 struct timeval buf[2];
2939#define ATIME buf[0].tv_sec
2940#define MTIME buf[1].tv_sec
2941#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002942/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002943 struct utimbuf buf;
2944#define ATIME buf.actime
2945#define MTIME buf.modtime
2946#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002947#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002948 time_t buf[2];
2949#define ATIME buf[0]
2950#define MTIME buf[1]
2951#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002952#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002953
Mark Hammond817c9292003-12-03 01:22:38 +00002954
Thomas Wouters477c8d52006-05-27 19:21:47 +00002955 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002956 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002957 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002958 if (arg == Py_None) {
2959 /* optional time values not given */
2960 Py_BEGIN_ALLOW_THREADS
2961 res = utime(path, NULL);
2962 Py_END_ALLOW_THREADS
2963 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002964 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002965 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002966 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002967 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002968 return NULL;
2969 }
2970 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002971 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002972 &atime, &ausec) == -1) {
2973 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002974 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002975 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002976 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002977 &mtime, &musec) == -1) {
2978 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002979 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002980 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002981 ATIME = atime;
2982 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002983#ifdef HAVE_UTIMES
2984 buf[0].tv_usec = ausec;
2985 buf[1].tv_usec = musec;
2986 Py_BEGIN_ALLOW_THREADS
2987 res = utimes(path, buf);
2988 Py_END_ALLOW_THREADS
2989#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002990 Py_BEGIN_ALLOW_THREADS
2991 res = utime(path, UTIME_ARG);
2992 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002993#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002994 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002995 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002996 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002997 }
Neal Norwitz96652712004-06-06 20:40:27 +00002998 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002999 Py_INCREF(Py_None);
3000 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003001#undef UTIME_ARG
3002#undef ATIME
3003#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00003004#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003005}
3006
Guido van Rossum85e3b011991-06-03 12:42:10 +00003007
Guido van Rossum3b066191991-06-04 19:40:25 +00003008/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003009
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003010PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003011"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003012Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003013
Barry Warsaw53699e91996-12-10 23:23:01 +00003014static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003015posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003016{
3017 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003018 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003019 return NULL;
3020 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00003021 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003022}
3023
Martin v. Löwis114619e2002-10-07 06:44:21 +00003024#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3025static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003026free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003027{
Martin v. Löwis725507b2006-03-07 12:08:51 +00003028 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00003029 for (i = 0; i < count; i++)
3030 PyMem_Free(array[i]);
3031 PyMem_DEL(array);
3032}
3033#endif
3034
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003035
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003036#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003037PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003038"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003039Execute an executable path with arguments, replacing current process.\n\
3040\n\
3041 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003042 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003043
Barry Warsaw53699e91996-12-10 23:23:01 +00003044static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003045posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003046{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003047 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003048 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003049 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003050 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003051 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003052
Guido van Rossum89b33251993-10-22 14:26:06 +00003053 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00003054 argv is a list or tuple of strings. */
3055
Martin v. Löwis114619e2002-10-07 06:44:21 +00003056 if (!PyArg_ParseTuple(args, "etO:execv",
3057 Py_FileSystemDefaultEncoding,
3058 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003059 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003060 if (PyList_Check(argv)) {
3061 argc = PyList_Size(argv);
3062 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003063 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003064 else if (PyTuple_Check(argv)) {
3065 argc = PyTuple_Size(argv);
3066 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003067 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003068 else {
Fred Drake661ea262000-10-24 19:57:45 +00003069 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003070 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00003071 return NULL;
3072 }
Thomas Heller6790d602007-08-30 17:15:14 +00003073 if (argc < 1) {
3074 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3075 PyMem_Free(path);
3076 return NULL;
3077 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003078
Barry Warsaw53699e91996-12-10 23:23:01 +00003079 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003080 if (argvlist == NULL) {
3081 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003082 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003083 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003084 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003085 if (!PyArg_Parse((*getitem)(argv, i), "et",
3086 Py_FileSystemDefaultEncoding,
3087 &argvlist[i])) {
3088 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00003089 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003090 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003091 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00003092 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003093
Guido van Rossum85e3b011991-06-03 12:42:10 +00003094 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003095 }
3096 argvlist[argc] = NULL;
3097
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003098 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003099
Guido van Rossum85e3b011991-06-03 12:42:10 +00003100 /* If we get here it's definitely an error */
3101
Martin v. Löwis114619e2002-10-07 06:44:21 +00003102 free_string_array(argvlist, argc);
3103 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003104 return posix_error();
3105}
3106
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003108PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003109"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003110Execute a path with arguments and environment, replacing current process.\n\
3111\n\
3112 path: path of executable file\n\
3113 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003114 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003115
Barry Warsaw53699e91996-12-10 23:23:01 +00003116static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003117posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003118{
3119 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003120 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003121 char **argvlist;
3122 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003123 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003124 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003125 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003126 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003127
3128 /* execve has three arguments: (path, argv, env), where
3129 argv is a list or tuple of strings and env is a dictionary
3130 like posix.environ. */
3131
Martin v. Löwis114619e2002-10-07 06:44:21 +00003132 if (!PyArg_ParseTuple(args, "etOO:execve",
3133 Py_FileSystemDefaultEncoding,
3134 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003135 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003136 if (PyList_Check(argv)) {
3137 argc = PyList_Size(argv);
3138 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003139 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003140 else if (PyTuple_Check(argv)) {
3141 argc = PyTuple_Size(argv);
3142 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003143 }
3144 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003145 PyErr_SetString(PyExc_TypeError,
3146 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003147 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003148 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003149 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003150 PyErr_SetString(PyExc_TypeError,
3151 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003152 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003153 }
3154
Barry Warsaw53699e91996-12-10 23:23:01 +00003155 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003156 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003157 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003158 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003159 }
3160 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003161 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00003162 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00003163 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00003164 &argvlist[i]))
3165 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003166 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003167 goto fail_1;
3168 }
3169 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003170 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003171 argvlist[argc] = NULL;
3172
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003173 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003174 if (i < 0)
3175 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003176 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003177 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003178 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003179 goto fail_1;
3180 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003181 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003182 keys = PyMapping_Keys(env);
3183 vals = PyMapping_Values(env);
3184 if (!keys || !vals)
3185 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003186 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3187 PyErr_SetString(PyExc_TypeError,
3188 "execve(): env.keys() or env.values() is not a list");
3189 goto fail_2;
3190 }
Tim Peters5aa91602002-01-30 05:46:57 +00003191
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003192 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003193 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003194 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003195
3196 key = PyList_GetItem(keys, pos);
3197 val = PyList_GetItem(vals, pos);
3198 if (!key || !val)
3199 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003200
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003201 if (!PyArg_Parse(
3202 key,
3203 "s;execve() arg 3 contains a non-string key",
3204 &k) ||
3205 !PyArg_Parse(
3206 val,
3207 "s;execve() arg 3 contains a non-string value",
3208 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003209 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003210 goto fail_2;
3211 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003212
3213#if defined(PYOS_OS2)
3214 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3215 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3216#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003217 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003218 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003219 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003220 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003221 goto fail_2;
3222 }
Tim Petersc8996f52001-12-03 20:41:00 +00003223 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003224 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003225#if defined(PYOS_OS2)
3226 }
3227#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003228 }
3229 envlist[envc] = 0;
3230
3231 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003232
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003233 /* If we get here it's definitely an error */
3234
3235 (void) posix_error();
3236
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003237 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003238 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003239 PyMem_DEL(envlist[envc]);
3240 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003241 fail_1:
3242 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003243 Py_XDECREF(vals);
3244 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003245 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003246 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003247 return NULL;
3248}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003249#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003250
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003251
Guido van Rossuma1065681999-01-25 23:20:23 +00003252#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003253PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003254"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003255Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003256\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003257 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003258 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003259 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003260
3261static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003262posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003263{
3264 char *path;
3265 PyObject *argv;
3266 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003267 int mode, i;
3268 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003269 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003270 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003271
3272 /* spawnv has three arguments: (mode, path, argv), where
3273 argv is a list or tuple of strings. */
3274
Martin v. Löwis114619e2002-10-07 06:44:21 +00003275 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3276 Py_FileSystemDefaultEncoding,
3277 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003278 return NULL;
3279 if (PyList_Check(argv)) {
3280 argc = PyList_Size(argv);
3281 getitem = PyList_GetItem;
3282 }
3283 else if (PyTuple_Check(argv)) {
3284 argc = PyTuple_Size(argv);
3285 getitem = PyTuple_GetItem;
3286 }
3287 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003288 PyErr_SetString(PyExc_TypeError,
3289 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003290 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003291 return NULL;
3292 }
3293
3294 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003295 if (argvlist == NULL) {
3296 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003297 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003298 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003299 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003300 if (!PyArg_Parse((*getitem)(argv, i), "et",
3301 Py_FileSystemDefaultEncoding,
3302 &argvlist[i])) {
3303 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003304 PyErr_SetString(
3305 PyExc_TypeError,
3306 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003307 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003308 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003309 }
3310 }
3311 argvlist[argc] = NULL;
3312
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003313#if defined(PYOS_OS2) && defined(PYCC_GCC)
3314 Py_BEGIN_ALLOW_THREADS
3315 spawnval = spawnv(mode, path, argvlist);
3316 Py_END_ALLOW_THREADS
3317#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003318 if (mode == _OLD_P_OVERLAY)
3319 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003320
Tim Peters25059d32001-12-07 20:35:43 +00003321 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003322 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003323 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003324#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003325
Martin v. Löwis114619e2002-10-07 06:44:21 +00003326 free_string_array(argvlist, argc);
3327 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003328
Fred Drake699f3522000-06-29 21:12:41 +00003329 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003330 return posix_error();
3331 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003332#if SIZEOF_LONG == SIZEOF_VOID_P
3333 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003334#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003335 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003336#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003337}
3338
3339
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003340PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003341"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003342Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003343\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003344 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003345 path: path of executable file\n\
3346 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003347 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003348
3349static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003350posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003351{
3352 char *path;
3353 PyObject *argv, *env;
3354 char **argvlist;
3355 char **envlist;
3356 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003357 int mode, pos, envc;
3358 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003359 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003360 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003361 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003362
3363 /* spawnve has four arguments: (mode, path, argv, env), where
3364 argv is a list or tuple of strings and env is a dictionary
3365 like posix.environ. */
3366
Martin v. Löwis114619e2002-10-07 06:44:21 +00003367 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3368 Py_FileSystemDefaultEncoding,
3369 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003370 return NULL;
3371 if (PyList_Check(argv)) {
3372 argc = PyList_Size(argv);
3373 getitem = PyList_GetItem;
3374 }
3375 else if (PyTuple_Check(argv)) {
3376 argc = PyTuple_Size(argv);
3377 getitem = PyTuple_GetItem;
3378 }
3379 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003380 PyErr_SetString(PyExc_TypeError,
3381 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003382 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003383 }
3384 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003385 PyErr_SetString(PyExc_TypeError,
3386 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003387 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003388 }
3389
3390 argvlist = PyMem_NEW(char *, argc+1);
3391 if (argvlist == NULL) {
3392 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003393 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003394 }
3395 for (i = 0; i < argc; i++) {
3396 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003397 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003398 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003399 &argvlist[i]))
3400 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003401 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003402 goto fail_1;
3403 }
3404 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003405 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003406 argvlist[argc] = NULL;
3407
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003408 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003409 if (i < 0)
3410 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003411 envlist = PyMem_NEW(char *, i + 1);
3412 if (envlist == NULL) {
3413 PyErr_NoMemory();
3414 goto fail_1;
3415 }
3416 envc = 0;
3417 keys = PyMapping_Keys(env);
3418 vals = PyMapping_Values(env);
3419 if (!keys || !vals)
3420 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003421 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3422 PyErr_SetString(PyExc_TypeError,
3423 "spawnve(): env.keys() or env.values() is not a list");
3424 goto fail_2;
3425 }
Tim Peters5aa91602002-01-30 05:46:57 +00003426
Guido van Rossuma1065681999-01-25 23:20:23 +00003427 for (pos = 0; pos < i; pos++) {
3428 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003429 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003430
3431 key = PyList_GetItem(keys, pos);
3432 val = PyList_GetItem(vals, pos);
3433 if (!key || !val)
3434 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003435
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003436 if (!PyArg_Parse(
3437 key,
3438 "s;spawnve() arg 3 contains a non-string key",
3439 &k) ||
3440 !PyArg_Parse(
3441 val,
3442 "s;spawnve() arg 3 contains a non-string value",
3443 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003444 {
3445 goto fail_2;
3446 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003447 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003448 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003449 if (p == NULL) {
3450 PyErr_NoMemory();
3451 goto fail_2;
3452 }
Tim Petersc8996f52001-12-03 20:41:00 +00003453 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003454 envlist[envc++] = p;
3455 }
3456 envlist[envc] = 0;
3457
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003458#if defined(PYOS_OS2) && defined(PYCC_GCC)
3459 Py_BEGIN_ALLOW_THREADS
3460 spawnval = spawnve(mode, path, argvlist, envlist);
3461 Py_END_ALLOW_THREADS
3462#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003463 if (mode == _OLD_P_OVERLAY)
3464 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003465
3466 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003467 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003468 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003469#endif
Tim Peters25059d32001-12-07 20:35:43 +00003470
Fred Drake699f3522000-06-29 21:12:41 +00003471 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003472 (void) posix_error();
3473 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003474#if SIZEOF_LONG == SIZEOF_VOID_P
3475 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003476#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003477 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003478#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003479
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003480 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003481 while (--envc >= 0)
3482 PyMem_DEL(envlist[envc]);
3483 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003484 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003485 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003486 Py_XDECREF(vals);
3487 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003488 fail_0:
3489 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003490 return res;
3491}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003492
3493/* OS/2 supports spawnvp & spawnvpe natively */
3494#if defined(PYOS_OS2)
3495PyDoc_STRVAR(posix_spawnvp__doc__,
3496"spawnvp(mode, file, args)\n\n\
3497Execute the program 'file' in a new process, using the environment\n\
3498search path to find the file.\n\
3499\n\
3500 mode: mode of process creation\n\
3501 file: executable file name\n\
3502 args: tuple or list of strings");
3503
3504static PyObject *
3505posix_spawnvp(PyObject *self, PyObject *args)
3506{
3507 char *path;
3508 PyObject *argv;
3509 char **argvlist;
3510 int mode, i, argc;
3511 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003512 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003513
3514 /* spawnvp has three arguments: (mode, path, argv), where
3515 argv is a list or tuple of strings. */
3516
3517 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3518 Py_FileSystemDefaultEncoding,
3519 &path, &argv))
3520 return NULL;
3521 if (PyList_Check(argv)) {
3522 argc = PyList_Size(argv);
3523 getitem = PyList_GetItem;
3524 }
3525 else if (PyTuple_Check(argv)) {
3526 argc = PyTuple_Size(argv);
3527 getitem = PyTuple_GetItem;
3528 }
3529 else {
3530 PyErr_SetString(PyExc_TypeError,
3531 "spawnvp() arg 2 must be a tuple or list");
3532 PyMem_Free(path);
3533 return NULL;
3534 }
3535
3536 argvlist = PyMem_NEW(char *, argc+1);
3537 if (argvlist == NULL) {
3538 PyMem_Free(path);
3539 return PyErr_NoMemory();
3540 }
3541 for (i = 0; i < argc; i++) {
3542 if (!PyArg_Parse((*getitem)(argv, i), "et",
3543 Py_FileSystemDefaultEncoding,
3544 &argvlist[i])) {
3545 free_string_array(argvlist, i);
3546 PyErr_SetString(
3547 PyExc_TypeError,
3548 "spawnvp() arg 2 must contain only strings");
3549 PyMem_Free(path);
3550 return NULL;
3551 }
3552 }
3553 argvlist[argc] = NULL;
3554
3555 Py_BEGIN_ALLOW_THREADS
3556#if defined(PYCC_GCC)
3557 spawnval = spawnvp(mode, path, argvlist);
3558#else
3559 spawnval = _spawnvp(mode, path, argvlist);
3560#endif
3561 Py_END_ALLOW_THREADS
3562
3563 free_string_array(argvlist, argc);
3564 PyMem_Free(path);
3565
3566 if (spawnval == -1)
3567 return posix_error();
3568 else
3569 return Py_BuildValue("l", (long) spawnval);
3570}
3571
3572
3573PyDoc_STRVAR(posix_spawnvpe__doc__,
3574"spawnvpe(mode, file, args, env)\n\n\
3575Execute the program 'file' in a new process, using the environment\n\
3576search path to find the file.\n\
3577\n\
3578 mode: mode of process creation\n\
3579 file: executable file name\n\
3580 args: tuple or list of arguments\n\
3581 env: dictionary of strings mapping to strings");
3582
3583static PyObject *
3584posix_spawnvpe(PyObject *self, PyObject *args)
3585{
3586 char *path;
3587 PyObject *argv, *env;
3588 char **argvlist;
3589 char **envlist;
3590 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3591 int mode, i, pos, argc, envc;
3592 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003593 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003594 int lastarg = 0;
3595
3596 /* spawnvpe has four arguments: (mode, path, argv, env), where
3597 argv is a list or tuple of strings and env is a dictionary
3598 like posix.environ. */
3599
3600 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3601 Py_FileSystemDefaultEncoding,
3602 &path, &argv, &env))
3603 return NULL;
3604 if (PyList_Check(argv)) {
3605 argc = PyList_Size(argv);
3606 getitem = PyList_GetItem;
3607 }
3608 else if (PyTuple_Check(argv)) {
3609 argc = PyTuple_Size(argv);
3610 getitem = PyTuple_GetItem;
3611 }
3612 else {
3613 PyErr_SetString(PyExc_TypeError,
3614 "spawnvpe() arg 2 must be a tuple or list");
3615 goto fail_0;
3616 }
3617 if (!PyMapping_Check(env)) {
3618 PyErr_SetString(PyExc_TypeError,
3619 "spawnvpe() arg 3 must be a mapping object");
3620 goto fail_0;
3621 }
3622
3623 argvlist = PyMem_NEW(char *, argc+1);
3624 if (argvlist == NULL) {
3625 PyErr_NoMemory();
3626 goto fail_0;
3627 }
3628 for (i = 0; i < argc; i++) {
3629 if (!PyArg_Parse((*getitem)(argv, i),
3630 "et;spawnvpe() arg 2 must contain only strings",
3631 Py_FileSystemDefaultEncoding,
3632 &argvlist[i]))
3633 {
3634 lastarg = i;
3635 goto fail_1;
3636 }
3637 }
3638 lastarg = argc;
3639 argvlist[argc] = NULL;
3640
3641 i = PyMapping_Size(env);
3642 if (i < 0)
3643 goto fail_1;
3644 envlist = PyMem_NEW(char *, i + 1);
3645 if (envlist == NULL) {
3646 PyErr_NoMemory();
3647 goto fail_1;
3648 }
3649 envc = 0;
3650 keys = PyMapping_Keys(env);
3651 vals = PyMapping_Values(env);
3652 if (!keys || !vals)
3653 goto fail_2;
3654 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3655 PyErr_SetString(PyExc_TypeError,
3656 "spawnvpe(): env.keys() or env.values() is not a list");
3657 goto fail_2;
3658 }
3659
3660 for (pos = 0; pos < i; pos++) {
3661 char *p, *k, *v;
3662 size_t len;
3663
3664 key = PyList_GetItem(keys, pos);
3665 val = PyList_GetItem(vals, pos);
3666 if (!key || !val)
3667 goto fail_2;
3668
3669 if (!PyArg_Parse(
3670 key,
3671 "s;spawnvpe() arg 3 contains a non-string key",
3672 &k) ||
3673 !PyArg_Parse(
3674 val,
3675 "s;spawnvpe() arg 3 contains a non-string value",
3676 &v))
3677 {
3678 goto fail_2;
3679 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003680 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003681 p = PyMem_NEW(char, len);
3682 if (p == NULL) {
3683 PyErr_NoMemory();
3684 goto fail_2;
3685 }
3686 PyOS_snprintf(p, len, "%s=%s", k, v);
3687 envlist[envc++] = p;
3688 }
3689 envlist[envc] = 0;
3690
3691 Py_BEGIN_ALLOW_THREADS
3692#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003693 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003694#else
Christian Heimes292d3512008-02-03 16:51:08 +00003695 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003696#endif
3697 Py_END_ALLOW_THREADS
3698
3699 if (spawnval == -1)
3700 (void) posix_error();
3701 else
3702 res = Py_BuildValue("l", (long) spawnval);
3703
3704 fail_2:
3705 while (--envc >= 0)
3706 PyMem_DEL(envlist[envc]);
3707 PyMem_DEL(envlist);
3708 fail_1:
3709 free_string_array(argvlist, lastarg);
3710 Py_XDECREF(vals);
3711 Py_XDECREF(keys);
3712 fail_0:
3713 PyMem_Free(path);
3714 return res;
3715}
3716#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003717#endif /* HAVE_SPAWNV */
3718
3719
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003720#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003721PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003722"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003723Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3724\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003725Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003726
3727static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003728posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003729{
Christian Heimes400adb02008-02-01 08:12:03 +00003730 pid_t pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003731 if (pid == -1)
3732 return posix_error();
Georg Brandl2ee470f2008-07-16 12:55:28 +00003733 if (pid == 0)
3734 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003735 return PyLong_FromLong(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003736}
3737#endif
3738
3739
Guido van Rossumad0ee831995-03-01 10:34:45 +00003740#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003741PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003742"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003743Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003744Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003745
Barry Warsaw53699e91996-12-10 23:23:01 +00003746static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003747posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003748{
Christian Heimes400adb02008-02-01 08:12:03 +00003749 pid_t pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003750 if (pid == -1)
3751 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003752 if (pid == 0)
3753 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003754 return PyLong_FromLong(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003755}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003756#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003757
Neal Norwitzb59798b2003-03-21 01:43:31 +00003758/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003759/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3760#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003761#define DEV_PTY_FILE "/dev/ptc"
3762#define HAVE_DEV_PTMX
3763#else
3764#define DEV_PTY_FILE "/dev/ptmx"
3765#endif
3766
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003767#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003768#ifdef HAVE_PTY_H
3769#include <pty.h>
3770#else
3771#ifdef HAVE_LIBUTIL_H
3772#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003773#endif /* HAVE_LIBUTIL_H */
3774#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003775#ifdef HAVE_STROPTS_H
3776#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003777#endif
3778#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003779
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003780#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003781PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003782"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003783Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003784
3785static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003786posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003787{
3788 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003789#ifndef HAVE_OPENPTY
3790 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003791#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003792#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003793 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003794#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003795 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003796#endif
3797#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003798
Thomas Wouters70c21a12000-07-14 14:28:33 +00003799#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003800 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3801 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003802#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003803 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3804 if (slave_name == NULL)
3805 return posix_error();
3806
3807 slave_fd = open(slave_name, O_RDWR);
3808 if (slave_fd < 0)
3809 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003810#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003811 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003812 if (master_fd < 0)
3813 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003814 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003815 /* change permission of slave */
3816 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003817 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003818 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003819 }
3820 /* unlock slave */
3821 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003822 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003823 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003824 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003825 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003826 slave_name = ptsname(master_fd); /* get name of slave */
3827 if (slave_name == NULL)
3828 return posix_error();
3829 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3830 if (slave_fd < 0)
3831 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003832#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003833 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3834 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003835#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003836 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003837#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003838#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003839#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003840
Fred Drake8cef4cf2000-06-28 16:40:38 +00003841 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003842
Fred Drake8cef4cf2000-06-28 16:40:38 +00003843}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003844#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003845
3846#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003847PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003848"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003849Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3850Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003851To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003852
3853static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003854posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003855{
Christian Heimes400adb02008-02-01 08:12:03 +00003856 int master_fd = -1;
3857 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003858
Fred Drake8cef4cf2000-06-28 16:40:38 +00003859 pid = forkpty(&master_fd, NULL, NULL, NULL);
3860 if (pid == -1)
3861 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003862 if (pid == 0)
3863 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003864 return Py_BuildValue("(li)", pid, master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003865}
3866#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003867
Guido van Rossumad0ee831995-03-01 10:34:45 +00003868#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003869PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003870"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003871Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003872
Barry Warsaw53699e91996-12-10 23:23:01 +00003873static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003874posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003875{
Christian Heimes217cfd12007-12-02 14:31:20 +00003876 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003877}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003878#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003879
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003880
Guido van Rossumad0ee831995-03-01 10:34:45 +00003881#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003882PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003883"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003884Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003885
Barry Warsaw53699e91996-12-10 23:23:01 +00003886static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003887posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003888{
Christian Heimes217cfd12007-12-02 14:31:20 +00003889 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003890}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003891#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003892
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003893
Guido van Rossumad0ee831995-03-01 10:34:45 +00003894#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003895PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003896"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003897Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003898
Barry Warsaw53699e91996-12-10 23:23:01 +00003899static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003900posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003901{
Christian Heimes217cfd12007-12-02 14:31:20 +00003902 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003903}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003904#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003905
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003906
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003907PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003908"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003909Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003910
Barry Warsaw53699e91996-12-10 23:23:01 +00003911static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003912posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003913{
Christian Heimes217cfd12007-12-02 14:31:20 +00003914 return PyLong_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003915}
3916
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003917
Fred Drakec9680921999-12-13 16:37:25 +00003918#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003919PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003920"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003921Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003922
3923static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003924posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003925{
3926 PyObject *result = NULL;
3927
Fred Drakec9680921999-12-13 16:37:25 +00003928#ifdef NGROUPS_MAX
3929#define MAX_GROUPS NGROUPS_MAX
3930#else
3931 /* defined to be 16 on Solaris7, so this should be a small number */
3932#define MAX_GROUPS 64
3933#endif
3934 gid_t grouplist[MAX_GROUPS];
3935 int n;
3936
3937 n = getgroups(MAX_GROUPS, grouplist);
3938 if (n < 0)
3939 posix_error();
3940 else {
3941 result = PyList_New(n);
3942 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003943 int i;
3944 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003945 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003946 if (o == NULL) {
3947 Py_DECREF(result);
3948 result = NULL;
3949 break;
3950 }
3951 PyList_SET_ITEM(result, i, o);
3952 }
3953 }
3954 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003955
Fred Drakec9680921999-12-13 16:37:25 +00003956 return result;
3957}
3958#endif
3959
Martin v. Löwis606edc12002-06-13 21:09:11 +00003960#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003961PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003962"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003963Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003964
3965static PyObject *
3966posix_getpgid(PyObject *self, PyObject *args)
3967{
3968 int pid, pgid;
3969 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3970 return NULL;
3971 pgid = getpgid(pid);
3972 if (pgid < 0)
3973 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00003974 return PyLong_FromLong((long)pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003975}
3976#endif /* HAVE_GETPGID */
3977
3978
Guido van Rossumb6775db1994-08-01 11:34:53 +00003979#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003980PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003981"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003982Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003983
Barry Warsaw53699e91996-12-10 23:23:01 +00003984static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003985posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003986{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003987#ifdef GETPGRP_HAVE_ARG
Christian Heimes217cfd12007-12-02 14:31:20 +00003988 return PyLong_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003989#else /* GETPGRP_HAVE_ARG */
Christian Heimes217cfd12007-12-02 14:31:20 +00003990 return PyLong_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003991#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003992}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003993#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003994
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003995
Guido van Rossumb6775db1994-08-01 11:34:53 +00003996#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003997PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003998"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003999Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004000
Barry Warsaw53699e91996-12-10 23:23:01 +00004001static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004002posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004003{
Guido van Rossum64933891994-10-20 21:56:42 +00004004#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00004005 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004006#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004007 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004008#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00004009 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004010 Py_INCREF(Py_None);
4011 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004012}
4013
Guido van Rossumb6775db1994-08-01 11:34:53 +00004014#endif /* HAVE_SETPGRP */
4015
Guido van Rossumad0ee831995-03-01 10:34:45 +00004016#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004017PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004018"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004019Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004020
Barry Warsaw53699e91996-12-10 23:23:01 +00004021static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004022posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004023{
Christian Heimes217cfd12007-12-02 14:31:20 +00004024 return PyLong_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004025}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004026#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004027
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004028
Fred Drake12c6e2d1999-12-14 21:25:03 +00004029#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004030PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004031"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004032Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004033
4034static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004035posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004036{
Neal Norwitze241ce82003-02-17 18:17:05 +00004037 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00004038 char *name;
4039 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004040
Fred Drakea30680b2000-12-06 21:24:28 +00004041 errno = 0;
4042 name = getlogin();
4043 if (name == NULL) {
4044 if (errno)
4045 posix_error();
4046 else
4047 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00004048 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00004049 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00004050 else
Neal Norwitz93c56822007-08-26 07:10:06 +00004051 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00004052 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004053
Fred Drake12c6e2d1999-12-14 21:25:03 +00004054 return result;
4055}
4056#endif
4057
Guido van Rossumad0ee831995-03-01 10:34:45 +00004058#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004059PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004060"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004061Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004062
Barry Warsaw53699e91996-12-10 23:23:01 +00004063static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004064posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004065{
Christian Heimes217cfd12007-12-02 14:31:20 +00004066 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004067}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004068#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004069
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004070
Guido van Rossumad0ee831995-03-01 10:34:45 +00004071#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004072PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004073"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004074Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004075
Barry Warsaw53699e91996-12-10 23:23:01 +00004076static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004077posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004078{
Christian Heimes292d3512008-02-03 16:51:08 +00004079 pid_t pid;
4080 int sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004081 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00004082 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004083#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004084 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4085 APIRET rc;
4086 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004087 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004088
4089 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4090 APIRET rc;
4091 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004092 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004093
4094 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004095 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004096#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004097 if (kill(pid, sig) == -1)
4098 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004099#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004100 Py_INCREF(Py_None);
4101 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004102}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004103#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004104
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004105#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004106PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004107"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004108Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004109
4110static PyObject *
4111posix_killpg(PyObject *self, PyObject *args)
4112{
4113 int pgid, sig;
4114 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
4115 return NULL;
4116 if (killpg(pgid, sig) == -1)
4117 return posix_error();
4118 Py_INCREF(Py_None);
4119 return Py_None;
4120}
4121#endif
4122
Guido van Rossumc0125471996-06-28 18:55:32 +00004123#ifdef HAVE_PLOCK
4124
4125#ifdef HAVE_SYS_LOCK_H
4126#include <sys/lock.h>
4127#endif
4128
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004129PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004130"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004131Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004132
Barry Warsaw53699e91996-12-10 23:23:01 +00004133static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004134posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004135{
4136 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004137 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004138 return NULL;
4139 if (plock(op) == -1)
4140 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004141 Py_INCREF(Py_None);
4142 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004143}
4144#endif
4145
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004146
Guido van Rossum3b066191991-06-04 19:40:25 +00004147
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004148
Guido van Rossumb6775db1994-08-01 11:34:53 +00004149#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004150PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004151"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004152Set the current process's user id.");
4153
Barry Warsaw53699e91996-12-10 23:23:01 +00004154static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004155posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004156{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004157 long uid_arg;
4158 uid_t uid;
4159 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004160 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004161 uid = uid_arg;
4162 if (uid != uid_arg) {
4163 PyErr_SetString(PyExc_OverflowError, "user id too big");
4164 return NULL;
4165 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004166 if (setuid(uid) < 0)
4167 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004168 Py_INCREF(Py_None);
4169 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004170}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004171#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004172
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004173
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004174#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004175PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004176"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004177Set the current process's effective user id.");
4178
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004179static PyObject *
4180posix_seteuid (PyObject *self, PyObject *args)
4181{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004182 long euid_arg;
4183 uid_t euid;
4184 if (!PyArg_ParseTuple(args, "l", &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004185 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004186 euid = euid_arg;
4187 if (euid != euid_arg) {
4188 PyErr_SetString(PyExc_OverflowError, "user id too big");
4189 return NULL;
4190 }
4191 if (seteuid(euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004192 return posix_error();
4193 } else {
4194 Py_INCREF(Py_None);
4195 return Py_None;
4196 }
4197}
4198#endif /* HAVE_SETEUID */
4199
4200#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004201PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004202"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004203Set the current process's effective group id.");
4204
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004205static PyObject *
4206posix_setegid (PyObject *self, PyObject *args)
4207{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004208 long egid_arg;
4209 gid_t egid;
4210 if (!PyArg_ParseTuple(args, "l", &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004211 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004212 egid = egid_arg;
4213 if (egid != egid_arg) {
4214 PyErr_SetString(PyExc_OverflowError, "group id too big");
4215 return NULL;
4216 }
4217 if (setegid(egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004218 return posix_error();
4219 } else {
4220 Py_INCREF(Py_None);
4221 return Py_None;
4222 }
4223}
4224#endif /* HAVE_SETEGID */
4225
4226#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004227PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004228"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004229Set the current process's real and effective user ids.");
4230
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004231static PyObject *
4232posix_setreuid (PyObject *self, PyObject *args)
4233{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004234 long ruid_arg, euid_arg;
4235 uid_t ruid, euid;
4236 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004237 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004238 ruid = ruid_arg;
4239 euid = euid_arg;
4240 if (euid != euid_arg || ruid != ruid_arg) {
4241 PyErr_SetString(PyExc_OverflowError, "user id too big");
4242 return NULL;
4243 }
4244 if (setreuid(ruid, euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004245 return posix_error();
4246 } else {
4247 Py_INCREF(Py_None);
4248 return Py_None;
4249 }
4250}
4251#endif /* HAVE_SETREUID */
4252
4253#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004254PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004255"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004256Set the current process's real and effective group ids.");
4257
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004258static PyObject *
4259posix_setregid (PyObject *self, PyObject *args)
4260{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004261 long rgid_arg, egid_arg;
4262 gid_t rgid, egid;
4263 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004264 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004265 rgid = rgid_arg;
4266 egid = egid_arg;
4267 if (egid != egid_arg || rgid != rgid_arg) {
4268 PyErr_SetString(PyExc_OverflowError, "group id too big");
4269 return NULL;
4270 }
4271 if (setregid(rgid, egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004272 return posix_error();
4273 } else {
4274 Py_INCREF(Py_None);
4275 return Py_None;
4276 }
4277}
4278#endif /* HAVE_SETREGID */
4279
Guido van Rossumb6775db1994-08-01 11:34:53 +00004280#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004281PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004282"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004283Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004284
Barry Warsaw53699e91996-12-10 23:23:01 +00004285static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004286posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004287{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004288 long gid_arg;
4289 gid_t gid;
4290 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004291 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004292 gid = gid_arg;
4293 if (gid != gid_arg) {
4294 PyErr_SetString(PyExc_OverflowError, "group id too big");
4295 return NULL;
4296 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004297 if (setgid(gid) < 0)
4298 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004299 Py_INCREF(Py_None);
4300 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004301}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004302#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004303
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004304#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004305PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004306"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004307Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004308
4309static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004310posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004311{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004312 int i, len;
4313 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004314
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004315 if (!PySequence_Check(groups)) {
4316 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4317 return NULL;
4318 }
4319 len = PySequence_Size(groups);
4320 if (len > MAX_GROUPS) {
4321 PyErr_SetString(PyExc_ValueError, "too many groups");
4322 return NULL;
4323 }
4324 for(i = 0; i < len; i++) {
4325 PyObject *elem;
4326 elem = PySequence_GetItem(groups, i);
4327 if (!elem)
4328 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004329 if (!PyLong_Check(elem)) {
4330 PyErr_SetString(PyExc_TypeError,
4331 "groups must be integers");
4332 Py_DECREF(elem);
4333 return NULL;
4334 } else {
4335 unsigned long x = PyLong_AsUnsignedLong(elem);
4336 if (PyErr_Occurred()) {
4337 PyErr_SetString(PyExc_TypeError,
4338 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004339 Py_DECREF(elem);
4340 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004341 }
Georg Brandla13c2442005-11-22 19:30:31 +00004342 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004343 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004344 if (grouplist[i] != x) {
4345 PyErr_SetString(PyExc_TypeError,
4346 "group id too big");
4347 Py_DECREF(elem);
4348 return NULL;
4349 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004350 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004351 Py_DECREF(elem);
4352 }
4353
4354 if (setgroups(len, grouplist) < 0)
4355 return posix_error();
4356 Py_INCREF(Py_None);
4357 return Py_None;
4358}
4359#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004360
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004361#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4362static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004363wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004364{
4365 PyObject *result;
4366 static PyObject *struct_rusage;
4367
4368 if (pid == -1)
4369 return posix_error();
4370
4371 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004372 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004373 if (m == NULL)
4374 return NULL;
4375 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4376 Py_DECREF(m);
4377 if (struct_rusage == NULL)
4378 return NULL;
4379 }
4380
4381 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4382 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4383 if (!result)
4384 return NULL;
4385
4386#ifndef doubletime
4387#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4388#endif
4389
4390 PyStructSequence_SET_ITEM(result, 0,
4391 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4392 PyStructSequence_SET_ITEM(result, 1,
4393 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4394#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004395 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004396 SET_INT(result, 2, ru->ru_maxrss);
4397 SET_INT(result, 3, ru->ru_ixrss);
4398 SET_INT(result, 4, ru->ru_idrss);
4399 SET_INT(result, 5, ru->ru_isrss);
4400 SET_INT(result, 6, ru->ru_minflt);
4401 SET_INT(result, 7, ru->ru_majflt);
4402 SET_INT(result, 8, ru->ru_nswap);
4403 SET_INT(result, 9, ru->ru_inblock);
4404 SET_INT(result, 10, ru->ru_oublock);
4405 SET_INT(result, 11, ru->ru_msgsnd);
4406 SET_INT(result, 12, ru->ru_msgrcv);
4407 SET_INT(result, 13, ru->ru_nsignals);
4408 SET_INT(result, 14, ru->ru_nvcsw);
4409 SET_INT(result, 15, ru->ru_nivcsw);
4410#undef SET_INT
4411
4412 if (PyErr_Occurred()) {
4413 Py_DECREF(result);
4414 return NULL;
4415 }
4416
4417 return Py_BuildValue("iiN", pid, status, result);
4418}
4419#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4420
4421#ifdef HAVE_WAIT3
4422PyDoc_STRVAR(posix_wait3__doc__,
4423"wait3(options) -> (pid, status, rusage)\n\n\
4424Wait for completion of a child process.");
4425
4426static PyObject *
4427posix_wait3(PyObject *self, PyObject *args)
4428{
Christian Heimes292d3512008-02-03 16:51:08 +00004429 pid_t pid;
4430 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004431 struct rusage ru;
4432 WAIT_TYPE status;
4433 WAIT_STATUS_INT(status) = 0;
4434
4435 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4436 return NULL;
4437
4438 Py_BEGIN_ALLOW_THREADS
4439 pid = wait3(&status, options, &ru);
4440 Py_END_ALLOW_THREADS
4441
4442 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4443}
4444#endif /* HAVE_WAIT3 */
4445
4446#ifdef HAVE_WAIT4
4447PyDoc_STRVAR(posix_wait4__doc__,
4448"wait4(pid, options) -> (pid, status, rusage)\n\n\
4449Wait for completion of a given child process.");
4450
4451static PyObject *
4452posix_wait4(PyObject *self, PyObject *args)
4453{
Christian Heimes292d3512008-02-03 16:51:08 +00004454 pid_t pid;
4455 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004456 struct rusage ru;
4457 WAIT_TYPE status;
4458 WAIT_STATUS_INT(status) = 0;
4459
4460 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
4461 return NULL;
4462
4463 Py_BEGIN_ALLOW_THREADS
4464 pid = wait4(pid, &status, options, &ru);
4465 Py_END_ALLOW_THREADS
4466
4467 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4468}
4469#endif /* HAVE_WAIT4 */
4470
Guido van Rossumb6775db1994-08-01 11:34:53 +00004471#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004472PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004473"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004474Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004475
Barry Warsaw53699e91996-12-10 23:23:01 +00004476static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004477posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004478{
Christian Heimes292d3512008-02-03 16:51:08 +00004479 pid_t pid;
4480 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004481 WAIT_TYPE status;
4482 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004483
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004484 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004485 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004486 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004487 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004488 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004489 if (pid == -1)
4490 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004491
4492 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004493}
4494
Tim Petersab034fa2002-02-01 11:27:43 +00004495#elif defined(HAVE_CWAIT)
4496
4497/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004498PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004499"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004500"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004501
4502static PyObject *
4503posix_waitpid(PyObject *self, PyObject *args)
4504{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004505 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004506 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004507
4508 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4509 return NULL;
4510 Py_BEGIN_ALLOW_THREADS
4511 pid = _cwait(&status, pid, options);
4512 Py_END_ALLOW_THREADS
4513 if (pid == -1)
4514 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004515
4516 /* shift the status left a byte so this is more like the POSIX waitpid */
4517 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004518}
4519#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004520
Guido van Rossumad0ee831995-03-01 10:34:45 +00004521#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004522PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004523"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004524Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004525
Barry Warsaw53699e91996-12-10 23:23:01 +00004526static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004527posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004528{
Christian Heimes292d3512008-02-03 16:51:08 +00004529 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004530 WAIT_TYPE status;
4531 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004532
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004533 Py_BEGIN_ALLOW_THREADS
4534 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004535 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004536 if (pid == -1)
4537 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004538
4539 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004540}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004541#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004542
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004543
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004544PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004545"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004546Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004547
Barry Warsaw53699e91996-12-10 23:23:01 +00004548static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004549posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004550{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004551#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004552 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004553#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004554#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00004555 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004556#else
4557 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4558#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004559#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004560}
4561
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004562
Guido van Rossumb6775db1994-08-01 11:34:53 +00004563#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004564PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004565"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004566Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004567
Barry Warsaw53699e91996-12-10 23:23:01 +00004568static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004569posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004570{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004571 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004572 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004573 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004574 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004575 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004576
4577 if (!PyArg_ParseTuple(args, "et:readlink",
4578 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004579 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004580 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004581 if (v == NULL) {
4582 PyMem_Free(path);
4583 return NULL;
4584 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004585
4586 if (PyUnicode_Check(v)) {
4587 arg_is_unicode = 1;
4588 }
4589 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004590
Barry Warsaw53699e91996-12-10 23:23:01 +00004591 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004592 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004593 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004594 if (n < 0)
Neal Norwitzfca70052007-08-12 16:56:02 +00004595 return posix_error_with_allocated_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004596
Neal Norwitzfca70052007-08-12 16:56:02 +00004597 PyMem_Free(path);
Christian Heimes72b710a2008-05-26 13:28:38 +00004598 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004599 if (arg_is_unicode) {
4600 PyObject *w;
4601
4602 w = PyUnicode_FromEncodedObject(v,
4603 Py_FileSystemDefaultEncoding,
4604 "strict");
4605 if (w != NULL) {
4606 Py_DECREF(v);
4607 v = w;
4608 }
4609 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00004610 v = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004611 }
4612 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004613 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004614}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004615#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004616
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004617
Guido van Rossumb6775db1994-08-01 11:34:53 +00004618#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004619PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004620"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004621Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004622
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004623static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004624posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004625{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004626 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004627}
4628#endif /* HAVE_SYMLINK */
4629
4630
4631#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004632#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4633static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004634system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004635{
4636 ULONG value = 0;
4637
4638 Py_BEGIN_ALLOW_THREADS
4639 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4640 Py_END_ALLOW_THREADS
4641
4642 return value;
4643}
4644
4645static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004646posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004647{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004648 /* Currently Only Uptime is Provided -- Others Later */
4649 return Py_BuildValue("ddddd",
4650 (double)0 /* t.tms_utime / HZ */,
4651 (double)0 /* t.tms_stime / HZ */,
4652 (double)0 /* t.tms_cutime / HZ */,
4653 (double)0 /* t.tms_cstime / HZ */,
4654 (double)system_uptime() / 1000);
4655}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004656#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004657#define NEED_TICKS_PER_SECOND
4658static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004659static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004660posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004661{
4662 struct tms t;
4663 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004664 errno = 0;
4665 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004666 if (c == (clock_t) -1)
4667 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004668 return Py_BuildValue("ddddd",
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004669 (double)t.tms_utime / ticks_per_second,
4670 (double)t.tms_stime / ticks_per_second,
4671 (double)t.tms_cutime / ticks_per_second,
4672 (double)t.tms_cstime / ticks_per_second,
4673 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004674}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004675#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004676#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004677
4678
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004679#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004680#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004681static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004682posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004683{
4684 FILETIME create, exit, kernel, user;
4685 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004686 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004687 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4688 /* The fields of a FILETIME structure are the hi and lo part
4689 of a 64-bit value expressed in 100 nanosecond units.
4690 1e7 is one second in such units; 1e-7 the inverse.
4691 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4692 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004693 return Py_BuildValue(
4694 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004695 (double)(user.dwHighDateTime*429.4967296 +
4696 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004697 (double)(kernel.dwHighDateTime*429.4967296 +
4698 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004699 (double)0,
4700 (double)0,
4701 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004702}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004703#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004704
4705#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004706PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004707"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004708Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004709#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004710
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004711
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004712#ifdef HAVE_GETSID
4713PyDoc_STRVAR(posix_getsid__doc__,
4714"getsid(pid) -> sid\n\n\
4715Call the system call getsid().");
4716
4717static PyObject *
4718posix_getsid(PyObject *self, PyObject *args)
4719{
Christian Heimes292d3512008-02-03 16:51:08 +00004720 pid_t pid;
4721 int sid;
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004722 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4723 return NULL;
4724 sid = getsid(pid);
4725 if (sid < 0)
4726 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004727 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004728}
4729#endif /* HAVE_GETSID */
4730
4731
Guido van Rossumb6775db1994-08-01 11:34:53 +00004732#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004733PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004734"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004735Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004736
Barry Warsaw53699e91996-12-10 23:23:01 +00004737static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004738posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004739{
Guido van Rossum687dd131993-05-17 08:34:16 +00004740 if (setsid() < 0)
4741 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004742 Py_INCREF(Py_None);
4743 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004744}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004745#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004746
Guido van Rossumb6775db1994-08-01 11:34:53 +00004747#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004748PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004749"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004750Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004751
Barry Warsaw53699e91996-12-10 23:23:01 +00004752static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004753posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004754{
Christian Heimes292d3512008-02-03 16:51:08 +00004755 pid_t pid;
4756 int pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004757 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004758 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004759 if (setpgid(pid, pgrp) < 0)
4760 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004761 Py_INCREF(Py_None);
4762 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004763}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004764#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004765
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004766
Guido van Rossumb6775db1994-08-01 11:34:53 +00004767#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004768PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004769"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004770Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004771
Barry Warsaw53699e91996-12-10 23:23:01 +00004772static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004773posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004774{
Christian Heimes15ebc882008-02-04 18:48:49 +00004775 int fd;
4776 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004777 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004778 return NULL;
4779 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004780 if (pgid < 0)
4781 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004782 return PyLong_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004783}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004784#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004785
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004786
Guido van Rossumb6775db1994-08-01 11:34:53 +00004787#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004788PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004789"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004790Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004791
Barry Warsaw53699e91996-12-10 23:23:01 +00004792static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004793posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004794{
4795 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004796 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004797 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004798 if (tcsetpgrp(fd, pgid) < 0)
4799 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004800 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004801 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004802}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004803#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004804
Guido van Rossum687dd131993-05-17 08:34:16 +00004805/* Functions acting on file descriptors */
4806
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004807PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004808"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004809Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004810
Barry Warsaw53699e91996-12-10 23:23:01 +00004811static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004812posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004813{
Mark Hammondef8b6542001-05-13 08:04:26 +00004814 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004815 int flag;
4816 int mode = 0777;
4817 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004818
4819#ifdef MS_WINDOWS
4820 if (unicode_file_names()) {
4821 PyUnicodeObject *po;
4822 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4823 Py_BEGIN_ALLOW_THREADS
4824 /* PyUnicode_AS_UNICODE OK without thread
4825 lock as it is a simple dereference. */
4826 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4827 Py_END_ALLOW_THREADS
4828 if (fd < 0)
4829 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004830 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004831 }
4832 /* Drop the argument parsing error as narrow strings
4833 are also valid. */
4834 PyErr_Clear();
4835 }
4836#endif
4837
Tim Peters5aa91602002-01-30 05:46:57 +00004838 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004839 Py_FileSystemDefaultEncoding, &file,
4840 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004841 return NULL;
4842
Barry Warsaw53699e91996-12-10 23:23:01 +00004843 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004844 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004845 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004846 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004847 return posix_error_with_allocated_filename(file);
4848 PyMem_Free(file);
Christian Heimes217cfd12007-12-02 14:31:20 +00004849 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004850}
4851
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004852
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004853PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004854"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004855Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004856
Barry Warsaw53699e91996-12-10 23:23:01 +00004857static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004858posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004859{
4860 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004861 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004862 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004863 if (!_PyVerify_fd(fd))
4864 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004865 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004866 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004867 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004868 if (res < 0)
4869 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004870 Py_INCREF(Py_None);
4871 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004872}
4873
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004874
Christian Heimesfdab48e2008-01-20 09:06:41 +00004875PyDoc_STRVAR(posix_closerange__doc__,
4876"closerange(fd_low, fd_high)\n\n\
4877Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4878
4879static PyObject *
4880posix_closerange(PyObject *self, PyObject *args)
4881{
4882 int fd_from, fd_to, i;
4883 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4884 return NULL;
4885 Py_BEGIN_ALLOW_THREADS
4886 for (i = fd_from; i < fd_to; i++)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004887 if (_PyVerify_fd(i))
4888 close(i);
Christian Heimesfdab48e2008-01-20 09:06:41 +00004889 Py_END_ALLOW_THREADS
4890 Py_RETURN_NONE;
4891}
4892
4893
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004894PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004895"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004896Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004897
Barry Warsaw53699e91996-12-10 23:23:01 +00004898static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004899posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004900{
4901 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004902 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004903 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004904 if (!_PyVerify_fd(fd))
4905 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004906 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004907 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004908 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004909 if (fd < 0)
4910 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004911 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004912}
4913
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004914
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004915PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004916"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004917Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004918
Barry Warsaw53699e91996-12-10 23:23:01 +00004919static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004920posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004921{
4922 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004923 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004924 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004925 if (!_PyVerify_fd_dup2(fd, fd2))
4926 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004927 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004928 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004929 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004930 if (res < 0)
4931 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004932 Py_INCREF(Py_None);
4933 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004934}
4935
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004936
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004937PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004938"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004939Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004940
Barry Warsaw53699e91996-12-10 23:23:01 +00004941static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004942posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004943{
4944 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004945#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004946 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004947#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004948 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004949#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004950 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004951 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004952 return NULL;
4953#ifdef SEEK_SET
4954 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4955 switch (how) {
4956 case 0: how = SEEK_SET; break;
4957 case 1: how = SEEK_CUR; break;
4958 case 2: how = SEEK_END; break;
4959 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004960#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004961
4962#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004963 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004964#else
4965 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00004966 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004967#endif
4968 if (PyErr_Occurred())
4969 return NULL;
4970
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004971 if (!_PyVerify_fd(fd))
4972 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004973 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004974#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004975 res = _lseeki64(fd, pos, how);
4976#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004977 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004978#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004979 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004980 if (res < 0)
4981 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004982
4983#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004984 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004985#else
4986 return PyLong_FromLongLong(res);
4987#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004988}
4989
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004990
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004991PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004992"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004993Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004994
Barry Warsaw53699e91996-12-10 23:23:01 +00004995static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004996posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004997{
Guido van Rossum572dbf82007-04-27 23:53:51 +00004998 int fd, size;
4999 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005000 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005001 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005002 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005003 if (size < 0) {
5004 errno = EINVAL;
5005 return posix_error();
5006 }
Christian Heimes72b710a2008-05-26 13:28:38 +00005007 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005008 if (buffer == NULL)
5009 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005010 if (!_PyVerify_fd(fd))
5011 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005012 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00005013 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005014 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005015 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005016 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005017 return posix_error();
5018 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005019 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00005020 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005021 return buffer;
5022}
5023
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005025PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005026"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005027Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005028
Barry Warsaw53699e91996-12-10 23:23:01 +00005029static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005030posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005031{
Martin v. Löwis423be952008-08-13 15:53:07 +00005032 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005033 int fd;
5034 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005035
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +00005036 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00005037 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005038 if (!_PyVerify_fd(fd))
5039 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005040 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005041 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00005042 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005043 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00005044 if (size < 0)
5045 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005046 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005047}
5048
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005049
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005050PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005051"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005052Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005053
Barry Warsaw53699e91996-12-10 23:23:01 +00005054static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005055posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005056{
5057 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005058 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005059 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005060 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005061 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005062#ifdef __VMS
5063 /* on OpenVMS we must ensure that all bytes are written to the file */
5064 fsync(fd);
5065#endif
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005066 if (!_PyVerify_fd(fd))
5067 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005068 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005069 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005070 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005071 if (res != 0) {
5072#ifdef MS_WINDOWS
5073 return win32_error("fstat", NULL);
5074#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005075 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005076#endif
5077 }
Tim Peters5aa91602002-01-30 05:46:57 +00005078
Martin v. Löwis14694662006-02-03 12:54:16 +00005079 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005080}
5081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005082PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005083"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005084Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005085connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005086
5087static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005088posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005089{
5090 int fd;
5091 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5092 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005093 if (!_PyVerify_fd(fd))
5094 return PyBool_FromLong(0);
Fred Drake106c1a02002-04-23 15:58:02 +00005095 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005096}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005097
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005098#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005099PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005100"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005101Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005102
Barry Warsaw53699e91996-12-10 23:23:01 +00005103static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005104posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005105{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005106#if defined(PYOS_OS2)
5107 HFILE read, write;
5108 APIRET rc;
5109
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005110 Py_BEGIN_ALLOW_THREADS
5111 rc = DosCreatePipe( &read, &write, 4096);
5112 Py_END_ALLOW_THREADS
5113 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005114 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005115
5116 return Py_BuildValue("(ii)", read, write);
5117#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005118#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005119 int fds[2];
5120 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005121 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005122 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005123 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005124 if (res != 0)
5125 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005126 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005127#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005128 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005129 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005130 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005131 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005132 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005133 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005134 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005135 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005136 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5137 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005138 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005139#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005140#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005141}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005142#endif /* HAVE_PIPE */
5143
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005144
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005145#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005146PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005147"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005148Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005149
Barry Warsaw53699e91996-12-10 23:23:01 +00005150static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005151posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005152{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005153 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005154 int mode = 0666;
5155 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005156 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005157 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005158 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005159 res = mkfifo(filename, mode);
5160 Py_END_ALLOW_THREADS
5161 if (res < 0)
5162 return posix_error();
5163 Py_INCREF(Py_None);
5164 return Py_None;
5165}
5166#endif
5167
5168
Neal Norwitz11690112002-07-30 01:08:28 +00005169#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005170PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005171"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005172Create a filesystem node (file, device special file or named pipe)\n\
5173named filename. mode specifies both the permissions to use and the\n\
5174type of node to be created, being combined (bitwise OR) with one of\n\
5175S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005176device defines the newly created device special file (probably using\n\
5177os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005178
5179
5180static PyObject *
5181posix_mknod(PyObject *self, PyObject *args)
5182{
5183 char *filename;
5184 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005185 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005186 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005187 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005188 return NULL;
5189 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005190 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005191 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005192 if (res < 0)
5193 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005194 Py_INCREF(Py_None);
5195 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005196}
5197#endif
5198
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005199#ifdef HAVE_DEVICE_MACROS
5200PyDoc_STRVAR(posix_major__doc__,
5201"major(device) -> major number\n\
5202Extracts a device major number from a raw device number.");
5203
5204static PyObject *
5205posix_major(PyObject *self, PyObject *args)
5206{
5207 int device;
5208 if (!PyArg_ParseTuple(args, "i:major", &device))
5209 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005210 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005211}
5212
5213PyDoc_STRVAR(posix_minor__doc__,
5214"minor(device) -> minor number\n\
5215Extracts a device minor number from a raw device number.");
5216
5217static PyObject *
5218posix_minor(PyObject *self, PyObject *args)
5219{
5220 int device;
5221 if (!PyArg_ParseTuple(args, "i:minor", &device))
5222 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005223 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005224}
5225
5226PyDoc_STRVAR(posix_makedev__doc__,
5227"makedev(major, minor) -> device number\n\
5228Composes a raw device number from the major and minor device numbers.");
5229
5230static PyObject *
5231posix_makedev(PyObject *self, PyObject *args)
5232{
5233 int major, minor;
5234 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5235 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005236 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005237}
5238#endif /* device macros */
5239
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005240
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005241#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005242PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005243"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005244Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005245
Barry Warsaw53699e91996-12-10 23:23:01 +00005246static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005247posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005248{
5249 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005250 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005251 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005252 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005253
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005254 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005255 return NULL;
5256
5257#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005258 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005259#else
5260 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005261 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005262#endif
5263 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005264 return NULL;
5265
Barry Warsaw53699e91996-12-10 23:23:01 +00005266 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005267 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005268 Py_END_ALLOW_THREADS
Benjamin Peterson9053d752009-01-19 17:53:36 +00005269 if (res < 0)
5270 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005271 Py_INCREF(Py_None);
5272 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005273}
5274#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005275
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005276#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005277PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005278"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005279Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005280
Fred Drake762e2061999-08-26 17:23:54 +00005281/* Save putenv() parameters as values here, so we can collect them when they
5282 * get re-set with another call for the same key. */
5283static PyObject *posix_putenv_garbage;
5284
Tim Peters5aa91602002-01-30 05:46:57 +00005285static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005286posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005287{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005288#ifdef MS_WINDOWS
5289 wchar_t *s1, *s2;
5290 wchar_t *newenv;
5291#else
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005292 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005293 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005294#endif
Fred Drake762e2061999-08-26 17:23:54 +00005295 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005296 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005297
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005298 if (!PyArg_ParseTuple(args,
5299#ifdef MS_WINDOWS
5300 "uu:putenv",
5301#else
5302 "ss:putenv",
5303#endif
5304 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005305 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005306
5307#if defined(PYOS_OS2)
5308 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5309 APIRET rc;
5310
Guido van Rossumd48f2521997-12-05 22:19:34 +00005311 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5312 if (rc != NO_ERROR)
5313 return os2_error(rc);
5314
5315 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5316 APIRET rc;
5317
Guido van Rossumd48f2521997-12-05 22:19:34 +00005318 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5319 if (rc != NO_ERROR)
5320 return os2_error(rc);
5321 } else {
5322#endif
Fred Drake762e2061999-08-26 17:23:54 +00005323 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005324 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005325 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005326#ifdef MS_WINDOWS
5327 len = wcslen(s1) + wcslen(s2) + 2;
5328 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5329#else
5330 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005331 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005332#endif
Fred Drake762e2061999-08-26 17:23:54 +00005333 if (newstr == NULL)
5334 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005335#ifdef MS_WINDOWS
5336 newenv = PyUnicode_AsUnicode(newstr);
5337 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5338 if (_wputenv(newenv)) {
5339 Py_DECREF(newstr);
5340 posix_error();
5341 return NULL;
5342 }
5343#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005344 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005345 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5346 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005347 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005348 posix_error();
5349 return NULL;
5350 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005351#endif
Fred Drake762e2061999-08-26 17:23:54 +00005352 /* Install the first arg and newstr in posix_putenv_garbage;
5353 * this will cause previous value to be collected. This has to
5354 * happen after the real putenv() call because the old value
5355 * was still accessible until then. */
5356 if (PyDict_SetItem(posix_putenv_garbage,
5357 PyTuple_GET_ITEM(args, 0), newstr)) {
5358 /* really not much we can do; just leak */
5359 PyErr_Clear();
5360 }
5361 else {
5362 Py_DECREF(newstr);
5363 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005364
5365#if defined(PYOS_OS2)
5366 }
5367#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005368 Py_INCREF(Py_None);
5369 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005370}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005371#endif /* putenv */
5372
Guido van Rossumc524d952001-10-19 01:31:59 +00005373#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005374PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005375"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005376Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005377
5378static PyObject *
5379posix_unsetenv(PyObject *self, PyObject *args)
5380{
5381 char *s1;
5382
5383 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5384 return NULL;
5385
5386 unsetenv(s1);
5387
5388 /* Remove the key from posix_putenv_garbage;
5389 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005390 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005391 * old value was still accessible until then.
5392 */
5393 if (PyDict_DelItem(posix_putenv_garbage,
5394 PyTuple_GET_ITEM(args, 0))) {
5395 /* really not much we can do; just leak */
5396 PyErr_Clear();
5397 }
5398
5399 Py_INCREF(Py_None);
5400 return Py_None;
5401}
5402#endif /* unsetenv */
5403
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005404PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005405"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005406Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005407
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005408static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005409posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005410{
5411 int code;
5412 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005413 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005414 return NULL;
5415 message = strerror(code);
5416 if (message == NULL) {
5417 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005418 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005419 return NULL;
5420 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005421 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005422}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005423
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005424
Guido van Rossumc9641791998-08-04 15:26:23 +00005425#ifdef HAVE_SYS_WAIT_H
5426
Fred Drake106c1a02002-04-23 15:58:02 +00005427#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005428PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005429"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005430Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005431
5432static PyObject *
5433posix_WCOREDUMP(PyObject *self, PyObject *args)
5434{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005435 WAIT_TYPE status;
5436 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005437
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005438 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005439 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005440
5441 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005442}
5443#endif /* WCOREDUMP */
5444
5445#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005446PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005447"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005448Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005449job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005450
5451static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005452posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005453{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005454 WAIT_TYPE status;
5455 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005456
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005457 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005458 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005459
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005460 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005461}
5462#endif /* WIFCONTINUED */
5463
Guido van Rossumc9641791998-08-04 15:26:23 +00005464#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005465PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005466"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005467Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005468
5469static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005470posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005471{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005472 WAIT_TYPE status;
5473 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005474
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005475 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005476 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005477
Fred Drake106c1a02002-04-23 15:58:02 +00005478 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005479}
5480#endif /* WIFSTOPPED */
5481
5482#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005483PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005484"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005485Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005486
5487static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005488posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005489{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005490 WAIT_TYPE status;
5491 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005492
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005493 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005494 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005495
Fred Drake106c1a02002-04-23 15:58:02 +00005496 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005497}
5498#endif /* WIFSIGNALED */
5499
5500#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005501PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005502"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005503Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005504system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005505
5506static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005507posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005508{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005509 WAIT_TYPE status;
5510 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005511
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005512 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005513 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005514
Fred Drake106c1a02002-04-23 15:58:02 +00005515 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005516}
5517#endif /* WIFEXITED */
5518
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005519#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005520PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005521"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005522Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005523
5524static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005525posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005526{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005527 WAIT_TYPE status;
5528 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005529
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005530 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005531 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005532
Guido van Rossumc9641791998-08-04 15:26:23 +00005533 return Py_BuildValue("i", WEXITSTATUS(status));
5534}
5535#endif /* WEXITSTATUS */
5536
5537#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005538PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005539"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005540Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005541value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005542
5543static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005544posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005545{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005546 WAIT_TYPE status;
5547 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005548
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005549 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005550 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005551
Guido van Rossumc9641791998-08-04 15:26:23 +00005552 return Py_BuildValue("i", WTERMSIG(status));
5553}
5554#endif /* WTERMSIG */
5555
5556#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005557PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005558"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005559Return the signal that stopped the process that provided\n\
5560the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005561
5562static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005563posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005564{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005565 WAIT_TYPE status;
5566 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005567
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005568 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005569 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005570
Guido van Rossumc9641791998-08-04 15:26:23 +00005571 return Py_BuildValue("i", WSTOPSIG(status));
5572}
5573#endif /* WSTOPSIG */
5574
5575#endif /* HAVE_SYS_WAIT_H */
5576
5577
Thomas Wouters477c8d52006-05-27 19:21:47 +00005578#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005579#ifdef _SCO_DS
5580/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5581 needed definitions in sys/statvfs.h */
5582#define _SVID3
5583#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005584#include <sys/statvfs.h>
5585
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005586static PyObject*
5587_pystatvfs_fromstructstatvfs(struct statvfs st) {
5588 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5589 if (v == NULL)
5590 return NULL;
5591
5592#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005593 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5594 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5595 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5596 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5597 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5598 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5599 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5600 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5601 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5602 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005603#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005604 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5605 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005606 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005607 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005608 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005609 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005610 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005611 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005612 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005613 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005614 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005615 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005616 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005617 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005618 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5619 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005620#endif
5621
5622 return v;
5623}
5624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005625PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005626"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005627Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005628
5629static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005630posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005631{
5632 int fd, res;
5633 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005634
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005635 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005636 return NULL;
5637 Py_BEGIN_ALLOW_THREADS
5638 res = fstatvfs(fd, &st);
5639 Py_END_ALLOW_THREADS
5640 if (res != 0)
5641 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005642
5643 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005644}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005645#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005646
5647
Thomas Wouters477c8d52006-05-27 19:21:47 +00005648#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005649#include <sys/statvfs.h>
5650
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005651PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005652"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005653Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005654
5655static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005656posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005657{
5658 char *path;
5659 int res;
5660 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005661 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005662 return NULL;
5663 Py_BEGIN_ALLOW_THREADS
5664 res = statvfs(path, &st);
5665 Py_END_ALLOW_THREADS
5666 if (res != 0)
5667 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005668
5669 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005670}
5671#endif /* HAVE_STATVFS */
5672
Fred Drakec9680921999-12-13 16:37:25 +00005673/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5674 * It maps strings representing configuration variable names to
5675 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005676 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005677 * rarely-used constants. There are three separate tables that use
5678 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005679 *
5680 * This code is always included, even if none of the interfaces that
5681 * need it are included. The #if hackery needed to avoid it would be
5682 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005683 */
5684struct constdef {
5685 char *name;
5686 long value;
5687};
5688
Fred Drake12c6e2d1999-12-14 21:25:03 +00005689static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005690conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005691 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005692{
Christian Heimes217cfd12007-12-02 14:31:20 +00005693 if (PyLong_Check(arg)) {
5694 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005695 return 1;
5696 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005697 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005698 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005699 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005700 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005701 size_t hi = tablesize;
5702 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005703 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005704 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005705 PyErr_SetString(PyExc_TypeError,
5706 "configuration names must be strings or integers");
5707 return 0;
5708 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005709 confname = _PyUnicode_AsString(arg);
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005710 if (confname == NULL)
5711 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005712 while (lo < hi) {
5713 mid = (lo + hi) / 2;
5714 cmp = strcmp(confname, table[mid].name);
5715 if (cmp < 0)
5716 hi = mid;
5717 else if (cmp > 0)
5718 lo = mid + 1;
5719 else {
5720 *valuep = table[mid].value;
5721 return 1;
5722 }
5723 }
5724 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005725 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005726 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005727}
5728
5729
5730#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5731static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005732#ifdef _PC_ABI_AIO_XFER_MAX
5733 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5734#endif
5735#ifdef _PC_ABI_ASYNC_IO
5736 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5737#endif
Fred Drakec9680921999-12-13 16:37:25 +00005738#ifdef _PC_ASYNC_IO
5739 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5740#endif
5741#ifdef _PC_CHOWN_RESTRICTED
5742 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5743#endif
5744#ifdef _PC_FILESIZEBITS
5745 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5746#endif
5747#ifdef _PC_LAST
5748 {"PC_LAST", _PC_LAST},
5749#endif
5750#ifdef _PC_LINK_MAX
5751 {"PC_LINK_MAX", _PC_LINK_MAX},
5752#endif
5753#ifdef _PC_MAX_CANON
5754 {"PC_MAX_CANON", _PC_MAX_CANON},
5755#endif
5756#ifdef _PC_MAX_INPUT
5757 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5758#endif
5759#ifdef _PC_NAME_MAX
5760 {"PC_NAME_MAX", _PC_NAME_MAX},
5761#endif
5762#ifdef _PC_NO_TRUNC
5763 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5764#endif
5765#ifdef _PC_PATH_MAX
5766 {"PC_PATH_MAX", _PC_PATH_MAX},
5767#endif
5768#ifdef _PC_PIPE_BUF
5769 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5770#endif
5771#ifdef _PC_PRIO_IO
5772 {"PC_PRIO_IO", _PC_PRIO_IO},
5773#endif
5774#ifdef _PC_SOCK_MAXBUF
5775 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5776#endif
5777#ifdef _PC_SYNC_IO
5778 {"PC_SYNC_IO", _PC_SYNC_IO},
5779#endif
5780#ifdef _PC_VDISABLE
5781 {"PC_VDISABLE", _PC_VDISABLE},
5782#endif
5783};
5784
Fred Drakec9680921999-12-13 16:37:25 +00005785static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005786conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005787{
5788 return conv_confname(arg, valuep, posix_constants_pathconf,
5789 sizeof(posix_constants_pathconf)
5790 / sizeof(struct constdef));
5791}
5792#endif
5793
5794#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005795PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005796"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005797Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005798If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005799
5800static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005801posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005802{
5803 PyObject *result = NULL;
5804 int name, fd;
5805
Fred Drake12c6e2d1999-12-14 21:25:03 +00005806 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5807 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005808 long limit;
5809
5810 errno = 0;
5811 limit = fpathconf(fd, name);
5812 if (limit == -1 && errno != 0)
5813 posix_error();
5814 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005815 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005816 }
5817 return result;
5818}
5819#endif
5820
5821
5822#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005823PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005824"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005825Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005826If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005827
5828static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005829posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005830{
5831 PyObject *result = NULL;
5832 int name;
5833 char *path;
5834
5835 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5836 conv_path_confname, &name)) {
5837 long limit;
5838
5839 errno = 0;
5840 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005841 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005842 if (errno == EINVAL)
5843 /* could be a path or name problem */
5844 posix_error();
5845 else
5846 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005847 }
Fred Drakec9680921999-12-13 16:37:25 +00005848 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005849 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005850 }
5851 return result;
5852}
5853#endif
5854
5855#ifdef HAVE_CONFSTR
5856static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005857#ifdef _CS_ARCHITECTURE
5858 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5859#endif
5860#ifdef _CS_HOSTNAME
5861 {"CS_HOSTNAME", _CS_HOSTNAME},
5862#endif
5863#ifdef _CS_HW_PROVIDER
5864 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5865#endif
5866#ifdef _CS_HW_SERIAL
5867 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5868#endif
5869#ifdef _CS_INITTAB_NAME
5870 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5871#endif
Fred Drakec9680921999-12-13 16:37:25 +00005872#ifdef _CS_LFS64_CFLAGS
5873 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5874#endif
5875#ifdef _CS_LFS64_LDFLAGS
5876 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5877#endif
5878#ifdef _CS_LFS64_LIBS
5879 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5880#endif
5881#ifdef _CS_LFS64_LINTFLAGS
5882 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5883#endif
5884#ifdef _CS_LFS_CFLAGS
5885 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5886#endif
5887#ifdef _CS_LFS_LDFLAGS
5888 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5889#endif
5890#ifdef _CS_LFS_LIBS
5891 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5892#endif
5893#ifdef _CS_LFS_LINTFLAGS
5894 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5895#endif
Fred Draked86ed291999-12-15 15:34:33 +00005896#ifdef _CS_MACHINE
5897 {"CS_MACHINE", _CS_MACHINE},
5898#endif
Fred Drakec9680921999-12-13 16:37:25 +00005899#ifdef _CS_PATH
5900 {"CS_PATH", _CS_PATH},
5901#endif
Fred Draked86ed291999-12-15 15:34:33 +00005902#ifdef _CS_RELEASE
5903 {"CS_RELEASE", _CS_RELEASE},
5904#endif
5905#ifdef _CS_SRPC_DOMAIN
5906 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5907#endif
5908#ifdef _CS_SYSNAME
5909 {"CS_SYSNAME", _CS_SYSNAME},
5910#endif
5911#ifdef _CS_VERSION
5912 {"CS_VERSION", _CS_VERSION},
5913#endif
Fred Drakec9680921999-12-13 16:37:25 +00005914#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5915 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5916#endif
5917#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5918 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5919#endif
5920#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5921 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5922#endif
5923#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5924 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5925#endif
5926#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5927 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5928#endif
5929#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5930 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5931#endif
5932#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5933 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5934#endif
5935#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5936 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5937#endif
5938#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5939 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5940#endif
5941#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5942 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5943#endif
5944#ifdef _CS_XBS5_LP64_OFF64_LIBS
5945 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5946#endif
5947#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5948 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5949#endif
5950#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5951 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5952#endif
5953#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5954 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5955#endif
5956#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5957 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5958#endif
5959#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5960 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5961#endif
Fred Draked86ed291999-12-15 15:34:33 +00005962#ifdef _MIPS_CS_AVAIL_PROCESSORS
5963 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5964#endif
5965#ifdef _MIPS_CS_BASE
5966 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5967#endif
5968#ifdef _MIPS_CS_HOSTID
5969 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5970#endif
5971#ifdef _MIPS_CS_HW_NAME
5972 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5973#endif
5974#ifdef _MIPS_CS_NUM_PROCESSORS
5975 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5976#endif
5977#ifdef _MIPS_CS_OSREL_MAJ
5978 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5979#endif
5980#ifdef _MIPS_CS_OSREL_MIN
5981 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5982#endif
5983#ifdef _MIPS_CS_OSREL_PATCH
5984 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5985#endif
5986#ifdef _MIPS_CS_OS_NAME
5987 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5988#endif
5989#ifdef _MIPS_CS_OS_PROVIDER
5990 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5991#endif
5992#ifdef _MIPS_CS_PROCESSORS
5993 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5994#endif
5995#ifdef _MIPS_CS_SERIAL
5996 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5997#endif
5998#ifdef _MIPS_CS_VENDOR
5999 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6000#endif
Fred Drakec9680921999-12-13 16:37:25 +00006001};
6002
6003static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006004conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006005{
6006 return conv_confname(arg, valuep, posix_constants_confstr,
6007 sizeof(posix_constants_confstr)
6008 / sizeof(struct constdef));
6009}
6010
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006011PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006012"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006013Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006014
6015static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006016posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006017{
6018 PyObject *result = NULL;
6019 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006020 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006021
6022 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006023 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006024
Fred Drakec9680921999-12-13 16:37:25 +00006025 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006026 len = confstr(name, buffer, sizeof(buffer));
6027 if (len == 0) {
6028 if (errno) {
6029 posix_error();
6030 }
6031 else {
6032 result = Py_None;
6033 Py_INCREF(Py_None);
6034 }
Fred Drakec9680921999-12-13 16:37:25 +00006035 }
6036 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006037 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006038 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006039 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006040 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006041 }
6042 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006043 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006044 }
6045 }
6046 return result;
6047}
6048#endif
6049
6050
6051#ifdef HAVE_SYSCONF
6052static struct constdef posix_constants_sysconf[] = {
6053#ifdef _SC_2_CHAR_TERM
6054 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6055#endif
6056#ifdef _SC_2_C_BIND
6057 {"SC_2_C_BIND", _SC_2_C_BIND},
6058#endif
6059#ifdef _SC_2_C_DEV
6060 {"SC_2_C_DEV", _SC_2_C_DEV},
6061#endif
6062#ifdef _SC_2_C_VERSION
6063 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6064#endif
6065#ifdef _SC_2_FORT_DEV
6066 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6067#endif
6068#ifdef _SC_2_FORT_RUN
6069 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6070#endif
6071#ifdef _SC_2_LOCALEDEF
6072 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6073#endif
6074#ifdef _SC_2_SW_DEV
6075 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6076#endif
6077#ifdef _SC_2_UPE
6078 {"SC_2_UPE", _SC_2_UPE},
6079#endif
6080#ifdef _SC_2_VERSION
6081 {"SC_2_VERSION", _SC_2_VERSION},
6082#endif
Fred Draked86ed291999-12-15 15:34:33 +00006083#ifdef _SC_ABI_ASYNCHRONOUS_IO
6084 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6085#endif
6086#ifdef _SC_ACL
6087 {"SC_ACL", _SC_ACL},
6088#endif
Fred Drakec9680921999-12-13 16:37:25 +00006089#ifdef _SC_AIO_LISTIO_MAX
6090 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6091#endif
Fred Drakec9680921999-12-13 16:37:25 +00006092#ifdef _SC_AIO_MAX
6093 {"SC_AIO_MAX", _SC_AIO_MAX},
6094#endif
6095#ifdef _SC_AIO_PRIO_DELTA_MAX
6096 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6097#endif
6098#ifdef _SC_ARG_MAX
6099 {"SC_ARG_MAX", _SC_ARG_MAX},
6100#endif
6101#ifdef _SC_ASYNCHRONOUS_IO
6102 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6103#endif
6104#ifdef _SC_ATEXIT_MAX
6105 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6106#endif
Fred Draked86ed291999-12-15 15:34:33 +00006107#ifdef _SC_AUDIT
6108 {"SC_AUDIT", _SC_AUDIT},
6109#endif
Fred Drakec9680921999-12-13 16:37:25 +00006110#ifdef _SC_AVPHYS_PAGES
6111 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6112#endif
6113#ifdef _SC_BC_BASE_MAX
6114 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6115#endif
6116#ifdef _SC_BC_DIM_MAX
6117 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6118#endif
6119#ifdef _SC_BC_SCALE_MAX
6120 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6121#endif
6122#ifdef _SC_BC_STRING_MAX
6123 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6124#endif
Fred Draked86ed291999-12-15 15:34:33 +00006125#ifdef _SC_CAP
6126 {"SC_CAP", _SC_CAP},
6127#endif
Fred Drakec9680921999-12-13 16:37:25 +00006128#ifdef _SC_CHARCLASS_NAME_MAX
6129 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6130#endif
6131#ifdef _SC_CHAR_BIT
6132 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6133#endif
6134#ifdef _SC_CHAR_MAX
6135 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6136#endif
6137#ifdef _SC_CHAR_MIN
6138 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6139#endif
6140#ifdef _SC_CHILD_MAX
6141 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6142#endif
6143#ifdef _SC_CLK_TCK
6144 {"SC_CLK_TCK", _SC_CLK_TCK},
6145#endif
6146#ifdef _SC_COHER_BLKSZ
6147 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6148#endif
6149#ifdef _SC_COLL_WEIGHTS_MAX
6150 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6151#endif
6152#ifdef _SC_DCACHE_ASSOC
6153 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6154#endif
6155#ifdef _SC_DCACHE_BLKSZ
6156 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6157#endif
6158#ifdef _SC_DCACHE_LINESZ
6159 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6160#endif
6161#ifdef _SC_DCACHE_SZ
6162 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6163#endif
6164#ifdef _SC_DCACHE_TBLKSZ
6165 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6166#endif
6167#ifdef _SC_DELAYTIMER_MAX
6168 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6169#endif
6170#ifdef _SC_EQUIV_CLASS_MAX
6171 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6172#endif
6173#ifdef _SC_EXPR_NEST_MAX
6174 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6175#endif
6176#ifdef _SC_FSYNC
6177 {"SC_FSYNC", _SC_FSYNC},
6178#endif
6179#ifdef _SC_GETGR_R_SIZE_MAX
6180 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6181#endif
6182#ifdef _SC_GETPW_R_SIZE_MAX
6183 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6184#endif
6185#ifdef _SC_ICACHE_ASSOC
6186 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6187#endif
6188#ifdef _SC_ICACHE_BLKSZ
6189 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6190#endif
6191#ifdef _SC_ICACHE_LINESZ
6192 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6193#endif
6194#ifdef _SC_ICACHE_SZ
6195 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6196#endif
Fred Draked86ed291999-12-15 15:34:33 +00006197#ifdef _SC_INF
6198 {"SC_INF", _SC_INF},
6199#endif
Fred Drakec9680921999-12-13 16:37:25 +00006200#ifdef _SC_INT_MAX
6201 {"SC_INT_MAX", _SC_INT_MAX},
6202#endif
6203#ifdef _SC_INT_MIN
6204 {"SC_INT_MIN", _SC_INT_MIN},
6205#endif
6206#ifdef _SC_IOV_MAX
6207 {"SC_IOV_MAX", _SC_IOV_MAX},
6208#endif
Fred Draked86ed291999-12-15 15:34:33 +00006209#ifdef _SC_IP_SECOPTS
6210 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6211#endif
Fred Drakec9680921999-12-13 16:37:25 +00006212#ifdef _SC_JOB_CONTROL
6213 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6214#endif
Fred Draked86ed291999-12-15 15:34:33 +00006215#ifdef _SC_KERN_POINTERS
6216 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6217#endif
6218#ifdef _SC_KERN_SIM
6219 {"SC_KERN_SIM", _SC_KERN_SIM},
6220#endif
Fred Drakec9680921999-12-13 16:37:25 +00006221#ifdef _SC_LINE_MAX
6222 {"SC_LINE_MAX", _SC_LINE_MAX},
6223#endif
6224#ifdef _SC_LOGIN_NAME_MAX
6225 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6226#endif
6227#ifdef _SC_LOGNAME_MAX
6228 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6229#endif
6230#ifdef _SC_LONG_BIT
6231 {"SC_LONG_BIT", _SC_LONG_BIT},
6232#endif
Fred Draked86ed291999-12-15 15:34:33 +00006233#ifdef _SC_MAC
6234 {"SC_MAC", _SC_MAC},
6235#endif
Fred Drakec9680921999-12-13 16:37:25 +00006236#ifdef _SC_MAPPED_FILES
6237 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6238#endif
6239#ifdef _SC_MAXPID
6240 {"SC_MAXPID", _SC_MAXPID},
6241#endif
6242#ifdef _SC_MB_LEN_MAX
6243 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6244#endif
6245#ifdef _SC_MEMLOCK
6246 {"SC_MEMLOCK", _SC_MEMLOCK},
6247#endif
6248#ifdef _SC_MEMLOCK_RANGE
6249 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6250#endif
6251#ifdef _SC_MEMORY_PROTECTION
6252 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6253#endif
6254#ifdef _SC_MESSAGE_PASSING
6255 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6256#endif
Fred Draked86ed291999-12-15 15:34:33 +00006257#ifdef _SC_MMAP_FIXED_ALIGNMENT
6258 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6259#endif
Fred Drakec9680921999-12-13 16:37:25 +00006260#ifdef _SC_MQ_OPEN_MAX
6261 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6262#endif
6263#ifdef _SC_MQ_PRIO_MAX
6264 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6265#endif
Fred Draked86ed291999-12-15 15:34:33 +00006266#ifdef _SC_NACLS_MAX
6267 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6268#endif
Fred Drakec9680921999-12-13 16:37:25 +00006269#ifdef _SC_NGROUPS_MAX
6270 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6271#endif
6272#ifdef _SC_NL_ARGMAX
6273 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6274#endif
6275#ifdef _SC_NL_LANGMAX
6276 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6277#endif
6278#ifdef _SC_NL_MSGMAX
6279 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6280#endif
6281#ifdef _SC_NL_NMAX
6282 {"SC_NL_NMAX", _SC_NL_NMAX},
6283#endif
6284#ifdef _SC_NL_SETMAX
6285 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6286#endif
6287#ifdef _SC_NL_TEXTMAX
6288 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6289#endif
6290#ifdef _SC_NPROCESSORS_CONF
6291 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6292#endif
6293#ifdef _SC_NPROCESSORS_ONLN
6294 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6295#endif
Fred Draked86ed291999-12-15 15:34:33 +00006296#ifdef _SC_NPROC_CONF
6297 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6298#endif
6299#ifdef _SC_NPROC_ONLN
6300 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6301#endif
Fred Drakec9680921999-12-13 16:37:25 +00006302#ifdef _SC_NZERO
6303 {"SC_NZERO", _SC_NZERO},
6304#endif
6305#ifdef _SC_OPEN_MAX
6306 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6307#endif
6308#ifdef _SC_PAGESIZE
6309 {"SC_PAGESIZE", _SC_PAGESIZE},
6310#endif
6311#ifdef _SC_PAGE_SIZE
6312 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6313#endif
6314#ifdef _SC_PASS_MAX
6315 {"SC_PASS_MAX", _SC_PASS_MAX},
6316#endif
6317#ifdef _SC_PHYS_PAGES
6318 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6319#endif
6320#ifdef _SC_PII
6321 {"SC_PII", _SC_PII},
6322#endif
6323#ifdef _SC_PII_INTERNET
6324 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6325#endif
6326#ifdef _SC_PII_INTERNET_DGRAM
6327 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6328#endif
6329#ifdef _SC_PII_INTERNET_STREAM
6330 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6331#endif
6332#ifdef _SC_PII_OSI
6333 {"SC_PII_OSI", _SC_PII_OSI},
6334#endif
6335#ifdef _SC_PII_OSI_CLTS
6336 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6337#endif
6338#ifdef _SC_PII_OSI_COTS
6339 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6340#endif
6341#ifdef _SC_PII_OSI_M
6342 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6343#endif
6344#ifdef _SC_PII_SOCKET
6345 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6346#endif
6347#ifdef _SC_PII_XTI
6348 {"SC_PII_XTI", _SC_PII_XTI},
6349#endif
6350#ifdef _SC_POLL
6351 {"SC_POLL", _SC_POLL},
6352#endif
6353#ifdef _SC_PRIORITIZED_IO
6354 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6355#endif
6356#ifdef _SC_PRIORITY_SCHEDULING
6357 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6358#endif
6359#ifdef _SC_REALTIME_SIGNALS
6360 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6361#endif
6362#ifdef _SC_RE_DUP_MAX
6363 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6364#endif
6365#ifdef _SC_RTSIG_MAX
6366 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6367#endif
6368#ifdef _SC_SAVED_IDS
6369 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6370#endif
6371#ifdef _SC_SCHAR_MAX
6372 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6373#endif
6374#ifdef _SC_SCHAR_MIN
6375 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6376#endif
6377#ifdef _SC_SELECT
6378 {"SC_SELECT", _SC_SELECT},
6379#endif
6380#ifdef _SC_SEMAPHORES
6381 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6382#endif
6383#ifdef _SC_SEM_NSEMS_MAX
6384 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6385#endif
6386#ifdef _SC_SEM_VALUE_MAX
6387 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6388#endif
6389#ifdef _SC_SHARED_MEMORY_OBJECTS
6390 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6391#endif
6392#ifdef _SC_SHRT_MAX
6393 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6394#endif
6395#ifdef _SC_SHRT_MIN
6396 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6397#endif
6398#ifdef _SC_SIGQUEUE_MAX
6399 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6400#endif
6401#ifdef _SC_SIGRT_MAX
6402 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6403#endif
6404#ifdef _SC_SIGRT_MIN
6405 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6406#endif
Fred Draked86ed291999-12-15 15:34:33 +00006407#ifdef _SC_SOFTPOWER
6408 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6409#endif
Fred Drakec9680921999-12-13 16:37:25 +00006410#ifdef _SC_SPLIT_CACHE
6411 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6412#endif
6413#ifdef _SC_SSIZE_MAX
6414 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6415#endif
6416#ifdef _SC_STACK_PROT
6417 {"SC_STACK_PROT", _SC_STACK_PROT},
6418#endif
6419#ifdef _SC_STREAM_MAX
6420 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6421#endif
6422#ifdef _SC_SYNCHRONIZED_IO
6423 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6424#endif
6425#ifdef _SC_THREADS
6426 {"SC_THREADS", _SC_THREADS},
6427#endif
6428#ifdef _SC_THREAD_ATTR_STACKADDR
6429 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6430#endif
6431#ifdef _SC_THREAD_ATTR_STACKSIZE
6432 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6433#endif
6434#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6435 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6436#endif
6437#ifdef _SC_THREAD_KEYS_MAX
6438 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6439#endif
6440#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6441 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6442#endif
6443#ifdef _SC_THREAD_PRIO_INHERIT
6444 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6445#endif
6446#ifdef _SC_THREAD_PRIO_PROTECT
6447 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6448#endif
6449#ifdef _SC_THREAD_PROCESS_SHARED
6450 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6451#endif
6452#ifdef _SC_THREAD_SAFE_FUNCTIONS
6453 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6454#endif
6455#ifdef _SC_THREAD_STACK_MIN
6456 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6457#endif
6458#ifdef _SC_THREAD_THREADS_MAX
6459 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6460#endif
6461#ifdef _SC_TIMERS
6462 {"SC_TIMERS", _SC_TIMERS},
6463#endif
6464#ifdef _SC_TIMER_MAX
6465 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6466#endif
6467#ifdef _SC_TTY_NAME_MAX
6468 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6469#endif
6470#ifdef _SC_TZNAME_MAX
6471 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6472#endif
6473#ifdef _SC_T_IOV_MAX
6474 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6475#endif
6476#ifdef _SC_UCHAR_MAX
6477 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6478#endif
6479#ifdef _SC_UINT_MAX
6480 {"SC_UINT_MAX", _SC_UINT_MAX},
6481#endif
6482#ifdef _SC_UIO_MAXIOV
6483 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6484#endif
6485#ifdef _SC_ULONG_MAX
6486 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6487#endif
6488#ifdef _SC_USHRT_MAX
6489 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6490#endif
6491#ifdef _SC_VERSION
6492 {"SC_VERSION", _SC_VERSION},
6493#endif
6494#ifdef _SC_WORD_BIT
6495 {"SC_WORD_BIT", _SC_WORD_BIT},
6496#endif
6497#ifdef _SC_XBS5_ILP32_OFF32
6498 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6499#endif
6500#ifdef _SC_XBS5_ILP32_OFFBIG
6501 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6502#endif
6503#ifdef _SC_XBS5_LP64_OFF64
6504 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6505#endif
6506#ifdef _SC_XBS5_LPBIG_OFFBIG
6507 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6508#endif
6509#ifdef _SC_XOPEN_CRYPT
6510 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6511#endif
6512#ifdef _SC_XOPEN_ENH_I18N
6513 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6514#endif
6515#ifdef _SC_XOPEN_LEGACY
6516 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6517#endif
6518#ifdef _SC_XOPEN_REALTIME
6519 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6520#endif
6521#ifdef _SC_XOPEN_REALTIME_THREADS
6522 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6523#endif
6524#ifdef _SC_XOPEN_SHM
6525 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6526#endif
6527#ifdef _SC_XOPEN_UNIX
6528 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6529#endif
6530#ifdef _SC_XOPEN_VERSION
6531 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6532#endif
6533#ifdef _SC_XOPEN_XCU_VERSION
6534 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6535#endif
6536#ifdef _SC_XOPEN_XPG2
6537 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6538#endif
6539#ifdef _SC_XOPEN_XPG3
6540 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6541#endif
6542#ifdef _SC_XOPEN_XPG4
6543 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6544#endif
6545};
6546
6547static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006548conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006549{
6550 return conv_confname(arg, valuep, posix_constants_sysconf,
6551 sizeof(posix_constants_sysconf)
6552 / sizeof(struct constdef));
6553}
6554
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006555PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006556"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006557Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006558
6559static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006560posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006561{
6562 PyObject *result = NULL;
6563 int name;
6564
6565 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6566 int value;
6567
6568 errno = 0;
6569 value = sysconf(name);
6570 if (value == -1 && errno != 0)
6571 posix_error();
6572 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006573 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006574 }
6575 return result;
6576}
6577#endif
6578
6579
Fred Drakebec628d1999-12-15 18:31:10 +00006580/* This code is used to ensure that the tables of configuration value names
6581 * are in sorted order as required by conv_confname(), and also to build the
6582 * the exported dictionaries that are used to publish information about the
6583 * names available on the host platform.
6584 *
6585 * Sorting the table at runtime ensures that the table is properly ordered
6586 * when used, even for platforms we're not able to test on. It also makes
6587 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006588 */
Fred Drakebec628d1999-12-15 18:31:10 +00006589
6590static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006591cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006592{
6593 const struct constdef *c1 =
6594 (const struct constdef *) v1;
6595 const struct constdef *c2 =
6596 (const struct constdef *) v2;
6597
6598 return strcmp(c1->name, c2->name);
6599}
6600
6601static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006602setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006603 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006604{
Fred Drakebec628d1999-12-15 18:31:10 +00006605 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006606 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006607
6608 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6609 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006610 if (d == NULL)
6611 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006612
Barry Warsaw3155db32000-04-13 15:20:40 +00006613 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006614 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006615 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6616 Py_XDECREF(o);
6617 Py_DECREF(d);
6618 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006619 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006620 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006621 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006622 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006623}
6624
Fred Drakebec628d1999-12-15 18:31:10 +00006625/* Return -1 on failure, 0 on success. */
6626static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006627setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006628{
6629#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006630 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006631 sizeof(posix_constants_pathconf)
6632 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006633 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006634 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006635#endif
6636#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006637 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006638 sizeof(posix_constants_confstr)
6639 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006640 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006641 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006642#endif
6643#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006644 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006645 sizeof(posix_constants_sysconf)
6646 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006647 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006648 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006649#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006650 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006651}
Fred Draked86ed291999-12-15 15:34:33 +00006652
6653
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006654PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006655"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006656Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006657in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006658
6659static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006660posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006661{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006662 abort();
6663 /*NOTREACHED*/
6664 Py_FatalError("abort() called from Python code didn't abort!");
6665 return NULL;
6666}
Fred Drakebec628d1999-12-15 18:31:10 +00006667
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006668#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006669PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006670"startfile(filepath [, operation]) - Start a file with its associated\n\
6671application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006672\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006673When \"operation\" is not specified or \"open\", this acts like\n\
6674double-clicking the file in Explorer, or giving the file name as an\n\
6675argument to the DOS \"start\" command: the file is opened with whatever\n\
6676application (if any) its extension is associated.\n\
6677When another \"operation\" is given, it specifies what should be done with\n\
6678the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006679\n\
6680startfile returns as soon as the associated application is launched.\n\
6681There is no option to wait for the application to close, and no way\n\
6682to retrieve the application's exit status.\n\
6683\n\
6684The filepath is relative to the current directory. If you want to use\n\
6685an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006686the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006687
6688static PyObject *
6689win32_startfile(PyObject *self, PyObject *args)
6690{
6691 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006692 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006693 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006694#ifdef Py_WIN_WIDE_FILENAMES
6695 if (unicode_file_names()) {
6696 PyObject *unipath, *woperation = NULL;
6697 if (!PyArg_ParseTuple(args, "U|s:startfile",
6698 &unipath, &operation)) {
6699 PyErr_Clear();
6700 goto normal;
6701 }
6702
6703
6704 if (operation) {
6705 woperation = PyUnicode_DecodeASCII(operation,
6706 strlen(operation), NULL);
6707 if (!woperation) {
6708 PyErr_Clear();
6709 operation = NULL;
6710 goto normal;
6711 }
6712 }
6713
6714 Py_BEGIN_ALLOW_THREADS
6715 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6716 PyUnicode_AS_UNICODE(unipath),
6717 NULL, NULL, SW_SHOWNORMAL);
6718 Py_END_ALLOW_THREADS
6719
6720 Py_XDECREF(woperation);
6721 if (rc <= (HINSTANCE)32) {
6722 PyObject *errval = win32_error_unicode("startfile",
6723 PyUnicode_AS_UNICODE(unipath));
6724 return errval;
6725 }
6726 Py_INCREF(Py_None);
6727 return Py_None;
6728 }
6729#endif
6730
6731normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00006732 if (!PyArg_ParseTuple(args, "et|s:startfile",
6733 Py_FileSystemDefaultEncoding, &filepath,
6734 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006735 return NULL;
6736 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006737 rc = ShellExecute((HWND)0, operation, filepath,
6738 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006739 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006740 if (rc <= (HINSTANCE)32) {
6741 PyObject *errval = win32_error("startfile", filepath);
6742 PyMem_Free(filepath);
6743 return errval;
6744 }
6745 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006746 Py_INCREF(Py_None);
6747 return Py_None;
6748}
6749#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006750
Martin v. Löwis438b5342002-12-27 10:16:42 +00006751#ifdef HAVE_GETLOADAVG
6752PyDoc_STRVAR(posix_getloadavg__doc__,
6753"getloadavg() -> (float, float, float)\n\n\
6754Return the number of processes in the system run queue averaged over\n\
6755the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6756was unobtainable");
6757
6758static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006759posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006760{
6761 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006762 if (getloadavg(loadavg, 3)!=3) {
6763 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6764 return NULL;
6765 } else
6766 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6767}
6768#endif
6769
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006770#ifdef MS_WINDOWS
6771
6772PyDoc_STRVAR(win32_urandom__doc__,
6773"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006774Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006775
6776typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6777 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6778 DWORD dwFlags );
6779typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6780 BYTE *pbBuffer );
6781
6782static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006783/* This handle is never explicitly released. Instead, the operating
6784 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006785static HCRYPTPROV hCryptProv = 0;
6786
Tim Peters4ad82172004-08-30 17:02:04 +00006787static PyObject*
6788win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006789{
Tim Petersd3115382004-08-30 17:36:46 +00006790 int howMany;
6791 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006792
Tim Peters4ad82172004-08-30 17:02:04 +00006793 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006794 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006795 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006796 if (howMany < 0)
6797 return PyErr_Format(PyExc_ValueError,
6798 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006799
Tim Peters4ad82172004-08-30 17:02:04 +00006800 if (hCryptProv == 0) {
6801 HINSTANCE hAdvAPI32 = NULL;
6802 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006803
Tim Peters4ad82172004-08-30 17:02:04 +00006804 /* Obtain handle to the DLL containing CryptoAPI
6805 This should not fail */
6806 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6807 if(hAdvAPI32 == NULL)
6808 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006809
Tim Peters4ad82172004-08-30 17:02:04 +00006810 /* Obtain pointers to the CryptoAPI functions
6811 This will fail on some early versions of Win95 */
6812 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6813 hAdvAPI32,
6814 "CryptAcquireContextA");
6815 if (pCryptAcquireContext == NULL)
6816 return PyErr_Format(PyExc_NotImplementedError,
6817 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006818
Tim Peters4ad82172004-08-30 17:02:04 +00006819 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6820 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006821 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006822 return PyErr_Format(PyExc_NotImplementedError,
6823 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006824
Tim Peters4ad82172004-08-30 17:02:04 +00006825 /* Acquire context */
6826 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6827 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6828 return win32_error("CryptAcquireContext", NULL);
6829 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006830
Tim Peters4ad82172004-08-30 17:02:04 +00006831 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006832 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006833 if (result != NULL) {
6834 /* Get random data */
Amaury Forgeot d'Arca05ada32008-07-21 21:13:14 +00006835 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00006836 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006837 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006838 Py_DECREF(result);
6839 return win32_error("CryptGenRandom", NULL);
6840 }
Tim Peters4ad82172004-08-30 17:02:04 +00006841 }
Tim Petersd3115382004-08-30 17:36:46 +00006842 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006843}
6844#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006845
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006846PyDoc_STRVAR(device_encoding__doc__,
6847"device_encoding(fd) -> str\n\n\
6848Return a string describing the encoding of the device\n\
6849if the output is a terminal; else return None.");
6850
6851static PyObject *
6852device_encoding(PyObject *self, PyObject *args)
6853{
6854 int fd;
6855 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6856 return NULL;
Kristján Valur Jónsson649170b2009-03-24 14:15:49 +00006857 if (!_PyVerify_fd(fd) || !isatty(fd)) {
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006858 Py_INCREF(Py_None);
6859 return Py_None;
6860 }
6861#if defined(MS_WINDOWS) || defined(MS_WIN64)
6862 if (fd == 0) {
6863 char buf[100];
6864 sprintf(buf, "cp%d", GetConsoleCP());
6865 return PyUnicode_FromString(buf);
6866 }
6867 if (fd == 1 || fd == 2) {
6868 char buf[100];
6869 sprintf(buf, "cp%d", GetConsoleOutputCP());
6870 return PyUnicode_FromString(buf);
6871 }
6872#elif defined(CODESET)
6873 {
6874 char *codeset = nl_langinfo(CODESET);
Mark Dickinsonda2706b2008-12-11 18:03:03 +00006875 if (codeset != NULL && codeset[0] != 0)
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006876 return PyUnicode_FromString(codeset);
6877 }
6878#endif
6879 Py_INCREF(Py_None);
6880 return Py_None;
6881}
6882
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006883#ifdef __VMS
6884/* Use openssl random routine */
6885#include <openssl/rand.h>
6886PyDoc_STRVAR(vms_urandom__doc__,
6887"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006888Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006889
6890static PyObject*
6891vms_urandom(PyObject *self, PyObject *args)
6892{
6893 int howMany;
6894 PyObject* result;
6895
6896 /* Read arguments */
6897 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6898 return NULL;
6899 if (howMany < 0)
6900 return PyErr_Format(PyExc_ValueError,
6901 "negative argument not allowed");
6902
6903 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006904 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006905 if (result != NULL) {
6906 /* Get random data */
6907 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006908 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006909 howMany) < 0) {
6910 Py_DECREF(result);
6911 return PyErr_Format(PyExc_ValueError,
6912 "RAND_pseudo_bytes");
6913 }
6914 }
6915 return result;
6916}
6917#endif
6918
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006919static PyMethodDef posix_methods[] = {
6920 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6921#ifdef HAVE_TTYNAME
6922 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6923#endif
6924 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006925#ifdef HAVE_CHFLAGS
6926 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6927#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006928 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00006929#ifdef HAVE_FCHMOD
6930 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
6931#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006932#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006933 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006934#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00006935#ifdef HAVE_LCHMOD
6936 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
6937#endif /* HAVE_LCHMOD */
6938#ifdef HAVE_FCHOWN
6939 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
6940#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006941#ifdef HAVE_LCHFLAGS
6942 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6943#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006944#ifdef HAVE_LCHOWN
6945 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6946#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006947#ifdef HAVE_CHROOT
6948 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6949#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006950#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006951 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006952#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006953#ifdef HAVE_GETCWD
Guido van Rossumf0af3e32008-10-02 18:55:37 +00006954 {"getcwd", (PyCFunction)posix_getcwd_unicode,
6955 METH_NOARGS, posix_getcwd__doc__},
6956 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
6957 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006958#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006959#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006960 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006961#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006962 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6963 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6964 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006965#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006966 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006967#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006968#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006969 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006970#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006971 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6972 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6973 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006974 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006975#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006976 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006977#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006978#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006979 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006980#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006981 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006982#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006983 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006984#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006985 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6986 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6987 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006988#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006989 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006990#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006991 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006992#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006993 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6994 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006995#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006996#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006997 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6998 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006999#if defined(PYOS_OS2)
7000 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7001 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7002#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007003#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007004#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007005 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007006#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007007#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007008 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007009#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007010#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007011 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007012#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007013#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007014 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007015#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007016#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007017 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007018#endif /* HAVE_GETEGID */
7019#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007020 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007021#endif /* HAVE_GETEUID */
7022#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007023 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007024#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007025#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007026 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007027#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007028 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007029#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007030 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007031#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007032#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007033 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007034#endif /* HAVE_GETPPID */
7035#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007036 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007037#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007038#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007039 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007040#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007041#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007042 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007043#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007044#ifdef HAVE_KILLPG
7045 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7046#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007047#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007048 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007049#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007050#ifdef MS_WINDOWS
7051 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7052#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007053#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007054 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007055#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007056#ifdef HAVE_SETEUID
7057 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7058#endif /* HAVE_SETEUID */
7059#ifdef HAVE_SETEGID
7060 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7061#endif /* HAVE_SETEGID */
7062#ifdef HAVE_SETREUID
7063 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7064#endif /* HAVE_SETREUID */
7065#ifdef HAVE_SETREGID
7066 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7067#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007068#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007069 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007070#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007071#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00007072 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007073#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007074#ifdef HAVE_GETPGID
7075 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7076#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007077#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007078 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007079#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007080#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007081 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007082#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007083#ifdef HAVE_WAIT3
7084 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7085#endif /* HAVE_WAIT3 */
7086#ifdef HAVE_WAIT4
7087 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7088#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007089#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007090 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007091#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007092#ifdef HAVE_GETSID
7093 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7094#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007095#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007096 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007097#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007098#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007099 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007100#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007101#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007102 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007103#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007104#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007105 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007106#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007107 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7108 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00007109 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007110 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007111 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7112 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7113 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7114 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7115 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7116 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007117 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007118#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007119 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007120#endif
7121#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007122 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007123#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007124#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007125 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7126#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007127#ifdef HAVE_DEVICE_MACROS
7128 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7129 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7130 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7131#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007132#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007133 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007134#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007135#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007136 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007137#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007138#ifdef HAVE_UNSETENV
7139 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7140#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007141 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007142#ifdef HAVE_FCHDIR
7143 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7144#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007145#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007146 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007147#endif
7148#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007149 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007150#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007151#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007152#ifdef WCOREDUMP
7153 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7154#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007155#ifdef WIFCONTINUED
7156 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7157#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007158#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007159 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007160#endif /* WIFSTOPPED */
7161#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007162 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007163#endif /* WIFSIGNALED */
7164#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007165 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007166#endif /* WIFEXITED */
7167#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007168 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007169#endif /* WEXITSTATUS */
7170#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007171 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007172#endif /* WTERMSIG */
7173#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007174 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007175#endif /* WSTOPSIG */
7176#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007177#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007178 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007179#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007180#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007181 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007182#endif
Fred Drakec9680921999-12-13 16:37:25 +00007183#ifdef HAVE_CONFSTR
7184 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7185#endif
7186#ifdef HAVE_SYSCONF
7187 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7188#endif
7189#ifdef HAVE_FPATHCONF
7190 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7191#endif
7192#ifdef HAVE_PATHCONF
7193 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7194#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007195 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007196#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007197 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7198#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007199#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007200 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007201#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007202 #ifdef MS_WINDOWS
7203 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7204 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007205 #ifdef __VMS
7206 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7207 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007208 {NULL, NULL} /* Sentinel */
7209};
7210
7211
Barry Warsaw4a342091996-12-19 23:50:02 +00007212static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007213ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007214{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007215 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007216}
7217
Guido van Rossumd48f2521997-12-05 22:19:34 +00007218#if defined(PYOS_OS2)
7219/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007220static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007221{
7222 APIRET rc;
7223 ULONG values[QSV_MAX+1];
7224 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007225 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007226
7227 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007228 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007229 Py_END_ALLOW_THREADS
7230
7231 if (rc != NO_ERROR) {
7232 os2_error(rc);
7233 return -1;
7234 }
7235
Fred Drake4d1e64b2002-04-15 19:40:07 +00007236 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7237 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7238 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7239 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7240 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7241 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7242 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007243
7244 switch (values[QSV_VERSION_MINOR]) {
7245 case 0: ver = "2.00"; break;
7246 case 10: ver = "2.10"; break;
7247 case 11: ver = "2.11"; break;
7248 case 30: ver = "3.00"; break;
7249 case 40: ver = "4.00"; break;
7250 case 50: ver = "5.00"; break;
7251 default:
Tim Peters885d4572001-11-28 20:27:42 +00007252 PyOS_snprintf(tmp, sizeof(tmp),
7253 "%d-%d", values[QSV_VERSION_MAJOR],
7254 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007255 ver = &tmp[0];
7256 }
7257
7258 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007259 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007260 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007261
7262 /* Add Indicator of Which Drive was Used to Boot the System */
7263 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7264 tmp[1] = ':';
7265 tmp[2] = '\0';
7266
Fred Drake4d1e64b2002-04-15 19:40:07 +00007267 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007268}
7269#endif
7270
Barry Warsaw4a342091996-12-19 23:50:02 +00007271static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007272all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007273{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007274#ifdef F_OK
7275 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007276#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007277#ifdef R_OK
7278 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007279#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007280#ifdef W_OK
7281 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007282#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007283#ifdef X_OK
7284 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007285#endif
Fred Drakec9680921999-12-13 16:37:25 +00007286#ifdef NGROUPS_MAX
7287 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7288#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007289#ifdef TMP_MAX
7290 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7291#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007292#ifdef WCONTINUED
7293 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7294#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007295#ifdef WNOHANG
7296 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007297#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007298#ifdef WUNTRACED
7299 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7300#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007301#ifdef O_RDONLY
7302 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7303#endif
7304#ifdef O_WRONLY
7305 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7306#endif
7307#ifdef O_RDWR
7308 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7309#endif
7310#ifdef O_NDELAY
7311 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7312#endif
7313#ifdef O_NONBLOCK
7314 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7315#endif
7316#ifdef O_APPEND
7317 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7318#endif
7319#ifdef O_DSYNC
7320 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7321#endif
7322#ifdef O_RSYNC
7323 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7324#endif
7325#ifdef O_SYNC
7326 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7327#endif
7328#ifdef O_NOCTTY
7329 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7330#endif
7331#ifdef O_CREAT
7332 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7333#endif
7334#ifdef O_EXCL
7335 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7336#endif
7337#ifdef O_TRUNC
7338 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7339#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007340#ifdef O_BINARY
7341 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7342#endif
7343#ifdef O_TEXT
7344 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7345#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007346#ifdef O_LARGEFILE
7347 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7348#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007349#ifdef O_SHLOCK
7350 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7351#endif
7352#ifdef O_EXLOCK
7353 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7354#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007355
Tim Peters5aa91602002-01-30 05:46:57 +00007356/* MS Windows */
7357#ifdef O_NOINHERIT
7358 /* Don't inherit in child processes. */
7359 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7360#endif
7361#ifdef _O_SHORT_LIVED
7362 /* Optimize for short life (keep in memory). */
7363 /* MS forgot to define this one with a non-underscore form too. */
7364 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7365#endif
7366#ifdef O_TEMPORARY
7367 /* Automatically delete when last handle is closed. */
7368 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7369#endif
7370#ifdef O_RANDOM
7371 /* Optimize for random access. */
7372 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7373#endif
7374#ifdef O_SEQUENTIAL
7375 /* Optimize for sequential access. */
7376 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7377#endif
7378
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007379/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007380#ifdef O_ASYNC
7381 /* Send a SIGIO signal whenever input or output
7382 becomes available on file descriptor */
7383 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7384#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007385#ifdef O_DIRECT
7386 /* Direct disk access. */
7387 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7388#endif
7389#ifdef O_DIRECTORY
7390 /* Must be a directory. */
7391 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7392#endif
7393#ifdef O_NOFOLLOW
7394 /* Do not follow links. */
7395 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7396#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007397#ifdef O_NOATIME
7398 /* Do not update the access time. */
7399 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7400#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007401
Barry Warsaw5676bd12003-01-07 20:57:09 +00007402 /* These come from sysexits.h */
7403#ifdef EX_OK
7404 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007405#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007406#ifdef EX_USAGE
7407 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007408#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007409#ifdef EX_DATAERR
7410 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007411#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007412#ifdef EX_NOINPUT
7413 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007414#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007415#ifdef EX_NOUSER
7416 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007417#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007418#ifdef EX_NOHOST
7419 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007420#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007421#ifdef EX_UNAVAILABLE
7422 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007423#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007424#ifdef EX_SOFTWARE
7425 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007426#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007427#ifdef EX_OSERR
7428 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007429#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007430#ifdef EX_OSFILE
7431 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007432#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007433#ifdef EX_CANTCREAT
7434 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007435#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007436#ifdef EX_IOERR
7437 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007438#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007439#ifdef EX_TEMPFAIL
7440 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007441#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007442#ifdef EX_PROTOCOL
7443 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007444#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007445#ifdef EX_NOPERM
7446 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007447#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007448#ifdef EX_CONFIG
7449 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007450#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007451#ifdef EX_NOTFOUND
7452 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007453#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007454
Guido van Rossum246bc171999-02-01 23:54:31 +00007455#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007456#if defined(PYOS_OS2) && defined(PYCC_GCC)
7457 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7458 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7459 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7460 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7461 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7462 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7463 if (ins(d, "P_PM", (long)P_PM)) return -1;
7464 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7465 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7466 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7467 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7468 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7469 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7470 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7471 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7472 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7473 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7474 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7475 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7476 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7477#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007478 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7479 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7480 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7481 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7482 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007483#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007484#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007485
Guido van Rossumd48f2521997-12-05 22:19:34 +00007486#if defined(PYOS_OS2)
7487 if (insertvalues(d)) return -1;
7488#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007489 return 0;
7490}
7491
7492
Tim Peters5aa91602002-01-30 05:46:57 +00007493#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007494#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007495#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007496
7497#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007498#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007499#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007500
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007501#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007502#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007503#define MODNAME "posix"
7504#endif
7505
Martin v. Löwis1a214512008-06-11 05:26:20 +00007506static struct PyModuleDef posixmodule = {
7507 PyModuleDef_HEAD_INIT,
7508 MODNAME,
7509 posix__doc__,
7510 -1,
7511 posix_methods,
7512 NULL,
7513 NULL,
7514 NULL,
7515 NULL
7516};
7517
7518
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007519PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007520INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007521{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007522 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007523
Martin v. Löwis1a214512008-06-11 05:26:20 +00007524 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007525 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007526 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007527
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007528 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007529 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007530 Py_XINCREF(v);
7531 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007532 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007533 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007534
Fred Drake4d1e64b2002-04-15 19:40:07 +00007535 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007536 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007537
Fred Drake4d1e64b2002-04-15 19:40:07 +00007538 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007539 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007540
Fred Drake4d1e64b2002-04-15 19:40:07 +00007541 Py_INCREF(PyExc_OSError);
7542 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007543
Guido van Rossumb3d39562000-01-31 18:41:26 +00007544#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007545 if (posix_putenv_garbage == NULL)
7546 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007547#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007548
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007549 if (!initialized) {
7550 stat_result_desc.name = MODNAME ".stat_result";
7551 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7552 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7553 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7554 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7555 structseq_new = StatResultType.tp_new;
7556 StatResultType.tp_new = statresult_new;
7557
7558 statvfs_result_desc.name = MODNAME ".statvfs_result";
7559 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007560#ifdef NEED_TICKS_PER_SECOND
7561# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7562 ticks_per_second = sysconf(_SC_CLK_TCK);
7563# elif defined(HZ)
7564 ticks_per_second = HZ;
7565# else
7566 ticks_per_second = 60; /* magic fallback value; may be bogus */
7567# endif
7568#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007569 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007570 Py_INCREF((PyObject*) &StatResultType);
7571 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007572 Py_INCREF((PyObject*) &StatVFSResultType);
7573 PyModule_AddObject(m, "statvfs_result",
7574 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007575 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007576
7577#ifdef __APPLE__
7578 /*
7579 * Step 2 of weak-linking support on Mac OS X.
7580 *
7581 * The code below removes functions that are not available on the
7582 * currently active platform.
7583 *
7584 * This block allow one to use a python binary that was build on
7585 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7586 * OSX 10.4.
7587 */
7588#ifdef HAVE_FSTATVFS
7589 if (fstatvfs == NULL) {
7590 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007591 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007592 }
7593 }
7594#endif /* HAVE_FSTATVFS */
7595
7596#ifdef HAVE_STATVFS
7597 if (statvfs == NULL) {
7598 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007599 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007600 }
7601 }
7602#endif /* HAVE_STATVFS */
7603
7604# ifdef HAVE_LCHOWN
7605 if (lchown == NULL) {
7606 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007607 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007608 }
7609 }
7610#endif /* HAVE_LCHOWN */
7611
7612
7613#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007614 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007615
Guido van Rossumb6775db1994-08-01 11:34:53 +00007616}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007617
7618#ifdef __cplusplus
7619}
7620#endif