blob: 7b8c0574199375c92e2e0456b693111733f1f6ec [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;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002186 Py_UNICODE wch;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002187 /* Overallocate for \\*.*\0 */
2188 len = PyUnicode_GET_SIZE(po);
2189 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2190 if (!wnamebuf) {
2191 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002192 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002193 }
2194 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2195 wch = len > 0 ? wnamebuf[len-1] : '\0';
2196 if (wch != L'/' && wch != L'\\' && wch != L':')
2197 wnamebuf[len++] = L'\\';
2198 wcscpy(wnamebuf + len, L"*.*");
2199 if ((d = PyList_New(0)) == NULL) {
2200 free(wnamebuf);
2201 return NULL;
2202 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002203 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2204 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002205 int error = GetLastError();
2206 if (error == ERROR_FILE_NOT_FOUND) {
2207 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002208 return d;
2209 }
2210 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002211 win32_error_unicode("FindFirstFileW", wnamebuf);
2212 free(wnamebuf);
2213 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002214 }
2215 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002216 /* Skip over . and .. */
2217 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2218 wcscmp(wFileData.cFileName, L"..") != 0) {
2219 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2220 if (v == NULL) {
2221 Py_DECREF(d);
2222 d = NULL;
2223 break;
2224 }
2225 if (PyList_Append(d, v) != 0) {
2226 Py_DECREF(v);
2227 Py_DECREF(d);
2228 d = NULL;
2229 break;
2230 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002231 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002232 }
Georg Brandl622927b2006-03-07 12:48:03 +00002233 Py_BEGIN_ALLOW_THREADS
2234 result = FindNextFileW(hFindFile, &wFileData);
2235 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002236 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2237 it got to the end of the directory. */
2238 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2239 Py_DECREF(d);
2240 win32_error_unicode("FindNextFileW", wnamebuf);
2241 FindClose(hFindFile);
2242 free(wnamebuf);
2243 return NULL;
2244 }
Georg Brandl622927b2006-03-07 12:48:03 +00002245 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002246
2247 if (FindClose(hFindFile) == FALSE) {
2248 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002249 win32_error_unicode("FindClose", wnamebuf);
2250 free(wnamebuf);
2251 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002252 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002253 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002254 return d;
2255 }
2256 /* Drop the argument parsing error as narrow strings
2257 are also valid. */
2258 PyErr_Clear();
2259 }
2260#endif
2261
Tim Peters5aa91602002-01-30 05:46:57 +00002262 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002263 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002264 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002265 if (len > 0) {
2266 char ch = namebuf[len-1];
2267 if (ch != SEP && ch != ALTSEP && ch != ':')
2268 namebuf[len++] = '/';
2269 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002270 strcpy(namebuf + len, "*.*");
2271
Barry Warsaw53699e91996-12-10 23:23:01 +00002272 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002273 return NULL;
2274
2275 hFindFile = FindFirstFile(namebuf, &FileData);
2276 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002277 int error = GetLastError();
2278 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002279 return d;
2280 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002281 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002282 }
2283 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002284 /* Skip over . and .. */
2285 if (strcmp(FileData.cFileName, ".") != 0 &&
2286 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002287 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002288 if (v == NULL) {
2289 Py_DECREF(d);
2290 d = NULL;
2291 break;
2292 }
2293 if (PyList_Append(d, v) != 0) {
2294 Py_DECREF(v);
2295 Py_DECREF(d);
2296 d = NULL;
2297 break;
2298 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002299 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002300 }
Georg Brandl622927b2006-03-07 12:48:03 +00002301 Py_BEGIN_ALLOW_THREADS
2302 result = FindNextFile(hFindFile, &FileData);
2303 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002304 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2305 it got to the end of the directory. */
2306 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2307 Py_DECREF(d);
2308 win32_error("FindNextFile", namebuf);
2309 FindClose(hFindFile);
2310 return NULL;
2311 }
Georg Brandl622927b2006-03-07 12:48:03 +00002312 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002313
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002314 if (FindClose(hFindFile) == FALSE) {
2315 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002316 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002317 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002318
2319 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002320
Tim Peters0bb44a42000-09-15 07:44:49 +00002321#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002322
2323#ifndef MAX_PATH
2324#define MAX_PATH CCHMAXPATH
2325#endif
2326 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002327 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002328 PyObject *d, *v;
2329 char namebuf[MAX_PATH+5];
2330 HDIR hdir = 1;
2331 ULONG srchcnt = 1;
2332 FILEFINDBUF3 ep;
2333 APIRET rc;
2334
Alexandre Vassalotti70a23712007-10-14 02:05:51 +00002335 if (!PyArg_ParseTuple(args, "et#:listdir",
2336 Py_FileSystemDefaultEncoding, &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002337 return NULL;
2338 if (len >= MAX_PATH) {
Neal Norwitz6c913782007-10-14 03:23:09 +00002339 PyMem_Free(name);
2340 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002341 return NULL;
2342 }
2343 strcpy(namebuf, name);
2344 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002345 if (*pt == ALTSEP)
2346 *pt = SEP;
2347 if (namebuf[len-1] != SEP)
2348 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002349 strcpy(namebuf + len, "*.*");
2350
Neal Norwitz6c913782007-10-14 03:23:09 +00002351 if ((d = PyList_New(0)) == NULL) {
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002352 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002353 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002354 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002355
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002356 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2357 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002358 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002359 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2360 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2361 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002362
2363 if (rc != NO_ERROR) {
2364 errno = ENOENT;
Neal Norwitz6c913782007-10-14 03:23:09 +00002365 return posix_error_with_allocated_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002366 }
2367
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002368 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002369 do {
2370 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002371 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002372 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002373
2374 strcpy(namebuf, ep.achName);
2375
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002376 /* Leave Case of Name Alone -- In Native Form */
2377 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002378
Christian Heimes72b710a2008-05-26 13:28:38 +00002379 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002380 if (v == NULL) {
2381 Py_DECREF(d);
2382 d = NULL;
2383 break;
2384 }
2385 if (PyList_Append(d, v) != 0) {
2386 Py_DECREF(v);
2387 Py_DECREF(d);
2388 d = NULL;
2389 break;
2390 }
2391 Py_DECREF(v);
2392 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2393 }
2394
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002395 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002396 return d;
2397#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002398
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002399 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002400 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002401 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002402 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002403 int arg_is_unicode = 1;
2404
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002405 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002406 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2407 arg_is_unicode = 0;
2408 PyErr_Clear();
2409 }
2410 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002411 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002412 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002413 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002414 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002415 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002416 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002417 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002418 return NULL;
2419 }
Georg Brandl622927b2006-03-07 12:48:03 +00002420 for (;;) {
Georg Brandl3dbca812008-07-23 16:10:53 +00002421 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002422 Py_BEGIN_ALLOW_THREADS
2423 ep = readdir(dirp);
2424 Py_END_ALLOW_THREADS
Georg Brandl3dbca812008-07-23 16:10:53 +00002425 if (ep == NULL) {
2426 if (errno == 0) {
2427 break;
2428 } else {
2429 closedir(dirp);
2430 Py_DECREF(d);
2431 return posix_error_with_allocated_filename(name);
2432 }
2433 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002434 if (ep->d_name[0] == '.' &&
2435 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002436 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002437 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002438 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002439 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002440 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002441 d = NULL;
2442 break;
2443 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002444 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002445 PyObject *w;
2446
2447 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002448 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002449 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002450 if (w != NULL) {
2451 Py_DECREF(v);
2452 v = w;
2453 }
2454 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002455 /* Ignore undecodable filenames, as discussed
2456 * in issue 3187. To include these,
2457 * use getcwdb(). */
Just van Rossum6a421832003-03-04 19:30:44 +00002458 PyErr_Clear();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002459 Py_DECREF(v);
2460 continue;
Just van Rossum46c97842003-02-25 21:42:15 +00002461 }
Just van Rossum46c97842003-02-25 21:42:15 +00002462 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002463 if (PyList_Append(d, v) != 0) {
2464 Py_DECREF(v);
2465 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002466 d = NULL;
2467 break;
2468 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002469 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002470 }
2471 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002472 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002473
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002474 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002475
Tim Peters0bb44a42000-09-15 07:44:49 +00002476#endif /* which OS */
2477} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002478
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002479#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002480/* A helper function for abspath on win32 */
2481static PyObject *
2482posix__getfullpathname(PyObject *self, PyObject *args)
2483{
Mark Dickinson934896d2009-02-21 20:59:32 +00002484 /* assume encoded strings won't more than double no of chars */
Mark Hammondef8b6542001-05-13 08:04:26 +00002485 char inbuf[MAX_PATH*2];
2486 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002487 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002488 char outbuf[MAX_PATH*2];
2489 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002490#ifdef Py_WIN_WIDE_FILENAMES
2491 if (unicode_file_names()) {
2492 PyUnicodeObject *po;
2493 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
Benjamin Petersonf608c612008-11-16 18:33:53 +00002494 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2495 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002496 Py_UNICODE *wtemp;
Benjamin Petersonf608c612008-11-16 18:33:53 +00002497 DWORD result;
2498 PyObject *v;
2499 result = GetFullPathNameW(wpath,
2500 sizeof(woutbuf)/sizeof(woutbuf[0]),
2501 woutbuf, &wtemp);
2502 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2503 woutbufp = malloc(result * sizeof(Py_UNICODE));
2504 if (!woutbufp)
2505 return PyErr_NoMemory();
2506 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2507 }
2508 if (result)
2509 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2510 else
2511 v = win32_error_unicode("GetFullPathNameW", wpath);
2512 if (woutbufp != woutbuf)
2513 free(woutbufp);
2514 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002515 }
2516 /* Drop the argument parsing error as narrow strings
2517 are also valid. */
2518 PyErr_Clear();
2519 }
2520#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002521 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2522 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002523 &insize))
2524 return NULL;
2525 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2526 outbuf, &temp))
2527 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002528 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2529 return PyUnicode_Decode(outbuf, strlen(outbuf),
2530 Py_FileSystemDefaultEncoding, NULL);
2531 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002532 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002533} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002534#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002536PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002537"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002538Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002539
Barry Warsaw53699e91996-12-10 23:23:01 +00002540static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002541posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002542{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002543 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002544 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002545 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002546
2547#ifdef Py_WIN_WIDE_FILENAMES
2548 if (unicode_file_names()) {
2549 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002550 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002551 Py_BEGIN_ALLOW_THREADS
2552 /* PyUnicode_AS_UNICODE OK without thread lock as
2553 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002554 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002555 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002556 if (!res)
2557 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002558 Py_INCREF(Py_None);
2559 return Py_None;
2560 }
2561 /* Drop the argument parsing error as narrow strings
2562 are also valid. */
2563 PyErr_Clear();
2564 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002565 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2566 Py_FileSystemDefaultEncoding, &path, &mode))
2567 return NULL;
2568 Py_BEGIN_ALLOW_THREADS
2569 /* PyUnicode_AS_UNICODE OK without thread lock as
2570 it is a simple dereference. */
2571 res = CreateDirectoryA(path, NULL);
2572 Py_END_ALLOW_THREADS
2573 if (!res) {
2574 win32_error("mkdir", path);
2575 PyMem_Free(path);
2576 return NULL;
2577 }
2578 PyMem_Free(path);
2579 Py_INCREF(Py_None);
2580 return Py_None;
2581#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002582
Tim Peters5aa91602002-01-30 05:46:57 +00002583 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002584 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002585 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002586 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002587#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002588 res = mkdir(path);
2589#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002590 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002591#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002592 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002593 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002594 return posix_error_with_allocated_filename(path);
2595 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002596 Py_INCREF(Py_None);
2597 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002598#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002599}
2600
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002601
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002602/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2603#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002604#include <sys/resource.h>
2605#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002606
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002607
2608#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002609PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002610"nice(inc) -> new_priority\n\n\
2611Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002612
Barry Warsaw53699e91996-12-10 23:23:01 +00002613static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002614posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002615{
2616 int increment, value;
2617
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002618 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002619 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002620
2621 /* There are two flavours of 'nice': one that returns the new
2622 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002623 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2624 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002625
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002626 If we are of the nice family that returns the new priority, we
2627 need to clear errno before the call, and check if errno is filled
2628 before calling posix_error() on a returnvalue of -1, because the
2629 -1 may be the actual new priority! */
2630
2631 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002632 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002633#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002634 if (value == 0)
2635 value = getpriority(PRIO_PROCESS, 0);
2636#endif
2637 if (value == -1 && errno != 0)
2638 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002639 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002640 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002641}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002642#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002643
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002644PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002645"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002646Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002647
Barry Warsaw53699e91996-12-10 23:23:01 +00002648static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002649posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002650{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002651#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002652 PyObject *o1, *o2;
2653 char *p1, *p2;
2654 BOOL result;
2655 if (unicode_file_names()) {
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002656 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2657 goto error;
2658 if (!convert_to_unicode(&o1))
2659 goto error;
2660 if (!convert_to_unicode(&o2)) {
2661 Py_DECREF(o1);
2662 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002663 }
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002664 Py_BEGIN_ALLOW_THREADS
2665 result = MoveFileW(PyUnicode_AsUnicode(o1),
2666 PyUnicode_AsUnicode(o2));
2667 Py_END_ALLOW_THREADS
2668 Py_DECREF(o1);
2669 Py_DECREF(o2);
2670 if (!result)
2671 return win32_error("rename", NULL);
2672 Py_INCREF(Py_None);
2673 return Py_None;
2674error:
2675 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002676 }
2677 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2678 return NULL;
2679 Py_BEGIN_ALLOW_THREADS
2680 result = MoveFileA(p1, p2);
2681 Py_END_ALLOW_THREADS
2682 if (!result)
2683 return win32_error("rename", NULL);
2684 Py_INCREF(Py_None);
2685 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002686#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002687 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002688#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002689}
2690
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002691
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002692PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002693"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002694Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002695
Barry Warsaw53699e91996-12-10 23:23:01 +00002696static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002697posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002698{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002699#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002700 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002701#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002702 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002703#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002704}
2705
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002706
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002707PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002708"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002709Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002710
Barry Warsaw53699e91996-12-10 23:23:01 +00002711static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002712posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002713{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002714#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002715 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002716#else
2717 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2718#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002719}
2720
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002721
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002722#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002723PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002724"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002725Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002726
Barry Warsaw53699e91996-12-10 23:23:01 +00002727static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002728posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002729{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002730 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002731#ifdef MS_WINDOWS
2732 wchar_t *command;
2733 if (!PyArg_ParseTuple(args, "u:system", &command))
2734 return NULL;
2735#else
2736 char *command;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002737 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002738 return NULL;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002739#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002740 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002741#ifdef MS_WINDOWS
2742 sts = _wsystem(command);
2743#else
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002744 sts = system(command);
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002745#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002746 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +00002747 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002748}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002749#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002750
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002751
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002752PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002753"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002754Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002755
Barry Warsaw53699e91996-12-10 23:23:01 +00002756static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002757posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002758{
2759 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002760 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002761 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002762 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002763 if (i < 0)
2764 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002765 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002766}
2767
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002768
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002769PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002770"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002771Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002772
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002773PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002774"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002775Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002776
Barry Warsaw53699e91996-12-10 23:23:01 +00002777static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002778posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002779{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002780#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002781 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002782#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002783 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002784#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002785}
2786
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002787
Guido van Rossumb6775db1994-08-01 11:34:53 +00002788#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002789PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002790"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002791Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002792
Barry Warsaw53699e91996-12-10 23:23:01 +00002793static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002794posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002795{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002796 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002797 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002798
Barry Warsaw53699e91996-12-10 23:23:01 +00002799 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002800 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002801 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002802 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002803 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002804 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002805 u.sysname,
2806 u.nodename,
2807 u.release,
2808 u.version,
2809 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002810}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002811#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002812
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002813static int
2814extract_time(PyObject *t, long* sec, long* usec)
2815{
2816 long intval;
2817 if (PyFloat_Check(t)) {
2818 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002819 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002820 if (!intobj)
2821 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002822 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002823 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002824 if (intval == -1 && PyErr_Occurred())
2825 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002826 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002827 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002828 if (*usec < 0)
2829 /* If rounding gave us a negative number,
2830 truncate. */
2831 *usec = 0;
2832 return 0;
2833 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002834 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002835 if (intval == -1 && PyErr_Occurred())
2836 return -1;
2837 *sec = intval;
2838 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002839 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002840}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002841
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002842PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002843"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002844utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002845Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002846second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002847
Barry Warsaw53699e91996-12-10 23:23:01 +00002848static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002849posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002850{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002851#ifdef Py_WIN_WIDE_FILENAMES
2852 PyObject *arg;
2853 PyUnicodeObject *obwpath;
2854 wchar_t *wpath = NULL;
2855 char *apath = NULL;
2856 HANDLE hFile;
2857 long atimesec, mtimesec, ausec, musec;
2858 FILETIME atime, mtime;
2859 PyObject *result = NULL;
2860
2861 if (unicode_file_names()) {
2862 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2863 wpath = PyUnicode_AS_UNICODE(obwpath);
2864 Py_BEGIN_ALLOW_THREADS
2865 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002866 NULL, OPEN_EXISTING,
2867 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002868 Py_END_ALLOW_THREADS
2869 if (hFile == INVALID_HANDLE_VALUE)
2870 return win32_error_unicode("utime", wpath);
2871 } else
2872 /* Drop the argument parsing error as narrow strings
2873 are also valid. */
2874 PyErr_Clear();
2875 }
2876 if (!wpath) {
2877 if (!PyArg_ParseTuple(args, "etO:utime",
2878 Py_FileSystemDefaultEncoding, &apath, &arg))
2879 return NULL;
2880 Py_BEGIN_ALLOW_THREADS
2881 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002882 NULL, OPEN_EXISTING,
2883 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002884 Py_END_ALLOW_THREADS
2885 if (hFile == INVALID_HANDLE_VALUE) {
2886 win32_error("utime", apath);
2887 PyMem_Free(apath);
2888 return NULL;
2889 }
2890 PyMem_Free(apath);
2891 }
2892
2893 if (arg == Py_None) {
2894 SYSTEMTIME now;
2895 GetSystemTime(&now);
2896 if (!SystemTimeToFileTime(&now, &mtime) ||
2897 !SystemTimeToFileTime(&now, &atime)) {
2898 win32_error("utime", NULL);
2899 goto done;
2900 }
2901 }
2902 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2903 PyErr_SetString(PyExc_TypeError,
2904 "utime() arg 2 must be a tuple (atime, mtime)");
2905 goto done;
2906 }
2907 else {
2908 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2909 &atimesec, &ausec) == -1)
2910 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002911 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002912 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2913 &mtimesec, &musec) == -1)
2914 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002915 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002916 }
2917 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2918 /* Avoid putting the file name into the error here,
2919 as that may confuse the user into believing that
2920 something is wrong with the file, when it also
2921 could be the time stamp that gives a problem. */
2922 win32_error("utime", NULL);
2923 }
2924 Py_INCREF(Py_None);
2925 result = Py_None;
2926done:
2927 CloseHandle(hFile);
2928 return result;
2929#else /* Py_WIN_WIDE_FILENAMES */
2930
Neal Norwitz2adf2102004-06-09 01:46:02 +00002931 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002932 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002933 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002934 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002935
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002936#if defined(HAVE_UTIMES)
2937 struct timeval buf[2];
2938#define ATIME buf[0].tv_sec
2939#define MTIME buf[1].tv_sec
2940#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002941/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002942 struct utimbuf buf;
2943#define ATIME buf.actime
2944#define MTIME buf.modtime
2945#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002946#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002947 time_t buf[2];
2948#define ATIME buf[0]
2949#define MTIME buf[1]
2950#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002951#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002952
Mark Hammond817c9292003-12-03 01:22:38 +00002953
Thomas Wouters477c8d52006-05-27 19:21:47 +00002954 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002955 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002956 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002957 if (arg == Py_None) {
2958 /* optional time values not given */
2959 Py_BEGIN_ALLOW_THREADS
2960 res = utime(path, NULL);
2961 Py_END_ALLOW_THREADS
2962 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002963 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002964 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002965 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002966 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002967 return NULL;
2968 }
2969 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002970 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002971 &atime, &ausec) == -1) {
2972 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002973 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002974 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002975 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002976 &mtime, &musec) == -1) {
2977 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002978 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002979 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002980 ATIME = atime;
2981 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002982#ifdef HAVE_UTIMES
2983 buf[0].tv_usec = ausec;
2984 buf[1].tv_usec = musec;
2985 Py_BEGIN_ALLOW_THREADS
2986 res = utimes(path, buf);
2987 Py_END_ALLOW_THREADS
2988#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002989 Py_BEGIN_ALLOW_THREADS
2990 res = utime(path, UTIME_ARG);
2991 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002992#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002993 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002994 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002995 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002996 }
Neal Norwitz96652712004-06-06 20:40:27 +00002997 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002998 Py_INCREF(Py_None);
2999 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003000#undef UTIME_ARG
3001#undef ATIME
3002#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00003003#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003004}
3005
Guido van Rossum85e3b011991-06-03 12:42:10 +00003006
Guido van Rossum3b066191991-06-04 19:40:25 +00003007/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003008
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003009PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003010"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003011Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003012
Barry Warsaw53699e91996-12-10 23:23:01 +00003013static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003014posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003015{
3016 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003017 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003018 return NULL;
3019 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00003020 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003021}
3022
Martin v. Löwis114619e2002-10-07 06:44:21 +00003023#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3024static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003025free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003026{
Martin v. Löwis725507b2006-03-07 12:08:51 +00003027 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00003028 for (i = 0; i < count; i++)
3029 PyMem_Free(array[i]);
3030 PyMem_DEL(array);
3031}
3032#endif
3033
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003034
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003035#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003036PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003037"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003038Execute an executable path with arguments, replacing current process.\n\
3039\n\
3040 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003041 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003042
Barry Warsaw53699e91996-12-10 23:23:01 +00003043static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003044posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003045{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003046 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003047 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003048 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003049 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003050 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003051
Guido van Rossum89b33251993-10-22 14:26:06 +00003052 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00003053 argv is a list or tuple of strings. */
3054
Martin v. Löwis114619e2002-10-07 06:44:21 +00003055 if (!PyArg_ParseTuple(args, "etO:execv",
3056 Py_FileSystemDefaultEncoding,
3057 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003058 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003059 if (PyList_Check(argv)) {
3060 argc = PyList_Size(argv);
3061 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003062 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003063 else if (PyTuple_Check(argv)) {
3064 argc = PyTuple_Size(argv);
3065 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003066 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003067 else {
Fred Drake661ea262000-10-24 19:57:45 +00003068 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003069 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00003070 return NULL;
3071 }
Thomas Heller6790d602007-08-30 17:15:14 +00003072 if (argc < 1) {
3073 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3074 PyMem_Free(path);
3075 return NULL;
3076 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003077
Barry Warsaw53699e91996-12-10 23:23:01 +00003078 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003079 if (argvlist == NULL) {
3080 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003081 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003082 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003083 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003084 if (!PyArg_Parse((*getitem)(argv, i), "et",
3085 Py_FileSystemDefaultEncoding,
3086 &argvlist[i])) {
3087 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00003088 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003089 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003090 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00003091 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003092
Guido van Rossum85e3b011991-06-03 12:42:10 +00003093 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003094 }
3095 argvlist[argc] = NULL;
3096
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003097 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003098
Guido van Rossum85e3b011991-06-03 12:42:10 +00003099 /* If we get here it's definitely an error */
3100
Martin v. Löwis114619e2002-10-07 06:44:21 +00003101 free_string_array(argvlist, argc);
3102 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003103 return posix_error();
3104}
3105
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003106
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003107PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003108"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003109Execute a path with arguments and environment, replacing current process.\n\
3110\n\
3111 path: path of executable file\n\
3112 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003113 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003114
Barry Warsaw53699e91996-12-10 23:23:01 +00003115static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003116posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003117{
3118 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003119 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003120 char **argvlist;
3121 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003122 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003123 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003124 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003125 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003126
3127 /* execve has three arguments: (path, argv, env), where
3128 argv is a list or tuple of strings and env is a dictionary
3129 like posix.environ. */
3130
Martin v. Löwis114619e2002-10-07 06:44:21 +00003131 if (!PyArg_ParseTuple(args, "etOO:execve",
3132 Py_FileSystemDefaultEncoding,
3133 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003134 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003135 if (PyList_Check(argv)) {
3136 argc = PyList_Size(argv);
3137 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003138 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003139 else if (PyTuple_Check(argv)) {
3140 argc = PyTuple_Size(argv);
3141 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003142 }
3143 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003144 PyErr_SetString(PyExc_TypeError,
3145 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003146 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003147 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003148 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003149 PyErr_SetString(PyExc_TypeError,
3150 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003151 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003152 }
3153
Barry Warsaw53699e91996-12-10 23:23:01 +00003154 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003155 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003156 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003157 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003158 }
3159 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003160 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00003161 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00003162 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00003163 &argvlist[i]))
3164 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003165 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003166 goto fail_1;
3167 }
3168 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003169 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003170 argvlist[argc] = NULL;
3171
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003172 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003173 if (i < 0)
3174 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003175 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003176 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003177 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003178 goto fail_1;
3179 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003180 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003181 keys = PyMapping_Keys(env);
3182 vals = PyMapping_Values(env);
3183 if (!keys || !vals)
3184 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003185 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3186 PyErr_SetString(PyExc_TypeError,
3187 "execve(): env.keys() or env.values() is not a list");
3188 goto fail_2;
3189 }
Tim Peters5aa91602002-01-30 05:46:57 +00003190
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003191 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003192 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003193 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003194
3195 key = PyList_GetItem(keys, pos);
3196 val = PyList_GetItem(vals, pos);
3197 if (!key || !val)
3198 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003199
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003200 if (!PyArg_Parse(
3201 key,
3202 "s;execve() arg 3 contains a non-string key",
3203 &k) ||
3204 !PyArg_Parse(
3205 val,
3206 "s;execve() arg 3 contains a non-string value",
3207 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003208 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003209 goto fail_2;
3210 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003211
3212#if defined(PYOS_OS2)
3213 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3214 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3215#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003216 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003217 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003218 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003219 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003220 goto fail_2;
3221 }
Tim Petersc8996f52001-12-03 20:41:00 +00003222 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003223 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003224#if defined(PYOS_OS2)
3225 }
3226#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003227 }
3228 envlist[envc] = 0;
3229
3230 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003231
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003232 /* If we get here it's definitely an error */
3233
3234 (void) posix_error();
3235
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003236 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003237 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003238 PyMem_DEL(envlist[envc]);
3239 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003240 fail_1:
3241 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003242 Py_XDECREF(vals);
3243 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003244 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003245 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003246 return NULL;
3247}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003248#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003249
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003250
Guido van Rossuma1065681999-01-25 23:20:23 +00003251#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003252PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003253"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003254Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003255\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003256 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003257 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003258 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003259
3260static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003261posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003262{
3263 char *path;
3264 PyObject *argv;
3265 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003266 int mode, i;
3267 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003268 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003269 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003270
3271 /* spawnv has three arguments: (mode, path, argv), where
3272 argv is a list or tuple of strings. */
3273
Martin v. Löwis114619e2002-10-07 06:44:21 +00003274 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3275 Py_FileSystemDefaultEncoding,
3276 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003277 return NULL;
3278 if (PyList_Check(argv)) {
3279 argc = PyList_Size(argv);
3280 getitem = PyList_GetItem;
3281 }
3282 else if (PyTuple_Check(argv)) {
3283 argc = PyTuple_Size(argv);
3284 getitem = PyTuple_GetItem;
3285 }
3286 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003287 PyErr_SetString(PyExc_TypeError,
3288 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003289 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003290 return NULL;
3291 }
3292
3293 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003294 if (argvlist == NULL) {
3295 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003296 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003297 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003298 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003299 if (!PyArg_Parse((*getitem)(argv, i), "et",
3300 Py_FileSystemDefaultEncoding,
3301 &argvlist[i])) {
3302 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003303 PyErr_SetString(
3304 PyExc_TypeError,
3305 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003306 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003307 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003308 }
3309 }
3310 argvlist[argc] = NULL;
3311
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003312#if defined(PYOS_OS2) && defined(PYCC_GCC)
3313 Py_BEGIN_ALLOW_THREADS
3314 spawnval = spawnv(mode, path, argvlist);
3315 Py_END_ALLOW_THREADS
3316#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003317 if (mode == _OLD_P_OVERLAY)
3318 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003319
Tim Peters25059d32001-12-07 20:35:43 +00003320 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003321 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003322 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003323#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003324
Martin v. Löwis114619e2002-10-07 06:44:21 +00003325 free_string_array(argvlist, argc);
3326 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003327
Fred Drake699f3522000-06-29 21:12:41 +00003328 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003329 return posix_error();
3330 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003331#if SIZEOF_LONG == SIZEOF_VOID_P
3332 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003333#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003334 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003335#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003336}
3337
3338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003339PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003340"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003341Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003342\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003343 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003344 path: path of executable file\n\
3345 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003346 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003347
3348static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003349posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003350{
3351 char *path;
3352 PyObject *argv, *env;
3353 char **argvlist;
3354 char **envlist;
3355 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003356 int mode, pos, envc;
3357 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003358 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003359 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003360 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003361
3362 /* spawnve has four arguments: (mode, path, argv, env), where
3363 argv is a list or tuple of strings and env is a dictionary
3364 like posix.environ. */
3365
Martin v. Löwis114619e2002-10-07 06:44:21 +00003366 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3367 Py_FileSystemDefaultEncoding,
3368 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003369 return NULL;
3370 if (PyList_Check(argv)) {
3371 argc = PyList_Size(argv);
3372 getitem = PyList_GetItem;
3373 }
3374 else if (PyTuple_Check(argv)) {
3375 argc = PyTuple_Size(argv);
3376 getitem = PyTuple_GetItem;
3377 }
3378 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003379 PyErr_SetString(PyExc_TypeError,
3380 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003381 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003382 }
3383 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003384 PyErr_SetString(PyExc_TypeError,
3385 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003386 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003387 }
3388
3389 argvlist = PyMem_NEW(char *, argc+1);
3390 if (argvlist == NULL) {
3391 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003392 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003393 }
3394 for (i = 0; i < argc; i++) {
3395 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003396 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003397 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003398 &argvlist[i]))
3399 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003400 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003401 goto fail_1;
3402 }
3403 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003404 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003405 argvlist[argc] = NULL;
3406
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003407 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003408 if (i < 0)
3409 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003410 envlist = PyMem_NEW(char *, i + 1);
3411 if (envlist == NULL) {
3412 PyErr_NoMemory();
3413 goto fail_1;
3414 }
3415 envc = 0;
3416 keys = PyMapping_Keys(env);
3417 vals = PyMapping_Values(env);
3418 if (!keys || !vals)
3419 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003420 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3421 PyErr_SetString(PyExc_TypeError,
3422 "spawnve(): env.keys() or env.values() is not a list");
3423 goto fail_2;
3424 }
Tim Peters5aa91602002-01-30 05:46:57 +00003425
Guido van Rossuma1065681999-01-25 23:20:23 +00003426 for (pos = 0; pos < i; pos++) {
3427 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003428 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003429
3430 key = PyList_GetItem(keys, pos);
3431 val = PyList_GetItem(vals, pos);
3432 if (!key || !val)
3433 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003434
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003435 if (!PyArg_Parse(
3436 key,
3437 "s;spawnve() arg 3 contains a non-string key",
3438 &k) ||
3439 !PyArg_Parse(
3440 val,
3441 "s;spawnve() arg 3 contains a non-string value",
3442 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003443 {
3444 goto fail_2;
3445 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003446 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003447 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003448 if (p == NULL) {
3449 PyErr_NoMemory();
3450 goto fail_2;
3451 }
Tim Petersc8996f52001-12-03 20:41:00 +00003452 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003453 envlist[envc++] = p;
3454 }
3455 envlist[envc] = 0;
3456
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003457#if defined(PYOS_OS2) && defined(PYCC_GCC)
3458 Py_BEGIN_ALLOW_THREADS
3459 spawnval = spawnve(mode, path, argvlist, envlist);
3460 Py_END_ALLOW_THREADS
3461#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003462 if (mode == _OLD_P_OVERLAY)
3463 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003464
3465 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003466 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003467 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003468#endif
Tim Peters25059d32001-12-07 20:35:43 +00003469
Fred Drake699f3522000-06-29 21:12:41 +00003470 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003471 (void) posix_error();
3472 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003473#if SIZEOF_LONG == SIZEOF_VOID_P
3474 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003475#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003476 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003477#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003478
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003479 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003480 while (--envc >= 0)
3481 PyMem_DEL(envlist[envc]);
3482 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003483 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003484 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003485 Py_XDECREF(vals);
3486 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003487 fail_0:
3488 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003489 return res;
3490}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003491
3492/* OS/2 supports spawnvp & spawnvpe natively */
3493#if defined(PYOS_OS2)
3494PyDoc_STRVAR(posix_spawnvp__doc__,
3495"spawnvp(mode, file, args)\n\n\
3496Execute the program 'file' in a new process, using the environment\n\
3497search path to find the file.\n\
3498\n\
3499 mode: mode of process creation\n\
3500 file: executable file name\n\
3501 args: tuple or list of strings");
3502
3503static PyObject *
3504posix_spawnvp(PyObject *self, PyObject *args)
3505{
3506 char *path;
3507 PyObject *argv;
3508 char **argvlist;
3509 int mode, i, argc;
3510 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003511 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003512
3513 /* spawnvp has three arguments: (mode, path, argv), where
3514 argv is a list or tuple of strings. */
3515
3516 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3517 Py_FileSystemDefaultEncoding,
3518 &path, &argv))
3519 return NULL;
3520 if (PyList_Check(argv)) {
3521 argc = PyList_Size(argv);
3522 getitem = PyList_GetItem;
3523 }
3524 else if (PyTuple_Check(argv)) {
3525 argc = PyTuple_Size(argv);
3526 getitem = PyTuple_GetItem;
3527 }
3528 else {
3529 PyErr_SetString(PyExc_TypeError,
3530 "spawnvp() arg 2 must be a tuple or list");
3531 PyMem_Free(path);
3532 return NULL;
3533 }
3534
3535 argvlist = PyMem_NEW(char *, argc+1);
3536 if (argvlist == NULL) {
3537 PyMem_Free(path);
3538 return PyErr_NoMemory();
3539 }
3540 for (i = 0; i < argc; i++) {
3541 if (!PyArg_Parse((*getitem)(argv, i), "et",
3542 Py_FileSystemDefaultEncoding,
3543 &argvlist[i])) {
3544 free_string_array(argvlist, i);
3545 PyErr_SetString(
3546 PyExc_TypeError,
3547 "spawnvp() arg 2 must contain only strings");
3548 PyMem_Free(path);
3549 return NULL;
3550 }
3551 }
3552 argvlist[argc] = NULL;
3553
3554 Py_BEGIN_ALLOW_THREADS
3555#if defined(PYCC_GCC)
3556 spawnval = spawnvp(mode, path, argvlist);
3557#else
3558 spawnval = _spawnvp(mode, path, argvlist);
3559#endif
3560 Py_END_ALLOW_THREADS
3561
3562 free_string_array(argvlist, argc);
3563 PyMem_Free(path);
3564
3565 if (spawnval == -1)
3566 return posix_error();
3567 else
3568 return Py_BuildValue("l", (long) spawnval);
3569}
3570
3571
3572PyDoc_STRVAR(posix_spawnvpe__doc__,
3573"spawnvpe(mode, file, args, env)\n\n\
3574Execute the program 'file' in a new process, using the environment\n\
3575search path to find the file.\n\
3576\n\
3577 mode: mode of process creation\n\
3578 file: executable file name\n\
3579 args: tuple or list of arguments\n\
3580 env: dictionary of strings mapping to strings");
3581
3582static PyObject *
3583posix_spawnvpe(PyObject *self, PyObject *args)
3584{
3585 char *path;
3586 PyObject *argv, *env;
3587 char **argvlist;
3588 char **envlist;
3589 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3590 int mode, i, pos, argc, envc;
3591 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003592 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003593 int lastarg = 0;
3594
3595 /* spawnvpe has four arguments: (mode, path, argv, env), where
3596 argv is a list or tuple of strings and env is a dictionary
3597 like posix.environ. */
3598
3599 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3600 Py_FileSystemDefaultEncoding,
3601 &path, &argv, &env))
3602 return NULL;
3603 if (PyList_Check(argv)) {
3604 argc = PyList_Size(argv);
3605 getitem = PyList_GetItem;
3606 }
3607 else if (PyTuple_Check(argv)) {
3608 argc = PyTuple_Size(argv);
3609 getitem = PyTuple_GetItem;
3610 }
3611 else {
3612 PyErr_SetString(PyExc_TypeError,
3613 "spawnvpe() arg 2 must be a tuple or list");
3614 goto fail_0;
3615 }
3616 if (!PyMapping_Check(env)) {
3617 PyErr_SetString(PyExc_TypeError,
3618 "spawnvpe() arg 3 must be a mapping object");
3619 goto fail_0;
3620 }
3621
3622 argvlist = PyMem_NEW(char *, argc+1);
3623 if (argvlist == NULL) {
3624 PyErr_NoMemory();
3625 goto fail_0;
3626 }
3627 for (i = 0; i < argc; i++) {
3628 if (!PyArg_Parse((*getitem)(argv, i),
3629 "et;spawnvpe() arg 2 must contain only strings",
3630 Py_FileSystemDefaultEncoding,
3631 &argvlist[i]))
3632 {
3633 lastarg = i;
3634 goto fail_1;
3635 }
3636 }
3637 lastarg = argc;
3638 argvlist[argc] = NULL;
3639
3640 i = PyMapping_Size(env);
3641 if (i < 0)
3642 goto fail_1;
3643 envlist = PyMem_NEW(char *, i + 1);
3644 if (envlist == NULL) {
3645 PyErr_NoMemory();
3646 goto fail_1;
3647 }
3648 envc = 0;
3649 keys = PyMapping_Keys(env);
3650 vals = PyMapping_Values(env);
3651 if (!keys || !vals)
3652 goto fail_2;
3653 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3654 PyErr_SetString(PyExc_TypeError,
3655 "spawnvpe(): env.keys() or env.values() is not a list");
3656 goto fail_2;
3657 }
3658
3659 for (pos = 0; pos < i; pos++) {
3660 char *p, *k, *v;
3661 size_t len;
3662
3663 key = PyList_GetItem(keys, pos);
3664 val = PyList_GetItem(vals, pos);
3665 if (!key || !val)
3666 goto fail_2;
3667
3668 if (!PyArg_Parse(
3669 key,
3670 "s;spawnvpe() arg 3 contains a non-string key",
3671 &k) ||
3672 !PyArg_Parse(
3673 val,
3674 "s;spawnvpe() arg 3 contains a non-string value",
3675 &v))
3676 {
3677 goto fail_2;
3678 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003679 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003680 p = PyMem_NEW(char, len);
3681 if (p == NULL) {
3682 PyErr_NoMemory();
3683 goto fail_2;
3684 }
3685 PyOS_snprintf(p, len, "%s=%s", k, v);
3686 envlist[envc++] = p;
3687 }
3688 envlist[envc] = 0;
3689
3690 Py_BEGIN_ALLOW_THREADS
3691#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003692 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003693#else
Christian Heimes292d3512008-02-03 16:51:08 +00003694 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003695#endif
3696 Py_END_ALLOW_THREADS
3697
3698 if (spawnval == -1)
3699 (void) posix_error();
3700 else
3701 res = Py_BuildValue("l", (long) spawnval);
3702
3703 fail_2:
3704 while (--envc >= 0)
3705 PyMem_DEL(envlist[envc]);
3706 PyMem_DEL(envlist);
3707 fail_1:
3708 free_string_array(argvlist, lastarg);
3709 Py_XDECREF(vals);
3710 Py_XDECREF(keys);
3711 fail_0:
3712 PyMem_Free(path);
3713 return res;
3714}
3715#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003716#endif /* HAVE_SPAWNV */
3717
3718
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003719#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003720PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003721"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003722Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3723\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003724Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003725
3726static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003727posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003728{
Christian Heimes400adb02008-02-01 08:12:03 +00003729 pid_t pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003730 if (pid == -1)
3731 return posix_error();
Georg Brandl2ee470f2008-07-16 12:55:28 +00003732 if (pid == 0)
3733 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003734 return PyLong_FromLong(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003735}
3736#endif
3737
3738
Guido van Rossumad0ee831995-03-01 10:34:45 +00003739#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003740PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003741"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003742Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003743Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003744
Barry Warsaw53699e91996-12-10 23:23:01 +00003745static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003746posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003747{
Christian Heimes400adb02008-02-01 08:12:03 +00003748 pid_t pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003749 if (pid == -1)
3750 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003751 if (pid == 0)
3752 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003753 return PyLong_FromLong(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003754}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003755#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003756
Neal Norwitzb59798b2003-03-21 01:43:31 +00003757/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003758/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3759#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003760#define DEV_PTY_FILE "/dev/ptc"
3761#define HAVE_DEV_PTMX
3762#else
3763#define DEV_PTY_FILE "/dev/ptmx"
3764#endif
3765
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003766#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003767#ifdef HAVE_PTY_H
3768#include <pty.h>
3769#else
3770#ifdef HAVE_LIBUTIL_H
3771#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003772#endif /* HAVE_LIBUTIL_H */
3773#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003774#ifdef HAVE_STROPTS_H
3775#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003776#endif
3777#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003778
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003779#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003780PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003781"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003782Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003783
3784static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003785posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003786{
3787 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003788#ifndef HAVE_OPENPTY
3789 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003790#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003791#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003792 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003793#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003794 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003795#endif
3796#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003797
Thomas Wouters70c21a12000-07-14 14:28:33 +00003798#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003799 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3800 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003801#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003802 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3803 if (slave_name == NULL)
3804 return posix_error();
3805
3806 slave_fd = open(slave_name, O_RDWR);
3807 if (slave_fd < 0)
3808 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003809#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003810 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003811 if (master_fd < 0)
3812 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003813 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003814 /* change permission of slave */
3815 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003816 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003817 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003818 }
3819 /* unlock slave */
3820 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003821 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003822 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003823 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003824 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003825 slave_name = ptsname(master_fd); /* get name of slave */
3826 if (slave_name == NULL)
3827 return posix_error();
3828 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3829 if (slave_fd < 0)
3830 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003831#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003832 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3833 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003834#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003835 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003836#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003837#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003838#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003839
Fred Drake8cef4cf2000-06-28 16:40:38 +00003840 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003841
Fred Drake8cef4cf2000-06-28 16:40:38 +00003842}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003843#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003844
3845#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003846PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003847"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003848Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3849Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003850To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003851
3852static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003853posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003854{
Christian Heimes400adb02008-02-01 08:12:03 +00003855 int master_fd = -1;
3856 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003857
Fred Drake8cef4cf2000-06-28 16:40:38 +00003858 pid = forkpty(&master_fd, NULL, NULL, NULL);
3859 if (pid == -1)
3860 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003861 if (pid == 0)
3862 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003863 return Py_BuildValue("(li)", pid, master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003864}
3865#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003866
Guido van Rossumad0ee831995-03-01 10:34:45 +00003867#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003868PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003869"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003870Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003871
Barry Warsaw53699e91996-12-10 23:23:01 +00003872static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003873posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003874{
Christian Heimes217cfd12007-12-02 14:31:20 +00003875 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003876}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003877#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003878
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003879
Guido van Rossumad0ee831995-03-01 10:34:45 +00003880#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003881PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003882"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003883Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003884
Barry Warsaw53699e91996-12-10 23:23:01 +00003885static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003886posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003887{
Christian Heimes217cfd12007-12-02 14:31:20 +00003888 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003889}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003890#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003891
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003892
Guido van Rossumad0ee831995-03-01 10:34:45 +00003893#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003894PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003895"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003896Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003897
Barry Warsaw53699e91996-12-10 23:23:01 +00003898static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003899posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003900{
Christian Heimes217cfd12007-12-02 14:31:20 +00003901 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003902}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003903#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003904
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003905
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003906PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003907"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003908Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003909
Barry Warsaw53699e91996-12-10 23:23:01 +00003910static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003911posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003912{
Christian Heimes217cfd12007-12-02 14:31:20 +00003913 return PyLong_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003914}
3915
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003916
Fred Drakec9680921999-12-13 16:37:25 +00003917#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003918PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003919"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003920Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003921
3922static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003923posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003924{
3925 PyObject *result = NULL;
3926
Fred Drakec9680921999-12-13 16:37:25 +00003927#ifdef NGROUPS_MAX
3928#define MAX_GROUPS NGROUPS_MAX
3929#else
3930 /* defined to be 16 on Solaris7, so this should be a small number */
3931#define MAX_GROUPS 64
3932#endif
3933 gid_t grouplist[MAX_GROUPS];
3934 int n;
3935
3936 n = getgroups(MAX_GROUPS, grouplist);
3937 if (n < 0)
3938 posix_error();
3939 else {
3940 result = PyList_New(n);
3941 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003942 int i;
3943 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003944 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003945 if (o == NULL) {
3946 Py_DECREF(result);
3947 result = NULL;
3948 break;
3949 }
3950 PyList_SET_ITEM(result, i, o);
3951 }
3952 }
3953 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003954
Fred Drakec9680921999-12-13 16:37:25 +00003955 return result;
3956}
3957#endif
3958
Martin v. Löwis606edc12002-06-13 21:09:11 +00003959#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003960PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003961"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003962Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003963
3964static PyObject *
3965posix_getpgid(PyObject *self, PyObject *args)
3966{
3967 int pid, pgid;
3968 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3969 return NULL;
3970 pgid = getpgid(pid);
3971 if (pgid < 0)
3972 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00003973 return PyLong_FromLong((long)pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003974}
3975#endif /* HAVE_GETPGID */
3976
3977
Guido van Rossumb6775db1994-08-01 11:34:53 +00003978#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003979PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003980"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003981Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003982
Barry Warsaw53699e91996-12-10 23:23:01 +00003983static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003984posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003985{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003986#ifdef GETPGRP_HAVE_ARG
Christian Heimes217cfd12007-12-02 14:31:20 +00003987 return PyLong_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003988#else /* GETPGRP_HAVE_ARG */
Christian Heimes217cfd12007-12-02 14:31:20 +00003989 return PyLong_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003990#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003991}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003992#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003993
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003994
Guido van Rossumb6775db1994-08-01 11:34:53 +00003995#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003996PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003997"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003998Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003999
Barry Warsaw53699e91996-12-10 23:23:01 +00004000static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004001posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004002{
Guido van Rossum64933891994-10-20 21:56:42 +00004003#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00004004 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004005#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004006 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004007#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00004008 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004009 Py_INCREF(Py_None);
4010 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004011}
4012
Guido van Rossumb6775db1994-08-01 11:34:53 +00004013#endif /* HAVE_SETPGRP */
4014
Guido van Rossumad0ee831995-03-01 10:34:45 +00004015#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004016PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004017"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004018Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004019
Barry Warsaw53699e91996-12-10 23:23:01 +00004020static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004021posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004022{
Christian Heimes217cfd12007-12-02 14:31:20 +00004023 return PyLong_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004024}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004025#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004026
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004027
Fred Drake12c6e2d1999-12-14 21:25:03 +00004028#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004029PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004030"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004031Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004032
4033static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004034posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004035{
Neal Norwitze241ce82003-02-17 18:17:05 +00004036 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00004037 char *name;
4038 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004039
Fred Drakea30680b2000-12-06 21:24:28 +00004040 errno = 0;
4041 name = getlogin();
4042 if (name == NULL) {
4043 if (errno)
4044 posix_error();
4045 else
4046 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00004047 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00004048 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00004049 else
Neal Norwitz93c56822007-08-26 07:10:06 +00004050 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00004051 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004052
Fred Drake12c6e2d1999-12-14 21:25:03 +00004053 return result;
4054}
4055#endif
4056
Guido van Rossumad0ee831995-03-01 10:34:45 +00004057#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004058PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004059"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004060Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004061
Barry Warsaw53699e91996-12-10 23:23:01 +00004062static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004063posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004064{
Christian Heimes217cfd12007-12-02 14:31:20 +00004065 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004066}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004067#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004068
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004069
Guido van Rossumad0ee831995-03-01 10:34:45 +00004070#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004071PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004072"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004073Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004074
Barry Warsaw53699e91996-12-10 23:23:01 +00004075static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004076posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004077{
Christian Heimes292d3512008-02-03 16:51:08 +00004078 pid_t pid;
4079 int sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004080 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00004081 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004082#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004083 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4084 APIRET rc;
4085 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004086 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004087
4088 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4089 APIRET rc;
4090 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004091 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004092
4093 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004094 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004095#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004096 if (kill(pid, sig) == -1)
4097 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004098#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004099 Py_INCREF(Py_None);
4100 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004101}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004102#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004103
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004104#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004105PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004106"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004107Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004108
4109static PyObject *
4110posix_killpg(PyObject *self, PyObject *args)
4111{
4112 int pgid, sig;
4113 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
4114 return NULL;
4115 if (killpg(pgid, sig) == -1)
4116 return posix_error();
4117 Py_INCREF(Py_None);
4118 return Py_None;
4119}
4120#endif
4121
Guido van Rossumc0125471996-06-28 18:55:32 +00004122#ifdef HAVE_PLOCK
4123
4124#ifdef HAVE_SYS_LOCK_H
4125#include <sys/lock.h>
4126#endif
4127
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004128PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004129"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004130Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004131
Barry Warsaw53699e91996-12-10 23:23:01 +00004132static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004133posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004134{
4135 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004136 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004137 return NULL;
4138 if (plock(op) == -1)
4139 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004140 Py_INCREF(Py_None);
4141 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004142}
4143#endif
4144
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004145
Guido van Rossum3b066191991-06-04 19:40:25 +00004146
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004147
Guido van Rossumb6775db1994-08-01 11:34:53 +00004148#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004149PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004150"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004151Set the current process's user id.");
4152
Barry Warsaw53699e91996-12-10 23:23:01 +00004153static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004154posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004155{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004156 long uid_arg;
4157 uid_t uid;
4158 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004159 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004160 uid = uid_arg;
4161 if (uid != uid_arg) {
4162 PyErr_SetString(PyExc_OverflowError, "user id too big");
4163 return NULL;
4164 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004165 if (setuid(uid) < 0)
4166 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004167 Py_INCREF(Py_None);
4168 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004169}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004170#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004171
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004172
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004173#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004174PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004175"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004176Set the current process's effective user id.");
4177
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004178static PyObject *
4179posix_seteuid (PyObject *self, PyObject *args)
4180{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004181 long euid_arg;
4182 uid_t euid;
4183 if (!PyArg_ParseTuple(args, "l", &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004184 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004185 euid = euid_arg;
4186 if (euid != euid_arg) {
4187 PyErr_SetString(PyExc_OverflowError, "user id too big");
4188 return NULL;
4189 }
4190 if (seteuid(euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004191 return posix_error();
4192 } else {
4193 Py_INCREF(Py_None);
4194 return Py_None;
4195 }
4196}
4197#endif /* HAVE_SETEUID */
4198
4199#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004200PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004201"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004202Set the current process's effective group id.");
4203
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004204static PyObject *
4205posix_setegid (PyObject *self, PyObject *args)
4206{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004207 long egid_arg;
4208 gid_t egid;
4209 if (!PyArg_ParseTuple(args, "l", &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004210 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004211 egid = egid_arg;
4212 if (egid != egid_arg) {
4213 PyErr_SetString(PyExc_OverflowError, "group id too big");
4214 return NULL;
4215 }
4216 if (setegid(egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004217 return posix_error();
4218 } else {
4219 Py_INCREF(Py_None);
4220 return Py_None;
4221 }
4222}
4223#endif /* HAVE_SETEGID */
4224
4225#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004226PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004227"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004228Set the current process's real and effective user ids.");
4229
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004230static PyObject *
4231posix_setreuid (PyObject *self, PyObject *args)
4232{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004233 long ruid_arg, euid_arg;
4234 uid_t ruid, euid;
4235 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004236 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004237 ruid = ruid_arg;
4238 euid = euid_arg;
4239 if (euid != euid_arg || ruid != ruid_arg) {
4240 PyErr_SetString(PyExc_OverflowError, "user id too big");
4241 return NULL;
4242 }
4243 if (setreuid(ruid, euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004244 return posix_error();
4245 } else {
4246 Py_INCREF(Py_None);
4247 return Py_None;
4248 }
4249}
4250#endif /* HAVE_SETREUID */
4251
4252#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004253PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004254"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004255Set the current process's real and effective group ids.");
4256
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004257static PyObject *
4258posix_setregid (PyObject *self, PyObject *args)
4259{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004260 long rgid_arg, egid_arg;
4261 gid_t rgid, egid;
4262 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004263 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004264 rgid = rgid_arg;
4265 egid = egid_arg;
4266 if (egid != egid_arg || rgid != rgid_arg) {
4267 PyErr_SetString(PyExc_OverflowError, "group id too big");
4268 return NULL;
4269 }
4270 if (setregid(rgid, egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004271 return posix_error();
4272 } else {
4273 Py_INCREF(Py_None);
4274 return Py_None;
4275 }
4276}
4277#endif /* HAVE_SETREGID */
4278
Guido van Rossumb6775db1994-08-01 11:34:53 +00004279#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004280PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004281"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004282Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004283
Barry Warsaw53699e91996-12-10 23:23:01 +00004284static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004285posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004286{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004287 long gid_arg;
4288 gid_t gid;
4289 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004290 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004291 gid = gid_arg;
4292 if (gid != gid_arg) {
4293 PyErr_SetString(PyExc_OverflowError, "group id too big");
4294 return NULL;
4295 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004296 if (setgid(gid) < 0)
4297 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004298 Py_INCREF(Py_None);
4299 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004300}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004301#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004302
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004303#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004304PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004305"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004306Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004307
4308static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004309posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004310{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004311 int i, len;
4312 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004313
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004314 if (!PySequence_Check(groups)) {
4315 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4316 return NULL;
4317 }
4318 len = PySequence_Size(groups);
4319 if (len > MAX_GROUPS) {
4320 PyErr_SetString(PyExc_ValueError, "too many groups");
4321 return NULL;
4322 }
4323 for(i = 0; i < len; i++) {
4324 PyObject *elem;
4325 elem = PySequence_GetItem(groups, i);
4326 if (!elem)
4327 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004328 if (!PyLong_Check(elem)) {
4329 PyErr_SetString(PyExc_TypeError,
4330 "groups must be integers");
4331 Py_DECREF(elem);
4332 return NULL;
4333 } else {
4334 unsigned long x = PyLong_AsUnsignedLong(elem);
4335 if (PyErr_Occurred()) {
4336 PyErr_SetString(PyExc_TypeError,
4337 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004338 Py_DECREF(elem);
4339 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004340 }
Georg Brandla13c2442005-11-22 19:30:31 +00004341 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004342 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004343 if (grouplist[i] != x) {
4344 PyErr_SetString(PyExc_TypeError,
4345 "group id too big");
4346 Py_DECREF(elem);
4347 return NULL;
4348 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004349 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004350 Py_DECREF(elem);
4351 }
4352
4353 if (setgroups(len, grouplist) < 0)
4354 return posix_error();
4355 Py_INCREF(Py_None);
4356 return Py_None;
4357}
4358#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004359
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004360#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4361static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004362wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004363{
4364 PyObject *result;
4365 static PyObject *struct_rusage;
4366
4367 if (pid == -1)
4368 return posix_error();
4369
4370 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004371 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004372 if (m == NULL)
4373 return NULL;
4374 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4375 Py_DECREF(m);
4376 if (struct_rusage == NULL)
4377 return NULL;
4378 }
4379
4380 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4381 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4382 if (!result)
4383 return NULL;
4384
4385#ifndef doubletime
4386#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4387#endif
4388
4389 PyStructSequence_SET_ITEM(result, 0,
4390 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4391 PyStructSequence_SET_ITEM(result, 1,
4392 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4393#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004394 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004395 SET_INT(result, 2, ru->ru_maxrss);
4396 SET_INT(result, 3, ru->ru_ixrss);
4397 SET_INT(result, 4, ru->ru_idrss);
4398 SET_INT(result, 5, ru->ru_isrss);
4399 SET_INT(result, 6, ru->ru_minflt);
4400 SET_INT(result, 7, ru->ru_majflt);
4401 SET_INT(result, 8, ru->ru_nswap);
4402 SET_INT(result, 9, ru->ru_inblock);
4403 SET_INT(result, 10, ru->ru_oublock);
4404 SET_INT(result, 11, ru->ru_msgsnd);
4405 SET_INT(result, 12, ru->ru_msgrcv);
4406 SET_INT(result, 13, ru->ru_nsignals);
4407 SET_INT(result, 14, ru->ru_nvcsw);
4408 SET_INT(result, 15, ru->ru_nivcsw);
4409#undef SET_INT
4410
4411 if (PyErr_Occurred()) {
4412 Py_DECREF(result);
4413 return NULL;
4414 }
4415
4416 return Py_BuildValue("iiN", pid, status, result);
4417}
4418#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4419
4420#ifdef HAVE_WAIT3
4421PyDoc_STRVAR(posix_wait3__doc__,
4422"wait3(options) -> (pid, status, rusage)\n\n\
4423Wait for completion of a child process.");
4424
4425static PyObject *
4426posix_wait3(PyObject *self, PyObject *args)
4427{
Christian Heimes292d3512008-02-03 16:51:08 +00004428 pid_t pid;
4429 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004430 struct rusage ru;
4431 WAIT_TYPE status;
4432 WAIT_STATUS_INT(status) = 0;
4433
4434 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4435 return NULL;
4436
4437 Py_BEGIN_ALLOW_THREADS
4438 pid = wait3(&status, options, &ru);
4439 Py_END_ALLOW_THREADS
4440
4441 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4442}
4443#endif /* HAVE_WAIT3 */
4444
4445#ifdef HAVE_WAIT4
4446PyDoc_STRVAR(posix_wait4__doc__,
4447"wait4(pid, options) -> (pid, status, rusage)\n\n\
4448Wait for completion of a given child process.");
4449
4450static PyObject *
4451posix_wait4(PyObject *self, PyObject *args)
4452{
Christian Heimes292d3512008-02-03 16:51:08 +00004453 pid_t pid;
4454 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004455 struct rusage ru;
4456 WAIT_TYPE status;
4457 WAIT_STATUS_INT(status) = 0;
4458
4459 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
4460 return NULL;
4461
4462 Py_BEGIN_ALLOW_THREADS
4463 pid = wait4(pid, &status, options, &ru);
4464 Py_END_ALLOW_THREADS
4465
4466 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4467}
4468#endif /* HAVE_WAIT4 */
4469
Guido van Rossumb6775db1994-08-01 11:34:53 +00004470#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004471PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004472"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004473Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004474
Barry Warsaw53699e91996-12-10 23:23:01 +00004475static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004476posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004477{
Christian Heimes292d3512008-02-03 16:51:08 +00004478 pid_t pid;
4479 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004480 WAIT_TYPE status;
4481 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004482
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004483 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004484 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004485 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004486 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004487 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004488 if (pid == -1)
4489 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004490
4491 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004492}
4493
Tim Petersab034fa2002-02-01 11:27:43 +00004494#elif defined(HAVE_CWAIT)
4495
4496/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004497PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004498"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004499"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004500
4501static PyObject *
4502posix_waitpid(PyObject *self, PyObject *args)
4503{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004504 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004505 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004506
4507 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4508 return NULL;
4509 Py_BEGIN_ALLOW_THREADS
4510 pid = _cwait(&status, pid, options);
4511 Py_END_ALLOW_THREADS
4512 if (pid == -1)
4513 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004514
4515 /* shift the status left a byte so this is more like the POSIX waitpid */
4516 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004517}
4518#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004519
Guido van Rossumad0ee831995-03-01 10:34:45 +00004520#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004521PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004522"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004523Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004524
Barry Warsaw53699e91996-12-10 23:23:01 +00004525static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004526posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004527{
Christian Heimes292d3512008-02-03 16:51:08 +00004528 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004529 WAIT_TYPE status;
4530 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004531
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004532 Py_BEGIN_ALLOW_THREADS
4533 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004534 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004535 if (pid == -1)
4536 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004537
4538 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004539}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004540#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004541
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004543PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004544"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004545Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004546
Barry Warsaw53699e91996-12-10 23:23:01 +00004547static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004548posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004549{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004550#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004551 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004552#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004553#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00004554 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004555#else
4556 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4557#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004558#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004559}
4560
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004561
Guido van Rossumb6775db1994-08-01 11:34:53 +00004562#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004563PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004564"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004565Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004566
Barry Warsaw53699e91996-12-10 23:23:01 +00004567static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004568posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004569{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004570 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004571 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004572 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004573 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004574 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004575
4576 if (!PyArg_ParseTuple(args, "et:readlink",
4577 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004578 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004579 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004580 if (v == NULL) {
4581 PyMem_Free(path);
4582 return NULL;
4583 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004584
4585 if (PyUnicode_Check(v)) {
4586 arg_is_unicode = 1;
4587 }
4588 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004589
Barry Warsaw53699e91996-12-10 23:23:01 +00004590 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004591 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004592 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004593 if (n < 0)
Neal Norwitzfca70052007-08-12 16:56:02 +00004594 return posix_error_with_allocated_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004595
Neal Norwitzfca70052007-08-12 16:56:02 +00004596 PyMem_Free(path);
Christian Heimes72b710a2008-05-26 13:28:38 +00004597 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004598 if (arg_is_unicode) {
4599 PyObject *w;
4600
4601 w = PyUnicode_FromEncodedObject(v,
4602 Py_FileSystemDefaultEncoding,
4603 "strict");
4604 if (w != NULL) {
4605 Py_DECREF(v);
4606 v = w;
4607 }
4608 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00004609 v = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004610 }
4611 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004612 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004613}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004614#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004615
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004616
Guido van Rossumb6775db1994-08-01 11:34:53 +00004617#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004618PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004619"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004620Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004621
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004622static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004623posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004624{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004625 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004626}
4627#endif /* HAVE_SYMLINK */
4628
4629
4630#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004631#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4632static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004633system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004634{
4635 ULONG value = 0;
4636
4637 Py_BEGIN_ALLOW_THREADS
4638 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4639 Py_END_ALLOW_THREADS
4640
4641 return value;
4642}
4643
4644static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004645posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004646{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004647 /* Currently Only Uptime is Provided -- Others Later */
4648 return Py_BuildValue("ddddd",
4649 (double)0 /* t.tms_utime / HZ */,
4650 (double)0 /* t.tms_stime / HZ */,
4651 (double)0 /* t.tms_cutime / HZ */,
4652 (double)0 /* t.tms_cstime / HZ */,
4653 (double)system_uptime() / 1000);
4654}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004655#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004656#define NEED_TICKS_PER_SECOND
4657static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004658static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004659posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004660{
4661 struct tms t;
4662 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004663 errno = 0;
4664 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004665 if (c == (clock_t) -1)
4666 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004667 return Py_BuildValue("ddddd",
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004668 (double)t.tms_utime / ticks_per_second,
4669 (double)t.tms_stime / ticks_per_second,
4670 (double)t.tms_cutime / ticks_per_second,
4671 (double)t.tms_cstime / ticks_per_second,
4672 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004673}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004674#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004675#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004676
4677
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004678#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004679#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004680static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004681posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004682{
4683 FILETIME create, exit, kernel, user;
4684 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004685 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004686 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4687 /* The fields of a FILETIME structure are the hi and lo part
4688 of a 64-bit value expressed in 100 nanosecond units.
4689 1e7 is one second in such units; 1e-7 the inverse.
4690 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4691 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004692 return Py_BuildValue(
4693 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004694 (double)(user.dwHighDateTime*429.4967296 +
4695 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004696 (double)(kernel.dwHighDateTime*429.4967296 +
4697 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004698 (double)0,
4699 (double)0,
4700 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004701}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004702#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004703
4704#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004705PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004706"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004707Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004708#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004709
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004710
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004711#ifdef HAVE_GETSID
4712PyDoc_STRVAR(posix_getsid__doc__,
4713"getsid(pid) -> sid\n\n\
4714Call the system call getsid().");
4715
4716static PyObject *
4717posix_getsid(PyObject *self, PyObject *args)
4718{
Christian Heimes292d3512008-02-03 16:51:08 +00004719 pid_t pid;
4720 int sid;
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004721 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4722 return NULL;
4723 sid = getsid(pid);
4724 if (sid < 0)
4725 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004726 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004727}
4728#endif /* HAVE_GETSID */
4729
4730
Guido van Rossumb6775db1994-08-01 11:34:53 +00004731#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004732PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004733"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004734Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004735
Barry Warsaw53699e91996-12-10 23:23:01 +00004736static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004737posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004738{
Guido van Rossum687dd131993-05-17 08:34:16 +00004739 if (setsid() < 0)
4740 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004741 Py_INCREF(Py_None);
4742 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004743}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004744#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004745
Guido van Rossumb6775db1994-08-01 11:34:53 +00004746#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004747PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004748"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004749Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004750
Barry Warsaw53699e91996-12-10 23:23:01 +00004751static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004752posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004753{
Christian Heimes292d3512008-02-03 16:51:08 +00004754 pid_t pid;
4755 int pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004756 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004757 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004758 if (setpgid(pid, pgrp) < 0)
4759 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004760 Py_INCREF(Py_None);
4761 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004762}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004763#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004764
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004765
Guido van Rossumb6775db1994-08-01 11:34:53 +00004766#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004767PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004768"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004769Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004770
Barry Warsaw53699e91996-12-10 23:23:01 +00004771static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004772posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004773{
Christian Heimes15ebc882008-02-04 18:48:49 +00004774 int fd;
4775 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004776 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004777 return NULL;
4778 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004779 if (pgid < 0)
4780 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004781 return PyLong_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004782}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004783#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004784
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004785
Guido van Rossumb6775db1994-08-01 11:34:53 +00004786#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004787PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004788"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004789Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004790
Barry Warsaw53699e91996-12-10 23:23:01 +00004791static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004792posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004793{
4794 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004795 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004796 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004797 if (tcsetpgrp(fd, pgid) < 0)
4798 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004799 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004800 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004801}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004802#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004803
Guido van Rossum687dd131993-05-17 08:34:16 +00004804/* Functions acting on file descriptors */
4805
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004806PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004807"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004808Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004809
Barry Warsaw53699e91996-12-10 23:23:01 +00004810static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004811posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004812{
Mark Hammondef8b6542001-05-13 08:04:26 +00004813 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004814 int flag;
4815 int mode = 0777;
4816 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004817
4818#ifdef MS_WINDOWS
4819 if (unicode_file_names()) {
4820 PyUnicodeObject *po;
4821 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4822 Py_BEGIN_ALLOW_THREADS
4823 /* PyUnicode_AS_UNICODE OK without thread
4824 lock as it is a simple dereference. */
4825 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4826 Py_END_ALLOW_THREADS
4827 if (fd < 0)
4828 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004829 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004830 }
4831 /* Drop the argument parsing error as narrow strings
4832 are also valid. */
4833 PyErr_Clear();
4834 }
4835#endif
4836
Tim Peters5aa91602002-01-30 05:46:57 +00004837 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004838 Py_FileSystemDefaultEncoding, &file,
4839 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004840 return NULL;
4841
Barry Warsaw53699e91996-12-10 23:23:01 +00004842 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004843 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004844 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004845 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004846 return posix_error_with_allocated_filename(file);
4847 PyMem_Free(file);
Christian Heimes217cfd12007-12-02 14:31:20 +00004848 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004849}
4850
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004851
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004852PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004853"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004854Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004855
Barry Warsaw53699e91996-12-10 23:23:01 +00004856static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004857posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004858{
4859 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004860 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004861 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004862 if (!_PyVerify_fd(fd))
4863 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004864 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004865 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004866 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004867 if (res < 0)
4868 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004869 Py_INCREF(Py_None);
4870 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004871}
4872
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004873
Christian Heimesfdab48e2008-01-20 09:06:41 +00004874PyDoc_STRVAR(posix_closerange__doc__,
4875"closerange(fd_low, fd_high)\n\n\
4876Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4877
4878static PyObject *
4879posix_closerange(PyObject *self, PyObject *args)
4880{
4881 int fd_from, fd_to, i;
4882 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4883 return NULL;
4884 Py_BEGIN_ALLOW_THREADS
4885 for (i = fd_from; i < fd_to; i++)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004886 if (_PyVerify_fd(i))
4887 close(i);
Christian Heimesfdab48e2008-01-20 09:06:41 +00004888 Py_END_ALLOW_THREADS
4889 Py_RETURN_NONE;
4890}
4891
4892
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004893PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004894"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004895Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004896
Barry Warsaw53699e91996-12-10 23:23:01 +00004897static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004898posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004899{
4900 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004901 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004902 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004903 if (!_PyVerify_fd(fd))
4904 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004905 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004906 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004907 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004908 if (fd < 0)
4909 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004910 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004911}
4912
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004913
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004914PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004915"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004916Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004917
Barry Warsaw53699e91996-12-10 23:23:01 +00004918static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004919posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004920{
4921 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004922 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004923 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004924 if (!_PyVerify_fd_dup2(fd, fd2))
4925 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004926 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004927 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004928 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004929 if (res < 0)
4930 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004931 Py_INCREF(Py_None);
4932 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004933}
4934
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004935
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004936PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004937"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004938Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004939
Barry Warsaw53699e91996-12-10 23:23:01 +00004940static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004941posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004942{
4943 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004944#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004945 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004946#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004947 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004948#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004949 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004950 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004951 return NULL;
4952#ifdef SEEK_SET
4953 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4954 switch (how) {
4955 case 0: how = SEEK_SET; break;
4956 case 1: how = SEEK_CUR; break;
4957 case 2: how = SEEK_END; break;
4958 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004959#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004960
4961#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004962 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004963#else
4964 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00004965 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004966#endif
4967 if (PyErr_Occurred())
4968 return NULL;
4969
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004970 if (!_PyVerify_fd(fd))
4971 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004972 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004973#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004974 res = _lseeki64(fd, pos, how);
4975#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004976 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004977#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004978 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004979 if (res < 0)
4980 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004981
4982#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004983 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004984#else
4985 return PyLong_FromLongLong(res);
4986#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004987}
4988
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004989
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004990PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004991"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004992Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004993
Barry Warsaw53699e91996-12-10 23:23:01 +00004994static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004995posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004996{
Guido van Rossum572dbf82007-04-27 23:53:51 +00004997 int fd, size;
4998 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004999 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005000 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005001 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005002 if (size < 0) {
5003 errno = EINVAL;
5004 return posix_error();
5005 }
Christian Heimes72b710a2008-05-26 13:28:38 +00005006 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005007 if (buffer == NULL)
5008 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005009 if (!_PyVerify_fd(fd))
5010 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005011 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00005012 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005013 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005014 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005015 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005016 return posix_error();
5017 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005018 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00005019 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005020 return buffer;
5021}
5022
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005023
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005024PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005025"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005026Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005027
Barry Warsaw53699e91996-12-10 23:23:01 +00005028static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005029posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005030{
Martin v. Löwis423be952008-08-13 15:53:07 +00005031 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005032 int fd;
5033 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005034
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +00005035 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00005036 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005037 if (!_PyVerify_fd(fd))
5038 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005039 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005040 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00005041 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005042 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00005043 if (size < 0)
5044 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005045 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005046}
5047
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005048
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005049PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005050"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005051Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005052
Barry Warsaw53699e91996-12-10 23:23:01 +00005053static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005054posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005055{
5056 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005057 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005058 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005059 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005060 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005061#ifdef __VMS
5062 /* on OpenVMS we must ensure that all bytes are written to the file */
5063 fsync(fd);
5064#endif
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005065 if (!_PyVerify_fd(fd))
5066 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005067 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005068 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005069 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005070 if (res != 0) {
5071#ifdef MS_WINDOWS
5072 return win32_error("fstat", NULL);
5073#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005074 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005075#endif
5076 }
Tim Peters5aa91602002-01-30 05:46:57 +00005077
Martin v. Löwis14694662006-02-03 12:54:16 +00005078 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005079}
5080
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005081PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005082"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005083Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005084connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005085
5086static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005087posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005088{
5089 int fd;
5090 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5091 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005092 if (!_PyVerify_fd(fd))
5093 return PyBool_FromLong(0);
Fred Drake106c1a02002-04-23 15:58:02 +00005094 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005095}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005096
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005097#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005098PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005099"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005100Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005101
Barry Warsaw53699e91996-12-10 23:23:01 +00005102static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005103posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005104{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005105#if defined(PYOS_OS2)
5106 HFILE read, write;
5107 APIRET rc;
5108
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005109 Py_BEGIN_ALLOW_THREADS
5110 rc = DosCreatePipe( &read, &write, 4096);
5111 Py_END_ALLOW_THREADS
5112 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005113 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005114
5115 return Py_BuildValue("(ii)", read, write);
5116#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005117#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005118 int fds[2];
5119 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005120 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005121 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005122 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005123 if (res != 0)
5124 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005125 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005126#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005127 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005128 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005129 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005130 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005131 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005132 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005133 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005134 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005135 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5136 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005137 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005138#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005139#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005140}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005141#endif /* HAVE_PIPE */
5142
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005143
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005144#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005145PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005146"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005147Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005148
Barry Warsaw53699e91996-12-10 23:23:01 +00005149static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005150posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005151{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005152 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005153 int mode = 0666;
5154 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005155 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005156 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005157 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005158 res = mkfifo(filename, mode);
5159 Py_END_ALLOW_THREADS
5160 if (res < 0)
5161 return posix_error();
5162 Py_INCREF(Py_None);
5163 return Py_None;
5164}
5165#endif
5166
5167
Neal Norwitz11690112002-07-30 01:08:28 +00005168#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005169PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005170"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005171Create a filesystem node (file, device special file or named pipe)\n\
5172named filename. mode specifies both the permissions to use and the\n\
5173type of node to be created, being combined (bitwise OR) with one of\n\
5174S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005175device defines the newly created device special file (probably using\n\
5176os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005177
5178
5179static PyObject *
5180posix_mknod(PyObject *self, PyObject *args)
5181{
5182 char *filename;
5183 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005184 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005185 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005186 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005187 return NULL;
5188 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005189 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005190 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005191 if (res < 0)
5192 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005193 Py_INCREF(Py_None);
5194 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005195}
5196#endif
5197
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005198#ifdef HAVE_DEVICE_MACROS
5199PyDoc_STRVAR(posix_major__doc__,
5200"major(device) -> major number\n\
5201Extracts a device major number from a raw device number.");
5202
5203static PyObject *
5204posix_major(PyObject *self, PyObject *args)
5205{
5206 int device;
5207 if (!PyArg_ParseTuple(args, "i:major", &device))
5208 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005209 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005210}
5211
5212PyDoc_STRVAR(posix_minor__doc__,
5213"minor(device) -> minor number\n\
5214Extracts a device minor number from a raw device number.");
5215
5216static PyObject *
5217posix_minor(PyObject *self, PyObject *args)
5218{
5219 int device;
5220 if (!PyArg_ParseTuple(args, "i:minor", &device))
5221 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005222 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005223}
5224
5225PyDoc_STRVAR(posix_makedev__doc__,
5226"makedev(major, minor) -> device number\n\
5227Composes a raw device number from the major and minor device numbers.");
5228
5229static PyObject *
5230posix_makedev(PyObject *self, PyObject *args)
5231{
5232 int major, minor;
5233 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5234 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005235 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005236}
5237#endif /* device macros */
5238
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005239
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005240#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005241PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005242"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005243Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005244
Barry Warsaw53699e91996-12-10 23:23:01 +00005245static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005246posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005247{
5248 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005249 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005250 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005251 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005252
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005253 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005254 return NULL;
5255
5256#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005257 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005258#else
5259 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005260 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005261#endif
5262 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005263 return NULL;
5264
Barry Warsaw53699e91996-12-10 23:23:01 +00005265 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005266 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005267 Py_END_ALLOW_THREADS
Benjamin Peterson9053d752009-01-19 17:53:36 +00005268 if (res < 0)
5269 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005270 Py_INCREF(Py_None);
5271 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005272}
5273#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005274
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005275#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005276PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005277"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005278Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005279
Fred Drake762e2061999-08-26 17:23:54 +00005280/* Save putenv() parameters as values here, so we can collect them when they
5281 * get re-set with another call for the same key. */
5282static PyObject *posix_putenv_garbage;
5283
Tim Peters5aa91602002-01-30 05:46:57 +00005284static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005285posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005286{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005287#ifdef MS_WINDOWS
5288 wchar_t *s1, *s2;
5289 wchar_t *newenv;
5290#else
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005291 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005292 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005293#endif
Fred Drake762e2061999-08-26 17:23:54 +00005294 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005295 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005296
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005297 if (!PyArg_ParseTuple(args,
5298#ifdef MS_WINDOWS
5299 "uu:putenv",
5300#else
5301 "ss:putenv",
5302#endif
5303 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005304 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005305
5306#if defined(PYOS_OS2)
5307 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5308 APIRET rc;
5309
Guido van Rossumd48f2521997-12-05 22:19:34 +00005310 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5311 if (rc != NO_ERROR)
5312 return os2_error(rc);
5313
5314 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5315 APIRET rc;
5316
Guido van Rossumd48f2521997-12-05 22:19:34 +00005317 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5318 if (rc != NO_ERROR)
5319 return os2_error(rc);
5320 } else {
5321#endif
Fred Drake762e2061999-08-26 17:23:54 +00005322 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005323 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005324 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005325#ifdef MS_WINDOWS
5326 len = wcslen(s1) + wcslen(s2) + 2;
5327 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5328#else
5329 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005330 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005331#endif
Fred Drake762e2061999-08-26 17:23:54 +00005332 if (newstr == NULL)
5333 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005334#ifdef MS_WINDOWS
5335 newenv = PyUnicode_AsUnicode(newstr);
5336 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5337 if (_wputenv(newenv)) {
5338 Py_DECREF(newstr);
5339 posix_error();
5340 return NULL;
5341 }
5342#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005343 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005344 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5345 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005346 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005347 posix_error();
5348 return NULL;
5349 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005350#endif
Fred Drake762e2061999-08-26 17:23:54 +00005351 /* Install the first arg and newstr in posix_putenv_garbage;
5352 * this will cause previous value to be collected. This has to
5353 * happen after the real putenv() call because the old value
5354 * was still accessible until then. */
5355 if (PyDict_SetItem(posix_putenv_garbage,
5356 PyTuple_GET_ITEM(args, 0), newstr)) {
5357 /* really not much we can do; just leak */
5358 PyErr_Clear();
5359 }
5360 else {
5361 Py_DECREF(newstr);
5362 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005363
5364#if defined(PYOS_OS2)
5365 }
5366#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005367 Py_INCREF(Py_None);
5368 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005369}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005370#endif /* putenv */
5371
Guido van Rossumc524d952001-10-19 01:31:59 +00005372#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005373PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005374"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005375Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005376
5377static PyObject *
5378posix_unsetenv(PyObject *self, PyObject *args)
5379{
5380 char *s1;
5381
5382 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5383 return NULL;
5384
5385 unsetenv(s1);
5386
5387 /* Remove the key from posix_putenv_garbage;
5388 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005389 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005390 * old value was still accessible until then.
5391 */
5392 if (PyDict_DelItem(posix_putenv_garbage,
5393 PyTuple_GET_ITEM(args, 0))) {
5394 /* really not much we can do; just leak */
5395 PyErr_Clear();
5396 }
5397
5398 Py_INCREF(Py_None);
5399 return Py_None;
5400}
5401#endif /* unsetenv */
5402
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005403PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005404"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005405Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005406
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005407static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005408posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005409{
5410 int code;
5411 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005412 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005413 return NULL;
5414 message = strerror(code);
5415 if (message == NULL) {
5416 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005417 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005418 return NULL;
5419 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005420 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005421}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005422
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005423
Guido van Rossumc9641791998-08-04 15:26:23 +00005424#ifdef HAVE_SYS_WAIT_H
5425
Fred Drake106c1a02002-04-23 15:58:02 +00005426#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005427PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005428"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005429Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005430
5431static PyObject *
5432posix_WCOREDUMP(PyObject *self, PyObject *args)
5433{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005434 WAIT_TYPE status;
5435 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005436
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005437 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005438 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005439
5440 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005441}
5442#endif /* WCOREDUMP */
5443
5444#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005445PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005446"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005447Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005448job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005449
5450static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005451posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005452{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005453 WAIT_TYPE status;
5454 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005455
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005456 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005457 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005458
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005459 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005460}
5461#endif /* WIFCONTINUED */
5462
Guido van Rossumc9641791998-08-04 15:26:23 +00005463#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005464PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005465"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005466Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005467
5468static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005469posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005470{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005471 WAIT_TYPE status;
5472 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005473
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005474 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005475 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005476
Fred Drake106c1a02002-04-23 15:58:02 +00005477 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005478}
5479#endif /* WIFSTOPPED */
5480
5481#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005482PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005483"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005484Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005485
5486static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005487posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005488{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005489 WAIT_TYPE status;
5490 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005491
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005492 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005493 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005494
Fred Drake106c1a02002-04-23 15:58:02 +00005495 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005496}
5497#endif /* WIFSIGNALED */
5498
5499#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005500PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005501"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005502Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005503system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005504
5505static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005506posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005507{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005508 WAIT_TYPE status;
5509 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005510
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005511 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005512 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005513
Fred Drake106c1a02002-04-23 15:58:02 +00005514 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005515}
5516#endif /* WIFEXITED */
5517
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005518#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005519PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005520"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005521Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005522
5523static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005524posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005525{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005526 WAIT_TYPE status;
5527 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005528
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005529 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005530 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005531
Guido van Rossumc9641791998-08-04 15:26:23 +00005532 return Py_BuildValue("i", WEXITSTATUS(status));
5533}
5534#endif /* WEXITSTATUS */
5535
5536#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005537PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005538"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005539Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005540value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005541
5542static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005543posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005544{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005545 WAIT_TYPE status;
5546 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005547
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005548 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005549 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005550
Guido van Rossumc9641791998-08-04 15:26:23 +00005551 return Py_BuildValue("i", WTERMSIG(status));
5552}
5553#endif /* WTERMSIG */
5554
5555#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005556PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005557"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005558Return the signal that stopped the process that provided\n\
5559the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005560
5561static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005562posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005563{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005564 WAIT_TYPE status;
5565 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005566
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005567 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005568 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005569
Guido van Rossumc9641791998-08-04 15:26:23 +00005570 return Py_BuildValue("i", WSTOPSIG(status));
5571}
5572#endif /* WSTOPSIG */
5573
5574#endif /* HAVE_SYS_WAIT_H */
5575
5576
Thomas Wouters477c8d52006-05-27 19:21:47 +00005577#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005578#ifdef _SCO_DS
5579/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5580 needed definitions in sys/statvfs.h */
5581#define _SVID3
5582#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005583#include <sys/statvfs.h>
5584
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005585static PyObject*
5586_pystatvfs_fromstructstatvfs(struct statvfs st) {
5587 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5588 if (v == NULL)
5589 return NULL;
5590
5591#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005592 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5593 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5594 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5595 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5596 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5597 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5598 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5599 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5600 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5601 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005602#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005603 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5604 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005605 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005606 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005607 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005608 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005609 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005610 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005611 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005612 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005613 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005614 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005615 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005616 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005617 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5618 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005619#endif
5620
5621 return v;
5622}
5623
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005624PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005625"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005626Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005627
5628static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005629posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005630{
5631 int fd, res;
5632 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005633
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005634 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005635 return NULL;
5636 Py_BEGIN_ALLOW_THREADS
5637 res = fstatvfs(fd, &st);
5638 Py_END_ALLOW_THREADS
5639 if (res != 0)
5640 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005641
5642 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005643}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005644#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005645
5646
Thomas Wouters477c8d52006-05-27 19:21:47 +00005647#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005648#include <sys/statvfs.h>
5649
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005650PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005651"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005652Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005653
5654static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005655posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005656{
5657 char *path;
5658 int res;
5659 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005660 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005661 return NULL;
5662 Py_BEGIN_ALLOW_THREADS
5663 res = statvfs(path, &st);
5664 Py_END_ALLOW_THREADS
5665 if (res != 0)
5666 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005667
5668 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005669}
5670#endif /* HAVE_STATVFS */
5671
Fred Drakec9680921999-12-13 16:37:25 +00005672/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5673 * It maps strings representing configuration variable names to
5674 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005675 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005676 * rarely-used constants. There are three separate tables that use
5677 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005678 *
5679 * This code is always included, even if none of the interfaces that
5680 * need it are included. The #if hackery needed to avoid it would be
5681 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005682 */
5683struct constdef {
5684 char *name;
5685 long value;
5686};
5687
Fred Drake12c6e2d1999-12-14 21:25:03 +00005688static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005689conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005690 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005691{
Christian Heimes217cfd12007-12-02 14:31:20 +00005692 if (PyLong_Check(arg)) {
5693 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005694 return 1;
5695 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005696 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005697 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005698 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005699 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005700 size_t hi = tablesize;
5701 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005702 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005703 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005704 PyErr_SetString(PyExc_TypeError,
5705 "configuration names must be strings or integers");
5706 return 0;
5707 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005708 confname = _PyUnicode_AsString(arg);
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005709 if (confname == NULL)
5710 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005711 while (lo < hi) {
5712 mid = (lo + hi) / 2;
5713 cmp = strcmp(confname, table[mid].name);
5714 if (cmp < 0)
5715 hi = mid;
5716 else if (cmp > 0)
5717 lo = mid + 1;
5718 else {
5719 *valuep = table[mid].value;
5720 return 1;
5721 }
5722 }
5723 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005724 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005725 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005726}
5727
5728
5729#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5730static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005731#ifdef _PC_ABI_AIO_XFER_MAX
5732 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5733#endif
5734#ifdef _PC_ABI_ASYNC_IO
5735 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5736#endif
Fred Drakec9680921999-12-13 16:37:25 +00005737#ifdef _PC_ASYNC_IO
5738 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5739#endif
5740#ifdef _PC_CHOWN_RESTRICTED
5741 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5742#endif
5743#ifdef _PC_FILESIZEBITS
5744 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5745#endif
5746#ifdef _PC_LAST
5747 {"PC_LAST", _PC_LAST},
5748#endif
5749#ifdef _PC_LINK_MAX
5750 {"PC_LINK_MAX", _PC_LINK_MAX},
5751#endif
5752#ifdef _PC_MAX_CANON
5753 {"PC_MAX_CANON", _PC_MAX_CANON},
5754#endif
5755#ifdef _PC_MAX_INPUT
5756 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5757#endif
5758#ifdef _PC_NAME_MAX
5759 {"PC_NAME_MAX", _PC_NAME_MAX},
5760#endif
5761#ifdef _PC_NO_TRUNC
5762 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5763#endif
5764#ifdef _PC_PATH_MAX
5765 {"PC_PATH_MAX", _PC_PATH_MAX},
5766#endif
5767#ifdef _PC_PIPE_BUF
5768 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5769#endif
5770#ifdef _PC_PRIO_IO
5771 {"PC_PRIO_IO", _PC_PRIO_IO},
5772#endif
5773#ifdef _PC_SOCK_MAXBUF
5774 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5775#endif
5776#ifdef _PC_SYNC_IO
5777 {"PC_SYNC_IO", _PC_SYNC_IO},
5778#endif
5779#ifdef _PC_VDISABLE
5780 {"PC_VDISABLE", _PC_VDISABLE},
5781#endif
5782};
5783
Fred Drakec9680921999-12-13 16:37:25 +00005784static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005785conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005786{
5787 return conv_confname(arg, valuep, posix_constants_pathconf,
5788 sizeof(posix_constants_pathconf)
5789 / sizeof(struct constdef));
5790}
5791#endif
5792
5793#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005794PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005795"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005796Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005797If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005798
5799static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005800posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005801{
5802 PyObject *result = NULL;
5803 int name, fd;
5804
Fred Drake12c6e2d1999-12-14 21:25:03 +00005805 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5806 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005807 long limit;
5808
5809 errno = 0;
5810 limit = fpathconf(fd, name);
5811 if (limit == -1 && errno != 0)
5812 posix_error();
5813 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005814 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005815 }
5816 return result;
5817}
5818#endif
5819
5820
5821#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005822PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005823"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005824Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005825If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005826
5827static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005828posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005829{
5830 PyObject *result = NULL;
5831 int name;
5832 char *path;
5833
5834 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5835 conv_path_confname, &name)) {
5836 long limit;
5837
5838 errno = 0;
5839 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005840 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005841 if (errno == EINVAL)
5842 /* could be a path or name problem */
5843 posix_error();
5844 else
5845 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005846 }
Fred Drakec9680921999-12-13 16:37:25 +00005847 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005848 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005849 }
5850 return result;
5851}
5852#endif
5853
5854#ifdef HAVE_CONFSTR
5855static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005856#ifdef _CS_ARCHITECTURE
5857 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5858#endif
5859#ifdef _CS_HOSTNAME
5860 {"CS_HOSTNAME", _CS_HOSTNAME},
5861#endif
5862#ifdef _CS_HW_PROVIDER
5863 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5864#endif
5865#ifdef _CS_HW_SERIAL
5866 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5867#endif
5868#ifdef _CS_INITTAB_NAME
5869 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5870#endif
Fred Drakec9680921999-12-13 16:37:25 +00005871#ifdef _CS_LFS64_CFLAGS
5872 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5873#endif
5874#ifdef _CS_LFS64_LDFLAGS
5875 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5876#endif
5877#ifdef _CS_LFS64_LIBS
5878 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5879#endif
5880#ifdef _CS_LFS64_LINTFLAGS
5881 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5882#endif
5883#ifdef _CS_LFS_CFLAGS
5884 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5885#endif
5886#ifdef _CS_LFS_LDFLAGS
5887 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5888#endif
5889#ifdef _CS_LFS_LIBS
5890 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5891#endif
5892#ifdef _CS_LFS_LINTFLAGS
5893 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5894#endif
Fred Draked86ed291999-12-15 15:34:33 +00005895#ifdef _CS_MACHINE
5896 {"CS_MACHINE", _CS_MACHINE},
5897#endif
Fred Drakec9680921999-12-13 16:37:25 +00005898#ifdef _CS_PATH
5899 {"CS_PATH", _CS_PATH},
5900#endif
Fred Draked86ed291999-12-15 15:34:33 +00005901#ifdef _CS_RELEASE
5902 {"CS_RELEASE", _CS_RELEASE},
5903#endif
5904#ifdef _CS_SRPC_DOMAIN
5905 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5906#endif
5907#ifdef _CS_SYSNAME
5908 {"CS_SYSNAME", _CS_SYSNAME},
5909#endif
5910#ifdef _CS_VERSION
5911 {"CS_VERSION", _CS_VERSION},
5912#endif
Fred Drakec9680921999-12-13 16:37:25 +00005913#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5914 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5915#endif
5916#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5917 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5918#endif
5919#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5920 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5921#endif
5922#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5923 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5924#endif
5925#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5926 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5927#endif
5928#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5929 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5930#endif
5931#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5932 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5933#endif
5934#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5935 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5936#endif
5937#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5938 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5939#endif
5940#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5941 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5942#endif
5943#ifdef _CS_XBS5_LP64_OFF64_LIBS
5944 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5945#endif
5946#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5947 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5948#endif
5949#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5950 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5951#endif
5952#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5953 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5954#endif
5955#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5956 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5957#endif
5958#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5959 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5960#endif
Fred Draked86ed291999-12-15 15:34:33 +00005961#ifdef _MIPS_CS_AVAIL_PROCESSORS
5962 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5963#endif
5964#ifdef _MIPS_CS_BASE
5965 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5966#endif
5967#ifdef _MIPS_CS_HOSTID
5968 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5969#endif
5970#ifdef _MIPS_CS_HW_NAME
5971 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5972#endif
5973#ifdef _MIPS_CS_NUM_PROCESSORS
5974 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5975#endif
5976#ifdef _MIPS_CS_OSREL_MAJ
5977 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5978#endif
5979#ifdef _MIPS_CS_OSREL_MIN
5980 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5981#endif
5982#ifdef _MIPS_CS_OSREL_PATCH
5983 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5984#endif
5985#ifdef _MIPS_CS_OS_NAME
5986 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5987#endif
5988#ifdef _MIPS_CS_OS_PROVIDER
5989 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5990#endif
5991#ifdef _MIPS_CS_PROCESSORS
5992 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5993#endif
5994#ifdef _MIPS_CS_SERIAL
5995 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5996#endif
5997#ifdef _MIPS_CS_VENDOR
5998 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
5999#endif
Fred Drakec9680921999-12-13 16:37:25 +00006000};
6001
6002static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006003conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006004{
6005 return conv_confname(arg, valuep, posix_constants_confstr,
6006 sizeof(posix_constants_confstr)
6007 / sizeof(struct constdef));
6008}
6009
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006010PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006011"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006012Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006013
6014static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006015posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006016{
6017 PyObject *result = NULL;
6018 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006019 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006020
6021 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006022 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006023
Fred Drakec9680921999-12-13 16:37:25 +00006024 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006025 len = confstr(name, buffer, sizeof(buffer));
6026 if (len == 0) {
6027 if (errno) {
6028 posix_error();
6029 }
6030 else {
6031 result = Py_None;
6032 Py_INCREF(Py_None);
6033 }
Fred Drakec9680921999-12-13 16:37:25 +00006034 }
6035 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006036 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006037 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006038 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006039 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006040 }
6041 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006042 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006043 }
6044 }
6045 return result;
6046}
6047#endif
6048
6049
6050#ifdef HAVE_SYSCONF
6051static struct constdef posix_constants_sysconf[] = {
6052#ifdef _SC_2_CHAR_TERM
6053 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6054#endif
6055#ifdef _SC_2_C_BIND
6056 {"SC_2_C_BIND", _SC_2_C_BIND},
6057#endif
6058#ifdef _SC_2_C_DEV
6059 {"SC_2_C_DEV", _SC_2_C_DEV},
6060#endif
6061#ifdef _SC_2_C_VERSION
6062 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6063#endif
6064#ifdef _SC_2_FORT_DEV
6065 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6066#endif
6067#ifdef _SC_2_FORT_RUN
6068 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6069#endif
6070#ifdef _SC_2_LOCALEDEF
6071 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6072#endif
6073#ifdef _SC_2_SW_DEV
6074 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6075#endif
6076#ifdef _SC_2_UPE
6077 {"SC_2_UPE", _SC_2_UPE},
6078#endif
6079#ifdef _SC_2_VERSION
6080 {"SC_2_VERSION", _SC_2_VERSION},
6081#endif
Fred Draked86ed291999-12-15 15:34:33 +00006082#ifdef _SC_ABI_ASYNCHRONOUS_IO
6083 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6084#endif
6085#ifdef _SC_ACL
6086 {"SC_ACL", _SC_ACL},
6087#endif
Fred Drakec9680921999-12-13 16:37:25 +00006088#ifdef _SC_AIO_LISTIO_MAX
6089 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6090#endif
Fred Drakec9680921999-12-13 16:37:25 +00006091#ifdef _SC_AIO_MAX
6092 {"SC_AIO_MAX", _SC_AIO_MAX},
6093#endif
6094#ifdef _SC_AIO_PRIO_DELTA_MAX
6095 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6096#endif
6097#ifdef _SC_ARG_MAX
6098 {"SC_ARG_MAX", _SC_ARG_MAX},
6099#endif
6100#ifdef _SC_ASYNCHRONOUS_IO
6101 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6102#endif
6103#ifdef _SC_ATEXIT_MAX
6104 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6105#endif
Fred Draked86ed291999-12-15 15:34:33 +00006106#ifdef _SC_AUDIT
6107 {"SC_AUDIT", _SC_AUDIT},
6108#endif
Fred Drakec9680921999-12-13 16:37:25 +00006109#ifdef _SC_AVPHYS_PAGES
6110 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6111#endif
6112#ifdef _SC_BC_BASE_MAX
6113 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6114#endif
6115#ifdef _SC_BC_DIM_MAX
6116 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6117#endif
6118#ifdef _SC_BC_SCALE_MAX
6119 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6120#endif
6121#ifdef _SC_BC_STRING_MAX
6122 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6123#endif
Fred Draked86ed291999-12-15 15:34:33 +00006124#ifdef _SC_CAP
6125 {"SC_CAP", _SC_CAP},
6126#endif
Fred Drakec9680921999-12-13 16:37:25 +00006127#ifdef _SC_CHARCLASS_NAME_MAX
6128 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6129#endif
6130#ifdef _SC_CHAR_BIT
6131 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6132#endif
6133#ifdef _SC_CHAR_MAX
6134 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6135#endif
6136#ifdef _SC_CHAR_MIN
6137 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6138#endif
6139#ifdef _SC_CHILD_MAX
6140 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6141#endif
6142#ifdef _SC_CLK_TCK
6143 {"SC_CLK_TCK", _SC_CLK_TCK},
6144#endif
6145#ifdef _SC_COHER_BLKSZ
6146 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6147#endif
6148#ifdef _SC_COLL_WEIGHTS_MAX
6149 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6150#endif
6151#ifdef _SC_DCACHE_ASSOC
6152 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6153#endif
6154#ifdef _SC_DCACHE_BLKSZ
6155 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6156#endif
6157#ifdef _SC_DCACHE_LINESZ
6158 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6159#endif
6160#ifdef _SC_DCACHE_SZ
6161 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6162#endif
6163#ifdef _SC_DCACHE_TBLKSZ
6164 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6165#endif
6166#ifdef _SC_DELAYTIMER_MAX
6167 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6168#endif
6169#ifdef _SC_EQUIV_CLASS_MAX
6170 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6171#endif
6172#ifdef _SC_EXPR_NEST_MAX
6173 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6174#endif
6175#ifdef _SC_FSYNC
6176 {"SC_FSYNC", _SC_FSYNC},
6177#endif
6178#ifdef _SC_GETGR_R_SIZE_MAX
6179 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6180#endif
6181#ifdef _SC_GETPW_R_SIZE_MAX
6182 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6183#endif
6184#ifdef _SC_ICACHE_ASSOC
6185 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6186#endif
6187#ifdef _SC_ICACHE_BLKSZ
6188 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6189#endif
6190#ifdef _SC_ICACHE_LINESZ
6191 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6192#endif
6193#ifdef _SC_ICACHE_SZ
6194 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6195#endif
Fred Draked86ed291999-12-15 15:34:33 +00006196#ifdef _SC_INF
6197 {"SC_INF", _SC_INF},
6198#endif
Fred Drakec9680921999-12-13 16:37:25 +00006199#ifdef _SC_INT_MAX
6200 {"SC_INT_MAX", _SC_INT_MAX},
6201#endif
6202#ifdef _SC_INT_MIN
6203 {"SC_INT_MIN", _SC_INT_MIN},
6204#endif
6205#ifdef _SC_IOV_MAX
6206 {"SC_IOV_MAX", _SC_IOV_MAX},
6207#endif
Fred Draked86ed291999-12-15 15:34:33 +00006208#ifdef _SC_IP_SECOPTS
6209 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6210#endif
Fred Drakec9680921999-12-13 16:37:25 +00006211#ifdef _SC_JOB_CONTROL
6212 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6213#endif
Fred Draked86ed291999-12-15 15:34:33 +00006214#ifdef _SC_KERN_POINTERS
6215 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6216#endif
6217#ifdef _SC_KERN_SIM
6218 {"SC_KERN_SIM", _SC_KERN_SIM},
6219#endif
Fred Drakec9680921999-12-13 16:37:25 +00006220#ifdef _SC_LINE_MAX
6221 {"SC_LINE_MAX", _SC_LINE_MAX},
6222#endif
6223#ifdef _SC_LOGIN_NAME_MAX
6224 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6225#endif
6226#ifdef _SC_LOGNAME_MAX
6227 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6228#endif
6229#ifdef _SC_LONG_BIT
6230 {"SC_LONG_BIT", _SC_LONG_BIT},
6231#endif
Fred Draked86ed291999-12-15 15:34:33 +00006232#ifdef _SC_MAC
6233 {"SC_MAC", _SC_MAC},
6234#endif
Fred Drakec9680921999-12-13 16:37:25 +00006235#ifdef _SC_MAPPED_FILES
6236 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6237#endif
6238#ifdef _SC_MAXPID
6239 {"SC_MAXPID", _SC_MAXPID},
6240#endif
6241#ifdef _SC_MB_LEN_MAX
6242 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6243#endif
6244#ifdef _SC_MEMLOCK
6245 {"SC_MEMLOCK", _SC_MEMLOCK},
6246#endif
6247#ifdef _SC_MEMLOCK_RANGE
6248 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6249#endif
6250#ifdef _SC_MEMORY_PROTECTION
6251 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6252#endif
6253#ifdef _SC_MESSAGE_PASSING
6254 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6255#endif
Fred Draked86ed291999-12-15 15:34:33 +00006256#ifdef _SC_MMAP_FIXED_ALIGNMENT
6257 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6258#endif
Fred Drakec9680921999-12-13 16:37:25 +00006259#ifdef _SC_MQ_OPEN_MAX
6260 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6261#endif
6262#ifdef _SC_MQ_PRIO_MAX
6263 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6264#endif
Fred Draked86ed291999-12-15 15:34:33 +00006265#ifdef _SC_NACLS_MAX
6266 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6267#endif
Fred Drakec9680921999-12-13 16:37:25 +00006268#ifdef _SC_NGROUPS_MAX
6269 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6270#endif
6271#ifdef _SC_NL_ARGMAX
6272 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6273#endif
6274#ifdef _SC_NL_LANGMAX
6275 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6276#endif
6277#ifdef _SC_NL_MSGMAX
6278 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6279#endif
6280#ifdef _SC_NL_NMAX
6281 {"SC_NL_NMAX", _SC_NL_NMAX},
6282#endif
6283#ifdef _SC_NL_SETMAX
6284 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6285#endif
6286#ifdef _SC_NL_TEXTMAX
6287 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6288#endif
6289#ifdef _SC_NPROCESSORS_CONF
6290 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6291#endif
6292#ifdef _SC_NPROCESSORS_ONLN
6293 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6294#endif
Fred Draked86ed291999-12-15 15:34:33 +00006295#ifdef _SC_NPROC_CONF
6296 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6297#endif
6298#ifdef _SC_NPROC_ONLN
6299 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6300#endif
Fred Drakec9680921999-12-13 16:37:25 +00006301#ifdef _SC_NZERO
6302 {"SC_NZERO", _SC_NZERO},
6303#endif
6304#ifdef _SC_OPEN_MAX
6305 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6306#endif
6307#ifdef _SC_PAGESIZE
6308 {"SC_PAGESIZE", _SC_PAGESIZE},
6309#endif
6310#ifdef _SC_PAGE_SIZE
6311 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6312#endif
6313#ifdef _SC_PASS_MAX
6314 {"SC_PASS_MAX", _SC_PASS_MAX},
6315#endif
6316#ifdef _SC_PHYS_PAGES
6317 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6318#endif
6319#ifdef _SC_PII
6320 {"SC_PII", _SC_PII},
6321#endif
6322#ifdef _SC_PII_INTERNET
6323 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6324#endif
6325#ifdef _SC_PII_INTERNET_DGRAM
6326 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6327#endif
6328#ifdef _SC_PII_INTERNET_STREAM
6329 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6330#endif
6331#ifdef _SC_PII_OSI
6332 {"SC_PII_OSI", _SC_PII_OSI},
6333#endif
6334#ifdef _SC_PII_OSI_CLTS
6335 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6336#endif
6337#ifdef _SC_PII_OSI_COTS
6338 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6339#endif
6340#ifdef _SC_PII_OSI_M
6341 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6342#endif
6343#ifdef _SC_PII_SOCKET
6344 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6345#endif
6346#ifdef _SC_PII_XTI
6347 {"SC_PII_XTI", _SC_PII_XTI},
6348#endif
6349#ifdef _SC_POLL
6350 {"SC_POLL", _SC_POLL},
6351#endif
6352#ifdef _SC_PRIORITIZED_IO
6353 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6354#endif
6355#ifdef _SC_PRIORITY_SCHEDULING
6356 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6357#endif
6358#ifdef _SC_REALTIME_SIGNALS
6359 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6360#endif
6361#ifdef _SC_RE_DUP_MAX
6362 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6363#endif
6364#ifdef _SC_RTSIG_MAX
6365 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6366#endif
6367#ifdef _SC_SAVED_IDS
6368 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6369#endif
6370#ifdef _SC_SCHAR_MAX
6371 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6372#endif
6373#ifdef _SC_SCHAR_MIN
6374 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6375#endif
6376#ifdef _SC_SELECT
6377 {"SC_SELECT", _SC_SELECT},
6378#endif
6379#ifdef _SC_SEMAPHORES
6380 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6381#endif
6382#ifdef _SC_SEM_NSEMS_MAX
6383 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6384#endif
6385#ifdef _SC_SEM_VALUE_MAX
6386 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6387#endif
6388#ifdef _SC_SHARED_MEMORY_OBJECTS
6389 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6390#endif
6391#ifdef _SC_SHRT_MAX
6392 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6393#endif
6394#ifdef _SC_SHRT_MIN
6395 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6396#endif
6397#ifdef _SC_SIGQUEUE_MAX
6398 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6399#endif
6400#ifdef _SC_SIGRT_MAX
6401 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6402#endif
6403#ifdef _SC_SIGRT_MIN
6404 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6405#endif
Fred Draked86ed291999-12-15 15:34:33 +00006406#ifdef _SC_SOFTPOWER
6407 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6408#endif
Fred Drakec9680921999-12-13 16:37:25 +00006409#ifdef _SC_SPLIT_CACHE
6410 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6411#endif
6412#ifdef _SC_SSIZE_MAX
6413 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6414#endif
6415#ifdef _SC_STACK_PROT
6416 {"SC_STACK_PROT", _SC_STACK_PROT},
6417#endif
6418#ifdef _SC_STREAM_MAX
6419 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6420#endif
6421#ifdef _SC_SYNCHRONIZED_IO
6422 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6423#endif
6424#ifdef _SC_THREADS
6425 {"SC_THREADS", _SC_THREADS},
6426#endif
6427#ifdef _SC_THREAD_ATTR_STACKADDR
6428 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6429#endif
6430#ifdef _SC_THREAD_ATTR_STACKSIZE
6431 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6432#endif
6433#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6434 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6435#endif
6436#ifdef _SC_THREAD_KEYS_MAX
6437 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6438#endif
6439#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6440 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6441#endif
6442#ifdef _SC_THREAD_PRIO_INHERIT
6443 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6444#endif
6445#ifdef _SC_THREAD_PRIO_PROTECT
6446 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6447#endif
6448#ifdef _SC_THREAD_PROCESS_SHARED
6449 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6450#endif
6451#ifdef _SC_THREAD_SAFE_FUNCTIONS
6452 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6453#endif
6454#ifdef _SC_THREAD_STACK_MIN
6455 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6456#endif
6457#ifdef _SC_THREAD_THREADS_MAX
6458 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6459#endif
6460#ifdef _SC_TIMERS
6461 {"SC_TIMERS", _SC_TIMERS},
6462#endif
6463#ifdef _SC_TIMER_MAX
6464 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6465#endif
6466#ifdef _SC_TTY_NAME_MAX
6467 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6468#endif
6469#ifdef _SC_TZNAME_MAX
6470 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6471#endif
6472#ifdef _SC_T_IOV_MAX
6473 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6474#endif
6475#ifdef _SC_UCHAR_MAX
6476 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6477#endif
6478#ifdef _SC_UINT_MAX
6479 {"SC_UINT_MAX", _SC_UINT_MAX},
6480#endif
6481#ifdef _SC_UIO_MAXIOV
6482 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6483#endif
6484#ifdef _SC_ULONG_MAX
6485 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6486#endif
6487#ifdef _SC_USHRT_MAX
6488 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6489#endif
6490#ifdef _SC_VERSION
6491 {"SC_VERSION", _SC_VERSION},
6492#endif
6493#ifdef _SC_WORD_BIT
6494 {"SC_WORD_BIT", _SC_WORD_BIT},
6495#endif
6496#ifdef _SC_XBS5_ILP32_OFF32
6497 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6498#endif
6499#ifdef _SC_XBS5_ILP32_OFFBIG
6500 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6501#endif
6502#ifdef _SC_XBS5_LP64_OFF64
6503 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6504#endif
6505#ifdef _SC_XBS5_LPBIG_OFFBIG
6506 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6507#endif
6508#ifdef _SC_XOPEN_CRYPT
6509 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6510#endif
6511#ifdef _SC_XOPEN_ENH_I18N
6512 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6513#endif
6514#ifdef _SC_XOPEN_LEGACY
6515 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6516#endif
6517#ifdef _SC_XOPEN_REALTIME
6518 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6519#endif
6520#ifdef _SC_XOPEN_REALTIME_THREADS
6521 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6522#endif
6523#ifdef _SC_XOPEN_SHM
6524 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6525#endif
6526#ifdef _SC_XOPEN_UNIX
6527 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6528#endif
6529#ifdef _SC_XOPEN_VERSION
6530 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6531#endif
6532#ifdef _SC_XOPEN_XCU_VERSION
6533 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6534#endif
6535#ifdef _SC_XOPEN_XPG2
6536 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6537#endif
6538#ifdef _SC_XOPEN_XPG3
6539 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6540#endif
6541#ifdef _SC_XOPEN_XPG4
6542 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6543#endif
6544};
6545
6546static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006547conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006548{
6549 return conv_confname(arg, valuep, posix_constants_sysconf,
6550 sizeof(posix_constants_sysconf)
6551 / sizeof(struct constdef));
6552}
6553
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006554PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006555"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006556Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006557
6558static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006559posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006560{
6561 PyObject *result = NULL;
6562 int name;
6563
6564 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6565 int value;
6566
6567 errno = 0;
6568 value = sysconf(name);
6569 if (value == -1 && errno != 0)
6570 posix_error();
6571 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006572 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006573 }
6574 return result;
6575}
6576#endif
6577
6578
Fred Drakebec628d1999-12-15 18:31:10 +00006579/* This code is used to ensure that the tables of configuration value names
6580 * are in sorted order as required by conv_confname(), and also to build the
6581 * the exported dictionaries that are used to publish information about the
6582 * names available on the host platform.
6583 *
6584 * Sorting the table at runtime ensures that the table is properly ordered
6585 * when used, even for platforms we're not able to test on. It also makes
6586 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006587 */
Fred Drakebec628d1999-12-15 18:31:10 +00006588
6589static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006590cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006591{
6592 const struct constdef *c1 =
6593 (const struct constdef *) v1;
6594 const struct constdef *c2 =
6595 (const struct constdef *) v2;
6596
6597 return strcmp(c1->name, c2->name);
6598}
6599
6600static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006601setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006602 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006603{
Fred Drakebec628d1999-12-15 18:31:10 +00006604 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006605 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006606
6607 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6608 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006609 if (d == NULL)
6610 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006611
Barry Warsaw3155db32000-04-13 15:20:40 +00006612 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006613 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006614 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6615 Py_XDECREF(o);
6616 Py_DECREF(d);
6617 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006618 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006619 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006620 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006621 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006622}
6623
Fred Drakebec628d1999-12-15 18:31:10 +00006624/* Return -1 on failure, 0 on success. */
6625static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006626setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006627{
6628#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006629 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006630 sizeof(posix_constants_pathconf)
6631 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006632 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006633 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006634#endif
6635#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006636 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006637 sizeof(posix_constants_confstr)
6638 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006639 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006640 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006641#endif
6642#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006643 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006644 sizeof(posix_constants_sysconf)
6645 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006646 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006647 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006648#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006649 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006650}
Fred Draked86ed291999-12-15 15:34:33 +00006651
6652
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006653PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006654"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006655Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006656in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006657
6658static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006659posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006660{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006661 abort();
6662 /*NOTREACHED*/
6663 Py_FatalError("abort() called from Python code didn't abort!");
6664 return NULL;
6665}
Fred Drakebec628d1999-12-15 18:31:10 +00006666
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006667#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006668PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006669"startfile(filepath [, operation]) - Start a file with its associated\n\
6670application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006671\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006672When \"operation\" is not specified or \"open\", this acts like\n\
6673double-clicking the file in Explorer, or giving the file name as an\n\
6674argument to the DOS \"start\" command: the file is opened with whatever\n\
6675application (if any) its extension is associated.\n\
6676When another \"operation\" is given, it specifies what should be done with\n\
6677the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006678\n\
6679startfile returns as soon as the associated application is launched.\n\
6680There is no option to wait for the application to close, and no way\n\
6681to retrieve the application's exit status.\n\
6682\n\
6683The filepath is relative to the current directory. If you want to use\n\
6684an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006685the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006686
6687static PyObject *
6688win32_startfile(PyObject *self, PyObject *args)
6689{
6690 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006691 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006692 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006693#ifdef Py_WIN_WIDE_FILENAMES
6694 if (unicode_file_names()) {
6695 PyObject *unipath, *woperation = NULL;
6696 if (!PyArg_ParseTuple(args, "U|s:startfile",
6697 &unipath, &operation)) {
6698 PyErr_Clear();
6699 goto normal;
6700 }
6701
6702
6703 if (operation) {
6704 woperation = PyUnicode_DecodeASCII(operation,
6705 strlen(operation), NULL);
6706 if (!woperation) {
6707 PyErr_Clear();
6708 operation = NULL;
6709 goto normal;
6710 }
6711 }
6712
6713 Py_BEGIN_ALLOW_THREADS
6714 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6715 PyUnicode_AS_UNICODE(unipath),
6716 NULL, NULL, SW_SHOWNORMAL);
6717 Py_END_ALLOW_THREADS
6718
6719 Py_XDECREF(woperation);
6720 if (rc <= (HINSTANCE)32) {
6721 PyObject *errval = win32_error_unicode("startfile",
6722 PyUnicode_AS_UNICODE(unipath));
6723 return errval;
6724 }
6725 Py_INCREF(Py_None);
6726 return Py_None;
6727 }
6728#endif
6729
6730normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00006731 if (!PyArg_ParseTuple(args, "et|s:startfile",
6732 Py_FileSystemDefaultEncoding, &filepath,
6733 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006734 return NULL;
6735 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006736 rc = ShellExecute((HWND)0, operation, filepath,
6737 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006738 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006739 if (rc <= (HINSTANCE)32) {
6740 PyObject *errval = win32_error("startfile", filepath);
6741 PyMem_Free(filepath);
6742 return errval;
6743 }
6744 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006745 Py_INCREF(Py_None);
6746 return Py_None;
6747}
6748#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006749
Martin v. Löwis438b5342002-12-27 10:16:42 +00006750#ifdef HAVE_GETLOADAVG
6751PyDoc_STRVAR(posix_getloadavg__doc__,
6752"getloadavg() -> (float, float, float)\n\n\
6753Return the number of processes in the system run queue averaged over\n\
6754the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6755was unobtainable");
6756
6757static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006758posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006759{
6760 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006761 if (getloadavg(loadavg, 3)!=3) {
6762 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6763 return NULL;
6764 } else
6765 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6766}
6767#endif
6768
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006769#ifdef MS_WINDOWS
6770
6771PyDoc_STRVAR(win32_urandom__doc__,
6772"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006773Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006774
6775typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6776 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6777 DWORD dwFlags );
6778typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6779 BYTE *pbBuffer );
6780
6781static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006782/* This handle is never explicitly released. Instead, the operating
6783 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006784static HCRYPTPROV hCryptProv = 0;
6785
Tim Peters4ad82172004-08-30 17:02:04 +00006786static PyObject*
6787win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006788{
Tim Petersd3115382004-08-30 17:36:46 +00006789 int howMany;
6790 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006791
Tim Peters4ad82172004-08-30 17:02:04 +00006792 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006793 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006794 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006795 if (howMany < 0)
6796 return PyErr_Format(PyExc_ValueError,
6797 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006798
Tim Peters4ad82172004-08-30 17:02:04 +00006799 if (hCryptProv == 0) {
6800 HINSTANCE hAdvAPI32 = NULL;
6801 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006802
Tim Peters4ad82172004-08-30 17:02:04 +00006803 /* Obtain handle to the DLL containing CryptoAPI
6804 This should not fail */
6805 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6806 if(hAdvAPI32 == NULL)
6807 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006808
Tim Peters4ad82172004-08-30 17:02:04 +00006809 /* Obtain pointers to the CryptoAPI functions
6810 This will fail on some early versions of Win95 */
6811 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6812 hAdvAPI32,
6813 "CryptAcquireContextA");
6814 if (pCryptAcquireContext == NULL)
6815 return PyErr_Format(PyExc_NotImplementedError,
6816 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006817
Tim Peters4ad82172004-08-30 17:02:04 +00006818 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6819 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006820 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006821 return PyErr_Format(PyExc_NotImplementedError,
6822 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006823
Tim Peters4ad82172004-08-30 17:02:04 +00006824 /* Acquire context */
6825 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6826 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6827 return win32_error("CryptAcquireContext", NULL);
6828 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006829
Tim Peters4ad82172004-08-30 17:02:04 +00006830 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006831 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006832 if (result != NULL) {
6833 /* Get random data */
Amaury Forgeot d'Arca05ada32008-07-21 21:13:14 +00006834 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00006835 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006836 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006837 Py_DECREF(result);
6838 return win32_error("CryptGenRandom", NULL);
6839 }
Tim Peters4ad82172004-08-30 17:02:04 +00006840 }
Tim Petersd3115382004-08-30 17:36:46 +00006841 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006842}
6843#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006844
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006845PyDoc_STRVAR(device_encoding__doc__,
6846"device_encoding(fd) -> str\n\n\
6847Return a string describing the encoding of the device\n\
6848if the output is a terminal; else return None.");
6849
6850static PyObject *
6851device_encoding(PyObject *self, PyObject *args)
6852{
6853 int fd;
6854 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6855 return NULL;
Kristján Valur Jónsson649170b2009-03-24 14:15:49 +00006856 if (!_PyVerify_fd(fd) || !isatty(fd)) {
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006857 Py_INCREF(Py_None);
6858 return Py_None;
6859 }
6860#if defined(MS_WINDOWS) || defined(MS_WIN64)
6861 if (fd == 0) {
6862 char buf[100];
6863 sprintf(buf, "cp%d", GetConsoleCP());
6864 return PyUnicode_FromString(buf);
6865 }
6866 if (fd == 1 || fd == 2) {
6867 char buf[100];
6868 sprintf(buf, "cp%d", GetConsoleOutputCP());
6869 return PyUnicode_FromString(buf);
6870 }
6871#elif defined(CODESET)
6872 {
6873 char *codeset = nl_langinfo(CODESET);
Mark Dickinsonda2706b2008-12-11 18:03:03 +00006874 if (codeset != NULL && codeset[0] != 0)
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006875 return PyUnicode_FromString(codeset);
6876 }
6877#endif
6878 Py_INCREF(Py_None);
6879 return Py_None;
6880}
6881
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006882#ifdef __VMS
6883/* Use openssl random routine */
6884#include <openssl/rand.h>
6885PyDoc_STRVAR(vms_urandom__doc__,
6886"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006887Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006888
6889static PyObject*
6890vms_urandom(PyObject *self, PyObject *args)
6891{
6892 int howMany;
6893 PyObject* result;
6894
6895 /* Read arguments */
6896 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6897 return NULL;
6898 if (howMany < 0)
6899 return PyErr_Format(PyExc_ValueError,
6900 "negative argument not allowed");
6901
6902 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006903 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006904 if (result != NULL) {
6905 /* Get random data */
6906 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006907 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006908 howMany) < 0) {
6909 Py_DECREF(result);
6910 return PyErr_Format(PyExc_ValueError,
6911 "RAND_pseudo_bytes");
6912 }
6913 }
6914 return result;
6915}
6916#endif
6917
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006918static PyMethodDef posix_methods[] = {
6919 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6920#ifdef HAVE_TTYNAME
6921 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6922#endif
6923 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006924#ifdef HAVE_CHFLAGS
6925 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6926#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006927 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00006928#ifdef HAVE_FCHMOD
6929 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
6930#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006931#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006932 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006933#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00006934#ifdef HAVE_LCHMOD
6935 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
6936#endif /* HAVE_LCHMOD */
6937#ifdef HAVE_FCHOWN
6938 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
6939#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006940#ifdef HAVE_LCHFLAGS
6941 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6942#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006943#ifdef HAVE_LCHOWN
6944 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6945#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006946#ifdef HAVE_CHROOT
6947 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6948#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006949#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006950 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006951#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006952#ifdef HAVE_GETCWD
Guido van Rossumf0af3e32008-10-02 18:55:37 +00006953 {"getcwd", (PyCFunction)posix_getcwd_unicode,
6954 METH_NOARGS, posix_getcwd__doc__},
6955 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
6956 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006957#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006958#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006959 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006960#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006961 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6962 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6963 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006964#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006965 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006966#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006967#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006968 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006969#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006970 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6971 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6972 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006973 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006974#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006975 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006976#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006977#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006978 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006979#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006980 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006981#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006982 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006983#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006984 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6985 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6986 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006987#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006988 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006989#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006990 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006991#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006992 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6993 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006994#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006995#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006996 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6997 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006998#if defined(PYOS_OS2)
6999 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7000 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7001#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007002#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007003#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007004 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007005#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007006#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007007 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007008#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007009#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007010 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007011#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007012#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007013 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007014#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007015#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007016 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007017#endif /* HAVE_GETEGID */
7018#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007019 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007020#endif /* HAVE_GETEUID */
7021#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007022 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007023#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007024#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007025 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007026#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007027 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007028#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007029 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007030#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007031#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007032 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007033#endif /* HAVE_GETPPID */
7034#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007035 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007036#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007037#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007038 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007039#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007040#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007041 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007042#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007043#ifdef HAVE_KILLPG
7044 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7045#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007046#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007047 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007048#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007049#ifdef MS_WINDOWS
7050 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7051#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007052#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007053 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007054#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007055#ifdef HAVE_SETEUID
7056 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7057#endif /* HAVE_SETEUID */
7058#ifdef HAVE_SETEGID
7059 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7060#endif /* HAVE_SETEGID */
7061#ifdef HAVE_SETREUID
7062 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7063#endif /* HAVE_SETREUID */
7064#ifdef HAVE_SETREGID
7065 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7066#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007067#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007068 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007069#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007070#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00007071 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007072#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007073#ifdef HAVE_GETPGID
7074 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7075#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007076#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007077 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007078#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007079#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007080 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007081#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007082#ifdef HAVE_WAIT3
7083 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7084#endif /* HAVE_WAIT3 */
7085#ifdef HAVE_WAIT4
7086 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7087#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007088#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007089 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007090#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007091#ifdef HAVE_GETSID
7092 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7093#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007094#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007095 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007096#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007097#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007098 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007099#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007100#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007101 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007102#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007103#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007104 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007105#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007106 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7107 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00007108 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007109 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007110 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7111 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7112 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7113 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7114 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7115 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007116 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007117#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007118 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007119#endif
7120#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007121 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007122#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007123#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007124 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7125#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007126#ifdef HAVE_DEVICE_MACROS
7127 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7128 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7129 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7130#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007131#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007132 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007133#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007134#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007135 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007136#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007137#ifdef HAVE_UNSETENV
7138 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7139#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007140 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007141#ifdef HAVE_FCHDIR
7142 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7143#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007144#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007145 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007146#endif
7147#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007148 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007149#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007150#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007151#ifdef WCOREDUMP
7152 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7153#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007154#ifdef WIFCONTINUED
7155 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7156#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007157#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007158 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007159#endif /* WIFSTOPPED */
7160#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007161 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007162#endif /* WIFSIGNALED */
7163#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007164 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007165#endif /* WIFEXITED */
7166#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007167 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007168#endif /* WEXITSTATUS */
7169#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007170 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007171#endif /* WTERMSIG */
7172#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007173 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007174#endif /* WSTOPSIG */
7175#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007176#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007177 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007178#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007179#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007180 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007181#endif
Fred Drakec9680921999-12-13 16:37:25 +00007182#ifdef HAVE_CONFSTR
7183 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7184#endif
7185#ifdef HAVE_SYSCONF
7186 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7187#endif
7188#ifdef HAVE_FPATHCONF
7189 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7190#endif
7191#ifdef HAVE_PATHCONF
7192 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7193#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007194 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007195#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007196 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7197#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007198#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007199 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007200#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007201 #ifdef MS_WINDOWS
7202 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7203 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007204 #ifdef __VMS
7205 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7206 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007207 {NULL, NULL} /* Sentinel */
7208};
7209
7210
Barry Warsaw4a342091996-12-19 23:50:02 +00007211static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007212ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007213{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007214 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007215}
7216
Guido van Rossumd48f2521997-12-05 22:19:34 +00007217#if defined(PYOS_OS2)
7218/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007219static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007220{
7221 APIRET rc;
7222 ULONG values[QSV_MAX+1];
7223 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007224 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007225
7226 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007227 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007228 Py_END_ALLOW_THREADS
7229
7230 if (rc != NO_ERROR) {
7231 os2_error(rc);
7232 return -1;
7233 }
7234
Fred Drake4d1e64b2002-04-15 19:40:07 +00007235 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7236 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7237 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7238 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7239 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7240 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7241 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007242
7243 switch (values[QSV_VERSION_MINOR]) {
7244 case 0: ver = "2.00"; break;
7245 case 10: ver = "2.10"; break;
7246 case 11: ver = "2.11"; break;
7247 case 30: ver = "3.00"; break;
7248 case 40: ver = "4.00"; break;
7249 case 50: ver = "5.00"; break;
7250 default:
Tim Peters885d4572001-11-28 20:27:42 +00007251 PyOS_snprintf(tmp, sizeof(tmp),
7252 "%d-%d", values[QSV_VERSION_MAJOR],
7253 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007254 ver = &tmp[0];
7255 }
7256
7257 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007258 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007259 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007260
7261 /* Add Indicator of Which Drive was Used to Boot the System */
7262 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7263 tmp[1] = ':';
7264 tmp[2] = '\0';
7265
Fred Drake4d1e64b2002-04-15 19:40:07 +00007266 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007267}
7268#endif
7269
Barry Warsaw4a342091996-12-19 23:50:02 +00007270static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007271all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007272{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007273#ifdef F_OK
7274 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007275#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007276#ifdef R_OK
7277 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007278#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007279#ifdef W_OK
7280 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007281#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007282#ifdef X_OK
7283 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007284#endif
Fred Drakec9680921999-12-13 16:37:25 +00007285#ifdef NGROUPS_MAX
7286 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7287#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007288#ifdef TMP_MAX
7289 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7290#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007291#ifdef WCONTINUED
7292 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7293#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007294#ifdef WNOHANG
7295 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007296#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007297#ifdef WUNTRACED
7298 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7299#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007300#ifdef O_RDONLY
7301 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7302#endif
7303#ifdef O_WRONLY
7304 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7305#endif
7306#ifdef O_RDWR
7307 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7308#endif
7309#ifdef O_NDELAY
7310 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7311#endif
7312#ifdef O_NONBLOCK
7313 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7314#endif
7315#ifdef O_APPEND
7316 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7317#endif
7318#ifdef O_DSYNC
7319 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7320#endif
7321#ifdef O_RSYNC
7322 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7323#endif
7324#ifdef O_SYNC
7325 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7326#endif
7327#ifdef O_NOCTTY
7328 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7329#endif
7330#ifdef O_CREAT
7331 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7332#endif
7333#ifdef O_EXCL
7334 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7335#endif
7336#ifdef O_TRUNC
7337 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7338#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007339#ifdef O_BINARY
7340 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7341#endif
7342#ifdef O_TEXT
7343 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7344#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007345#ifdef O_LARGEFILE
7346 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7347#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007348#ifdef O_SHLOCK
7349 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7350#endif
7351#ifdef O_EXLOCK
7352 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7353#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007354
Tim Peters5aa91602002-01-30 05:46:57 +00007355/* MS Windows */
7356#ifdef O_NOINHERIT
7357 /* Don't inherit in child processes. */
7358 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7359#endif
7360#ifdef _O_SHORT_LIVED
7361 /* Optimize for short life (keep in memory). */
7362 /* MS forgot to define this one with a non-underscore form too. */
7363 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7364#endif
7365#ifdef O_TEMPORARY
7366 /* Automatically delete when last handle is closed. */
7367 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7368#endif
7369#ifdef O_RANDOM
7370 /* Optimize for random access. */
7371 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7372#endif
7373#ifdef O_SEQUENTIAL
7374 /* Optimize for sequential access. */
7375 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7376#endif
7377
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007378/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007379#ifdef O_ASYNC
7380 /* Send a SIGIO signal whenever input or output
7381 becomes available on file descriptor */
7382 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7383#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007384#ifdef O_DIRECT
7385 /* Direct disk access. */
7386 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7387#endif
7388#ifdef O_DIRECTORY
7389 /* Must be a directory. */
7390 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7391#endif
7392#ifdef O_NOFOLLOW
7393 /* Do not follow links. */
7394 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7395#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007396#ifdef O_NOATIME
7397 /* Do not update the access time. */
7398 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7399#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007400
Barry Warsaw5676bd12003-01-07 20:57:09 +00007401 /* These come from sysexits.h */
7402#ifdef EX_OK
7403 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007404#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007405#ifdef EX_USAGE
7406 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007407#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007408#ifdef EX_DATAERR
7409 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007410#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007411#ifdef EX_NOINPUT
7412 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007413#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007414#ifdef EX_NOUSER
7415 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007416#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007417#ifdef EX_NOHOST
7418 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007419#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007420#ifdef EX_UNAVAILABLE
7421 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007422#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007423#ifdef EX_SOFTWARE
7424 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007425#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007426#ifdef EX_OSERR
7427 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007428#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007429#ifdef EX_OSFILE
7430 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007431#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007432#ifdef EX_CANTCREAT
7433 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007434#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007435#ifdef EX_IOERR
7436 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007437#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007438#ifdef EX_TEMPFAIL
7439 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007440#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007441#ifdef EX_PROTOCOL
7442 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007443#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007444#ifdef EX_NOPERM
7445 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007446#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007447#ifdef EX_CONFIG
7448 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007449#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007450#ifdef EX_NOTFOUND
7451 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007452#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007453
Guido van Rossum246bc171999-02-01 23:54:31 +00007454#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007455#if defined(PYOS_OS2) && defined(PYCC_GCC)
7456 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7457 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7458 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7459 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7460 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7461 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7462 if (ins(d, "P_PM", (long)P_PM)) return -1;
7463 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7464 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7465 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7466 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7467 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7468 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7469 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7470 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7471 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7472 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7473 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7474 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7475 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7476#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007477 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7478 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7479 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7480 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7481 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007482#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007483#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007484
Guido van Rossumd48f2521997-12-05 22:19:34 +00007485#if defined(PYOS_OS2)
7486 if (insertvalues(d)) return -1;
7487#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007488 return 0;
7489}
7490
7491
Tim Peters5aa91602002-01-30 05:46:57 +00007492#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007493#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007494#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007495
7496#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007497#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007498#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007499
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007500#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007501#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007502#define MODNAME "posix"
7503#endif
7504
Martin v. Löwis1a214512008-06-11 05:26:20 +00007505static struct PyModuleDef posixmodule = {
7506 PyModuleDef_HEAD_INIT,
7507 MODNAME,
7508 posix__doc__,
7509 -1,
7510 posix_methods,
7511 NULL,
7512 NULL,
7513 NULL,
7514 NULL
7515};
7516
7517
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007518PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007519INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007520{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007521 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007522
Martin v. Löwis1a214512008-06-11 05:26:20 +00007523 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007524 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007525 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007526
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007527 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007528 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007529 Py_XINCREF(v);
7530 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007531 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007532 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007533
Fred Drake4d1e64b2002-04-15 19:40:07 +00007534 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007535 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007536
Fred Drake4d1e64b2002-04-15 19:40:07 +00007537 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007538 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007539
Fred Drake4d1e64b2002-04-15 19:40:07 +00007540 Py_INCREF(PyExc_OSError);
7541 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007542
Guido van Rossumb3d39562000-01-31 18:41:26 +00007543#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007544 if (posix_putenv_garbage == NULL)
7545 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007546#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007547
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007548 if (!initialized) {
7549 stat_result_desc.name = MODNAME ".stat_result";
7550 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7551 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7552 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7553 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7554 structseq_new = StatResultType.tp_new;
7555 StatResultType.tp_new = statresult_new;
7556
7557 statvfs_result_desc.name = MODNAME ".statvfs_result";
7558 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007559#ifdef NEED_TICKS_PER_SECOND
7560# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7561 ticks_per_second = sysconf(_SC_CLK_TCK);
7562# elif defined(HZ)
7563 ticks_per_second = HZ;
7564# else
7565 ticks_per_second = 60; /* magic fallback value; may be bogus */
7566# endif
7567#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007568 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007569 Py_INCREF((PyObject*) &StatResultType);
7570 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007571 Py_INCREF((PyObject*) &StatVFSResultType);
7572 PyModule_AddObject(m, "statvfs_result",
7573 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007574 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007575
7576#ifdef __APPLE__
7577 /*
7578 * Step 2 of weak-linking support on Mac OS X.
7579 *
7580 * The code below removes functions that are not available on the
7581 * currently active platform.
7582 *
7583 * This block allow one to use a python binary that was build on
7584 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7585 * OSX 10.4.
7586 */
7587#ifdef HAVE_FSTATVFS
7588 if (fstatvfs == NULL) {
7589 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007590 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007591 }
7592 }
7593#endif /* HAVE_FSTATVFS */
7594
7595#ifdef HAVE_STATVFS
7596 if (statvfs == NULL) {
7597 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007598 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007599 }
7600 }
7601#endif /* HAVE_STATVFS */
7602
7603# ifdef HAVE_LCHOWN
7604 if (lchown == NULL) {
7605 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007606 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007607 }
7608 }
7609#endif /* HAVE_LCHOWN */
7610
7611
7612#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007613 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007614
Guido van Rossumb6775db1994-08-01 11:34:53 +00007615}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007616
7617#ifdef __cplusplus
7618}
7619#endif