blob: c0280de81e7f647192ec17d0a948d3c4f4dae769 [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#ifndef Py_USING_UNICODE
48/* This is used in signatures of functions. */
49#define Py_UNICODE void
50#endif
51
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000052#if defined(PYOS_OS2)
53#define INCL_DOS
54#define INCL_DOSERRORS
55#define INCL_DOSPROCESS
56#define INCL_NOPMAPI
57#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#if defined(PYCC_GCC)
59#include <ctype.h>
60#include <io.h>
61#include <stdio.h>
62#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000063#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000064#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000065#endif
66
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/types.h>
68#include <sys/stat.h>
Guido van Rossuma6535fd2001-10-18 19:44:10 +000069
Guido van Rossum36bc6801995-06-14 22:54:23 +000070#ifdef HAVE_SYS_WAIT_H
71#include <sys/wait.h> /* For WNOHANG */
72#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000073
Guido van Rossuma376cc51996-12-05 23:43:35 +000074#include <signal.h>
Guido van Rossuma376cc51996-12-05 23:43:35 +000075
Guido van Rossumb6775db1994-08-01 11:34:53 +000076#ifdef HAVE_FCNTL_H
77#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000078#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000079
Guido van Rossuma6535fd2001-10-18 19:44:10 +000080#ifdef HAVE_GRP_H
81#include <grp.h>
82#endif
83
Barry Warsaw5676bd12003-01-07 20:57:09 +000084#ifdef HAVE_SYSEXITS_H
85#include <sysexits.h>
86#endif /* HAVE_SYSEXITS_H */
87
Anthony Baxter8a560de2004-10-13 15:30:56 +000088#ifdef HAVE_SYS_LOADAVG_H
89#include <sys/loadavg.h>
90#endif
91
Guido van Rossuma4916fa1996-05-23 22:58:55 +000092/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000093/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000094#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000095#include <process.h>
96#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000097#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000098#define HAVE_GETCWD 1
99#define HAVE_OPENDIR 1
100#define HAVE_SYSTEM 1
101#if defined(__OS2__)
102#define HAVE_EXECV 1
103#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000104#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#include <process.h>
106#else
107#ifdef __BORLANDC__ /* Borland compiler */
108#define HAVE_EXECV 1
109#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000110#define HAVE_OPENDIR 1
111#define HAVE_PIPE 1
112#define HAVE_POPEN 1
113#define HAVE_SYSTEM 1
114#define HAVE_WAIT 1
115#else
116#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000117#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000118#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_EXECV 1
120#define HAVE_PIPE 1
121#define HAVE_POPEN 1
122#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000123#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000124#define HAVE_FSYNC 1
125#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000126#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000127#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
128/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000129#else /* all other compilers */
130/* Unix functions that the configure script doesn't check for */
131#define HAVE_EXECV 1
132#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000133#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
134#define HAVE_FORK1 1
135#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136#define HAVE_GETCWD 1
137#define HAVE_GETEGID 1
138#define HAVE_GETEUID 1
139#define HAVE_GETGID 1
140#define HAVE_GETPPID 1
141#define HAVE_GETUID 1
142#define HAVE_KILL 1
143#define HAVE_OPENDIR 1
144#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000145#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000146#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000147#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000148#define HAVE_SYSTEM 1
149#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000150#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000151#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000152#endif /* _MSC_VER */
153#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000154#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000155#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000156
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000157#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000158
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000159#if defined(__sgi)&&_COMPILER_VERSION>=700
160/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
161 (default) */
162extern char *ctermid_r(char *);
163#endif
164
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000165#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000166#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000167extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000169#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000170extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000171#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000172extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000173#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000174#endif
175#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000176extern int chdir(char *);
177extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000178#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int chdir(const char *);
180extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000182#ifdef __BORLANDC__
183extern int chmod(const char *, int);
184#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000185extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000186#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int chown(const char *, uid_t, gid_t);
188extern char *getcwd(char *, int);
189extern char *strerror(int);
190extern int link(const char *, const char *);
191extern int rename(const char *, const char *);
192extern int stat(const char *, struct stat *);
193extern int unlink(const char *);
194extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000196extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000197#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000199extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000200#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000201#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000202
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000203#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#ifdef HAVE_UTIME_H
206#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000207#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000209#ifdef HAVE_SYS_UTIME_H
210#include <sys/utime.h>
211#define HAVE_UTIME_H /* pretend we do for the rest of this file */
212#endif /* HAVE_SYS_UTIME_H */
213
Guido van Rossumb6775db1994-08-01 11:34:53 +0000214#ifdef HAVE_SYS_TIMES_H
215#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000216#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217
218#ifdef HAVE_SYS_PARAM_H
219#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000220#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221
222#ifdef HAVE_SYS_UTSNAME_H
223#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000224#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000226#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000228#define NAMLEN(dirent) strlen((dirent)->d_name)
229#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000230#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000231#include <direct.h>
232#define NAMLEN(dirent) strlen((dirent)->d_name)
233#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000236#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000237#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000239#endif
240#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#endif
243#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000244#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000245#endif
246#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000248#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249#include <direct.h>
250#include <io.h>
251#include <process.h>
Tim Petersbc2e10e2002-03-03 23:17:02 +0000252#include "osdefs.h"
Martin v. Löwisdc3883f2004-08-29 15:46:35 +0000253#define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000255#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000256#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000257#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000258#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259
Guido van Rossumd48f2521997-12-05 22:19:34 +0000260#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000262#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263
Tim Petersbc2e10e2002-03-03 23:17:02 +0000264#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000265#if defined(PATH_MAX) && PATH_MAX > 1024
266#define MAXPATHLEN PATH_MAX
267#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000268#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000269#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000270#endif /* MAXPATHLEN */
271
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000272#ifdef UNION_WAIT
273/* Emulate some macros on systems that have a union instead of macros */
274
275#ifndef WIFEXITED
276#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
277#endif
278
279#ifndef WEXITSTATUS
280#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
281#endif
282
283#ifndef WTERMSIG
284#define WTERMSIG(u_wait) ((u_wait).w_termsig)
285#endif
286
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000287#define WAIT_TYPE union wait
288#define WAIT_STATUS_INT(s) (s.w_status)
289
290#else /* !UNION_WAIT */
291#define WAIT_TYPE int
292#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000293#endif /* UNION_WAIT */
294
Greg Wardb48bc172000-03-01 21:51:56 +0000295/* Don't use the "_r" form if we don't need it (also, won't have a
296 prototype for it, at least on Solaris -- maybe others as well?). */
297#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
298#define USE_CTERMID_R
299#endif
300
301#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
302#define USE_TMPNAM_R
303#endif
304
Fred Drake699f3522000-06-29 21:12:41 +0000305/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000306#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000307#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000308# define STAT win32_stat
309# define FSTAT win32_fstat
310# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000311#else
312# define STAT stat
313# define FSTAT fstat
314# define STRUCT_STAT struct stat
315#endif
316
Tim Peters11b23062003-04-23 02:39:17 +0000317#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000318#include <sys/mkdev.h>
319#else
320#if defined(MAJOR_IN_SYSMACROS)
321#include <sys/sysmacros.h>
322#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000323#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
324#include <sys/mkdev.h>
325#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000326#endif
Fred Drake699f3522000-06-29 21:12:41 +0000327
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000329#ifdef WITH_NEXT_FRAMEWORK
330/* On Darwin/MacOSX a shared library or framework has no access to
331** environ directly, we must obtain it with _NSGetEnviron().
332*/
333#include <crt_externs.h>
334static char **environ;
335#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000337#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338
Barry Warsaw53699e91996-12-10 23:23:01 +0000339static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000340convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341{
Barry Warsaw53699e91996-12-10 23:23:01 +0000342 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000344 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345 if (d == NULL)
346 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000347#ifdef WITH_NEXT_FRAMEWORK
348 if (environ == NULL)
349 environ = *_NSGetEnviron();
350#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351 if (environ == NULL)
352 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000353 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000355 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000356 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357 char *p = strchr(*e, '=');
358 if (p == NULL)
359 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000360 k = PyString_FromStringAndSize(*e, (int)(p-*e));
361 if (k == NULL) {
362 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000364 }
365 v = PyString_FromString(p+1);
366 if (v == NULL) {
367 PyErr_Clear();
368 Py_DECREF(k);
369 continue;
370 }
371 if (PyDict_GetItem(d, k) == NULL) {
372 if (PyDict_SetItem(d, k, v) != 0)
373 PyErr_Clear();
374 }
375 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000376 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000378#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000379 {
380 APIRET rc;
381 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
382
383 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000384 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000385 PyObject *v = PyString_FromString(buffer);
386 PyDict_SetItemString(d, "BEGINLIBPATH", v);
387 Py_DECREF(v);
388 }
389 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
390 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
391 PyObject *v = PyString_FromString(buffer);
392 PyDict_SetItemString(d, "ENDLIBPATH", v);
393 Py_DECREF(v);
394 }
395 }
396#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397 return d;
398}
399
400
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401/* Set a POSIX-specific error from errno, and return NULL */
402
Barry Warsawd58d7641998-07-23 16:14:40 +0000403static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000404posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000405{
Barry Warsawca74da41999-02-09 19:31:45 +0000406 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407}
Barry Warsawd58d7641998-07-23 16:14:40 +0000408static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000409posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000410{
Barry Warsawca74da41999-02-09 19:31:45 +0000411 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000412}
413
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000414#ifdef Py_WIN_WIDE_FILENAMES
415static PyObject *
416posix_error_with_unicode_filename(Py_UNICODE* name)
417{
418 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
419}
420#endif /* Py_WIN_WIDE_FILENAMES */
421
422
Mark Hammondef8b6542001-05-13 08:04:26 +0000423static PyObject *
424posix_error_with_allocated_filename(char* name)
425{
426 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
427 PyMem_Free(name);
428 return rc;
429}
430
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000431#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000432static PyObject *
433win32_error(char* function, char* filename)
434{
Mark Hammond33a6da92000-08-15 00:46:38 +0000435 /* XXX We should pass the function name along in the future.
436 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000437 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000438 Windows error object, which is non-trivial.
439 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000440 errno = GetLastError();
441 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000442 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000443 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000444 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000445}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000446
447#ifdef Py_WIN_WIDE_FILENAMES
448static PyObject *
449win32_error_unicode(char* function, Py_UNICODE* filename)
450{
451 /* XXX - see win32_error for comments on 'function' */
452 errno = GetLastError();
453 if (filename)
454 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
455 else
456 return PyErr_SetFromWindowsErr(errno);
457}
458
459static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
460{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000461}
462
463/* Function suitable for O& conversion */
464static int
465convert_to_unicode(PyObject *arg, void* _param)
466{
467 PyObject **param = (PyObject**)_param;
468 if (PyUnicode_CheckExact(arg)) {
469 Py_INCREF(arg);
470 *param = arg;
471 }
472 else if (PyUnicode_Check(arg)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000473 /* For a Unicode subtype that's not a Unicode object,
474 return a true Unicode object with the same data. */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000475 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
476 PyUnicode_GET_SIZE(arg));
477 return *param != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000478 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000479 else
480 *param = PyUnicode_FromEncodedObject(arg,
481 Py_FileSystemDefaultEncoding,
482 "strict");
483 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000484}
485
486#endif /* Py_WIN_WIDE_FILENAMES */
487
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000488#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489
Guido van Rossumd48f2521997-12-05 22:19:34 +0000490#if defined(PYOS_OS2)
491/**********************************************************************
492 * Helper Function to Trim and Format OS/2 Messages
493 **********************************************************************/
494 static void
495os2_formatmsg(char *msgbuf, int msglen, char *reason)
496{
497 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
498
499 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
500 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
501
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000502 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000503 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
504 }
505
506 /* Add Optional Reason Text */
507 if (reason) {
508 strcat(msgbuf, " : ");
509 strcat(msgbuf, reason);
510 }
511}
512
513/**********************************************************************
514 * Decode an OS/2 Operating System Error Code
515 *
516 * A convenience function to lookup an OS/2 error code and return a
517 * text message we can use to raise a Python exception.
518 *
519 * Notes:
520 * The messages for errors returned from the OS/2 kernel reside in
521 * the file OSO001.MSG in the \OS2 directory hierarchy.
522 *
523 **********************************************************************/
524 static char *
525os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
526{
527 APIRET rc;
528 ULONG msglen;
529
530 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
531 Py_BEGIN_ALLOW_THREADS
532 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
533 errorcode, "oso001.msg", &msglen);
534 Py_END_ALLOW_THREADS
535
536 if (rc == NO_ERROR)
537 os2_formatmsg(msgbuf, msglen, reason);
538 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000539 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000540 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000541
542 return msgbuf;
543}
544
545/* Set an OS/2-specific error and return NULL. OS/2 kernel
546 errors are not in a global variable e.g. 'errno' nor are
547 they congruent with posix error numbers. */
548
549static PyObject * os2_error(int code)
550{
551 char text[1024];
552 PyObject *v;
553
554 os2_strerror(text, sizeof(text), code, "");
555
556 v = Py_BuildValue("(is)", code, text);
557 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000558 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000559 Py_DECREF(v);
560 }
561 return NULL; /* Signal to Python that an Exception is Pending */
562}
563
564#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565
566/* POSIX generic methods */
567
Barry Warsaw53699e91996-12-10 23:23:01 +0000568static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000569posix_fildes(PyObject *fdobj, int (*func)(int))
570{
571 int fd;
572 int res;
573 fd = PyObject_AsFileDescriptor(fdobj);
574 if (fd < 0)
575 return NULL;
576 Py_BEGIN_ALLOW_THREADS
577 res = (*func)(fd);
578 Py_END_ALLOW_THREADS
579 if (res < 0)
580 return posix_error();
581 Py_INCREF(Py_None);
582 return Py_None;
583}
Guido van Rossum21142a01999-01-08 21:05:37 +0000584
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000585#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000586static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000587unicode_file_names(void)
588{
589 static int canusewide = -1;
590 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000591 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000592 the Windows NT family. */
593 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
594 }
595 return canusewide;
596}
597#endif
Tim Peters11b23062003-04-23 02:39:17 +0000598
Guido van Rossum21142a01999-01-08 21:05:37 +0000599static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000600posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000601{
Mark Hammondef8b6542001-05-13 08:04:26 +0000602 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000603 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000604 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000605 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000606 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000607 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000608 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000609 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000610 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000611 return posix_error_with_allocated_filename(path1);
612 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000613 Py_INCREF(Py_None);
614 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000615}
616
Barry Warsaw53699e91996-12-10 23:23:01 +0000617static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000618posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000619 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000620 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000621{
Mark Hammondef8b6542001-05-13 08:04:26 +0000622 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000623 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000624 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000625 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000626 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000627 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000628 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000629 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000630 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000631 PyMem_Free(path1);
632 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000633 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000634 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000635 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000636 Py_INCREF(Py_None);
637 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000638}
639
Thomas Wouters477c8d52006-05-27 19:21:47 +0000640#ifdef Py_WIN_WIDE_FILENAMES
641static PyObject*
642win32_1str(PyObject* args, char* func,
643 char* format, BOOL (__stdcall *funcA)(LPCSTR),
644 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
645{
646 PyObject *uni;
647 char *ansi;
648 BOOL result;
649 if (unicode_file_names()) {
650 if (!PyArg_ParseTuple(args, wformat, &uni))
651 PyErr_Clear();
652 else {
653 Py_BEGIN_ALLOW_THREADS
654 result = funcW(PyUnicode_AsUnicode(uni));
655 Py_END_ALLOW_THREADS
656 if (!result)
657 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
658 Py_INCREF(Py_None);
659 return Py_None;
660 }
661 }
662 if (!PyArg_ParseTuple(args, format, &ansi))
663 return NULL;
664 Py_BEGIN_ALLOW_THREADS
665 result = funcA(ansi);
666 Py_END_ALLOW_THREADS
667 if (!result)
668 return win32_error(func, ansi);
669 Py_INCREF(Py_None);
670 return Py_None;
671
672}
673
674/* This is a reimplementation of the C library's chdir function,
675 but one that produces Win32 errors instead of DOS error codes.
676 chdir is essentially a wrapper around SetCurrentDirectory; however,
677 it also needs to set "magic" environment variables indicating
678 the per-drive current directory, which are of the form =<drive>: */
679BOOL __stdcall
680win32_chdir(LPCSTR path)
681{
682 char new_path[MAX_PATH+1];
683 int result;
684 char env[4] = "=x:";
685
686 if(!SetCurrentDirectoryA(path))
687 return FALSE;
688 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
689 if (!result)
690 return FALSE;
691 /* In the ANSI API, there should not be any paths longer
692 than MAX_PATH. */
693 assert(result <= MAX_PATH+1);
694 if (strncmp(new_path, "\\\\", 2) == 0 ||
695 strncmp(new_path, "//", 2) == 0)
696 /* UNC path, nothing to do. */
697 return TRUE;
698 env[1] = new_path[0];
699 return SetEnvironmentVariableA(env, new_path);
700}
701
702/* The Unicode version differs from the ANSI version
703 since the current directory might exceed MAX_PATH characters */
704BOOL __stdcall
705win32_wchdir(LPCWSTR path)
706{
707 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
708 int result;
709 wchar_t env[4] = L"=x:";
710
711 if(!SetCurrentDirectoryW(path))
712 return FALSE;
713 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
714 if (!result)
715 return FALSE;
716 if (result > MAX_PATH+1) {
717 new_path = malloc(result);
718 if (!new_path) {
719 SetLastError(ERROR_OUTOFMEMORY);
720 return FALSE;
721 }
722 }
723 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
724 wcsncmp(new_path, L"//", 2) == 0)
725 /* UNC path, nothing to do. */
726 return TRUE;
727 env[1] = new_path[0];
728 result = SetEnvironmentVariableW(env, new_path);
729 if (new_path != _new_path)
730 free(new_path);
731 return result;
732}
733#endif
734
Martin v. Löwis14694662006-02-03 12:54:16 +0000735#ifdef MS_WINDOWS
736/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
737 - time stamps are restricted to second resolution
738 - file modification times suffer from forth-and-back conversions between
739 UTC and local time
740 Therefore, we implement our own stat, based on the Win32 API directly.
741*/
742#define HAVE_STAT_NSEC 1
743
744struct win32_stat{
745 int st_dev;
746 __int64 st_ino;
747 unsigned short st_mode;
748 int st_nlink;
749 int st_uid;
750 int st_gid;
751 int st_rdev;
752 __int64 st_size;
753 int st_atime;
754 int st_atime_nsec;
755 int st_mtime;
756 int st_mtime_nsec;
757 int st_ctime;
758 int st_ctime_nsec;
759};
760
761static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
762
763static void
764FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
765{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000766 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
767 /* Cannot simply cast and dereference in_ptr,
768 since it might not be aligned properly */
769 __int64 in;
770 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000771 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
772 /* XXX Win32 supports time stamps past 2038; we currently don't */
773 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
774}
775
Thomas Wouters477c8d52006-05-27 19:21:47 +0000776static void
777time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
778{
779 /* XXX endianness */
780 __int64 out;
781 out = time_in + secs_between_epochs;
782 out = out * 10000000 + nsec_in;
783 memcpy(out_ptr, &out, sizeof(out));
784}
785
Martin v. Löwis14694662006-02-03 12:54:16 +0000786/* Below, we *know* that ugo+r is 0444 */
787#if _S_IREAD != 0400
788#error Unsupported C library
789#endif
790static int
791attributes_to_mode(DWORD attr)
792{
793 int m = 0;
794 if (attr & FILE_ATTRIBUTE_DIRECTORY)
795 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
796 else
797 m |= _S_IFREG;
798 if (attr & FILE_ATTRIBUTE_READONLY)
799 m |= 0444;
800 else
801 m |= 0666;
802 return m;
803}
804
805static int
806attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
807{
808 memset(result, 0, sizeof(*result));
809 result->st_mode = attributes_to_mode(info->dwFileAttributes);
810 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
811 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
812 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
813 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
814
815 return 0;
816}
817
818static int
819win32_stat(const char* path, struct win32_stat *result)
820{
821 WIN32_FILE_ATTRIBUTE_DATA info;
822 int code;
823 char *dot;
824 /* XXX not supported on Win95 and NT 3.x */
825 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
826 /* Protocol violation: we explicitly clear errno, instead of
827 setting it to a POSIX error. Callers should use GetLastError. */
828 errno = 0;
829 return -1;
830 }
831 code = attribute_data_to_stat(&info, result);
832 if (code != 0)
833 return code;
834 /* Set S_IFEXEC if it is an .exe, .bat, ... */
835 dot = strrchr(path, '.');
836 if (dot) {
837 if (stricmp(dot, ".bat") == 0 ||
838 stricmp(dot, ".cmd") == 0 ||
839 stricmp(dot, ".exe") == 0 ||
840 stricmp(dot, ".com") == 0)
841 result->st_mode |= 0111;
842 }
843 return code;
844}
845
846static int
847win32_wstat(const wchar_t* path, struct win32_stat *result)
848{
849 int code;
850 const wchar_t *dot;
851 WIN32_FILE_ATTRIBUTE_DATA info;
852 /* XXX not supported on Win95 and NT 3.x */
853 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
854 /* Protocol violation: we explicitly clear errno, instead of
855 setting it to a POSIX error. Callers should use GetLastError. */
856 errno = 0;
857 return -1;
858 }
859 code = attribute_data_to_stat(&info, result);
860 if (code < 0)
861 return code;
862 /* Set IFEXEC if it is an .exe, .bat, ... */
863 dot = wcsrchr(path, '.');
864 if (dot) {
865 if (_wcsicmp(dot, L".bat") == 0 ||
866 _wcsicmp(dot, L".cmd") == 0 ||
867 _wcsicmp(dot, L".exe") == 0 ||
868 _wcsicmp(dot, L".com") == 0)
869 result->st_mode |= 0111;
870 }
871 return code;
872}
873
874static int
875win32_fstat(int file_number, struct win32_stat *result)
876{
877 BY_HANDLE_FILE_INFORMATION info;
878 HANDLE h;
879 int type;
880
881 h = (HANDLE)_get_osfhandle(file_number);
882
883 /* Protocol violation: we explicitly clear errno, instead of
884 setting it to a POSIX error. Callers should use GetLastError. */
885 errno = 0;
886
887 if (h == INVALID_HANDLE_VALUE) {
888 /* This is really a C library error (invalid file handle).
889 We set the Win32 error to the closes one matching. */
890 SetLastError(ERROR_INVALID_HANDLE);
891 return -1;
892 }
893 memset(result, 0, sizeof(*result));
894
895 type = GetFileType(h);
896 if (type == FILE_TYPE_UNKNOWN) {
897 DWORD error = GetLastError();
898 if (error != 0) {
899 return -1;
900 }
901 /* else: valid but unknown file */
902 }
903
904 if (type != FILE_TYPE_DISK) {
905 if (type == FILE_TYPE_CHAR)
906 result->st_mode = _S_IFCHR;
907 else if (type == FILE_TYPE_PIPE)
908 result->st_mode = _S_IFIFO;
909 return 0;
910 }
911
912 if (!GetFileInformationByHandle(h, &info)) {
913 return -1;
914 }
915
916 /* similar to stat() */
917 result->st_mode = attributes_to_mode(info.dwFileAttributes);
918 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
919 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
920 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
921 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
922 /* specific to fstat() */
923 result->st_nlink = info.nNumberOfLinks;
924 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
925 return 0;
926}
927
928#endif /* MS_WINDOWS */
929
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000930PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000931"stat_result: Result from stat or lstat.\n\n\
932This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000933 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000934or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
935\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +0000936Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
937or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000938\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000939See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000940
941static PyStructSequence_Field stat_result_fields[] = {
942 {"st_mode", "protection bits"},
943 {"st_ino", "inode"},
944 {"st_dev", "device"},
945 {"st_nlink", "number of hard links"},
946 {"st_uid", "user ID of owner"},
947 {"st_gid", "group ID of owner"},
948 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000949 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
950 {NULL, "integer time of last access"},
951 {NULL, "integer time of last modification"},
952 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000953 {"st_atime", "time of last access"},
954 {"st_mtime", "time of last modification"},
955 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000956#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000957 {"st_blksize", "blocksize for filesystem I/O"},
958#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000959#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000960 {"st_blocks", "number of blocks allocated"},
961#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000962#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000963 {"st_rdev", "device type (if inode device)"},
964#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +0000965#ifdef HAVE_STRUCT_STAT_ST_FLAGS
966 {"st_flags", "user defined flags for file"},
967#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +0000968#ifdef HAVE_STRUCT_STAT_ST_GEN
969 {"st_gen", "generation number"},
970#endif
971#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
972 {"st_birthtime", "time of creation"},
973#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000974 {0}
975};
976
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000977#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000978#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000979#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000980#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000981#endif
982
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000983#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000984#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
985#else
986#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
987#endif
988
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000989#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000990#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
991#else
992#define ST_RDEV_IDX ST_BLOCKS_IDX
993#endif
994
Hye-Shik Chang5f937a72005-06-02 13:09:30 +0000995#ifdef HAVE_STRUCT_STAT_ST_FLAGS
996#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
997#else
998#define ST_FLAGS_IDX ST_RDEV_IDX
999#endif
1000
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001001#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001002#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001003#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001004#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001005#endif
1006
1007#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1008#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1009#else
1010#define ST_BIRTHTIME_IDX ST_GEN_IDX
1011#endif
1012
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001013static PyStructSequence_Desc stat_result_desc = {
1014 "stat_result", /* name */
1015 stat_result__doc__, /* doc */
1016 stat_result_fields,
1017 10
1018};
1019
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001020PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001021"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1022This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001023 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001024or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001025\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001026See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001027
1028static PyStructSequence_Field statvfs_result_fields[] = {
1029 {"f_bsize", },
1030 {"f_frsize", },
1031 {"f_blocks", },
1032 {"f_bfree", },
1033 {"f_bavail", },
1034 {"f_files", },
1035 {"f_ffree", },
1036 {"f_favail", },
1037 {"f_flag", },
1038 {"f_namemax",},
1039 {0}
1040};
1041
1042static PyStructSequence_Desc statvfs_result_desc = {
1043 "statvfs_result", /* name */
1044 statvfs_result__doc__, /* doc */
1045 statvfs_result_fields,
1046 10
1047};
1048
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001049static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001050static PyTypeObject StatResultType;
1051static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001052static newfunc structseq_new;
1053
1054static PyObject *
1055statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1056{
1057 PyStructSequence *result;
1058 int i;
1059
1060 result = (PyStructSequence*)structseq_new(type, args, kwds);
1061 if (!result)
1062 return NULL;
1063 /* If we have been initialized from a tuple,
1064 st_?time might be set to None. Initialize it
1065 from the int slots. */
1066 for (i = 7; i <= 9; i++) {
1067 if (result->ob_item[i+3] == Py_None) {
1068 Py_DECREF(Py_None);
1069 Py_INCREF(result->ob_item[i]);
1070 result->ob_item[i+3] = result->ob_item[i];
1071 }
1072 }
1073 return (PyObject*)result;
1074}
1075
1076
1077
1078/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001079static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001080
1081PyDoc_STRVAR(stat_float_times__doc__,
1082"stat_float_times([newval]) -> oldval\n\n\
1083Determine whether os.[lf]stat represents time stamps as float objects.\n\
1084If newval is True, future calls to stat() return floats, if it is False,\n\
1085future calls return ints. \n\
1086If newval is omitted, return the current setting.\n");
1087
1088static PyObject*
1089stat_float_times(PyObject* self, PyObject *args)
1090{
1091 int newval = -1;
1092 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1093 return NULL;
1094 if (newval == -1)
1095 /* Return old value */
1096 return PyBool_FromLong(_stat_float_times);
1097 _stat_float_times = newval;
1098 Py_INCREF(Py_None);
1099 return Py_None;
1100}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001101
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001102static void
1103fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1104{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001105 PyObject *fval,*ival;
1106#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001107 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001108#else
1109 ival = PyInt_FromLong((long)sec);
1110#endif
1111 if (_stat_float_times) {
1112 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1113 } else {
1114 fval = ival;
1115 Py_INCREF(fval);
1116 }
1117 PyStructSequence_SET_ITEM(v, index, ival);
1118 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001119}
1120
Tim Peters5aa91602002-01-30 05:46:57 +00001121/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001122 (used by posix_stat() and posix_fstat()) */
1123static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001124_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001125{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001126 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001127 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001128 if (v == NULL)
1129 return NULL;
1130
Martin v. Löwis14694662006-02-03 12:54:16 +00001131 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001132#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001133 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001134 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001135#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001136 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001137#endif
1138#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001139 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001140 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001141#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001142 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001143#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001144 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1145 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1146 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001147#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001148 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001149 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001150#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001151 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001152#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001153
Martin v. Löwis14694662006-02-03 12:54:16 +00001154#if defined(HAVE_STAT_TV_NSEC)
1155 ansec = st->st_atim.tv_nsec;
1156 mnsec = st->st_mtim.tv_nsec;
1157 cnsec = st->st_ctim.tv_nsec;
1158#elif defined(HAVE_STAT_TV_NSEC2)
1159 ansec = st->st_atimespec.tv_nsec;
1160 mnsec = st->st_mtimespec.tv_nsec;
1161 cnsec = st->st_ctimespec.tv_nsec;
1162#elif defined(HAVE_STAT_NSEC)
1163 ansec = st->st_atime_nsec;
1164 mnsec = st->st_mtime_nsec;
1165 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001166#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001167 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001168#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001169 fill_time(v, 7, st->st_atime, ansec);
1170 fill_time(v, 8, st->st_mtime, mnsec);
1171 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001172
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001173#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001174 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001175 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001176#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001177#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001178 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001179 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001180#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001181#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001182 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001183 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001184#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001185#ifdef HAVE_STRUCT_STAT_ST_GEN
1186 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001187 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001188#endif
1189#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1190 {
1191 PyObject *val;
1192 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001193 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001194#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001195 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001196#else
1197 bnsec = 0;
1198#endif
1199 if (_stat_float_times) {
1200 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1201 } else {
1202 val = PyInt_FromLong((long)bsec);
1203 }
1204 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1205 val);
1206 }
1207#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001208#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1209 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001210 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001211#endif
Fred Drake699f3522000-06-29 21:12:41 +00001212
1213 if (PyErr_Occurred()) {
1214 Py_DECREF(v);
1215 return NULL;
1216 }
1217
1218 return v;
1219}
1220
Martin v. Löwisd8948722004-06-02 09:57:56 +00001221#ifdef MS_WINDOWS
1222
1223/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1224 where / can be used in place of \ and the trailing slash is optional.
1225 Both SERVER and SHARE must have at least one character.
1226*/
1227
1228#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1229#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
1230#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
1231
Tim Peters4ad82172004-08-30 17:02:04 +00001232static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001233IsUNCRootA(char *path, int pathlen)
1234{
1235 #define ISSLASH ISSLASHA
1236
1237 int i, share;
1238
1239 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1240 /* minimum UNCRoot is \\x\y */
1241 return FALSE;
1242 for (i = 2; i < pathlen ; i++)
1243 if (ISSLASH(path[i])) break;
1244 if (i == 2 || i == pathlen)
1245 /* do not allow \\\SHARE or \\SERVER */
1246 return FALSE;
1247 share = i+1;
1248 for (i = share; i < pathlen; i++)
1249 if (ISSLASH(path[i])) break;
1250 return (i != share && (i == pathlen || i == pathlen-1));
1251
1252 #undef ISSLASH
1253}
1254
1255#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001256static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001257IsUNCRootW(Py_UNICODE *path, int pathlen)
1258{
1259 #define ISSLASH ISSLASHW
1260
1261 int i, share;
1262
1263 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1264 /* minimum UNCRoot is \\x\y */
1265 return FALSE;
1266 for (i = 2; i < pathlen ; i++)
1267 if (ISSLASH(path[i])) break;
1268 if (i == 2 || i == pathlen)
1269 /* do not allow \\\SHARE or \\SERVER */
1270 return FALSE;
1271 share = i+1;
1272 for (i = share; i < pathlen; i++)
1273 if (ISSLASH(path[i])) break;
1274 return (i != share && (i == pathlen || i == pathlen-1));
1275
1276 #undef ISSLASH
1277}
1278#endif /* Py_WIN_WIDE_FILENAMES */
1279#endif /* MS_WINDOWS */
1280
Barry Warsaw53699e91996-12-10 23:23:01 +00001281static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001282posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001283 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001284#ifdef __VMS
1285 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1286#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001287 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001288#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001289 char *wformat,
1290 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001291{
Fred Drake699f3522000-06-29 21:12:41 +00001292 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001293 char *path = NULL; /* pass this to stat; do not free() it */
1294 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001295 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001296 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001297
1298#ifdef Py_WIN_WIDE_FILENAMES
1299 /* If on wide-character-capable OS see if argument
1300 is Unicode and if so use wide API. */
1301 if (unicode_file_names()) {
1302 PyUnicodeObject *po;
1303 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001304 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1305
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001306 Py_BEGIN_ALLOW_THREADS
1307 /* PyUnicode_AS_UNICODE result OK without
1308 thread lock as it is a simple dereference. */
1309 res = wstatfunc(wpath, &st);
1310 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001311
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001312 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001313 return win32_error_unicode("stat", wpath);
1314 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001315 }
1316 /* Drop the argument parsing error as narrow strings
1317 are also valid. */
1318 PyErr_Clear();
1319 }
1320#endif
1321
Tim Peters5aa91602002-01-30 05:46:57 +00001322 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001323 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001324 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001325 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001326
Barry Warsaw53699e91996-12-10 23:23:01 +00001327 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001328 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001329 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001330
1331 if (res != 0) {
1332#ifdef MS_WINDOWS
1333 result = win32_error("stat", pathfree);
1334#else
1335 result = posix_error_with_filename(pathfree);
1336#endif
1337 }
1338 else
1339 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001340
Tim Peters500bd032001-12-19 19:05:01 +00001341 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001342 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001343}
1344
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001345/* POSIX methods */
1346
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001347PyDoc_STRVAR(posix_access__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001348"access(path, mode) -> 1 if granted, 0 otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001349Use the real uid/gid to test for access to a path. Note that most\n\
1350operations will use the effective uid/gid, therefore this routine can\n\
1351be used in a suid/sgid environment to test if the invoking user has the\n\
1352specified access to the path. The mode argument can be F_OK to test\n\
1353existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001354
1355static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001356posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001357{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001358 char *path;
1359 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001360
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001361#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001362 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001363 if (unicode_file_names()) {
1364 PyUnicodeObject *po;
1365 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1366 Py_BEGIN_ALLOW_THREADS
1367 /* PyUnicode_AS_UNICODE OK without thread lock as
1368 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001369 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001370 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001371 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001372 }
1373 /* Drop the argument parsing error as narrow strings
1374 are also valid. */
1375 PyErr_Clear();
1376 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001377 if (!PyArg_ParseTuple(args, "eti:access",
1378 Py_FileSystemDefaultEncoding, &path, &mode))
1379 return 0;
1380 Py_BEGIN_ALLOW_THREADS
1381 attr = GetFileAttributesA(path);
1382 Py_END_ALLOW_THREADS
1383 PyMem_Free(path);
1384finish:
1385 if (attr == 0xFFFFFFFF)
1386 /* File does not exist, or cannot read attributes */
1387 return PyBool_FromLong(0);
1388 /* Access is possible if either write access wasn't requested, or
1389 the file isn't read-only. */
1390 return PyBool_FromLong(!(mode & 2) || !(attr && FILE_ATTRIBUTE_READONLY));
1391#else
1392 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001393 if (!PyArg_ParseTuple(args, "eti:access",
1394 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001395 return NULL;
1396 Py_BEGIN_ALLOW_THREADS
1397 res = access(path, mode);
1398 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001399 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001400 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001401#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001402}
1403
Guido van Rossumd371ff11999-01-25 16:12:23 +00001404#ifndef F_OK
1405#define F_OK 0
1406#endif
1407#ifndef R_OK
1408#define R_OK 4
1409#endif
1410#ifndef W_OK
1411#define W_OK 2
1412#endif
1413#ifndef X_OK
1414#define X_OK 1
1415#endif
1416
1417#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001418PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001419"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001420Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001421
1422static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001423posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001424{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001425 int id;
1426 char *ret;
1427
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001428 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001429 return NULL;
1430
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001431#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001432 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001433 if (id == 0) {
1434 ret = ttyname();
1435 }
1436 else {
1437 ret = NULL;
1438 }
1439#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001440 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001441#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001442 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001443 return posix_error();
1444 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001445}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001446#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001447
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001448#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001449PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001450"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001451Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001452
1453static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001454posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001455{
1456 char *ret;
1457 char buffer[L_ctermid];
1458
Greg Wardb48bc172000-03-01 21:51:56 +00001459#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001460 ret = ctermid_r(buffer);
1461#else
1462 ret = ctermid(buffer);
1463#endif
1464 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001465 return posix_error();
1466 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001467}
1468#endif
1469
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001470PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001471"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001472Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001473
Barry Warsaw53699e91996-12-10 23:23:01 +00001474static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001475posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001476{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001477#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001478 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001479#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001480 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001481#elif defined(__VMS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001482 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001483#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00001484 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001485#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001486}
1487
Fred Drake4d1e64b2002-04-15 19:40:07 +00001488#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001489PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001490"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001491Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001492opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001493
1494static PyObject *
1495posix_fchdir(PyObject *self, PyObject *fdobj)
1496{
1497 return posix_fildes(fdobj, fchdir);
1498}
1499#endif /* HAVE_FCHDIR */
1500
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001501
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001502PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001503"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001504Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001505
Barry Warsaw53699e91996-12-10 23:23:01 +00001506static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001507posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001508{
Mark Hammondef8b6542001-05-13 08:04:26 +00001509 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001510 int i;
1511 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001512#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001513 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001514 if (unicode_file_names()) {
1515 PyUnicodeObject *po;
1516 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1517 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001518 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1519 if (attr != 0xFFFFFFFF) {
1520 if (i & _S_IWRITE)
1521 attr &= ~FILE_ATTRIBUTE_READONLY;
1522 else
1523 attr |= FILE_ATTRIBUTE_READONLY;
1524 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1525 }
1526 else
1527 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001528 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001529 if (!res)
1530 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001531 PyUnicode_AS_UNICODE(po));
1532 Py_INCREF(Py_None);
1533 return Py_None;
1534 }
1535 /* Drop the argument parsing error as narrow strings
1536 are also valid. */
1537 PyErr_Clear();
1538 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001539 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1540 &path, &i))
1541 return NULL;
1542 Py_BEGIN_ALLOW_THREADS
1543 attr = GetFileAttributesA(path);
1544 if (attr != 0xFFFFFFFF) {
1545 if (i & _S_IWRITE)
1546 attr &= ~FILE_ATTRIBUTE_READONLY;
1547 else
1548 attr |= FILE_ATTRIBUTE_READONLY;
1549 res = SetFileAttributesA(path, attr);
1550 }
1551 else
1552 res = 0;
1553 Py_END_ALLOW_THREADS
1554 if (!res) {
1555 win32_error("chmod", path);
1556 PyMem_Free(path);
1557 return NULL;
1558 }
1559 PyMem_Free(path);
1560 Py_INCREF(Py_None);
1561 return Py_None;
1562#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001563 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001564 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001565 return NULL;
1566 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001567 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001568 Py_END_ALLOW_THREADS
1569 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001570 return posix_error_with_allocated_filename(path);
1571 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001572 Py_INCREF(Py_None);
1573 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001574#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001575}
1576
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001577
Martin v. Löwis244edc82001-10-04 22:44:26 +00001578#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001579PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001580"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001581Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001582
1583static PyObject *
1584posix_chroot(PyObject *self, PyObject *args)
1585{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001586 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001587}
1588#endif
1589
Guido van Rossum21142a01999-01-08 21:05:37 +00001590#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001591PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001592"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001593force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001594
1595static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001596posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001597{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001598 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001599}
1600#endif /* HAVE_FSYNC */
1601
1602#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001603
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001604#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001605extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1606#endif
1607
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001608PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001609"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001610force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001611 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001612
1613static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001614posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001615{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001616 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001617}
1618#endif /* HAVE_FDATASYNC */
1619
1620
Fredrik Lundh10723342000-07-10 16:38:09 +00001621#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001622PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001623"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001624Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001625
Barry Warsaw53699e91996-12-10 23:23:01 +00001626static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001627posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001628{
Mark Hammondef8b6542001-05-13 08:04:26 +00001629 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001630 int uid, gid;
1631 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001632 if (!PyArg_ParseTuple(args, "etii:chown",
1633 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001634 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001635 return NULL;
1636 Py_BEGIN_ALLOW_THREADS
1637 res = chown(path, (uid_t) uid, (gid_t) gid);
1638 Py_END_ALLOW_THREADS
1639 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001640 return posix_error_with_allocated_filename(path);
1641 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001642 Py_INCREF(Py_None);
1643 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001644}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001645#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001646
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001647#ifdef HAVE_LCHOWN
1648PyDoc_STRVAR(posix_lchown__doc__,
1649"lchown(path, uid, gid)\n\n\
1650Change the owner and group id of path to the numeric uid and gid.\n\
1651This function will not follow symbolic links.");
1652
1653static PyObject *
1654posix_lchown(PyObject *self, PyObject *args)
1655{
1656 char *path = NULL;
1657 int uid, gid;
1658 int res;
1659 if (!PyArg_ParseTuple(args, "etii:lchown",
1660 Py_FileSystemDefaultEncoding, &path,
1661 &uid, &gid))
1662 return NULL;
1663 Py_BEGIN_ALLOW_THREADS
1664 res = lchown(path, (uid_t) uid, (gid_t) gid);
1665 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001666 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001667 return posix_error_with_allocated_filename(path);
1668 PyMem_Free(path);
1669 Py_INCREF(Py_None);
1670 return Py_None;
1671}
1672#endif /* HAVE_LCHOWN */
1673
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001674
Guido van Rossum36bc6801995-06-14 22:54:23 +00001675#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001676PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001677"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001678Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001679
Barry Warsaw53699e91996-12-10 23:23:01 +00001680static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001681posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001682{
1683 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001684 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001685
Barry Warsaw53699e91996-12-10 23:23:01 +00001686 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001687#if defined(PYOS_OS2) && defined(PYCC_GCC)
1688 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001689#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001690 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001691#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001692 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001693 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001694 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001695 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001696}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001697
Walter Dörwald3b918c32002-11-21 20:18:46 +00001698#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001699PyDoc_STRVAR(posix_getcwdu__doc__,
1700"getcwdu() -> path\n\n\
1701Return a unicode string representing the current working directory.");
1702
1703static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001704posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001705{
1706 char buf[1026];
1707 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001708
1709#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001710 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001711 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001712 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001713 wchar_t *wbuf2 = wbuf;
1714 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001715 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001716 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1717 /* If the buffer is large enough, len does not include the
1718 terminating \0. If the buffer is too small, len includes
1719 the space needed for the terminator. */
1720 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1721 wbuf2 = malloc(len * sizeof(wchar_t));
1722 if (wbuf2)
1723 len = GetCurrentDirectoryW(len, wbuf2);
1724 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001725 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001726 if (!wbuf2) {
1727 PyErr_NoMemory();
1728 return NULL;
1729 }
1730 if (!len) {
1731 if (wbuf2 != wbuf) free(wbuf2);
1732 return win32_error("getcwdu", NULL);
1733 }
1734 resobj = PyUnicode_FromWideChar(wbuf2, len);
1735 if (wbuf2 != wbuf) free(wbuf2);
1736 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001737 }
1738#endif
1739
1740 Py_BEGIN_ALLOW_THREADS
1741#if defined(PYOS_OS2) && defined(PYCC_GCC)
1742 res = _getcwd2(buf, sizeof buf);
1743#else
1744 res = getcwd(buf, sizeof buf);
1745#endif
1746 Py_END_ALLOW_THREADS
1747 if (res == NULL)
1748 return posix_error();
1749 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1750}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001751#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001752#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001753
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001754
Guido van Rossumb6775db1994-08-01 11:34:53 +00001755#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001756PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001757"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001758Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001759
Barry Warsaw53699e91996-12-10 23:23:01 +00001760static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001761posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001762{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001763 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001764}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001765#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001766
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001767
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001768PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001769"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001770Return a list containing the names of the entries in the directory.\n\
1771\n\
1772 path: path of directory to list\n\
1773\n\
1774The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001775entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001776
Barry Warsaw53699e91996-12-10 23:23:01 +00001777static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001778posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001779{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001780 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001781 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001782#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001783
Barry Warsaw53699e91996-12-10 23:23:01 +00001784 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001785 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001786 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001787 WIN32_FIND_DATA FileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001788 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00001789 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001790 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001791
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001792#ifdef Py_WIN_WIDE_FILENAMES
1793 /* If on wide-character-capable OS see if argument
1794 is Unicode and if so use wide API. */
1795 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001796 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001797 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1798 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001799 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001800 Py_UNICODE wch;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001801 /* Overallocate for \\*.*\0 */
1802 len = PyUnicode_GET_SIZE(po);
1803 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
1804 if (!wnamebuf) {
1805 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001806 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001807 }
1808 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
1809 wch = len > 0 ? wnamebuf[len-1] : '\0';
1810 if (wch != L'/' && wch != L'\\' && wch != L':')
1811 wnamebuf[len++] = L'\\';
1812 wcscpy(wnamebuf + len, L"*.*");
1813 if ((d = PyList_New(0)) == NULL) {
1814 free(wnamebuf);
1815 return NULL;
1816 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001817 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1818 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001819 int error = GetLastError();
1820 if (error == ERROR_FILE_NOT_FOUND) {
1821 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001822 return d;
1823 }
1824 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001825 win32_error_unicode("FindFirstFileW", wnamebuf);
1826 free(wnamebuf);
1827 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001828 }
1829 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001830 /* Skip over . and .. */
1831 if (wcscmp(wFileData.cFileName, L".") != 0 &&
1832 wcscmp(wFileData.cFileName, L"..") != 0) {
1833 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1834 if (v == NULL) {
1835 Py_DECREF(d);
1836 d = NULL;
1837 break;
1838 }
1839 if (PyList_Append(d, v) != 0) {
1840 Py_DECREF(v);
1841 Py_DECREF(d);
1842 d = NULL;
1843 break;
1844 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001845 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001846 }
Georg Brandl622927b2006-03-07 12:48:03 +00001847 Py_BEGIN_ALLOW_THREADS
1848 result = FindNextFileW(hFindFile, &wFileData);
1849 Py_END_ALLOW_THREADS
1850 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001851
1852 if (FindClose(hFindFile) == FALSE) {
1853 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001854 win32_error_unicode("FindClose", wnamebuf);
1855 free(wnamebuf);
1856 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001857 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001858 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001859 return d;
1860 }
1861 /* Drop the argument parsing error as narrow strings
1862 are also valid. */
1863 PyErr_Clear();
1864 }
1865#endif
1866
Tim Peters5aa91602002-01-30 05:46:57 +00001867 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001868 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00001869 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00001870 if (len > 0) {
1871 char ch = namebuf[len-1];
1872 if (ch != SEP && ch != ALTSEP && ch != ':')
1873 namebuf[len++] = '/';
1874 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001875 strcpy(namebuf + len, "*.*");
1876
Barry Warsaw53699e91996-12-10 23:23:01 +00001877 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001878 return NULL;
1879
1880 hFindFile = FindFirstFile(namebuf, &FileData);
1881 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001882 int error = GetLastError();
1883 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001884 return d;
1885 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001886 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001887 }
1888 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001889 /* Skip over . and .. */
1890 if (strcmp(FileData.cFileName, ".") != 0 &&
1891 strcmp(FileData.cFileName, "..") != 0) {
1892 v = PyString_FromString(FileData.cFileName);
1893 if (v == NULL) {
1894 Py_DECREF(d);
1895 d = NULL;
1896 break;
1897 }
1898 if (PyList_Append(d, v) != 0) {
1899 Py_DECREF(v);
1900 Py_DECREF(d);
1901 d = NULL;
1902 break;
1903 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001904 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001905 }
Georg Brandl622927b2006-03-07 12:48:03 +00001906 Py_BEGIN_ALLOW_THREADS
1907 result = FindNextFile(hFindFile, &FileData);
1908 Py_END_ALLOW_THREADS
1909 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001910
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001911 if (FindClose(hFindFile) == FALSE) {
1912 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001913 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001914 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001915
1916 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001917
Tim Peters0bb44a42000-09-15 07:44:49 +00001918#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001919
1920#ifndef MAX_PATH
1921#define MAX_PATH CCHMAXPATH
1922#endif
1923 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00001924 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001925 PyObject *d, *v;
1926 char namebuf[MAX_PATH+5];
1927 HDIR hdir = 1;
1928 ULONG srchcnt = 1;
1929 FILEFINDBUF3 ep;
1930 APIRET rc;
1931
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001932 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001933 return NULL;
1934 if (len >= MAX_PATH) {
1935 PyErr_SetString(PyExc_ValueError, "path too long");
1936 return NULL;
1937 }
1938 strcpy(namebuf, name);
1939 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001940 if (*pt == ALTSEP)
1941 *pt = SEP;
1942 if (namebuf[len-1] != SEP)
1943 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001944 strcpy(namebuf + len, "*.*");
1945
1946 if ((d = PyList_New(0)) == NULL)
1947 return NULL;
1948
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001949 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
1950 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001951 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001952 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
1953 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
1954 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001955
1956 if (rc != NO_ERROR) {
1957 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001958 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001959 }
1960
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001961 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001962 do {
1963 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001964 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001965 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001966
1967 strcpy(namebuf, ep.achName);
1968
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001969 /* Leave Case of Name Alone -- In Native Form */
1970 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001971
1972 v = PyString_FromString(namebuf);
1973 if (v == NULL) {
1974 Py_DECREF(d);
1975 d = NULL;
1976 break;
1977 }
1978 if (PyList_Append(d, v) != 0) {
1979 Py_DECREF(v);
1980 Py_DECREF(d);
1981 d = NULL;
1982 break;
1983 }
1984 Py_DECREF(v);
1985 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
1986 }
1987
1988 return d;
1989#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001990
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001991 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001992 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001993 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001994 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00001995 int arg_is_unicode = 1;
1996
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001997 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00001998 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
1999 arg_is_unicode = 0;
2000 PyErr_Clear();
2001 }
2002 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002003 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002004 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002005 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002006 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002007 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002008 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002009 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002010 return NULL;
2011 }
Georg Brandl622927b2006-03-07 12:48:03 +00002012 for (;;) {
2013 Py_BEGIN_ALLOW_THREADS
2014 ep = readdir(dirp);
2015 Py_END_ALLOW_THREADS
2016 if (ep == NULL)
2017 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002018 if (ep->d_name[0] == '.' &&
2019 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002020 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002021 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00002022 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002023 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002024 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002025 d = NULL;
2026 break;
2027 }
Just van Rossum46c97842003-02-25 21:42:15 +00002028#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00002029 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002030 PyObject *w;
2031
2032 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002033 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002034 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002035 if (w != NULL) {
2036 Py_DECREF(v);
2037 v = w;
2038 }
2039 else {
2040 /* fall back to the original byte string, as
2041 discussed in patch #683592 */
2042 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002043 }
Just van Rossum46c97842003-02-25 21:42:15 +00002044 }
2045#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002046 if (PyList_Append(d, v) != 0) {
2047 Py_DECREF(v);
2048 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002049 d = NULL;
2050 break;
2051 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002052 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002053 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002054 if (errno != 0 && d != NULL) {
2055 /* readdir() returned NULL and set errno */
2056 closedir(dirp);
2057 Py_DECREF(d);
2058 return posix_error_with_allocated_filename(name);
2059 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002060 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002061 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002062
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002063 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002064
Tim Peters0bb44a42000-09-15 07:44:49 +00002065#endif /* which OS */
2066} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002067
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002068#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002069/* A helper function for abspath on win32 */
2070static PyObject *
2071posix__getfullpathname(PyObject *self, PyObject *args)
2072{
2073 /* assume encoded strings wont more than double no of chars */
2074 char inbuf[MAX_PATH*2];
2075 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002076 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002077 char outbuf[MAX_PATH*2];
2078 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002079#ifdef Py_WIN_WIDE_FILENAMES
2080 if (unicode_file_names()) {
2081 PyUnicodeObject *po;
2082 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2083 Py_UNICODE woutbuf[MAX_PATH*2];
2084 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002085 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002086 sizeof(woutbuf)/sizeof(woutbuf[0]),
2087 woutbuf, &wtemp))
2088 return win32_error("GetFullPathName", "");
2089 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2090 }
2091 /* Drop the argument parsing error as narrow strings
2092 are also valid. */
2093 PyErr_Clear();
2094 }
2095#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002096 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2097 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002098 &insize))
2099 return NULL;
2100 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2101 outbuf, &temp))
2102 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002103 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2104 return PyUnicode_Decode(outbuf, strlen(outbuf),
2105 Py_FileSystemDefaultEncoding, NULL);
2106 }
Mark Hammondef8b6542001-05-13 08:04:26 +00002107 return PyString_FromString(outbuf);
2108} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002109#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002111PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002112"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002113Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002114
Barry Warsaw53699e91996-12-10 23:23:01 +00002115static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002116posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002117{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002118 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002119 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002120 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002121
2122#ifdef Py_WIN_WIDE_FILENAMES
2123 if (unicode_file_names()) {
2124 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002125 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002126 Py_BEGIN_ALLOW_THREADS
2127 /* PyUnicode_AS_UNICODE OK without thread lock as
2128 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002129 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002130 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002131 if (!res)
2132 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002133 Py_INCREF(Py_None);
2134 return Py_None;
2135 }
2136 /* Drop the argument parsing error as narrow strings
2137 are also valid. */
2138 PyErr_Clear();
2139 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002140 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2141 Py_FileSystemDefaultEncoding, &path, &mode))
2142 return NULL;
2143 Py_BEGIN_ALLOW_THREADS
2144 /* PyUnicode_AS_UNICODE OK without thread lock as
2145 it is a simple dereference. */
2146 res = CreateDirectoryA(path, NULL);
2147 Py_END_ALLOW_THREADS
2148 if (!res) {
2149 win32_error("mkdir", path);
2150 PyMem_Free(path);
2151 return NULL;
2152 }
2153 PyMem_Free(path);
2154 Py_INCREF(Py_None);
2155 return Py_None;
2156#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002157
Tim Peters5aa91602002-01-30 05:46:57 +00002158 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002159 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002160 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002161 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002162#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002163 res = mkdir(path);
2164#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002165 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002166#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002167 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002168 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002169 return posix_error_with_allocated_filename(path);
2170 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002171 Py_INCREF(Py_None);
2172 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002173#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002174}
2175
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002176
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002177/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2178#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002179#include <sys/resource.h>
2180#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002181
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002182
2183#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002184PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002185"nice(inc) -> new_priority\n\n\
2186Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002187
Barry Warsaw53699e91996-12-10 23:23:01 +00002188static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002189posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002190{
2191 int increment, value;
2192
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002193 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002194 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002195
2196 /* There are two flavours of 'nice': one that returns the new
2197 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002198 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2199 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002200
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002201 If we are of the nice family that returns the new priority, we
2202 need to clear errno before the call, and check if errno is filled
2203 before calling posix_error() on a returnvalue of -1, because the
2204 -1 may be the actual new priority! */
2205
2206 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002207 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002208#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002209 if (value == 0)
2210 value = getpriority(PRIO_PROCESS, 0);
2211#endif
2212 if (value == -1 && errno != 0)
2213 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002214 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002215 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002216}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002217#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002219PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002220"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002221Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002222
Barry Warsaw53699e91996-12-10 23:23:01 +00002223static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002224posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002225{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002226#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002227 PyObject *o1, *o2;
2228 char *p1, *p2;
2229 BOOL result;
2230 if (unicode_file_names()) {
2231 if (!PyArg_ParseTuple(args, "O&O&:rename",
2232 convert_to_unicode, &o1,
2233 convert_to_unicode, &o2))
2234 PyErr_Clear();
2235 else {
2236 Py_BEGIN_ALLOW_THREADS
2237 result = MoveFileW(PyUnicode_AsUnicode(o1),
2238 PyUnicode_AsUnicode(o2));
2239 Py_END_ALLOW_THREADS
2240 Py_DECREF(o1);
2241 Py_DECREF(o2);
2242 if (!result)
2243 return win32_error("rename", NULL);
2244 Py_INCREF(Py_None);
2245 return Py_None;
2246 }
2247 }
2248 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2249 return NULL;
2250 Py_BEGIN_ALLOW_THREADS
2251 result = MoveFileA(p1, p2);
2252 Py_END_ALLOW_THREADS
2253 if (!result)
2254 return win32_error("rename", NULL);
2255 Py_INCREF(Py_None);
2256 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002257#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002258 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002259#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002260}
2261
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002262
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002263PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002264"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002265Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002266
Barry Warsaw53699e91996-12-10 23:23:01 +00002267static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002268posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002269{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002270#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002271 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002272#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002273 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002274#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002275}
2276
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002277
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002278PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002279"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002280Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002281
Barry Warsaw53699e91996-12-10 23:23:01 +00002282static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002283posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002284{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002285#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002286 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002287#else
2288 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2289#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002290}
2291
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002292
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002293#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002294PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002295"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002296Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002297
Barry Warsaw53699e91996-12-10 23:23:01 +00002298static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002299posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002300{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002301 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002302 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002303 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002304 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002305 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002306 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002307 Py_END_ALLOW_THREADS
2308 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002309}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002310#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002311
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002312
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002313PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002314"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002315Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002316
Barry Warsaw53699e91996-12-10 23:23:01 +00002317static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002318posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002319{
2320 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002321 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002322 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002323 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002324 if (i < 0)
2325 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002326 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002327}
2328
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002329
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002330PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002331"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002332Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002334PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002335"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002336Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002337
Barry Warsaw53699e91996-12-10 23:23:01 +00002338static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002339posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002340{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002341#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002342 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002343#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002344 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002345#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002346}
2347
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002348
Guido van Rossumb6775db1994-08-01 11:34:53 +00002349#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002350PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002351"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002352Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002353
Barry Warsaw53699e91996-12-10 23:23:01 +00002354static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002355posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002356{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002357 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002358 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002359
Barry Warsaw53699e91996-12-10 23:23:01 +00002360 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002361 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002362 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002363 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002364 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002365 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002366 u.sysname,
2367 u.nodename,
2368 u.release,
2369 u.version,
2370 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002371}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002372#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002373
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002374static int
2375extract_time(PyObject *t, long* sec, long* usec)
2376{
2377 long intval;
2378 if (PyFloat_Check(t)) {
2379 double tval = PyFloat_AsDouble(t);
2380 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
2381 if (!intobj)
2382 return -1;
2383 intval = PyInt_AsLong(intobj);
2384 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002385 if (intval == -1 && PyErr_Occurred())
2386 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002387 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002388 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002389 if (*usec < 0)
2390 /* If rounding gave us a negative number,
2391 truncate. */
2392 *usec = 0;
2393 return 0;
2394 }
2395 intval = PyInt_AsLong(t);
2396 if (intval == -1 && PyErr_Occurred())
2397 return -1;
2398 *sec = intval;
2399 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002400 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002401}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002402
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002403PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002404"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002405utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002406Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002407second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002408
Barry Warsaw53699e91996-12-10 23:23:01 +00002409static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002410posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002411{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002412#ifdef Py_WIN_WIDE_FILENAMES
2413 PyObject *arg;
2414 PyUnicodeObject *obwpath;
2415 wchar_t *wpath = NULL;
2416 char *apath = NULL;
2417 HANDLE hFile;
2418 long atimesec, mtimesec, ausec, musec;
2419 FILETIME atime, mtime;
2420 PyObject *result = NULL;
2421
2422 if (unicode_file_names()) {
2423 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2424 wpath = PyUnicode_AS_UNICODE(obwpath);
2425 Py_BEGIN_ALLOW_THREADS
2426 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2427 NULL, OPEN_EXISTING, 0, NULL);
2428 Py_END_ALLOW_THREADS
2429 if (hFile == INVALID_HANDLE_VALUE)
2430 return win32_error_unicode("utime", wpath);
2431 } else
2432 /* Drop the argument parsing error as narrow strings
2433 are also valid. */
2434 PyErr_Clear();
2435 }
2436 if (!wpath) {
2437 if (!PyArg_ParseTuple(args, "etO:utime",
2438 Py_FileSystemDefaultEncoding, &apath, &arg))
2439 return NULL;
2440 Py_BEGIN_ALLOW_THREADS
2441 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2442 NULL, OPEN_EXISTING, 0, NULL);
2443 Py_END_ALLOW_THREADS
2444 if (hFile == INVALID_HANDLE_VALUE) {
2445 win32_error("utime", apath);
2446 PyMem_Free(apath);
2447 return NULL;
2448 }
2449 PyMem_Free(apath);
2450 }
2451
2452 if (arg == Py_None) {
2453 SYSTEMTIME now;
2454 GetSystemTime(&now);
2455 if (!SystemTimeToFileTime(&now, &mtime) ||
2456 !SystemTimeToFileTime(&now, &atime)) {
2457 win32_error("utime", NULL);
2458 goto done;
2459 }
2460 }
2461 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2462 PyErr_SetString(PyExc_TypeError,
2463 "utime() arg 2 must be a tuple (atime, mtime)");
2464 goto done;
2465 }
2466 else {
2467 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2468 &atimesec, &ausec) == -1)
2469 goto done;
2470 time_t_to_FILE_TIME(atimesec, ausec, &atime);
2471 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2472 &mtimesec, &musec) == -1)
2473 goto done;
2474 time_t_to_FILE_TIME(mtimesec, musec, &mtime);
2475 }
2476 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2477 /* Avoid putting the file name into the error here,
2478 as that may confuse the user into believing that
2479 something is wrong with the file, when it also
2480 could be the time stamp that gives a problem. */
2481 win32_error("utime", NULL);
2482 }
2483 Py_INCREF(Py_None);
2484 result = Py_None;
2485done:
2486 CloseHandle(hFile);
2487 return result;
2488#else /* Py_WIN_WIDE_FILENAMES */
2489
Neal Norwitz2adf2102004-06-09 01:46:02 +00002490 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002491 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002492 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002493 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002494
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002495#if defined(HAVE_UTIMES)
2496 struct timeval buf[2];
2497#define ATIME buf[0].tv_sec
2498#define MTIME buf[1].tv_sec
2499#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002500/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002501 struct utimbuf buf;
2502#define ATIME buf.actime
2503#define MTIME buf.modtime
2504#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002505#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002506 time_t buf[2];
2507#define ATIME buf[0]
2508#define MTIME buf[1]
2509#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002510#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002511
Mark Hammond817c9292003-12-03 01:22:38 +00002512
Thomas Wouters477c8d52006-05-27 19:21:47 +00002513 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002514 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002515 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002516 if (arg == Py_None) {
2517 /* optional time values not given */
2518 Py_BEGIN_ALLOW_THREADS
2519 res = utime(path, NULL);
2520 Py_END_ALLOW_THREADS
2521 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002522 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002523 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002524 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002525 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002526 return NULL;
2527 }
2528 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002529 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002530 &atime, &ausec) == -1) {
2531 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002532 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002533 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002534 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002535 &mtime, &musec) == -1) {
2536 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002537 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002538 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002539 ATIME = atime;
2540 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002541#ifdef HAVE_UTIMES
2542 buf[0].tv_usec = ausec;
2543 buf[1].tv_usec = musec;
2544 Py_BEGIN_ALLOW_THREADS
2545 res = utimes(path, buf);
2546 Py_END_ALLOW_THREADS
2547#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002548 Py_BEGIN_ALLOW_THREADS
2549 res = utime(path, UTIME_ARG);
2550 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002551#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002552 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002553 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002554 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002555 }
Neal Norwitz96652712004-06-06 20:40:27 +00002556 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002557 Py_INCREF(Py_None);
2558 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002559#undef UTIME_ARG
2560#undef ATIME
2561#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002562#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002563}
2564
Guido van Rossum85e3b011991-06-03 12:42:10 +00002565
Guido van Rossum3b066191991-06-04 19:40:25 +00002566/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002567
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002568PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002569"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002570Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002571
Barry Warsaw53699e91996-12-10 23:23:01 +00002572static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002573posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002574{
2575 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002576 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002577 return NULL;
2578 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002579 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002580}
2581
Martin v. Löwis114619e2002-10-07 06:44:21 +00002582#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2583static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002584free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002585{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002586 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002587 for (i = 0; i < count; i++)
2588 PyMem_Free(array[i]);
2589 PyMem_DEL(array);
2590}
2591#endif
2592
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002593
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002594#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002595PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002596"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002597Execute an executable path with arguments, replacing current process.\n\
2598\n\
2599 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002600 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002601
Barry Warsaw53699e91996-12-10 23:23:01 +00002602static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002603posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002604{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002605 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002606 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002607 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002608 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002609 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002610
Guido van Rossum89b33251993-10-22 14:26:06 +00002611 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002612 argv is a list or tuple of strings. */
2613
Martin v. Löwis114619e2002-10-07 06:44:21 +00002614 if (!PyArg_ParseTuple(args, "etO:execv",
2615 Py_FileSystemDefaultEncoding,
2616 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002617 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002618 if (PyList_Check(argv)) {
2619 argc = PyList_Size(argv);
2620 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002621 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002622 else if (PyTuple_Check(argv)) {
2623 argc = PyTuple_Size(argv);
2624 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002625 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002626 else {
Fred Drake661ea262000-10-24 19:57:45 +00002627 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002628 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002629 return NULL;
2630 }
2631
Barry Warsaw53699e91996-12-10 23:23:01 +00002632 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002633 if (argvlist == NULL) {
2634 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002635 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002636 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002637 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002638 if (!PyArg_Parse((*getitem)(argv, i), "et",
2639 Py_FileSystemDefaultEncoding,
2640 &argvlist[i])) {
2641 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002642 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002643 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002644 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002645 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002646
Guido van Rossum85e3b011991-06-03 12:42:10 +00002647 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002648 }
2649 argvlist[argc] = NULL;
2650
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002651 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002652
Guido van Rossum85e3b011991-06-03 12:42:10 +00002653 /* If we get here it's definitely an error */
2654
Martin v. Löwis114619e2002-10-07 06:44:21 +00002655 free_string_array(argvlist, argc);
2656 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002657 return posix_error();
2658}
2659
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002661PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002662"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002663Execute a path with arguments and environment, replacing current process.\n\
2664\n\
2665 path: path of executable file\n\
2666 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002667 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002668
Barry Warsaw53699e91996-12-10 23:23:01 +00002669static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002670posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002671{
2672 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002673 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002674 char **argvlist;
2675 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002676 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002677 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002678 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002679 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002680
2681 /* execve has three arguments: (path, argv, env), where
2682 argv is a list or tuple of strings and env is a dictionary
2683 like posix.environ. */
2684
Martin v. Löwis114619e2002-10-07 06:44:21 +00002685 if (!PyArg_ParseTuple(args, "etOO:execve",
2686 Py_FileSystemDefaultEncoding,
2687 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002688 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002689 if (PyList_Check(argv)) {
2690 argc = PyList_Size(argv);
2691 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002692 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002693 else if (PyTuple_Check(argv)) {
2694 argc = PyTuple_Size(argv);
2695 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002696 }
2697 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002698 PyErr_SetString(PyExc_TypeError,
2699 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002700 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002701 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002702 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002703 PyErr_SetString(PyExc_TypeError,
2704 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002705 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002706 }
2707
Barry Warsaw53699e91996-12-10 23:23:01 +00002708 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002709 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002710 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002711 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002712 }
2713 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002714 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002715 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002716 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002717 &argvlist[i]))
2718 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002719 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002720 goto fail_1;
2721 }
2722 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002723 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002724 argvlist[argc] = NULL;
2725
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002726 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002727 if (i < 0)
2728 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002729 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002730 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002731 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002732 goto fail_1;
2733 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002734 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002735 keys = PyMapping_Keys(env);
2736 vals = PyMapping_Values(env);
2737 if (!keys || !vals)
2738 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002739 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2740 PyErr_SetString(PyExc_TypeError,
2741 "execve(): env.keys() or env.values() is not a list");
2742 goto fail_2;
2743 }
Tim Peters5aa91602002-01-30 05:46:57 +00002744
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002745 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002746 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002747 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002748
2749 key = PyList_GetItem(keys, pos);
2750 val = PyList_GetItem(vals, pos);
2751 if (!key || !val)
2752 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002753
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002754 if (!PyArg_Parse(
2755 key,
2756 "s;execve() arg 3 contains a non-string key",
2757 &k) ||
2758 !PyArg_Parse(
2759 val,
2760 "s;execve() arg 3 contains a non-string value",
2761 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002762 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002763 goto fail_2;
2764 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002765
2766#if defined(PYOS_OS2)
2767 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2768 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2769#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002770 len = PyString_Size(key) + PyString_Size(val) + 2;
2771 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002772 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002773 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002774 goto fail_2;
2775 }
Tim Petersc8996f52001-12-03 20:41:00 +00002776 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002777 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002778#if defined(PYOS_OS2)
2779 }
2780#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002781 }
2782 envlist[envc] = 0;
2783
2784 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002785
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002786 /* If we get here it's definitely an error */
2787
2788 (void) posix_error();
2789
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002790 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002791 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002792 PyMem_DEL(envlist[envc]);
2793 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002794 fail_1:
2795 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002796 Py_XDECREF(vals);
2797 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002798 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002799 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002800 return NULL;
2801}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002802#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002803
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002804
Guido van Rossuma1065681999-01-25 23:20:23 +00002805#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002806PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002807"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002808Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002809\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002810 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002811 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002812 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002813
2814static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002815posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002816{
2817 char *path;
2818 PyObject *argv;
2819 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002820 int mode, i;
2821 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002822 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002823 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00002824
2825 /* spawnv has three arguments: (mode, path, argv), where
2826 argv is a list or tuple of strings. */
2827
Martin v. Löwis114619e2002-10-07 06:44:21 +00002828 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2829 Py_FileSystemDefaultEncoding,
2830 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002831 return NULL;
2832 if (PyList_Check(argv)) {
2833 argc = PyList_Size(argv);
2834 getitem = PyList_GetItem;
2835 }
2836 else if (PyTuple_Check(argv)) {
2837 argc = PyTuple_Size(argv);
2838 getitem = PyTuple_GetItem;
2839 }
2840 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002841 PyErr_SetString(PyExc_TypeError,
2842 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002843 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002844 return NULL;
2845 }
2846
2847 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002848 if (argvlist == NULL) {
2849 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002850 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002851 }
Guido van Rossuma1065681999-01-25 23:20:23 +00002852 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002853 if (!PyArg_Parse((*getitem)(argv, i), "et",
2854 Py_FileSystemDefaultEncoding,
2855 &argvlist[i])) {
2856 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002857 PyErr_SetString(
2858 PyExc_TypeError,
2859 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002860 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00002861 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00002862 }
2863 }
2864 argvlist[argc] = NULL;
2865
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002866#if defined(PYOS_OS2) && defined(PYCC_GCC)
2867 Py_BEGIN_ALLOW_THREADS
2868 spawnval = spawnv(mode, path, argvlist);
2869 Py_END_ALLOW_THREADS
2870#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002871 if (mode == _OLD_P_OVERLAY)
2872 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00002873
Tim Peters25059d32001-12-07 20:35:43 +00002874 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002875 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00002876 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002877#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002878
Martin v. Löwis114619e2002-10-07 06:44:21 +00002879 free_string_array(argvlist, argc);
2880 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002881
Fred Drake699f3522000-06-29 21:12:41 +00002882 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002883 return posix_error();
2884 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002885#if SIZEOF_LONG == SIZEOF_VOID_P
2886 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002887#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002888 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002889#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002890}
2891
2892
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002893PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002894"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002895Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002896\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002897 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002898 path: path of executable file\n\
2899 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002900 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002901
2902static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002903posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002904{
2905 char *path;
2906 PyObject *argv, *env;
2907 char **argvlist;
2908 char **envlist;
2909 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002910 int mode, pos, envc;
2911 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00002912 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002913 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002914 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002915
2916 /* spawnve has four arguments: (mode, path, argv, env), where
2917 argv is a list or tuple of strings and env is a dictionary
2918 like posix.environ. */
2919
Martin v. Löwis114619e2002-10-07 06:44:21 +00002920 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
2921 Py_FileSystemDefaultEncoding,
2922 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00002923 return NULL;
2924 if (PyList_Check(argv)) {
2925 argc = PyList_Size(argv);
2926 getitem = PyList_GetItem;
2927 }
2928 else if (PyTuple_Check(argv)) {
2929 argc = PyTuple_Size(argv);
2930 getitem = PyTuple_GetItem;
2931 }
2932 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002933 PyErr_SetString(PyExc_TypeError,
2934 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002935 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002936 }
2937 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002938 PyErr_SetString(PyExc_TypeError,
2939 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002940 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002941 }
2942
2943 argvlist = PyMem_NEW(char *, argc+1);
2944 if (argvlist == NULL) {
2945 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002946 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002947 }
2948 for (i = 0; i < argc; i++) {
2949 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002950 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00002951 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00002952 &argvlist[i]))
2953 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002954 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00002955 goto fail_1;
2956 }
2957 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002958 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00002959 argvlist[argc] = NULL;
2960
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002961 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002962 if (i < 0)
2963 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00002964 envlist = PyMem_NEW(char *, i + 1);
2965 if (envlist == NULL) {
2966 PyErr_NoMemory();
2967 goto fail_1;
2968 }
2969 envc = 0;
2970 keys = PyMapping_Keys(env);
2971 vals = PyMapping_Values(env);
2972 if (!keys || !vals)
2973 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002974 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2975 PyErr_SetString(PyExc_TypeError,
2976 "spawnve(): env.keys() or env.values() is not a list");
2977 goto fail_2;
2978 }
Tim Peters5aa91602002-01-30 05:46:57 +00002979
Guido van Rossuma1065681999-01-25 23:20:23 +00002980 for (pos = 0; pos < i; pos++) {
2981 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002982 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00002983
2984 key = PyList_GetItem(keys, pos);
2985 val = PyList_GetItem(vals, pos);
2986 if (!key || !val)
2987 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002988
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002989 if (!PyArg_Parse(
2990 key,
2991 "s;spawnve() arg 3 contains a non-string key",
2992 &k) ||
2993 !PyArg_Parse(
2994 val,
2995 "s;spawnve() arg 3 contains a non-string value",
2996 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00002997 {
2998 goto fail_2;
2999 }
Tim Petersc8996f52001-12-03 20:41:00 +00003000 len = PyString_Size(key) + PyString_Size(val) + 2;
3001 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003002 if (p == NULL) {
3003 PyErr_NoMemory();
3004 goto fail_2;
3005 }
Tim Petersc8996f52001-12-03 20:41:00 +00003006 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003007 envlist[envc++] = p;
3008 }
3009 envlist[envc] = 0;
3010
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003011#if defined(PYOS_OS2) && defined(PYCC_GCC)
3012 Py_BEGIN_ALLOW_THREADS
3013 spawnval = spawnve(mode, path, argvlist, envlist);
3014 Py_END_ALLOW_THREADS
3015#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003016 if (mode == _OLD_P_OVERLAY)
3017 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003018
3019 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003020 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003021 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003022#endif
Tim Peters25059d32001-12-07 20:35:43 +00003023
Fred Drake699f3522000-06-29 21:12:41 +00003024 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003025 (void) posix_error();
3026 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003027#if SIZEOF_LONG == SIZEOF_VOID_P
3028 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003029#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003030 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003031#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003032
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003033 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003034 while (--envc >= 0)
3035 PyMem_DEL(envlist[envc]);
3036 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003037 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003038 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003039 Py_XDECREF(vals);
3040 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003041 fail_0:
3042 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003043 return res;
3044}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003045
3046/* OS/2 supports spawnvp & spawnvpe natively */
3047#if defined(PYOS_OS2)
3048PyDoc_STRVAR(posix_spawnvp__doc__,
3049"spawnvp(mode, file, args)\n\n\
3050Execute the program 'file' in a new process, using the environment\n\
3051search path to find the file.\n\
3052\n\
3053 mode: mode of process creation\n\
3054 file: executable file name\n\
3055 args: tuple or list of strings");
3056
3057static PyObject *
3058posix_spawnvp(PyObject *self, PyObject *args)
3059{
3060 char *path;
3061 PyObject *argv;
3062 char **argvlist;
3063 int mode, i, argc;
3064 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003065 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003066
3067 /* spawnvp has three arguments: (mode, path, argv), where
3068 argv is a list or tuple of strings. */
3069
3070 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3071 Py_FileSystemDefaultEncoding,
3072 &path, &argv))
3073 return NULL;
3074 if (PyList_Check(argv)) {
3075 argc = PyList_Size(argv);
3076 getitem = PyList_GetItem;
3077 }
3078 else if (PyTuple_Check(argv)) {
3079 argc = PyTuple_Size(argv);
3080 getitem = PyTuple_GetItem;
3081 }
3082 else {
3083 PyErr_SetString(PyExc_TypeError,
3084 "spawnvp() arg 2 must be a tuple or list");
3085 PyMem_Free(path);
3086 return NULL;
3087 }
3088
3089 argvlist = PyMem_NEW(char *, argc+1);
3090 if (argvlist == NULL) {
3091 PyMem_Free(path);
3092 return PyErr_NoMemory();
3093 }
3094 for (i = 0; i < argc; i++) {
3095 if (!PyArg_Parse((*getitem)(argv, i), "et",
3096 Py_FileSystemDefaultEncoding,
3097 &argvlist[i])) {
3098 free_string_array(argvlist, i);
3099 PyErr_SetString(
3100 PyExc_TypeError,
3101 "spawnvp() arg 2 must contain only strings");
3102 PyMem_Free(path);
3103 return NULL;
3104 }
3105 }
3106 argvlist[argc] = NULL;
3107
3108 Py_BEGIN_ALLOW_THREADS
3109#if defined(PYCC_GCC)
3110 spawnval = spawnvp(mode, path, argvlist);
3111#else
3112 spawnval = _spawnvp(mode, path, argvlist);
3113#endif
3114 Py_END_ALLOW_THREADS
3115
3116 free_string_array(argvlist, argc);
3117 PyMem_Free(path);
3118
3119 if (spawnval == -1)
3120 return posix_error();
3121 else
3122 return Py_BuildValue("l", (long) spawnval);
3123}
3124
3125
3126PyDoc_STRVAR(posix_spawnvpe__doc__,
3127"spawnvpe(mode, file, args, env)\n\n\
3128Execute the program 'file' in a new process, using the environment\n\
3129search path to find the file.\n\
3130\n\
3131 mode: mode of process creation\n\
3132 file: executable file name\n\
3133 args: tuple or list of arguments\n\
3134 env: dictionary of strings mapping to strings");
3135
3136static PyObject *
3137posix_spawnvpe(PyObject *self, PyObject *args)
3138{
3139 char *path;
3140 PyObject *argv, *env;
3141 char **argvlist;
3142 char **envlist;
3143 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3144 int mode, i, pos, argc, envc;
3145 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003146 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003147 int lastarg = 0;
3148
3149 /* spawnvpe has four arguments: (mode, path, argv, env), where
3150 argv is a list or tuple of strings and env is a dictionary
3151 like posix.environ. */
3152
3153 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3154 Py_FileSystemDefaultEncoding,
3155 &path, &argv, &env))
3156 return NULL;
3157 if (PyList_Check(argv)) {
3158 argc = PyList_Size(argv);
3159 getitem = PyList_GetItem;
3160 }
3161 else if (PyTuple_Check(argv)) {
3162 argc = PyTuple_Size(argv);
3163 getitem = PyTuple_GetItem;
3164 }
3165 else {
3166 PyErr_SetString(PyExc_TypeError,
3167 "spawnvpe() arg 2 must be a tuple or list");
3168 goto fail_0;
3169 }
3170 if (!PyMapping_Check(env)) {
3171 PyErr_SetString(PyExc_TypeError,
3172 "spawnvpe() arg 3 must be a mapping object");
3173 goto fail_0;
3174 }
3175
3176 argvlist = PyMem_NEW(char *, argc+1);
3177 if (argvlist == NULL) {
3178 PyErr_NoMemory();
3179 goto fail_0;
3180 }
3181 for (i = 0; i < argc; i++) {
3182 if (!PyArg_Parse((*getitem)(argv, i),
3183 "et;spawnvpe() arg 2 must contain only strings",
3184 Py_FileSystemDefaultEncoding,
3185 &argvlist[i]))
3186 {
3187 lastarg = i;
3188 goto fail_1;
3189 }
3190 }
3191 lastarg = argc;
3192 argvlist[argc] = NULL;
3193
3194 i = PyMapping_Size(env);
3195 if (i < 0)
3196 goto fail_1;
3197 envlist = PyMem_NEW(char *, i + 1);
3198 if (envlist == NULL) {
3199 PyErr_NoMemory();
3200 goto fail_1;
3201 }
3202 envc = 0;
3203 keys = PyMapping_Keys(env);
3204 vals = PyMapping_Values(env);
3205 if (!keys || !vals)
3206 goto fail_2;
3207 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3208 PyErr_SetString(PyExc_TypeError,
3209 "spawnvpe(): env.keys() or env.values() is not a list");
3210 goto fail_2;
3211 }
3212
3213 for (pos = 0; pos < i; pos++) {
3214 char *p, *k, *v;
3215 size_t len;
3216
3217 key = PyList_GetItem(keys, pos);
3218 val = PyList_GetItem(vals, pos);
3219 if (!key || !val)
3220 goto fail_2;
3221
3222 if (!PyArg_Parse(
3223 key,
3224 "s;spawnvpe() arg 3 contains a non-string key",
3225 &k) ||
3226 !PyArg_Parse(
3227 val,
3228 "s;spawnvpe() arg 3 contains a non-string value",
3229 &v))
3230 {
3231 goto fail_2;
3232 }
3233 len = PyString_Size(key) + PyString_Size(val) + 2;
3234 p = PyMem_NEW(char, len);
3235 if (p == NULL) {
3236 PyErr_NoMemory();
3237 goto fail_2;
3238 }
3239 PyOS_snprintf(p, len, "%s=%s", k, v);
3240 envlist[envc++] = p;
3241 }
3242 envlist[envc] = 0;
3243
3244 Py_BEGIN_ALLOW_THREADS
3245#if defined(PYCC_GCC)
3246 spawnval = spawnve(mode, path, argvlist, envlist);
3247#else
3248 spawnval = _spawnve(mode, path, argvlist, envlist);
3249#endif
3250 Py_END_ALLOW_THREADS
3251
3252 if (spawnval == -1)
3253 (void) posix_error();
3254 else
3255 res = Py_BuildValue("l", (long) spawnval);
3256
3257 fail_2:
3258 while (--envc >= 0)
3259 PyMem_DEL(envlist[envc]);
3260 PyMem_DEL(envlist);
3261 fail_1:
3262 free_string_array(argvlist, lastarg);
3263 Py_XDECREF(vals);
3264 Py_XDECREF(keys);
3265 fail_0:
3266 PyMem_Free(path);
3267 return res;
3268}
3269#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003270#endif /* HAVE_SPAWNV */
3271
3272
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003273#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003274PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003275"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003276Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3277\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003278Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003279
3280static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003281posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003282{
Neal Norwitze241ce82003-02-17 18:17:05 +00003283 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003284 if (pid == -1)
3285 return posix_error();
3286 PyOS_AfterFork();
3287 return PyInt_FromLong((long)pid);
3288}
3289#endif
3290
3291
Guido van Rossumad0ee831995-03-01 10:34:45 +00003292#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003293PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003294"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003295Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003296Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003297
Barry Warsaw53699e91996-12-10 23:23:01 +00003298static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003299posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003300{
Neal Norwitze241ce82003-02-17 18:17:05 +00003301 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003302 if (pid == -1)
3303 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003304 if (pid == 0)
3305 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003306 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003307}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003308#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003309
Neal Norwitzb59798b2003-03-21 01:43:31 +00003310/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003311/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3312#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003313#define DEV_PTY_FILE "/dev/ptc"
3314#define HAVE_DEV_PTMX
3315#else
3316#define DEV_PTY_FILE "/dev/ptmx"
3317#endif
3318
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003319#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003320#ifdef HAVE_PTY_H
3321#include <pty.h>
3322#else
3323#ifdef HAVE_LIBUTIL_H
3324#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003325#endif /* HAVE_LIBUTIL_H */
3326#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003327#ifdef HAVE_STROPTS_H
3328#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003329#endif
3330#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003331
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003332#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003333PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003334"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003335Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003336
3337static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003338posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003339{
3340 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003341#ifndef HAVE_OPENPTY
3342 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003343#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003344#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003345 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003346#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003347 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003348#endif
3349#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003350
Thomas Wouters70c21a12000-07-14 14:28:33 +00003351#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003352 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3353 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003354#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003355 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3356 if (slave_name == NULL)
3357 return posix_error();
3358
3359 slave_fd = open(slave_name, O_RDWR);
3360 if (slave_fd < 0)
3361 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003362#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003363 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003364 if (master_fd < 0)
3365 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003366 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003367 /* change permission of slave */
3368 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003369 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003370 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003371 }
3372 /* unlock slave */
3373 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003374 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003375 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003376 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003377 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003378 slave_name = ptsname(master_fd); /* get name of slave */
3379 if (slave_name == NULL)
3380 return posix_error();
3381 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3382 if (slave_fd < 0)
3383 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003384#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003385 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3386 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003387#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003388 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003389#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003390#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003391#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003392
Fred Drake8cef4cf2000-06-28 16:40:38 +00003393 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003394
Fred Drake8cef4cf2000-06-28 16:40:38 +00003395}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003396#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003397
3398#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003399PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003400"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003401Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3402Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003403To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003404
3405static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003406posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003407{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003408 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003409
Fred Drake8cef4cf2000-06-28 16:40:38 +00003410 pid = forkpty(&master_fd, NULL, NULL, NULL);
3411 if (pid == -1)
3412 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003413 if (pid == 0)
3414 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003415 return Py_BuildValue("(ii)", pid, master_fd);
3416}
3417#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003418
Guido van Rossumad0ee831995-03-01 10:34:45 +00003419#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003420PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003421"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003422Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003423
Barry Warsaw53699e91996-12-10 23:23:01 +00003424static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003425posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003426{
Barry Warsaw53699e91996-12-10 23:23:01 +00003427 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003428}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003429#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003430
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003431
Guido van Rossumad0ee831995-03-01 10:34:45 +00003432#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003433PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003434"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003435Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003436
Barry Warsaw53699e91996-12-10 23:23:01 +00003437static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003438posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003439{
Barry Warsaw53699e91996-12-10 23:23:01 +00003440 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003441}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003442#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003443
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003444
Guido van Rossumad0ee831995-03-01 10:34:45 +00003445#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003446PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003447"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003448Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003449
Barry Warsaw53699e91996-12-10 23:23:01 +00003450static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003451posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003452{
Barry Warsaw53699e91996-12-10 23:23:01 +00003453 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003454}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003455#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003456
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003457
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003458PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003459"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003460Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003461
Barry Warsaw53699e91996-12-10 23:23:01 +00003462static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003463posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003464{
Barry Warsaw53699e91996-12-10 23:23:01 +00003465 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003466}
3467
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003468
Fred Drakec9680921999-12-13 16:37:25 +00003469#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003470PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003471"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003472Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003473
3474static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003475posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003476{
3477 PyObject *result = NULL;
3478
Fred Drakec9680921999-12-13 16:37:25 +00003479#ifdef NGROUPS_MAX
3480#define MAX_GROUPS NGROUPS_MAX
3481#else
3482 /* defined to be 16 on Solaris7, so this should be a small number */
3483#define MAX_GROUPS 64
3484#endif
3485 gid_t grouplist[MAX_GROUPS];
3486 int n;
3487
3488 n = getgroups(MAX_GROUPS, grouplist);
3489 if (n < 0)
3490 posix_error();
3491 else {
3492 result = PyList_New(n);
3493 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003494 int i;
3495 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003496 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003497 if (o == NULL) {
3498 Py_DECREF(result);
3499 result = NULL;
3500 break;
3501 }
3502 PyList_SET_ITEM(result, i, o);
3503 }
3504 }
3505 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003506
Fred Drakec9680921999-12-13 16:37:25 +00003507 return result;
3508}
3509#endif
3510
Martin v. Löwis606edc12002-06-13 21:09:11 +00003511#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003512PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003513"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003514Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003515
3516static PyObject *
3517posix_getpgid(PyObject *self, PyObject *args)
3518{
3519 int pid, pgid;
3520 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3521 return NULL;
3522 pgid = getpgid(pid);
3523 if (pgid < 0)
3524 return posix_error();
3525 return PyInt_FromLong((long)pgid);
3526}
3527#endif /* HAVE_GETPGID */
3528
3529
Guido van Rossumb6775db1994-08-01 11:34:53 +00003530#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003531PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003532"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003533Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003534
Barry Warsaw53699e91996-12-10 23:23:01 +00003535static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003536posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003537{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003538#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003539 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003540#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003541 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003542#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003543}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003544#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003545
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003546
Guido van Rossumb6775db1994-08-01 11:34:53 +00003547#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003548PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003549"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003550Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003551
Barry Warsaw53699e91996-12-10 23:23:01 +00003552static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003553posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003554{
Guido van Rossum64933891994-10-20 21:56:42 +00003555#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003556 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003557#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003558 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003559#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003560 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003561 Py_INCREF(Py_None);
3562 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003563}
3564
Guido van Rossumb6775db1994-08-01 11:34:53 +00003565#endif /* HAVE_SETPGRP */
3566
Guido van Rossumad0ee831995-03-01 10:34:45 +00003567#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003568PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003569"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003570Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003571
Barry Warsaw53699e91996-12-10 23:23:01 +00003572static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003573posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003574{
Barry Warsaw53699e91996-12-10 23:23:01 +00003575 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003576}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003577#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003578
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003579
Fred Drake12c6e2d1999-12-14 21:25:03 +00003580#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003581PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003582"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003583Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003584
3585static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003586posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003587{
Neal Norwitze241ce82003-02-17 18:17:05 +00003588 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003589 char *name;
3590 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003591
Fred Drakea30680b2000-12-06 21:24:28 +00003592 errno = 0;
3593 name = getlogin();
3594 if (name == NULL) {
3595 if (errno)
3596 posix_error();
3597 else
3598 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003599 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003600 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003601 else
3602 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003603 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003604
Fred Drake12c6e2d1999-12-14 21:25:03 +00003605 return result;
3606}
3607#endif
3608
Guido van Rossumad0ee831995-03-01 10:34:45 +00003609#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003610PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003611"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003612Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003613
Barry Warsaw53699e91996-12-10 23:23:01 +00003614static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003615posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003616{
Barry Warsaw53699e91996-12-10 23:23:01 +00003617 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003618}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003619#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003620
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003621
Guido van Rossumad0ee831995-03-01 10:34:45 +00003622#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003623PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003624"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003625Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003626
Barry Warsaw53699e91996-12-10 23:23:01 +00003627static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003628posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003629{
3630 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003631 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003632 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003633#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003634 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3635 APIRET rc;
3636 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003637 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003638
3639 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3640 APIRET rc;
3641 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003642 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003643
3644 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003645 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003646#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003647 if (kill(pid, sig) == -1)
3648 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003649#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003650 Py_INCREF(Py_None);
3651 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003652}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003653#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003654
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003655#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003656PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003657"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003658Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003659
3660static PyObject *
3661posix_killpg(PyObject *self, PyObject *args)
3662{
3663 int pgid, sig;
3664 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3665 return NULL;
3666 if (killpg(pgid, sig) == -1)
3667 return posix_error();
3668 Py_INCREF(Py_None);
3669 return Py_None;
3670}
3671#endif
3672
Guido van Rossumc0125471996-06-28 18:55:32 +00003673#ifdef HAVE_PLOCK
3674
3675#ifdef HAVE_SYS_LOCK_H
3676#include <sys/lock.h>
3677#endif
3678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003679PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003680"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003681Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003682
Barry Warsaw53699e91996-12-10 23:23:01 +00003683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003684posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003685{
3686 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003687 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003688 return NULL;
3689 if (plock(op) == -1)
3690 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003691 Py_INCREF(Py_None);
3692 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003693}
3694#endif
3695
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003696
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003697#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003698PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003699"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003700Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003701
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003702#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003703#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003704static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003705async_system(const char *command)
3706{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003707 char errormsg[256], args[1024];
3708 RESULTCODES rcodes;
3709 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003710
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003711 char *shell = getenv("COMSPEC");
3712 if (!shell)
3713 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003714
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003715 /* avoid overflowing the argument buffer */
3716 if (strlen(shell) + 3 + strlen(command) >= 1024)
3717 return ERROR_NOT_ENOUGH_MEMORY
3718
3719 args[0] = '\0';
3720 strcat(args, shell);
3721 strcat(args, "/c ");
3722 strcat(args, command);
3723
3724 /* execute asynchronously, inheriting the environment */
3725 rc = DosExecPgm(errormsg,
3726 sizeof(errormsg),
3727 EXEC_ASYNC,
3728 args,
3729 NULL,
3730 &rcodes,
3731 shell);
3732 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003733}
3734
Guido van Rossumd48f2521997-12-05 22:19:34 +00003735static FILE *
3736popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003737{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003738 int oldfd, tgtfd;
3739 HFILE pipeh[2];
3740 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003741
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003742 /* mode determines which of stdin or stdout is reconnected to
3743 * the pipe to the child
3744 */
3745 if (strchr(mode, 'r') != NULL) {
3746 tgt_fd = 1; /* stdout */
3747 } else if (strchr(mode, 'w')) {
3748 tgt_fd = 0; /* stdin */
3749 } else {
3750 *err = ERROR_INVALID_ACCESS;
3751 return NULL;
3752 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003753
Andrew MacIntyrea3be2582004-12-18 09:51:05 +00003754 /* setup the pipe */
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003755 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
3756 *err = rc;
3757 return NULL;
3758 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003759
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003760 /* prevent other threads accessing stdio */
3761 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003762
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003763 /* reconnect stdio and execute child */
3764 oldfd = dup(tgtfd);
3765 close(tgtfd);
3766 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
3767 DosClose(pipeh[tgtfd]);
3768 rc = async_system(command);
3769 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003770
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003771 /* restore stdio */
3772 dup2(oldfd, tgtfd);
3773 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003774
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003775 /* allow other threads access to stdio */
3776 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003777
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003778 /* if execution of child was successful return file stream */
3779 if (rc == NO_ERROR)
3780 return fdopen(pipeh[1 - tgtfd], mode);
3781 else {
3782 DosClose(pipeh[1 - tgtfd]);
3783 *err = rc;
3784 return NULL;
3785 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003786}
3787
3788static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003789posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003790{
3791 char *name;
3792 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003793 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003794 FILE *fp;
3795 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003796 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003797 return NULL;
3798 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003799 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003800 Py_END_ALLOW_THREADS
3801 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003802 return os2_error(err);
3803
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003804 f = PyFile_FromFile(fp, name, mode, fclose);
3805 if (f != NULL)
3806 PyFile_SetBufSize(f, bufsize);
3807 return f;
3808}
3809
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003810#elif defined(PYCC_GCC)
3811
3812/* standard posix version of popen() support */
3813static PyObject *
3814posix_popen(PyObject *self, PyObject *args)
3815{
3816 char *name;
3817 char *mode = "r";
3818 int bufsize = -1;
3819 FILE *fp;
3820 PyObject *f;
3821 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3822 return NULL;
3823 Py_BEGIN_ALLOW_THREADS
3824 fp = popen(name, mode);
3825 Py_END_ALLOW_THREADS
3826 if (fp == NULL)
3827 return posix_error();
3828 f = PyFile_FromFile(fp, name, mode, pclose);
3829 if (f != NULL)
3830 PyFile_SetBufSize(f, bufsize);
3831 return f;
3832}
3833
3834/* fork() under OS/2 has lots'o'warts
3835 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
3836 * most of this code is a ripoff of the win32 code, but using the
3837 * capabilities of EMX's C library routines
3838 */
3839
3840/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
3841#define POPEN_1 1
3842#define POPEN_2 2
3843#define POPEN_3 3
3844#define POPEN_4 4
3845
3846static PyObject *_PyPopen(char *, int, int, int);
3847static int _PyPclose(FILE *file);
3848
3849/*
3850 * Internal dictionary mapping popen* file pointers to process handles,
3851 * for use when retrieving the process exit code. See _PyPclose() below
3852 * for more information on this dictionary's use.
3853 */
3854static PyObject *_PyPopenProcs = NULL;
3855
3856/* os2emx version of popen2()
3857 *
3858 * The result of this function is a pipe (file) connected to the
3859 * process's stdin, and a pipe connected to the process's stdout.
3860 */
3861
3862static PyObject *
3863os2emx_popen2(PyObject *self, PyObject *args)
3864{
3865 PyObject *f;
3866 int tm=0;
3867
3868 char *cmdstring;
3869 char *mode = "t";
3870 int bufsize = -1;
3871 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
3872 return NULL;
3873
3874 if (*mode == 't')
3875 tm = O_TEXT;
3876 else if (*mode != 'b') {
3877 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3878 return NULL;
3879 } else
3880 tm = O_BINARY;
3881
3882 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
3883
3884 return f;
3885}
3886
3887/*
3888 * Variation on os2emx.popen2
3889 *
3890 * The result of this function is 3 pipes - the process's stdin,
3891 * stdout and stderr
3892 */
3893
3894static PyObject *
3895os2emx_popen3(PyObject *self, PyObject *args)
3896{
3897 PyObject *f;
3898 int tm = 0;
3899
3900 char *cmdstring;
3901 char *mode = "t";
3902 int bufsize = -1;
3903 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
3904 return NULL;
3905
3906 if (*mode == 't')
3907 tm = O_TEXT;
3908 else if (*mode != 'b') {
3909 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3910 return NULL;
3911 } else
3912 tm = O_BINARY;
3913
3914 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
3915
3916 return f;
3917}
3918
3919/*
3920 * Variation on os2emx.popen2
3921 *
Tim Peters11b23062003-04-23 02:39:17 +00003922 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003923 * and stdout+stderr combined as a single pipe.
3924 */
3925
3926static PyObject *
3927os2emx_popen4(PyObject *self, PyObject *args)
3928{
3929 PyObject *f;
3930 int tm = 0;
3931
3932 char *cmdstring;
3933 char *mode = "t";
3934 int bufsize = -1;
3935 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
3936 return NULL;
3937
3938 if (*mode == 't')
3939 tm = O_TEXT;
3940 else if (*mode != 'b') {
3941 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3942 return NULL;
3943 } else
3944 tm = O_BINARY;
3945
3946 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
3947
3948 return f;
3949}
3950
3951/* a couple of structures for convenient handling of multiple
3952 * file handles and pipes
3953 */
3954struct file_ref
3955{
3956 int handle;
3957 int flags;
3958};
3959
3960struct pipe_ref
3961{
3962 int rd;
3963 int wr;
3964};
3965
3966/* The following code is derived from the win32 code */
3967
3968static PyObject *
3969_PyPopen(char *cmdstring, int mode, int n, int bufsize)
3970{
3971 struct file_ref stdio[3];
3972 struct pipe_ref p_fd[3];
3973 FILE *p_s[3];
3974 int file_count, i, pipe_err, pipe_pid;
3975 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
3976 PyObject *f, *p_f[3];
3977
3978 /* file modes for subsequent fdopen's on pipe handles */
3979 if (mode == O_TEXT)
3980 {
3981 rd_mode = "rt";
3982 wr_mode = "wt";
3983 }
3984 else
3985 {
3986 rd_mode = "rb";
3987 wr_mode = "wb";
3988 }
3989
3990 /* prepare shell references */
3991 if ((shell = getenv("EMXSHELL")) == NULL)
3992 if ((shell = getenv("COMSPEC")) == NULL)
3993 {
3994 errno = ENOENT;
3995 return posix_error();
3996 }
3997
3998 sh_name = _getname(shell);
3999 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4000 opt = "/c";
4001 else
4002 opt = "-c";
4003
4004 /* save current stdio fds + their flags, and set not inheritable */
4005 i = pipe_err = 0;
4006 while (pipe_err >= 0 && i < 3)
4007 {
4008 pipe_err = stdio[i].handle = dup(i);
4009 stdio[i].flags = fcntl(i, F_GETFD, 0);
4010 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4011 i++;
4012 }
4013 if (pipe_err < 0)
4014 {
4015 /* didn't get them all saved - clean up and bail out */
4016 int saved_err = errno;
4017 while (i-- > 0)
4018 {
4019 close(stdio[i].handle);
4020 }
4021 errno = saved_err;
4022 return posix_error();
4023 }
4024
4025 /* create pipe ends */
4026 file_count = 2;
4027 if (n == POPEN_3)
4028 file_count = 3;
4029 i = pipe_err = 0;
4030 while ((pipe_err == 0) && (i < file_count))
4031 pipe_err = pipe((int *)&p_fd[i++]);
4032 if (pipe_err < 0)
4033 {
4034 /* didn't get them all made - clean up and bail out */
4035 while (i-- > 0)
4036 {
4037 close(p_fd[i].wr);
4038 close(p_fd[i].rd);
4039 }
4040 errno = EPIPE;
4041 return posix_error();
4042 }
4043
4044 /* change the actual standard IO streams over temporarily,
4045 * making the retained pipe ends non-inheritable
4046 */
4047 pipe_err = 0;
4048
4049 /* - stdin */
4050 if (dup2(p_fd[0].rd, 0) == 0)
4051 {
4052 close(p_fd[0].rd);
4053 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4054 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4055 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4056 {
4057 close(p_fd[0].wr);
4058 pipe_err = -1;
4059 }
4060 }
4061 else
4062 {
4063 pipe_err = -1;
4064 }
4065
4066 /* - stdout */
4067 if (pipe_err == 0)
4068 {
4069 if (dup2(p_fd[1].wr, 1) == 1)
4070 {
4071 close(p_fd[1].wr);
4072 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4073 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4074 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4075 {
4076 close(p_fd[1].rd);
4077 pipe_err = -1;
4078 }
4079 }
4080 else
4081 {
4082 pipe_err = -1;
4083 }
4084 }
4085
4086 /* - stderr, as required */
4087 if (pipe_err == 0)
4088 switch (n)
4089 {
4090 case POPEN_3:
4091 {
4092 if (dup2(p_fd[2].wr, 2) == 2)
4093 {
4094 close(p_fd[2].wr);
4095 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4096 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4097 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4098 {
4099 close(p_fd[2].rd);
4100 pipe_err = -1;
4101 }
4102 }
4103 else
4104 {
4105 pipe_err = -1;
4106 }
4107 break;
4108 }
4109
4110 case POPEN_4:
4111 {
4112 if (dup2(1, 2) != 2)
4113 {
4114 pipe_err = -1;
4115 }
4116 break;
4117 }
4118 }
4119
4120 /* spawn the child process */
4121 if (pipe_err == 0)
4122 {
4123 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4124 if (pipe_pid == -1)
4125 {
4126 pipe_err = -1;
4127 }
4128 else
4129 {
4130 /* save the PID into the FILE structure
4131 * NOTE: this implementation doesn't actually
4132 * take advantage of this, but do it for
4133 * completeness - AIM Apr01
4134 */
4135 for (i = 0; i < file_count; i++)
4136 p_s[i]->_pid = pipe_pid;
4137 }
4138 }
4139
4140 /* reset standard IO to normal */
4141 for (i = 0; i < 3; i++)
4142 {
4143 dup2(stdio[i].handle, i);
4144 fcntl(i, F_SETFD, stdio[i].flags);
4145 close(stdio[i].handle);
4146 }
4147
4148 /* if any remnant problems, clean up and bail out */
4149 if (pipe_err < 0)
4150 {
4151 for (i = 0; i < 3; i++)
4152 {
4153 close(p_fd[i].rd);
4154 close(p_fd[i].wr);
4155 }
4156 errno = EPIPE;
4157 return posix_error_with_filename(cmdstring);
4158 }
4159
4160 /* build tuple of file objects to return */
4161 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4162 PyFile_SetBufSize(p_f[0], bufsize);
4163 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4164 PyFile_SetBufSize(p_f[1], bufsize);
4165 if (n == POPEN_3)
4166 {
4167 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4168 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004169 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004170 }
4171 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004172 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004173
4174 /*
4175 * Insert the files we've created into the process dictionary
4176 * all referencing the list with the process handle and the
4177 * initial number of files (see description below in _PyPclose).
4178 * Since if _PyPclose later tried to wait on a process when all
4179 * handles weren't closed, it could create a deadlock with the
4180 * child, we spend some energy here to try to ensure that we
4181 * either insert all file handles into the dictionary or none
4182 * at all. It's a little clumsy with the various popen modes
4183 * and variable number of files involved.
4184 */
4185 if (!_PyPopenProcs)
4186 {
4187 _PyPopenProcs = PyDict_New();
4188 }
4189
4190 if (_PyPopenProcs)
4191 {
4192 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4193 int ins_rc[3];
4194
4195 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4196 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4197
4198 procObj = PyList_New(2);
4199 pidObj = PyInt_FromLong((long) pipe_pid);
4200 intObj = PyInt_FromLong((long) file_count);
4201
4202 if (procObj && pidObj && intObj)
4203 {
4204 PyList_SetItem(procObj, 0, pidObj);
4205 PyList_SetItem(procObj, 1, intObj);
4206
4207 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4208 if (fileObj[0])
4209 {
4210 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4211 fileObj[0],
4212 procObj);
4213 }
4214 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4215 if (fileObj[1])
4216 {
4217 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4218 fileObj[1],
4219 procObj);
4220 }
4221 if (file_count >= 3)
4222 {
4223 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4224 if (fileObj[2])
4225 {
4226 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4227 fileObj[2],
4228 procObj);
4229 }
4230 }
4231
4232 if (ins_rc[0] < 0 || !fileObj[0] ||
4233 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4234 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4235 {
4236 /* Something failed - remove any dictionary
4237 * entries that did make it.
4238 */
4239 if (!ins_rc[0] && fileObj[0])
4240 {
4241 PyDict_DelItem(_PyPopenProcs,
4242 fileObj[0]);
4243 }
4244 if (!ins_rc[1] && fileObj[1])
4245 {
4246 PyDict_DelItem(_PyPopenProcs,
4247 fileObj[1]);
4248 }
4249 if (!ins_rc[2] && fileObj[2])
4250 {
4251 PyDict_DelItem(_PyPopenProcs,
4252 fileObj[2]);
4253 }
4254 }
4255 }
Tim Peters11b23062003-04-23 02:39:17 +00004256
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004257 /*
4258 * Clean up our localized references for the dictionary keys
4259 * and value since PyDict_SetItem will Py_INCREF any copies
4260 * that got placed in the dictionary.
4261 */
4262 Py_XDECREF(procObj);
4263 Py_XDECREF(fileObj[0]);
4264 Py_XDECREF(fileObj[1]);
4265 Py_XDECREF(fileObj[2]);
4266 }
4267
4268 /* Child is launched. */
4269 return f;
4270}
4271
4272/*
4273 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4274 * exit code for the child process and return as a result of the close.
4275 *
4276 * This function uses the _PyPopenProcs dictionary in order to map the
4277 * input file pointer to information about the process that was
4278 * originally created by the popen* call that created the file pointer.
4279 * The dictionary uses the file pointer as a key (with one entry
4280 * inserted for each file returned by the original popen* call) and a
4281 * single list object as the value for all files from a single call.
4282 * The list object contains the Win32 process handle at [0], and a file
4283 * count at [1], which is initialized to the total number of file
4284 * handles using that list.
4285 *
4286 * This function closes whichever handle it is passed, and decrements
4287 * the file count in the dictionary for the process handle pointed to
4288 * by this file. On the last close (when the file count reaches zero),
4289 * this function will wait for the child process and then return its
4290 * exit code as the result of the close() operation. This permits the
4291 * files to be closed in any order - it is always the close() of the
4292 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004293 *
4294 * NOTE: This function is currently called with the GIL released.
4295 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004296 */
4297
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004298static int _PyPclose(FILE *file)
4299{
4300 int result;
4301 int exit_code;
4302 int pipe_pid;
4303 PyObject *procObj, *pidObj, *intObj, *fileObj;
4304 int file_count;
4305#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004306 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004307#endif
4308
4309 /* Close the file handle first, to ensure it can't block the
4310 * child from exiting if it's the last handle.
4311 */
4312 result = fclose(file);
4313
4314#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004315 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004316#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004317 if (_PyPopenProcs)
4318 {
4319 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4320 (procObj = PyDict_GetItem(_PyPopenProcs,
4321 fileObj)) != NULL &&
4322 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4323 (intObj = PyList_GetItem(procObj,1)) != NULL)
4324 {
4325 pipe_pid = (int) PyInt_AsLong(pidObj);
4326 file_count = (int) PyInt_AsLong(intObj);
4327
4328 if (file_count > 1)
4329 {
4330 /* Still other files referencing process */
4331 file_count--;
4332 PyList_SetItem(procObj,1,
4333 PyInt_FromLong((long) file_count));
4334 }
4335 else
4336 {
4337 /* Last file for this process */
4338 if (result != EOF &&
4339 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4340 {
4341 /* extract exit status */
4342 if (WIFEXITED(exit_code))
4343 {
4344 result = WEXITSTATUS(exit_code);
4345 }
4346 else
4347 {
4348 errno = EPIPE;
4349 result = -1;
4350 }
4351 }
4352 else
4353 {
4354 /* Indicate failure - this will cause the file object
4355 * to raise an I/O error and translate the last
4356 * error code from errno. We do have a problem with
4357 * last errors that overlap the normal errno table,
4358 * but that's a consistent problem with the file object.
4359 */
4360 result = -1;
4361 }
4362 }
4363
4364 /* Remove this file pointer from dictionary */
4365 PyDict_DelItem(_PyPopenProcs, fileObj);
4366
4367 if (PyDict_Size(_PyPopenProcs) == 0)
4368 {
4369 Py_DECREF(_PyPopenProcs);
4370 _PyPopenProcs = NULL;
4371 }
4372
4373 } /* if object retrieval ok */
4374
4375 Py_XDECREF(fileObj);
4376 } /* if _PyPopenProcs */
4377
4378#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004379 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004380#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004381 return result;
4382}
4383
4384#endif /* PYCC_??? */
4385
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004386#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004387
4388/*
4389 * Portable 'popen' replacement for Win32.
4390 *
4391 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4392 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004393 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004394 */
4395
4396#include <malloc.h>
4397#include <io.h>
4398#include <fcntl.h>
4399
4400/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4401#define POPEN_1 1
4402#define POPEN_2 2
4403#define POPEN_3 3
4404#define POPEN_4 4
4405
4406static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004407static int _PyPclose(FILE *file);
4408
4409/*
4410 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004411 * for use when retrieving the process exit code. See _PyPclose() below
4412 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004413 */
4414static PyObject *_PyPopenProcs = NULL;
4415
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004416
4417/* popen that works from a GUI.
4418 *
4419 * The result of this function is a pipe (file) connected to the
4420 * processes stdin or stdout, depending on the requested mode.
4421 */
4422
4423static PyObject *
4424posix_popen(PyObject *self, PyObject *args)
4425{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00004426 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004427 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004428
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004429 char *cmdstring;
4430 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004431 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004432 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004433 return NULL;
4434
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004435 if (*mode == 'r')
4436 tm = _O_RDONLY;
4437 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00004438 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004439 return NULL;
4440 } else
4441 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004442
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004443 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004444 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004445 return NULL;
4446 }
4447
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004448 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004449 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004450 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004451 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004452 else
4453 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4454
4455 return f;
4456}
4457
4458/* Variation on win32pipe.popen
4459 *
4460 * The result of this function is a pipe (file) connected to the
4461 * process's stdin, and a pipe connected to the process's stdout.
4462 */
4463
4464static PyObject *
4465win32_popen2(PyObject *self, PyObject *args)
4466{
4467 PyObject *f;
4468 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004469
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004470 char *cmdstring;
4471 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004472 int bufsize = -1;
4473 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004474 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004475
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004476 if (*mode == 't')
4477 tm = _O_TEXT;
4478 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004479 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004480 return NULL;
4481 } else
4482 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004483
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004484 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004485 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004486 return NULL;
4487 }
4488
4489 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004490
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004491 return f;
4492}
4493
4494/*
4495 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004496 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004497 * The result of this function is 3 pipes - the process's stdin,
4498 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004499 */
4500
4501static PyObject *
4502win32_popen3(PyObject *self, PyObject *args)
4503{
4504 PyObject *f;
4505 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004506
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004507 char *cmdstring;
4508 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004509 int bufsize = -1;
4510 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004511 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004512
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004513 if (*mode == 't')
4514 tm = _O_TEXT;
4515 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004516 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004517 return NULL;
4518 } else
4519 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004520
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004521 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004522 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004523 return NULL;
4524 }
4525
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004526 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004527
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004528 return f;
4529}
4530
4531/*
4532 * Variation on win32pipe.popen
4533 *
Tim Peters5aa91602002-01-30 05:46:57 +00004534 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004535 * and stdout+stderr combined as a single pipe.
4536 */
4537
4538static PyObject *
4539win32_popen4(PyObject *self, PyObject *args)
4540{
4541 PyObject *f;
4542 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004543
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004544 char *cmdstring;
4545 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004546 int bufsize = -1;
4547 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004548 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004549
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004550 if (*mode == 't')
4551 tm = _O_TEXT;
4552 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004553 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004554 return NULL;
4555 } else
4556 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004557
4558 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004559 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004560 return NULL;
4561 }
4562
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004563 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004564
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004565 return f;
4566}
4567
Mark Hammond08501372001-01-31 07:30:29 +00004568static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004569_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004570 HANDLE hStdin,
4571 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004572 HANDLE hStderr,
4573 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004574{
4575 PROCESS_INFORMATION piProcInfo;
4576 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004577 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004578 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004579 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004580 int i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004581 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004582
4583 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004584 char *comshell;
4585
Tim Peters92e4dd82002-10-05 01:47:34 +00004586 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004587 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
Martin v. Löwis18e16552006-02-15 17:27:45 +00004588 /* x < i, so x fits into an integer */
4589 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00004590
4591 /* Explicitly check if we are using COMMAND.COM. If we are
4592 * then use the w9xpopen hack.
4593 */
4594 comshell = s1 + x;
4595 while (comshell >= s1 && *comshell != '\\')
4596 --comshell;
4597 ++comshell;
4598
4599 if (GetVersion() < 0x80000000 &&
4600 _stricmp(comshell, "command.com") != 0) {
4601 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004602 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004603 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004604 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004605 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004606 }
4607 else {
4608 /*
Tim Peters402d5982001-08-27 06:37:48 +00004609 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4610 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004611 */
Mark Hammond08501372001-01-31 07:30:29 +00004612 char modulepath[_MAX_PATH];
4613 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004614 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
Thomas Wouters477c8d52006-05-27 19:21:47 +00004615 for (x = i = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004616 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004617 x = i+1;
4618 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004619 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004620 strncat(modulepath,
4621 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004622 (sizeof(modulepath)/sizeof(modulepath[0]))
4623 -strlen(modulepath));
4624 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004625 /* Eeek - file-not-found - possibly an embedding
4626 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004627 */
Tim Peters5aa91602002-01-30 05:46:57 +00004628 strncpy(modulepath,
4629 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00004630 sizeof(modulepath)/sizeof(modulepath[0]));
4631 if (modulepath[strlen(modulepath)-1] != '\\')
4632 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004633 strncat(modulepath,
4634 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004635 (sizeof(modulepath)/sizeof(modulepath[0]))
4636 -strlen(modulepath));
4637 /* No where else to look - raise an easily identifiable
4638 error, rather than leaving Windows to report
4639 "file not found" - as the user is probably blissfully
4640 unaware this shim EXE is used, and it will confuse them.
4641 (well, it confused me for a while ;-)
4642 */
4643 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004644 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004645 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004646 "for popen to work with your shell "
4647 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004648 szConsoleSpawn);
4649 return FALSE;
4650 }
4651 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004652 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004653 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004654 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004655
Tim Peters92e4dd82002-10-05 01:47:34 +00004656 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004657 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004658 /* To maintain correct argument passing semantics,
4659 we pass the command-line as it stands, and allow
4660 quoting to be applied. w9xpopen.exe will then
4661 use its argv vector, and re-quote the necessary
4662 args for the ultimate child process.
4663 */
Tim Peters75cdad52001-11-28 22:07:30 +00004664 PyOS_snprintf(
4665 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004666 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004667 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004668 s1,
4669 s3,
4670 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004671 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004672 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004673 dialog:
4674 "Your program accessed mem currently in use at xxx"
4675 and a hopeful warning about the stability of your
4676 system.
4677 Cost is Ctrl+C wont kill children, but anyone
4678 who cares can have a go!
4679 */
4680 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004681 }
4682 }
4683
4684 /* Could be an else here to try cmd.exe / command.com in the path
4685 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004686 else {
Tim Peters402d5982001-08-27 06:37:48 +00004687 PyErr_SetString(PyExc_RuntimeError,
4688 "Cannot locate a COMSPEC environment variable to "
4689 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004690 return FALSE;
4691 }
Tim Peters5aa91602002-01-30 05:46:57 +00004692
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004693 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4694 siStartInfo.cb = sizeof(STARTUPINFO);
4695 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4696 siStartInfo.hStdInput = hStdin;
4697 siStartInfo.hStdOutput = hStdout;
4698 siStartInfo.hStdError = hStderr;
4699 siStartInfo.wShowWindow = SW_HIDE;
4700
4701 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004702 s2,
4703 NULL,
4704 NULL,
4705 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004706 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004707 NULL,
4708 NULL,
4709 &siStartInfo,
4710 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004711 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004712 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004713
Mark Hammondb37a3732000-08-14 04:47:33 +00004714 /* Return process handle */
4715 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004716 return TRUE;
4717 }
Tim Peters402d5982001-08-27 06:37:48 +00004718 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004719 return FALSE;
4720}
4721
4722/* The following code is based off of KB: Q190351 */
4723
4724static PyObject *
4725_PyPopen(char *cmdstring, int mode, int n)
4726{
4727 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4728 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004729 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004730
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004731 SECURITY_ATTRIBUTES saAttr;
4732 BOOL fSuccess;
4733 int fd1, fd2, fd3;
4734 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004735 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004736 PyObject *f;
4737
4738 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4739 saAttr.bInheritHandle = TRUE;
4740 saAttr.lpSecurityDescriptor = NULL;
4741
4742 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4743 return win32_error("CreatePipe", NULL);
4744
4745 /* Create new output read handle and the input write handle. Set
4746 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004747 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004748 * being created. */
4749 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004750 GetCurrentProcess(), &hChildStdinWrDup, 0,
4751 FALSE,
4752 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004753 if (!fSuccess)
4754 return win32_error("DuplicateHandle", NULL);
4755
4756 /* Close the inheritable version of ChildStdin
4757 that we're using. */
4758 CloseHandle(hChildStdinWr);
4759
4760 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4761 return win32_error("CreatePipe", NULL);
4762
4763 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004764 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4765 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004766 if (!fSuccess)
4767 return win32_error("DuplicateHandle", NULL);
4768
4769 /* Close the inheritable version of ChildStdout
4770 that we're using. */
4771 CloseHandle(hChildStdoutRd);
4772
4773 if (n != POPEN_4) {
4774 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4775 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004776 fSuccess = DuplicateHandle(GetCurrentProcess(),
4777 hChildStderrRd,
4778 GetCurrentProcess(),
4779 &hChildStderrRdDup, 0,
4780 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004781 if (!fSuccess)
4782 return win32_error("DuplicateHandle", NULL);
4783 /* Close the inheritable version of ChildStdErr that we're using. */
4784 CloseHandle(hChildStderrRd);
4785 }
Tim Peters5aa91602002-01-30 05:46:57 +00004786
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004787 switch (n) {
4788 case POPEN_1:
4789 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4790 case _O_WRONLY | _O_TEXT:
4791 /* Case for writing to child Stdin in text mode. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00004792 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004793 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004794 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004795 PyFile_SetBufSize(f, 0);
4796 /* We don't care about these pipes anymore, so close them. */
4797 CloseHandle(hChildStdoutRdDup);
4798 CloseHandle(hChildStderrRdDup);
4799 break;
4800
4801 case _O_RDONLY | _O_TEXT:
4802 /* Case for reading from child Stdout in text mode. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00004803 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004804 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004805 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004806 PyFile_SetBufSize(f, 0);
4807 /* We don't care about these pipes anymore, so close them. */
4808 CloseHandle(hChildStdinWrDup);
4809 CloseHandle(hChildStderrRdDup);
4810 break;
4811
4812 case _O_RDONLY | _O_BINARY:
4813 /* Case for readinig from child Stdout in binary mode. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00004814 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004815 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004816 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004817 PyFile_SetBufSize(f, 0);
4818 /* We don't care about these pipes anymore, so close them. */
4819 CloseHandle(hChildStdinWrDup);
4820 CloseHandle(hChildStderrRdDup);
4821 break;
4822
4823 case _O_WRONLY | _O_BINARY:
4824 /* Case for writing to child Stdin in binary mode. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00004825 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004826 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004827 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004828 PyFile_SetBufSize(f, 0);
4829 /* We don't care about these pipes anymore, so close them. */
4830 CloseHandle(hChildStdoutRdDup);
4831 CloseHandle(hChildStderrRdDup);
4832 break;
4833 }
Mark Hammondb37a3732000-08-14 04:47:33 +00004834 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004835 break;
Tim Peters5aa91602002-01-30 05:46:57 +00004836
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004837 case POPEN_2:
4838 case POPEN_4:
4839 {
4840 char *m1, *m2;
4841 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00004842
Tim Peters7dca21e2002-08-19 00:42:29 +00004843 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004844 m1 = "r";
4845 m2 = "w";
4846 } else {
4847 m1 = "rb";
4848 m2 = "wb";
4849 }
4850
Thomas Wouters477c8d52006-05-27 19:21:47 +00004851 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004852 f1 = _fdopen(fd1, m2);
Thomas Wouters477c8d52006-05-27 19:21:47 +00004853 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004854 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004855 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004856 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00004857 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004858 PyFile_SetBufSize(p2, 0);
4859
4860 if (n != 4)
4861 CloseHandle(hChildStderrRdDup);
4862
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004863 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00004864 Py_XDECREF(p1);
4865 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00004866 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004867 break;
4868 }
Tim Peters5aa91602002-01-30 05:46:57 +00004869
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004870 case POPEN_3:
4871 {
4872 char *m1, *m2;
4873 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00004874
Tim Peters7dca21e2002-08-19 00:42:29 +00004875 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004876 m1 = "r";
4877 m2 = "w";
4878 } else {
4879 m1 = "rb";
4880 m2 = "wb";
4881 }
4882
Thomas Wouters477c8d52006-05-27 19:21:47 +00004883 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004884 f1 = _fdopen(fd1, m2);
Thomas Wouters477c8d52006-05-27 19:21:47 +00004885 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004886 f2 = _fdopen(fd2, m1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00004887 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004888 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004889 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00004890 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
4891 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004892 PyFile_SetBufSize(p1, 0);
4893 PyFile_SetBufSize(p2, 0);
4894 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004895 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00004896 Py_XDECREF(p1);
4897 Py_XDECREF(p2);
4898 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00004899 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004900 break;
4901 }
4902 }
4903
4904 if (n == POPEN_4) {
4905 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004906 hChildStdinRd,
4907 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004908 hChildStdoutWr,
4909 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004910 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004911 }
4912 else {
4913 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004914 hChildStdinRd,
4915 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004916 hChildStderrWr,
4917 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004918 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004919 }
4920
Mark Hammondb37a3732000-08-14 04:47:33 +00004921 /*
4922 * Insert the files we've created into the process dictionary
4923 * all referencing the list with the process handle and the
4924 * initial number of files (see description below in _PyPclose).
4925 * Since if _PyPclose later tried to wait on a process when all
4926 * handles weren't closed, it could create a deadlock with the
4927 * child, we spend some energy here to try to ensure that we
4928 * either insert all file handles into the dictionary or none
4929 * at all. It's a little clumsy with the various popen modes
4930 * and variable number of files involved.
4931 */
4932 if (!_PyPopenProcs) {
4933 _PyPopenProcs = PyDict_New();
4934 }
4935
4936 if (_PyPopenProcs) {
4937 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
4938 int ins_rc[3];
4939
4940 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4941 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4942
4943 procObj = PyList_New(2);
4944 hProcessObj = PyLong_FromVoidPtr(hProcess);
4945 intObj = PyInt_FromLong(file_count);
4946
4947 if (procObj && hProcessObj && intObj) {
4948 PyList_SetItem(procObj,0,hProcessObj);
4949 PyList_SetItem(procObj,1,intObj);
4950
4951 fileObj[0] = PyLong_FromVoidPtr(f1);
4952 if (fileObj[0]) {
4953 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4954 fileObj[0],
4955 procObj);
4956 }
4957 if (file_count >= 2) {
4958 fileObj[1] = PyLong_FromVoidPtr(f2);
4959 if (fileObj[1]) {
4960 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4961 fileObj[1],
4962 procObj);
4963 }
4964 }
4965 if (file_count >= 3) {
4966 fileObj[2] = PyLong_FromVoidPtr(f3);
4967 if (fileObj[2]) {
4968 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4969 fileObj[2],
4970 procObj);
4971 }
4972 }
4973
4974 if (ins_rc[0] < 0 || !fileObj[0] ||
4975 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4976 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
4977 /* Something failed - remove any dictionary
4978 * entries that did make it.
4979 */
4980 if (!ins_rc[0] && fileObj[0]) {
4981 PyDict_DelItem(_PyPopenProcs,
4982 fileObj[0]);
4983 }
4984 if (!ins_rc[1] && fileObj[1]) {
4985 PyDict_DelItem(_PyPopenProcs,
4986 fileObj[1]);
4987 }
4988 if (!ins_rc[2] && fileObj[2]) {
4989 PyDict_DelItem(_PyPopenProcs,
4990 fileObj[2]);
4991 }
4992 }
4993 }
Tim Peters5aa91602002-01-30 05:46:57 +00004994
Mark Hammondb37a3732000-08-14 04:47:33 +00004995 /*
4996 * Clean up our localized references for the dictionary keys
4997 * and value since PyDict_SetItem will Py_INCREF any copies
4998 * that got placed in the dictionary.
4999 */
5000 Py_XDECREF(procObj);
5001 Py_XDECREF(fileObj[0]);
5002 Py_XDECREF(fileObj[1]);
5003 Py_XDECREF(fileObj[2]);
5004 }
5005
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005006 /* Child is launched. Close the parents copy of those pipe
5007 * handles that only the child should have open. You need to
5008 * make sure that no handles to the write end of the output pipe
5009 * are maintained in this process or else the pipe will not close
5010 * when the child process exits and the ReadFile will hang. */
5011
5012 if (!CloseHandle(hChildStdinRd))
5013 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005014
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005015 if (!CloseHandle(hChildStdoutWr))
5016 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005017
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005018 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5019 return win32_error("CloseHandle", NULL);
5020
5021 return f;
5022}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005023
5024/*
5025 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5026 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005027 *
5028 * This function uses the _PyPopenProcs dictionary in order to map the
5029 * input file pointer to information about the process that was
5030 * originally created by the popen* call that created the file pointer.
5031 * The dictionary uses the file pointer as a key (with one entry
5032 * inserted for each file returned by the original popen* call) and a
5033 * single list object as the value for all files from a single call.
5034 * The list object contains the Win32 process handle at [0], and a file
5035 * count at [1], which is initialized to the total number of file
5036 * handles using that list.
5037 *
5038 * This function closes whichever handle it is passed, and decrements
5039 * the file count in the dictionary for the process handle pointed to
5040 * by this file. On the last close (when the file count reaches zero),
5041 * this function will wait for the child process and then return its
5042 * exit code as the result of the close() operation. This permits the
5043 * files to be closed in any order - it is always the close() of the
5044 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005045 *
5046 * NOTE: This function is currently called with the GIL released.
5047 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005048 */
Tim Peters736aa322000-09-01 06:51:24 +00005049
Fredrik Lundh56055a42000-07-23 19:47:12 +00005050static int _PyPclose(FILE *file)
5051{
Fredrik Lundh20318932000-07-26 17:29:12 +00005052 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005053 DWORD exit_code;
5054 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00005055 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5056 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005057#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005058 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005059#endif
5060
Fredrik Lundh20318932000-07-26 17:29:12 +00005061 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00005062 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00005063 */
5064 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005065#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005066 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005067#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005068 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005069 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5070 (procObj = PyDict_GetItem(_PyPopenProcs,
5071 fileObj)) != NULL &&
5072 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5073 (intObj = PyList_GetItem(procObj,1)) != NULL) {
5074
5075 hProcess = PyLong_AsVoidPtr(hProcessObj);
5076 file_count = PyInt_AsLong(intObj);
5077
5078 if (file_count > 1) {
5079 /* Still other files referencing process */
5080 file_count--;
5081 PyList_SetItem(procObj,1,
5082 PyInt_FromLong(file_count));
5083 } else {
5084 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00005085 if (result != EOF &&
5086 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5087 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00005088 /* Possible truncation here in 16-bit environments, but
5089 * real exit codes are just the lower byte in any event.
5090 */
5091 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005092 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00005093 /* Indicate failure - this will cause the file object
5094 * to raise an I/O error and translate the last Win32
5095 * error code from errno. We do have a problem with
5096 * last errors that overlap the normal errno table,
5097 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005098 */
Fredrik Lundh20318932000-07-26 17:29:12 +00005099 if (result != EOF) {
5100 /* If the error wasn't from the fclose(), then
5101 * set errno for the file object error handling.
5102 */
5103 errno = GetLastError();
5104 }
5105 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005106 }
5107
5108 /* Free up the native handle at this point */
5109 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00005110 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005111
Mark Hammondb37a3732000-08-14 04:47:33 +00005112 /* Remove this file pointer from dictionary */
5113 PyDict_DelItem(_PyPopenProcs, fileObj);
5114
5115 if (PyDict_Size(_PyPopenProcs) == 0) {
5116 Py_DECREF(_PyPopenProcs);
5117 _PyPopenProcs = NULL;
5118 }
5119
5120 } /* if object retrieval ok */
5121
5122 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005123 } /* if _PyPopenProcs */
5124
Tim Peters736aa322000-09-01 06:51:24 +00005125#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005126 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005127#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005128 return result;
5129}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005130
5131#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005132static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005133posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005134{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005135 char *name;
5136 char *mode = "r";
5137 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00005138 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005139 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005140 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00005141 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00005142 /* Strip mode of binary or text modifiers */
5143 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5144 mode = "r";
5145 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5146 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00005147 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005148 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005149 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00005150 if (fp == NULL)
5151 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005152 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005153 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005154 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005155 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005156}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005157
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005158#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005159#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005160
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005161
Guido van Rossumb6775db1994-08-01 11:34:53 +00005162#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005163PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005164"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005165Set the current process's user id.");
5166
Barry Warsaw53699e91996-12-10 23:23:01 +00005167static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005168posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005169{
5170 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005171 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005172 return NULL;
5173 if (setuid(uid) < 0)
5174 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005175 Py_INCREF(Py_None);
5176 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005177}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005178#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005179
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005180
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005181#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005182PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005183"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005184Set the current process's effective user id.");
5185
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005186static PyObject *
5187posix_seteuid (PyObject *self, PyObject *args)
5188{
5189 int euid;
5190 if (!PyArg_ParseTuple(args, "i", &euid)) {
5191 return NULL;
5192 } else if (seteuid(euid) < 0) {
5193 return posix_error();
5194 } else {
5195 Py_INCREF(Py_None);
5196 return Py_None;
5197 }
5198}
5199#endif /* HAVE_SETEUID */
5200
5201#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005202PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005203"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005204Set the current process's effective group id.");
5205
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005206static PyObject *
5207posix_setegid (PyObject *self, PyObject *args)
5208{
5209 int egid;
5210 if (!PyArg_ParseTuple(args, "i", &egid)) {
5211 return NULL;
5212 } else if (setegid(egid) < 0) {
5213 return posix_error();
5214 } else {
5215 Py_INCREF(Py_None);
5216 return Py_None;
5217 }
5218}
5219#endif /* HAVE_SETEGID */
5220
5221#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005222PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005223"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005224Set the current process's real and effective user ids.");
5225
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005226static PyObject *
5227posix_setreuid (PyObject *self, PyObject *args)
5228{
5229 int ruid, euid;
5230 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
5231 return NULL;
5232 } else if (setreuid(ruid, euid) < 0) {
5233 return posix_error();
5234 } else {
5235 Py_INCREF(Py_None);
5236 return Py_None;
5237 }
5238}
5239#endif /* HAVE_SETREUID */
5240
5241#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005242PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005243"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005244Set the current process's real and effective group ids.");
5245
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005246static PyObject *
5247posix_setregid (PyObject *self, PyObject *args)
5248{
5249 int rgid, egid;
5250 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
5251 return NULL;
5252 } else if (setregid(rgid, egid) < 0) {
5253 return posix_error();
5254 } else {
5255 Py_INCREF(Py_None);
5256 return Py_None;
5257 }
5258}
5259#endif /* HAVE_SETREGID */
5260
Guido van Rossumb6775db1994-08-01 11:34:53 +00005261#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005262PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005263"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005264Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005265
Barry Warsaw53699e91996-12-10 23:23:01 +00005266static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005267posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005268{
5269 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005270 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005271 return NULL;
5272 if (setgid(gid) < 0)
5273 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005274 Py_INCREF(Py_None);
5275 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005276}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005277#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005278
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005279#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005280PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005281"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005282Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005283
5284static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005285posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005286{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005287 int i, len;
5288 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005289
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005290 if (!PySequence_Check(groups)) {
5291 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5292 return NULL;
5293 }
5294 len = PySequence_Size(groups);
5295 if (len > MAX_GROUPS) {
5296 PyErr_SetString(PyExc_ValueError, "too many groups");
5297 return NULL;
5298 }
5299 for(i = 0; i < len; i++) {
5300 PyObject *elem;
5301 elem = PySequence_GetItem(groups, i);
5302 if (!elem)
5303 return NULL;
5304 if (!PyInt_Check(elem)) {
Georg Brandla13c2442005-11-22 19:30:31 +00005305 if (!PyLong_Check(elem)) {
5306 PyErr_SetString(PyExc_TypeError,
5307 "groups must be integers");
5308 Py_DECREF(elem);
5309 return NULL;
5310 } else {
5311 unsigned long x = PyLong_AsUnsignedLong(elem);
5312 if (PyErr_Occurred()) {
5313 PyErr_SetString(PyExc_TypeError,
5314 "group id too big");
5315 Py_DECREF(elem);
5316 return NULL;
5317 }
5318 grouplist[i] = x;
5319 /* read back the value to see if it fitted in gid_t */
5320 if (grouplist[i] != x) {
5321 PyErr_SetString(PyExc_TypeError,
5322 "group id too big");
5323 Py_DECREF(elem);
5324 return NULL;
5325 }
5326 }
5327 } else {
5328 long x = PyInt_AsLong(elem);
5329 grouplist[i] = x;
5330 if (grouplist[i] != x) {
5331 PyErr_SetString(PyExc_TypeError,
5332 "group id too big");
5333 Py_DECREF(elem);
5334 return NULL;
5335 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005336 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005337 Py_DECREF(elem);
5338 }
5339
5340 if (setgroups(len, grouplist) < 0)
5341 return posix_error();
5342 Py_INCREF(Py_None);
5343 return Py_None;
5344}
5345#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005346
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005347#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5348static PyObject *
5349wait_helper(int pid, int status, struct rusage *ru)
5350{
5351 PyObject *result;
5352 static PyObject *struct_rusage;
5353
5354 if (pid == -1)
5355 return posix_error();
5356
5357 if (struct_rusage == NULL) {
5358 PyObject *m = PyImport_ImportModule("resource");
5359 if (m == NULL)
5360 return NULL;
5361 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5362 Py_DECREF(m);
5363 if (struct_rusage == NULL)
5364 return NULL;
5365 }
5366
5367 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5368 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5369 if (!result)
5370 return NULL;
5371
5372#ifndef doubletime
5373#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5374#endif
5375
5376 PyStructSequence_SET_ITEM(result, 0,
5377 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5378 PyStructSequence_SET_ITEM(result, 1,
5379 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5380#define SET_INT(result, index, value)\
5381 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5382 SET_INT(result, 2, ru->ru_maxrss);
5383 SET_INT(result, 3, ru->ru_ixrss);
5384 SET_INT(result, 4, ru->ru_idrss);
5385 SET_INT(result, 5, ru->ru_isrss);
5386 SET_INT(result, 6, ru->ru_minflt);
5387 SET_INT(result, 7, ru->ru_majflt);
5388 SET_INT(result, 8, ru->ru_nswap);
5389 SET_INT(result, 9, ru->ru_inblock);
5390 SET_INT(result, 10, ru->ru_oublock);
5391 SET_INT(result, 11, ru->ru_msgsnd);
5392 SET_INT(result, 12, ru->ru_msgrcv);
5393 SET_INT(result, 13, ru->ru_nsignals);
5394 SET_INT(result, 14, ru->ru_nvcsw);
5395 SET_INT(result, 15, ru->ru_nivcsw);
5396#undef SET_INT
5397
5398 if (PyErr_Occurred()) {
5399 Py_DECREF(result);
5400 return NULL;
5401 }
5402
5403 return Py_BuildValue("iiN", pid, status, result);
5404}
5405#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5406
5407#ifdef HAVE_WAIT3
5408PyDoc_STRVAR(posix_wait3__doc__,
5409"wait3(options) -> (pid, status, rusage)\n\n\
5410Wait for completion of a child process.");
5411
5412static PyObject *
5413posix_wait3(PyObject *self, PyObject *args)
5414{
5415 int pid, options;
5416 struct rusage ru;
5417 WAIT_TYPE status;
5418 WAIT_STATUS_INT(status) = 0;
5419
5420 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5421 return NULL;
5422
5423 Py_BEGIN_ALLOW_THREADS
5424 pid = wait3(&status, options, &ru);
5425 Py_END_ALLOW_THREADS
5426
5427 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
5428}
5429#endif /* HAVE_WAIT3 */
5430
5431#ifdef HAVE_WAIT4
5432PyDoc_STRVAR(posix_wait4__doc__,
5433"wait4(pid, options) -> (pid, status, rusage)\n\n\
5434Wait for completion of a given child process.");
5435
5436static PyObject *
5437posix_wait4(PyObject *self, PyObject *args)
5438{
5439 int pid, options;
5440 struct rusage ru;
5441 WAIT_TYPE status;
5442 WAIT_STATUS_INT(status) = 0;
5443
5444 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
5445 return NULL;
5446
5447 Py_BEGIN_ALLOW_THREADS
5448 pid = wait4(pid, &status, options, &ru);
5449 Py_END_ALLOW_THREADS
5450
5451 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
5452}
5453#endif /* HAVE_WAIT4 */
5454
Guido van Rossumb6775db1994-08-01 11:34:53 +00005455#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005456PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005457"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005458Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005459
Barry Warsaw53699e91996-12-10 23:23:01 +00005460static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005461posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005462{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005463 int pid, options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005464 WAIT_TYPE status;
5465 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005466
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005467 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00005468 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005469 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00005470 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00005471 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00005472 if (pid == -1)
5473 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005474
5475 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005476}
5477
Tim Petersab034fa2002-02-01 11:27:43 +00005478#elif defined(HAVE_CWAIT)
5479
5480/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005481PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005482"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005483"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005484
5485static PyObject *
5486posix_waitpid(PyObject *self, PyObject *args)
5487{
Thomas Wouters477c8d52006-05-27 19:21:47 +00005488 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005489 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005490
5491 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5492 return NULL;
5493 Py_BEGIN_ALLOW_THREADS
5494 pid = _cwait(&status, pid, options);
5495 Py_END_ALLOW_THREADS
5496 if (pid == -1)
5497 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005498
5499 /* shift the status left a byte so this is more like the POSIX waitpid */
5500 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005501}
5502#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005503
Guido van Rossumad0ee831995-03-01 10:34:45 +00005504#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005505PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005506"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005507Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005508
Barry Warsaw53699e91996-12-10 23:23:01 +00005509static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005510posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005511{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005512 int pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005513 WAIT_TYPE status;
5514 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005515
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005516 Py_BEGIN_ALLOW_THREADS
5517 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00005518 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00005519 if (pid == -1)
5520 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005521
5522 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005523}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005524#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005525
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005526
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005527PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005528"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005529Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005530
Barry Warsaw53699e91996-12-10 23:23:01 +00005531static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005532posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005533{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005534#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005535 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005536#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005537#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00005538 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005539#else
5540 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5541#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005542#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005543}
5544
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005545
Guido van Rossumb6775db1994-08-01 11:34:53 +00005546#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005547PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005548"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005549Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005550
Barry Warsaw53699e91996-12-10 23:23:01 +00005551static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005552posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005553{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005554 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005555 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005556 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005557 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005558 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005559 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00005560 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00005561 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005562 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00005563 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00005564 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005565}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005566#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005567
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005568
Guido van Rossumb6775db1994-08-01 11:34:53 +00005569#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005570PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005571"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005572Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005573
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005574static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005575posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005576{
Thomas Wouters477c8d52006-05-27 19:21:47 +00005577 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005578}
5579#endif /* HAVE_SYMLINK */
5580
5581
5582#ifdef HAVE_TIMES
5583#ifndef HZ
5584#define HZ 60 /* Universal constant :-) */
5585#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005586
Guido van Rossumd48f2521997-12-05 22:19:34 +00005587#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5588static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005589system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005590{
5591 ULONG value = 0;
5592
5593 Py_BEGIN_ALLOW_THREADS
5594 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5595 Py_END_ALLOW_THREADS
5596
5597 return value;
5598}
5599
5600static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005601posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005602{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005603 /* Currently Only Uptime is Provided -- Others Later */
5604 return Py_BuildValue("ddddd",
5605 (double)0 /* t.tms_utime / HZ */,
5606 (double)0 /* t.tms_stime / HZ */,
5607 (double)0 /* t.tms_cutime / HZ */,
5608 (double)0 /* t.tms_cstime / HZ */,
5609 (double)system_uptime() / 1000);
5610}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005611#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005612static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005613posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005614{
5615 struct tms t;
5616 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005617 errno = 0;
5618 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005619 if (c == (clock_t) -1)
5620 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005621 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005622 (double)t.tms_utime / HZ,
5623 (double)t.tms_stime / HZ,
5624 (double)t.tms_cutime / HZ,
5625 (double)t.tms_cstime / HZ,
5626 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005627}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005628#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005629#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005630
5631
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005632#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005633#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005634static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005635posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005636{
5637 FILETIME create, exit, kernel, user;
5638 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005639 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005640 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5641 /* The fields of a FILETIME structure are the hi and lo part
5642 of a 64-bit value expressed in 100 nanosecond units.
5643 1e7 is one second in such units; 1e-7 the inverse.
5644 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5645 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005646 return Py_BuildValue(
5647 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005648 (double)(kernel.dwHighDateTime*429.4967296 +
5649 kernel.dwLowDateTime*1e-7),
5650 (double)(user.dwHighDateTime*429.4967296 +
5651 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005652 (double)0,
5653 (double)0,
5654 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005655}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005656#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005657
5658#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005659PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005660"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005661Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005662#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005663
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005664
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005665#ifdef HAVE_GETSID
5666PyDoc_STRVAR(posix_getsid__doc__,
5667"getsid(pid) -> sid\n\n\
5668Call the system call getsid().");
5669
5670static PyObject *
5671posix_getsid(PyObject *self, PyObject *args)
5672{
5673 int pid, sid;
5674 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5675 return NULL;
5676 sid = getsid(pid);
5677 if (sid < 0)
5678 return posix_error();
5679 return PyInt_FromLong((long)sid);
5680}
5681#endif /* HAVE_GETSID */
5682
5683
Guido van Rossumb6775db1994-08-01 11:34:53 +00005684#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005685PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005686"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005687Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005688
Barry Warsaw53699e91996-12-10 23:23:01 +00005689static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005690posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005691{
Guido van Rossum687dd131993-05-17 08:34:16 +00005692 if (setsid() < 0)
5693 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005694 Py_INCREF(Py_None);
5695 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005696}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005697#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005698
Guido van Rossumb6775db1994-08-01 11:34:53 +00005699#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005700PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005701"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005702Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005703
Barry Warsaw53699e91996-12-10 23:23:01 +00005704static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005705posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005706{
5707 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005708 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005709 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005710 if (setpgid(pid, pgrp) < 0)
5711 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005712 Py_INCREF(Py_None);
5713 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005714}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005715#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005717
Guido van Rossumb6775db1994-08-01 11:34:53 +00005718#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005719PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005720"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005721Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005722
Barry Warsaw53699e91996-12-10 23:23:01 +00005723static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005724posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005725{
5726 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005727 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005728 return NULL;
5729 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005730 if (pgid < 0)
5731 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005732 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005733}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005734#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005735
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005736
Guido van Rossumb6775db1994-08-01 11:34:53 +00005737#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005738PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005739"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005740Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005741
Barry Warsaw53699e91996-12-10 23:23:01 +00005742static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005743posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005744{
5745 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005746 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005747 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005748 if (tcsetpgrp(fd, pgid) < 0)
5749 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005750 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005751 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005752}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005753#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005754
Guido van Rossum687dd131993-05-17 08:34:16 +00005755/* Functions acting on file descriptors */
5756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005757PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005758"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005759Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005760
Barry Warsaw53699e91996-12-10 23:23:01 +00005761static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005762posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005763{
Mark Hammondef8b6542001-05-13 08:04:26 +00005764 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005765 int flag;
5766 int mode = 0777;
5767 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005768
5769#ifdef MS_WINDOWS
5770 if (unicode_file_names()) {
5771 PyUnicodeObject *po;
5772 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5773 Py_BEGIN_ALLOW_THREADS
5774 /* PyUnicode_AS_UNICODE OK without thread
5775 lock as it is a simple dereference. */
5776 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5777 Py_END_ALLOW_THREADS
5778 if (fd < 0)
5779 return posix_error();
5780 return PyInt_FromLong((long)fd);
5781 }
5782 /* Drop the argument parsing error as narrow strings
5783 are also valid. */
5784 PyErr_Clear();
5785 }
5786#endif
5787
Tim Peters5aa91602002-01-30 05:46:57 +00005788 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00005789 Py_FileSystemDefaultEncoding, &file,
5790 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00005791 return NULL;
5792
Barry Warsaw53699e91996-12-10 23:23:01 +00005793 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005794 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005795 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005796 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00005797 return posix_error_with_allocated_filename(file);
5798 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00005799 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005800}
5801
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005803PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005804"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005805Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005806
Barry Warsaw53699e91996-12-10 23:23:01 +00005807static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005808posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005809{
5810 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005811 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005812 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005813 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005814 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005815 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005816 if (res < 0)
5817 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005818 Py_INCREF(Py_None);
5819 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005820}
5821
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005822
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005823PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005824"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005825Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005826
Barry Warsaw53699e91996-12-10 23:23:01 +00005827static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005828posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005829{
5830 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005831 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005832 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005833 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005834 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005835 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005836 if (fd < 0)
5837 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005838 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005839}
5840
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005841
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005842PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005843"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005844Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005845
Barry Warsaw53699e91996-12-10 23:23:01 +00005846static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005847posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005848{
5849 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005850 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005851 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005852 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005853 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005854 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005855 if (res < 0)
5856 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005857 Py_INCREF(Py_None);
5858 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005859}
5860
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005861
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005862PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005863"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005864Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005865
Barry Warsaw53699e91996-12-10 23:23:01 +00005866static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005867posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005868{
5869 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005870#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005871 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005872#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005873 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005874#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005875 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005876 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005877 return NULL;
5878#ifdef SEEK_SET
5879 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5880 switch (how) {
5881 case 0: how = SEEK_SET; break;
5882 case 1: how = SEEK_CUR; break;
5883 case 2: how = SEEK_END; break;
5884 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005885#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005886
5887#if !defined(HAVE_LARGEFILE_SUPPORT)
5888 pos = PyInt_AsLong(posobj);
5889#else
5890 pos = PyLong_Check(posobj) ?
5891 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
5892#endif
5893 if (PyErr_Occurred())
5894 return NULL;
5895
Barry Warsaw53699e91996-12-10 23:23:01 +00005896 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005897#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005898 res = _lseeki64(fd, pos, how);
5899#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005900 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005901#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005902 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005903 if (res < 0)
5904 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005905
5906#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00005907 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005908#else
5909 return PyLong_FromLongLong(res);
5910#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005911}
5912
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005913
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005914PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005915"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005916Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005917
Barry Warsaw53699e91996-12-10 23:23:01 +00005918static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005919posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005920{
Guido van Rossum8bac5461996-06-11 18:38:48 +00005921 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005922 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005923 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005924 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005925 if (size < 0) {
5926 errno = EINVAL;
5927 return posix_error();
5928 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005929 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005930 if (buffer == NULL)
5931 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005932 Py_BEGIN_ALLOW_THREADS
5933 n = read(fd, PyString_AsString(buffer), size);
5934 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005935 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005936 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005937 return posix_error();
5938 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005939 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00005940 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005941 return buffer;
5942}
5943
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005944
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005945PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005946"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005947Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005948
Barry Warsaw53699e91996-12-10 23:23:01 +00005949static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005950posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005951{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005952 int fd;
5953 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00005954 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005955
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005956 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005957 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005958 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005959 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005960 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005961 if (size < 0)
5962 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005963 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005964}
5965
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005966
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005967PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005968"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005969Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005970
Barry Warsaw53699e91996-12-10 23:23:01 +00005971static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005972posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005973{
5974 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005975 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005976 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005977 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005978 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005979#ifdef __VMS
5980 /* on OpenVMS we must ensure that all bytes are written to the file */
5981 fsync(fd);
5982#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005983 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005984 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005985 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005986 if (res != 0) {
5987#ifdef MS_WINDOWS
5988 return win32_error("fstat", NULL);
5989#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005990 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005991#endif
5992 }
Tim Peters5aa91602002-01-30 05:46:57 +00005993
Martin v. Löwis14694662006-02-03 12:54:16 +00005994 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005995}
5996
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005997
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005998PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005999"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006000Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006001
Barry Warsaw53699e91996-12-10 23:23:01 +00006002static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006003posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006004{
Guido van Rossum687dd131993-05-17 08:34:16 +00006005 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006006 char *mode = "r";
6007 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00006008 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00006009 PyObject *f;
6010 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00006011 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006012
Thomas Heller1f043e22002-11-07 16:00:59 +00006013 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
6014 PyErr_Format(PyExc_ValueError,
6015 "invalid file mode '%s'", mode);
6016 return NULL;
6017 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006018 Py_BEGIN_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006019#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
6020 if (mode[0] == 'a') {
6021 /* try to make sure the O_APPEND flag is set */
6022 int flags;
6023 flags = fcntl(fd, F_GETFL);
6024 if (flags != -1)
6025 fcntl(fd, F_SETFL, flags | O_APPEND);
6026 fp = fdopen(fd, mode);
6027 if (fp == NULL && flags != -1)
6028 /* restore old mode if fdopen failed */
6029 fcntl(fd, F_SETFL, flags);
6030 } else {
6031 fp = fdopen(fd, mode);
6032 }
6033#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006034 fp = fdopen(fd, mode);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006035#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006036 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006037 if (fp == NULL)
6038 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00006039 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006040 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00006041 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006042 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006043}
6044
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006045PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006046"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006047Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006048connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006049
6050static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006051posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006052{
6053 int fd;
6054 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6055 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006056 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006057}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006058
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006059#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006060PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006061"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006062Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006063
Barry Warsaw53699e91996-12-10 23:23:01 +00006064static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006065posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006066{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006067#if defined(PYOS_OS2)
6068 HFILE read, write;
6069 APIRET rc;
6070
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006071 Py_BEGIN_ALLOW_THREADS
6072 rc = DosCreatePipe( &read, &write, 4096);
6073 Py_END_ALLOW_THREADS
6074 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006075 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006076
6077 return Py_BuildValue("(ii)", read, write);
6078#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006079#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00006080 int fds[2];
6081 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00006082 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006083 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00006084 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006085 if (res != 0)
6086 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006087 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006088#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00006089 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006090 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00006091 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00006092 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006093 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00006094 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00006095 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006096 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00006097 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6098 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006099 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006100#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006101#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006102}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006103#endif /* HAVE_PIPE */
6104
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006105
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006106#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006107PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006108"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006109Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006110
Barry Warsaw53699e91996-12-10 23:23:01 +00006111static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006112posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006113{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006114 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006115 int mode = 0666;
6116 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006117 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006118 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006119 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006120 res = mkfifo(filename, mode);
6121 Py_END_ALLOW_THREADS
6122 if (res < 0)
6123 return posix_error();
6124 Py_INCREF(Py_None);
6125 return Py_None;
6126}
6127#endif
6128
6129
Neal Norwitz11690112002-07-30 01:08:28 +00006130#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006131PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006132"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006133Create a filesystem node (file, device special file or named pipe)\n\
6134named filename. mode specifies both the permissions to use and the\n\
6135type of node to be created, being combined (bitwise OR) with one of\n\
6136S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006137device defines the newly created device special file (probably using\n\
6138os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006139
6140
6141static PyObject *
6142posix_mknod(PyObject *self, PyObject *args)
6143{
6144 char *filename;
6145 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006146 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006147 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00006148 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006149 return NULL;
6150 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006151 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00006152 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006153 if (res < 0)
6154 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006155 Py_INCREF(Py_None);
6156 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006157}
6158#endif
6159
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006160#ifdef HAVE_DEVICE_MACROS
6161PyDoc_STRVAR(posix_major__doc__,
6162"major(device) -> major number\n\
6163Extracts a device major number from a raw device number.");
6164
6165static PyObject *
6166posix_major(PyObject *self, PyObject *args)
6167{
6168 int device;
6169 if (!PyArg_ParseTuple(args, "i:major", &device))
6170 return NULL;
6171 return PyInt_FromLong((long)major(device));
6172}
6173
6174PyDoc_STRVAR(posix_minor__doc__,
6175"minor(device) -> minor number\n\
6176Extracts a device minor number from a raw device number.");
6177
6178static PyObject *
6179posix_minor(PyObject *self, PyObject *args)
6180{
6181 int device;
6182 if (!PyArg_ParseTuple(args, "i:minor", &device))
6183 return NULL;
6184 return PyInt_FromLong((long)minor(device));
6185}
6186
6187PyDoc_STRVAR(posix_makedev__doc__,
6188"makedev(major, minor) -> device number\n\
6189Composes a raw device number from the major and minor device numbers.");
6190
6191static PyObject *
6192posix_makedev(PyObject *self, PyObject *args)
6193{
6194 int major, minor;
6195 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6196 return NULL;
6197 return PyInt_FromLong((long)makedev(major, minor));
6198}
6199#endif /* device macros */
6200
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006201
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006202#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006203PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006204"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006205Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006206
Barry Warsaw53699e91996-12-10 23:23:01 +00006207static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006208posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006209{
6210 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006211 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006212 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006213 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006214
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006215 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006216 return NULL;
6217
6218#if !defined(HAVE_LARGEFILE_SUPPORT)
6219 length = PyInt_AsLong(lenobj);
6220#else
6221 length = PyLong_Check(lenobj) ?
6222 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6223#endif
6224 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006225 return NULL;
6226
Barry Warsaw53699e91996-12-10 23:23:01 +00006227 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006228 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00006229 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006230 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006231 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006232 return NULL;
6233 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006234 Py_INCREF(Py_None);
6235 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006236}
6237#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006238
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006239#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006240PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006241"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006242Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006243
Fred Drake762e2061999-08-26 17:23:54 +00006244/* Save putenv() parameters as values here, so we can collect them when they
6245 * get re-set with another call for the same key. */
6246static PyObject *posix_putenv_garbage;
6247
Tim Peters5aa91602002-01-30 05:46:57 +00006248static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006249posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006250{
6251 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006252 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00006253 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00006254 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006255
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006256 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006257 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006258
6259#if defined(PYOS_OS2)
6260 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6261 APIRET rc;
6262
Guido van Rossumd48f2521997-12-05 22:19:34 +00006263 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6264 if (rc != NO_ERROR)
6265 return os2_error(rc);
6266
6267 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6268 APIRET rc;
6269
Guido van Rossumd48f2521997-12-05 22:19:34 +00006270 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6271 if (rc != NO_ERROR)
6272 return os2_error(rc);
6273 } else {
6274#endif
6275
Fred Drake762e2061999-08-26 17:23:54 +00006276 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00006277 len = strlen(s1) + strlen(s2) + 2;
6278 /* len includes space for a trailing \0; the size arg to
6279 PyString_FromStringAndSize does not count that */
6280 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00006281 if (newstr == NULL)
6282 return PyErr_NoMemory();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006283 newenv = PyString_AS_STRING(newstr);
6284 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6285 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00006286 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006287 posix_error();
6288 return NULL;
6289 }
Fred Drake762e2061999-08-26 17:23:54 +00006290 /* Install the first arg and newstr in posix_putenv_garbage;
6291 * this will cause previous value to be collected. This has to
6292 * happen after the real putenv() call because the old value
6293 * was still accessible until then. */
6294 if (PyDict_SetItem(posix_putenv_garbage,
6295 PyTuple_GET_ITEM(args, 0), newstr)) {
6296 /* really not much we can do; just leak */
6297 PyErr_Clear();
6298 }
6299 else {
6300 Py_DECREF(newstr);
6301 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006302
6303#if defined(PYOS_OS2)
6304 }
6305#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006306 Py_INCREF(Py_None);
6307 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006308}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006309#endif /* putenv */
6310
Guido van Rossumc524d952001-10-19 01:31:59 +00006311#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006312PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006313"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006314Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006315
6316static PyObject *
6317posix_unsetenv(PyObject *self, PyObject *args)
6318{
6319 char *s1;
6320
6321 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6322 return NULL;
6323
6324 unsetenv(s1);
6325
6326 /* Remove the key from posix_putenv_garbage;
6327 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00006328 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00006329 * old value was still accessible until then.
6330 */
6331 if (PyDict_DelItem(posix_putenv_garbage,
6332 PyTuple_GET_ITEM(args, 0))) {
6333 /* really not much we can do; just leak */
6334 PyErr_Clear();
6335 }
6336
6337 Py_INCREF(Py_None);
6338 return Py_None;
6339}
6340#endif /* unsetenv */
6341
Guido van Rossumb6a47161997-09-15 22:54:34 +00006342#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006343PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006344"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006345Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006346
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006347static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006348posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006349{
6350 int code;
6351 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006352 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00006353 return NULL;
6354 message = strerror(code);
6355 if (message == NULL) {
6356 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00006357 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006358 return NULL;
6359 }
6360 return PyString_FromString(message);
6361}
6362#endif /* strerror */
6363
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006364
Guido van Rossumc9641791998-08-04 15:26:23 +00006365#ifdef HAVE_SYS_WAIT_H
6366
Fred Drake106c1a02002-04-23 15:58:02 +00006367#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006368PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006369"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006370Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006371
6372static PyObject *
6373posix_WCOREDUMP(PyObject *self, PyObject *args)
6374{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006375 WAIT_TYPE status;
6376 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006377
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006378 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006379 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006380
6381 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006382}
6383#endif /* WCOREDUMP */
6384
6385#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006386PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006387"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006388Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006389job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006390
6391static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006392posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006393{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006394 WAIT_TYPE status;
6395 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006396
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006397 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006398 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006399
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006400 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006401}
6402#endif /* WIFCONTINUED */
6403
Guido van Rossumc9641791998-08-04 15:26:23 +00006404#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006405PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006406"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006407Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006408
6409static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006410posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006411{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006412 WAIT_TYPE status;
6413 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006414
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006415 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006416 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006417
Fred Drake106c1a02002-04-23 15:58:02 +00006418 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006419}
6420#endif /* WIFSTOPPED */
6421
6422#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006423PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006424"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006425Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006426
6427static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006428posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006429{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006430 WAIT_TYPE status;
6431 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006432
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006433 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006434 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006435
Fred Drake106c1a02002-04-23 15:58:02 +00006436 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006437}
6438#endif /* WIFSIGNALED */
6439
6440#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006441PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006442"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006443Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006444system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006445
6446static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006447posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006448{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006449 WAIT_TYPE status;
6450 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006451
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006452 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006453 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006454
Fred Drake106c1a02002-04-23 15:58:02 +00006455 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006456}
6457#endif /* WIFEXITED */
6458
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006459#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006460PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006461"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006462Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006463
6464static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006465posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006466{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006467 WAIT_TYPE status;
6468 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006469
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006470 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006471 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006472
Guido van Rossumc9641791998-08-04 15:26:23 +00006473 return Py_BuildValue("i", WEXITSTATUS(status));
6474}
6475#endif /* WEXITSTATUS */
6476
6477#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006478PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006479"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006480Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006481value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006482
6483static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006484posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006485{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006486 WAIT_TYPE status;
6487 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006488
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006489 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006490 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006491
Guido van Rossumc9641791998-08-04 15:26:23 +00006492 return Py_BuildValue("i", WTERMSIG(status));
6493}
6494#endif /* WTERMSIG */
6495
6496#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006497PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006498"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006499Return the signal that stopped the process that provided\n\
6500the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006501
6502static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006503posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006504{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006505 WAIT_TYPE status;
6506 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006507
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006508 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006509 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006510
Guido van Rossumc9641791998-08-04 15:26:23 +00006511 return Py_BuildValue("i", WSTOPSIG(status));
6512}
6513#endif /* WSTOPSIG */
6514
6515#endif /* HAVE_SYS_WAIT_H */
6516
6517
Thomas Wouters477c8d52006-05-27 19:21:47 +00006518#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006519#ifdef _SCO_DS
6520/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6521 needed definitions in sys/statvfs.h */
6522#define _SVID3
6523#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006524#include <sys/statvfs.h>
6525
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006526static PyObject*
6527_pystatvfs_fromstructstatvfs(struct statvfs st) {
6528 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6529 if (v == NULL)
6530 return NULL;
6531
6532#if !defined(HAVE_LARGEFILE_SUPPORT)
6533 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6534 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6535 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6536 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6537 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6538 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6539 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6540 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6541 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6542 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6543#else
6544 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6545 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006546 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006547 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006548 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006549 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006550 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006551 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006552 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006553 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006554 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006555 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006556 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006557 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006558 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6559 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6560#endif
6561
6562 return v;
6563}
6564
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006565PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006566"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006567Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006568
6569static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006570posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006571{
6572 int fd, res;
6573 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006574
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006575 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006576 return NULL;
6577 Py_BEGIN_ALLOW_THREADS
6578 res = fstatvfs(fd, &st);
6579 Py_END_ALLOW_THREADS
6580 if (res != 0)
6581 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006582
6583 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006584}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006585#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006586
6587
Thomas Wouters477c8d52006-05-27 19:21:47 +00006588#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006589#include <sys/statvfs.h>
6590
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006591PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006592"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006593Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006594
6595static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006596posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006597{
6598 char *path;
6599 int res;
6600 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006601 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006602 return NULL;
6603 Py_BEGIN_ALLOW_THREADS
6604 res = statvfs(path, &st);
6605 Py_END_ALLOW_THREADS
6606 if (res != 0)
6607 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006608
6609 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006610}
6611#endif /* HAVE_STATVFS */
6612
6613
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006614#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006615PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006616"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006617Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006618The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006619or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006620
6621static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006622posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006623{
6624 PyObject *result = NULL;
6625 char *dir = NULL;
6626 char *pfx = NULL;
6627 char *name;
6628
6629 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6630 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006631
6632 if (PyErr_Warn(PyExc_RuntimeWarning,
6633 "tempnam is a potential security risk to your program") < 0)
6634 return NULL;
6635
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006636#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006637 name = _tempnam(dir, pfx);
6638#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006639 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006640#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006641 if (name == NULL)
6642 return PyErr_NoMemory();
6643 result = PyString_FromString(name);
6644 free(name);
6645 return result;
6646}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006647#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006648
6649
6650#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006651PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006652"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006653Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006654
6655static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006656posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006657{
6658 FILE *fp;
6659
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006660 fp = tmpfile();
6661 if (fp == NULL)
6662 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006663 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006664}
6665#endif
6666
6667
6668#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006669PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006670"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006671Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006672
6673static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006674posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006675{
6676 char buffer[L_tmpnam];
6677 char *name;
6678
Skip Montanaro95618b52001-08-18 18:52:10 +00006679 if (PyErr_Warn(PyExc_RuntimeWarning,
6680 "tmpnam is a potential security risk to your program") < 0)
6681 return NULL;
6682
Greg Wardb48bc172000-03-01 21:51:56 +00006683#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006684 name = tmpnam_r(buffer);
6685#else
6686 name = tmpnam(buffer);
6687#endif
6688 if (name == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006689 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006690#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006691 "unexpected NULL from tmpnam_r"
6692#else
6693 "unexpected NULL from tmpnam"
6694#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006695 );
6696 PyErr_SetObject(PyExc_OSError, err);
6697 Py_XDECREF(err);
6698 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006699 }
6700 return PyString_FromString(buffer);
6701}
6702#endif
6703
6704
Fred Drakec9680921999-12-13 16:37:25 +00006705/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6706 * It maps strings representing configuration variable names to
6707 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006708 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006709 * rarely-used constants. There are three separate tables that use
6710 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006711 *
6712 * This code is always included, even if none of the interfaces that
6713 * need it are included. The #if hackery needed to avoid it would be
6714 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006715 */
6716struct constdef {
6717 char *name;
6718 long value;
6719};
6720
Fred Drake12c6e2d1999-12-14 21:25:03 +00006721static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006722conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6723 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006724{
6725 if (PyInt_Check(arg)) {
6726 *valuep = PyInt_AS_LONG(arg);
6727 return 1;
6728 }
6729 if (PyString_Check(arg)) {
6730 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006731 size_t lo = 0;
6732 size_t mid;
6733 size_t hi = tablesize;
6734 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006735 char *confname = PyString_AS_STRING(arg);
6736 while (lo < hi) {
6737 mid = (lo + hi) / 2;
6738 cmp = strcmp(confname, table[mid].name);
6739 if (cmp < 0)
6740 hi = mid;
6741 else if (cmp > 0)
6742 lo = mid + 1;
6743 else {
6744 *valuep = table[mid].value;
6745 return 1;
6746 }
6747 }
6748 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6749 }
6750 else
6751 PyErr_SetString(PyExc_TypeError,
6752 "configuration names must be strings or integers");
6753 return 0;
6754}
6755
6756
6757#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6758static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006759#ifdef _PC_ABI_AIO_XFER_MAX
6760 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6761#endif
6762#ifdef _PC_ABI_ASYNC_IO
6763 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6764#endif
Fred Drakec9680921999-12-13 16:37:25 +00006765#ifdef _PC_ASYNC_IO
6766 {"PC_ASYNC_IO", _PC_ASYNC_IO},
6767#endif
6768#ifdef _PC_CHOWN_RESTRICTED
6769 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
6770#endif
6771#ifdef _PC_FILESIZEBITS
6772 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
6773#endif
6774#ifdef _PC_LAST
6775 {"PC_LAST", _PC_LAST},
6776#endif
6777#ifdef _PC_LINK_MAX
6778 {"PC_LINK_MAX", _PC_LINK_MAX},
6779#endif
6780#ifdef _PC_MAX_CANON
6781 {"PC_MAX_CANON", _PC_MAX_CANON},
6782#endif
6783#ifdef _PC_MAX_INPUT
6784 {"PC_MAX_INPUT", _PC_MAX_INPUT},
6785#endif
6786#ifdef _PC_NAME_MAX
6787 {"PC_NAME_MAX", _PC_NAME_MAX},
6788#endif
6789#ifdef _PC_NO_TRUNC
6790 {"PC_NO_TRUNC", _PC_NO_TRUNC},
6791#endif
6792#ifdef _PC_PATH_MAX
6793 {"PC_PATH_MAX", _PC_PATH_MAX},
6794#endif
6795#ifdef _PC_PIPE_BUF
6796 {"PC_PIPE_BUF", _PC_PIPE_BUF},
6797#endif
6798#ifdef _PC_PRIO_IO
6799 {"PC_PRIO_IO", _PC_PRIO_IO},
6800#endif
6801#ifdef _PC_SOCK_MAXBUF
6802 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
6803#endif
6804#ifdef _PC_SYNC_IO
6805 {"PC_SYNC_IO", _PC_SYNC_IO},
6806#endif
6807#ifdef _PC_VDISABLE
6808 {"PC_VDISABLE", _PC_VDISABLE},
6809#endif
6810};
6811
Fred Drakec9680921999-12-13 16:37:25 +00006812static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006813conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006814{
6815 return conv_confname(arg, valuep, posix_constants_pathconf,
6816 sizeof(posix_constants_pathconf)
6817 / sizeof(struct constdef));
6818}
6819#endif
6820
6821#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006822PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006823"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006824Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006825If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006826
6827static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006828posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006829{
6830 PyObject *result = NULL;
6831 int name, fd;
6832
Fred Drake12c6e2d1999-12-14 21:25:03 +00006833 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6834 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00006835 long limit;
6836
6837 errno = 0;
6838 limit = fpathconf(fd, name);
6839 if (limit == -1 && errno != 0)
6840 posix_error();
6841 else
6842 result = PyInt_FromLong(limit);
6843 }
6844 return result;
6845}
6846#endif
6847
6848
6849#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006850PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006851"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006852Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006853If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006854
6855static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006856posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006857{
6858 PyObject *result = NULL;
6859 int name;
6860 char *path;
6861
6862 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6863 conv_path_confname, &name)) {
6864 long limit;
6865
6866 errno = 0;
6867 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006868 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00006869 if (errno == EINVAL)
6870 /* could be a path or name problem */
6871 posix_error();
6872 else
6873 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006874 }
Fred Drakec9680921999-12-13 16:37:25 +00006875 else
6876 result = PyInt_FromLong(limit);
6877 }
6878 return result;
6879}
6880#endif
6881
6882#ifdef HAVE_CONFSTR
6883static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006884#ifdef _CS_ARCHITECTURE
6885 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6886#endif
6887#ifdef _CS_HOSTNAME
6888 {"CS_HOSTNAME", _CS_HOSTNAME},
6889#endif
6890#ifdef _CS_HW_PROVIDER
6891 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6892#endif
6893#ifdef _CS_HW_SERIAL
6894 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6895#endif
6896#ifdef _CS_INITTAB_NAME
6897 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6898#endif
Fred Drakec9680921999-12-13 16:37:25 +00006899#ifdef _CS_LFS64_CFLAGS
6900 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6901#endif
6902#ifdef _CS_LFS64_LDFLAGS
6903 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6904#endif
6905#ifdef _CS_LFS64_LIBS
6906 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6907#endif
6908#ifdef _CS_LFS64_LINTFLAGS
6909 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6910#endif
6911#ifdef _CS_LFS_CFLAGS
6912 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6913#endif
6914#ifdef _CS_LFS_LDFLAGS
6915 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6916#endif
6917#ifdef _CS_LFS_LIBS
6918 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6919#endif
6920#ifdef _CS_LFS_LINTFLAGS
6921 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6922#endif
Fred Draked86ed291999-12-15 15:34:33 +00006923#ifdef _CS_MACHINE
6924 {"CS_MACHINE", _CS_MACHINE},
6925#endif
Fred Drakec9680921999-12-13 16:37:25 +00006926#ifdef _CS_PATH
6927 {"CS_PATH", _CS_PATH},
6928#endif
Fred Draked86ed291999-12-15 15:34:33 +00006929#ifdef _CS_RELEASE
6930 {"CS_RELEASE", _CS_RELEASE},
6931#endif
6932#ifdef _CS_SRPC_DOMAIN
6933 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6934#endif
6935#ifdef _CS_SYSNAME
6936 {"CS_SYSNAME", _CS_SYSNAME},
6937#endif
6938#ifdef _CS_VERSION
6939 {"CS_VERSION", _CS_VERSION},
6940#endif
Fred Drakec9680921999-12-13 16:37:25 +00006941#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6942 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6943#endif
6944#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6945 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6946#endif
6947#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6948 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6949#endif
6950#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6951 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6952#endif
6953#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6954 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6955#endif
6956#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6957 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6958#endif
6959#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6960 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6961#endif
6962#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6963 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6964#endif
6965#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6966 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6967#endif
6968#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6969 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6970#endif
6971#ifdef _CS_XBS5_LP64_OFF64_LIBS
6972 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6973#endif
6974#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6975 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6976#endif
6977#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6978 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6979#endif
6980#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6981 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6982#endif
6983#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6984 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6985#endif
6986#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6987 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6988#endif
Fred Draked86ed291999-12-15 15:34:33 +00006989#ifdef _MIPS_CS_AVAIL_PROCESSORS
6990 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6991#endif
6992#ifdef _MIPS_CS_BASE
6993 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6994#endif
6995#ifdef _MIPS_CS_HOSTID
6996 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6997#endif
6998#ifdef _MIPS_CS_HW_NAME
6999 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7000#endif
7001#ifdef _MIPS_CS_NUM_PROCESSORS
7002 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7003#endif
7004#ifdef _MIPS_CS_OSREL_MAJ
7005 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7006#endif
7007#ifdef _MIPS_CS_OSREL_MIN
7008 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7009#endif
7010#ifdef _MIPS_CS_OSREL_PATCH
7011 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7012#endif
7013#ifdef _MIPS_CS_OS_NAME
7014 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7015#endif
7016#ifdef _MIPS_CS_OS_PROVIDER
7017 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7018#endif
7019#ifdef _MIPS_CS_PROCESSORS
7020 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7021#endif
7022#ifdef _MIPS_CS_SERIAL
7023 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7024#endif
7025#ifdef _MIPS_CS_VENDOR
7026 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7027#endif
Fred Drakec9680921999-12-13 16:37:25 +00007028};
7029
7030static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007031conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007032{
7033 return conv_confname(arg, valuep, posix_constants_confstr,
7034 sizeof(posix_constants_confstr)
7035 / sizeof(struct constdef));
7036}
7037
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007038PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007039"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007040Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007041
7042static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007043posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007044{
7045 PyObject *result = NULL;
7046 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007047 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007048
7049 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007050 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007051
Fred Drakec9680921999-12-13 16:37:25 +00007052 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007053 len = confstr(name, buffer, sizeof(buffer));
7054 if (len == 0) {
7055 if (errno) {
7056 posix_error();
7057 }
7058 else {
7059 result = Py_None;
7060 Py_INCREF(Py_None);
7061 }
Fred Drakec9680921999-12-13 16:37:25 +00007062 }
7063 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007064 if ((unsigned int)len >= sizeof(buffer)) {
7065 result = PyString_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007066 if (result != NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007067 confstr(name, PyString_AS_STRING(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00007068 }
7069 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007070 result = PyString_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007071 }
7072 }
7073 return result;
7074}
7075#endif
7076
7077
7078#ifdef HAVE_SYSCONF
7079static struct constdef posix_constants_sysconf[] = {
7080#ifdef _SC_2_CHAR_TERM
7081 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7082#endif
7083#ifdef _SC_2_C_BIND
7084 {"SC_2_C_BIND", _SC_2_C_BIND},
7085#endif
7086#ifdef _SC_2_C_DEV
7087 {"SC_2_C_DEV", _SC_2_C_DEV},
7088#endif
7089#ifdef _SC_2_C_VERSION
7090 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7091#endif
7092#ifdef _SC_2_FORT_DEV
7093 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7094#endif
7095#ifdef _SC_2_FORT_RUN
7096 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7097#endif
7098#ifdef _SC_2_LOCALEDEF
7099 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7100#endif
7101#ifdef _SC_2_SW_DEV
7102 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7103#endif
7104#ifdef _SC_2_UPE
7105 {"SC_2_UPE", _SC_2_UPE},
7106#endif
7107#ifdef _SC_2_VERSION
7108 {"SC_2_VERSION", _SC_2_VERSION},
7109#endif
Fred Draked86ed291999-12-15 15:34:33 +00007110#ifdef _SC_ABI_ASYNCHRONOUS_IO
7111 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7112#endif
7113#ifdef _SC_ACL
7114 {"SC_ACL", _SC_ACL},
7115#endif
Fred Drakec9680921999-12-13 16:37:25 +00007116#ifdef _SC_AIO_LISTIO_MAX
7117 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7118#endif
Fred Drakec9680921999-12-13 16:37:25 +00007119#ifdef _SC_AIO_MAX
7120 {"SC_AIO_MAX", _SC_AIO_MAX},
7121#endif
7122#ifdef _SC_AIO_PRIO_DELTA_MAX
7123 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7124#endif
7125#ifdef _SC_ARG_MAX
7126 {"SC_ARG_MAX", _SC_ARG_MAX},
7127#endif
7128#ifdef _SC_ASYNCHRONOUS_IO
7129 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7130#endif
7131#ifdef _SC_ATEXIT_MAX
7132 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7133#endif
Fred Draked86ed291999-12-15 15:34:33 +00007134#ifdef _SC_AUDIT
7135 {"SC_AUDIT", _SC_AUDIT},
7136#endif
Fred Drakec9680921999-12-13 16:37:25 +00007137#ifdef _SC_AVPHYS_PAGES
7138 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7139#endif
7140#ifdef _SC_BC_BASE_MAX
7141 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7142#endif
7143#ifdef _SC_BC_DIM_MAX
7144 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7145#endif
7146#ifdef _SC_BC_SCALE_MAX
7147 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7148#endif
7149#ifdef _SC_BC_STRING_MAX
7150 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7151#endif
Fred Draked86ed291999-12-15 15:34:33 +00007152#ifdef _SC_CAP
7153 {"SC_CAP", _SC_CAP},
7154#endif
Fred Drakec9680921999-12-13 16:37:25 +00007155#ifdef _SC_CHARCLASS_NAME_MAX
7156 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7157#endif
7158#ifdef _SC_CHAR_BIT
7159 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7160#endif
7161#ifdef _SC_CHAR_MAX
7162 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7163#endif
7164#ifdef _SC_CHAR_MIN
7165 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7166#endif
7167#ifdef _SC_CHILD_MAX
7168 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7169#endif
7170#ifdef _SC_CLK_TCK
7171 {"SC_CLK_TCK", _SC_CLK_TCK},
7172#endif
7173#ifdef _SC_COHER_BLKSZ
7174 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7175#endif
7176#ifdef _SC_COLL_WEIGHTS_MAX
7177 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7178#endif
7179#ifdef _SC_DCACHE_ASSOC
7180 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7181#endif
7182#ifdef _SC_DCACHE_BLKSZ
7183 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7184#endif
7185#ifdef _SC_DCACHE_LINESZ
7186 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7187#endif
7188#ifdef _SC_DCACHE_SZ
7189 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7190#endif
7191#ifdef _SC_DCACHE_TBLKSZ
7192 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7193#endif
7194#ifdef _SC_DELAYTIMER_MAX
7195 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7196#endif
7197#ifdef _SC_EQUIV_CLASS_MAX
7198 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7199#endif
7200#ifdef _SC_EXPR_NEST_MAX
7201 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7202#endif
7203#ifdef _SC_FSYNC
7204 {"SC_FSYNC", _SC_FSYNC},
7205#endif
7206#ifdef _SC_GETGR_R_SIZE_MAX
7207 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7208#endif
7209#ifdef _SC_GETPW_R_SIZE_MAX
7210 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7211#endif
7212#ifdef _SC_ICACHE_ASSOC
7213 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7214#endif
7215#ifdef _SC_ICACHE_BLKSZ
7216 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7217#endif
7218#ifdef _SC_ICACHE_LINESZ
7219 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7220#endif
7221#ifdef _SC_ICACHE_SZ
7222 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7223#endif
Fred Draked86ed291999-12-15 15:34:33 +00007224#ifdef _SC_INF
7225 {"SC_INF", _SC_INF},
7226#endif
Fred Drakec9680921999-12-13 16:37:25 +00007227#ifdef _SC_INT_MAX
7228 {"SC_INT_MAX", _SC_INT_MAX},
7229#endif
7230#ifdef _SC_INT_MIN
7231 {"SC_INT_MIN", _SC_INT_MIN},
7232#endif
7233#ifdef _SC_IOV_MAX
7234 {"SC_IOV_MAX", _SC_IOV_MAX},
7235#endif
Fred Draked86ed291999-12-15 15:34:33 +00007236#ifdef _SC_IP_SECOPTS
7237 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7238#endif
Fred Drakec9680921999-12-13 16:37:25 +00007239#ifdef _SC_JOB_CONTROL
7240 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7241#endif
Fred Draked86ed291999-12-15 15:34:33 +00007242#ifdef _SC_KERN_POINTERS
7243 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7244#endif
7245#ifdef _SC_KERN_SIM
7246 {"SC_KERN_SIM", _SC_KERN_SIM},
7247#endif
Fred Drakec9680921999-12-13 16:37:25 +00007248#ifdef _SC_LINE_MAX
7249 {"SC_LINE_MAX", _SC_LINE_MAX},
7250#endif
7251#ifdef _SC_LOGIN_NAME_MAX
7252 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7253#endif
7254#ifdef _SC_LOGNAME_MAX
7255 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7256#endif
7257#ifdef _SC_LONG_BIT
7258 {"SC_LONG_BIT", _SC_LONG_BIT},
7259#endif
Fred Draked86ed291999-12-15 15:34:33 +00007260#ifdef _SC_MAC
7261 {"SC_MAC", _SC_MAC},
7262#endif
Fred Drakec9680921999-12-13 16:37:25 +00007263#ifdef _SC_MAPPED_FILES
7264 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7265#endif
7266#ifdef _SC_MAXPID
7267 {"SC_MAXPID", _SC_MAXPID},
7268#endif
7269#ifdef _SC_MB_LEN_MAX
7270 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7271#endif
7272#ifdef _SC_MEMLOCK
7273 {"SC_MEMLOCK", _SC_MEMLOCK},
7274#endif
7275#ifdef _SC_MEMLOCK_RANGE
7276 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7277#endif
7278#ifdef _SC_MEMORY_PROTECTION
7279 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7280#endif
7281#ifdef _SC_MESSAGE_PASSING
7282 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7283#endif
Fred Draked86ed291999-12-15 15:34:33 +00007284#ifdef _SC_MMAP_FIXED_ALIGNMENT
7285 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7286#endif
Fred Drakec9680921999-12-13 16:37:25 +00007287#ifdef _SC_MQ_OPEN_MAX
7288 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7289#endif
7290#ifdef _SC_MQ_PRIO_MAX
7291 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7292#endif
Fred Draked86ed291999-12-15 15:34:33 +00007293#ifdef _SC_NACLS_MAX
7294 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7295#endif
Fred Drakec9680921999-12-13 16:37:25 +00007296#ifdef _SC_NGROUPS_MAX
7297 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7298#endif
7299#ifdef _SC_NL_ARGMAX
7300 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7301#endif
7302#ifdef _SC_NL_LANGMAX
7303 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7304#endif
7305#ifdef _SC_NL_MSGMAX
7306 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7307#endif
7308#ifdef _SC_NL_NMAX
7309 {"SC_NL_NMAX", _SC_NL_NMAX},
7310#endif
7311#ifdef _SC_NL_SETMAX
7312 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7313#endif
7314#ifdef _SC_NL_TEXTMAX
7315 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7316#endif
7317#ifdef _SC_NPROCESSORS_CONF
7318 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7319#endif
7320#ifdef _SC_NPROCESSORS_ONLN
7321 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7322#endif
Fred Draked86ed291999-12-15 15:34:33 +00007323#ifdef _SC_NPROC_CONF
7324 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7325#endif
7326#ifdef _SC_NPROC_ONLN
7327 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7328#endif
Fred Drakec9680921999-12-13 16:37:25 +00007329#ifdef _SC_NZERO
7330 {"SC_NZERO", _SC_NZERO},
7331#endif
7332#ifdef _SC_OPEN_MAX
7333 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7334#endif
7335#ifdef _SC_PAGESIZE
7336 {"SC_PAGESIZE", _SC_PAGESIZE},
7337#endif
7338#ifdef _SC_PAGE_SIZE
7339 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7340#endif
7341#ifdef _SC_PASS_MAX
7342 {"SC_PASS_MAX", _SC_PASS_MAX},
7343#endif
7344#ifdef _SC_PHYS_PAGES
7345 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7346#endif
7347#ifdef _SC_PII
7348 {"SC_PII", _SC_PII},
7349#endif
7350#ifdef _SC_PII_INTERNET
7351 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7352#endif
7353#ifdef _SC_PII_INTERNET_DGRAM
7354 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7355#endif
7356#ifdef _SC_PII_INTERNET_STREAM
7357 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7358#endif
7359#ifdef _SC_PII_OSI
7360 {"SC_PII_OSI", _SC_PII_OSI},
7361#endif
7362#ifdef _SC_PII_OSI_CLTS
7363 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7364#endif
7365#ifdef _SC_PII_OSI_COTS
7366 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7367#endif
7368#ifdef _SC_PII_OSI_M
7369 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7370#endif
7371#ifdef _SC_PII_SOCKET
7372 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7373#endif
7374#ifdef _SC_PII_XTI
7375 {"SC_PII_XTI", _SC_PII_XTI},
7376#endif
7377#ifdef _SC_POLL
7378 {"SC_POLL", _SC_POLL},
7379#endif
7380#ifdef _SC_PRIORITIZED_IO
7381 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7382#endif
7383#ifdef _SC_PRIORITY_SCHEDULING
7384 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7385#endif
7386#ifdef _SC_REALTIME_SIGNALS
7387 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7388#endif
7389#ifdef _SC_RE_DUP_MAX
7390 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7391#endif
7392#ifdef _SC_RTSIG_MAX
7393 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7394#endif
7395#ifdef _SC_SAVED_IDS
7396 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7397#endif
7398#ifdef _SC_SCHAR_MAX
7399 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7400#endif
7401#ifdef _SC_SCHAR_MIN
7402 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7403#endif
7404#ifdef _SC_SELECT
7405 {"SC_SELECT", _SC_SELECT},
7406#endif
7407#ifdef _SC_SEMAPHORES
7408 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7409#endif
7410#ifdef _SC_SEM_NSEMS_MAX
7411 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7412#endif
7413#ifdef _SC_SEM_VALUE_MAX
7414 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7415#endif
7416#ifdef _SC_SHARED_MEMORY_OBJECTS
7417 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7418#endif
7419#ifdef _SC_SHRT_MAX
7420 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7421#endif
7422#ifdef _SC_SHRT_MIN
7423 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7424#endif
7425#ifdef _SC_SIGQUEUE_MAX
7426 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7427#endif
7428#ifdef _SC_SIGRT_MAX
7429 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7430#endif
7431#ifdef _SC_SIGRT_MIN
7432 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7433#endif
Fred Draked86ed291999-12-15 15:34:33 +00007434#ifdef _SC_SOFTPOWER
7435 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7436#endif
Fred Drakec9680921999-12-13 16:37:25 +00007437#ifdef _SC_SPLIT_CACHE
7438 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7439#endif
7440#ifdef _SC_SSIZE_MAX
7441 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7442#endif
7443#ifdef _SC_STACK_PROT
7444 {"SC_STACK_PROT", _SC_STACK_PROT},
7445#endif
7446#ifdef _SC_STREAM_MAX
7447 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7448#endif
7449#ifdef _SC_SYNCHRONIZED_IO
7450 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7451#endif
7452#ifdef _SC_THREADS
7453 {"SC_THREADS", _SC_THREADS},
7454#endif
7455#ifdef _SC_THREAD_ATTR_STACKADDR
7456 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7457#endif
7458#ifdef _SC_THREAD_ATTR_STACKSIZE
7459 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7460#endif
7461#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7462 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7463#endif
7464#ifdef _SC_THREAD_KEYS_MAX
7465 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7466#endif
7467#ifdef _SC_THREAD_PRIORITY_SCHEDULING
7468 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7469#endif
7470#ifdef _SC_THREAD_PRIO_INHERIT
7471 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7472#endif
7473#ifdef _SC_THREAD_PRIO_PROTECT
7474 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7475#endif
7476#ifdef _SC_THREAD_PROCESS_SHARED
7477 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7478#endif
7479#ifdef _SC_THREAD_SAFE_FUNCTIONS
7480 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7481#endif
7482#ifdef _SC_THREAD_STACK_MIN
7483 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7484#endif
7485#ifdef _SC_THREAD_THREADS_MAX
7486 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7487#endif
7488#ifdef _SC_TIMERS
7489 {"SC_TIMERS", _SC_TIMERS},
7490#endif
7491#ifdef _SC_TIMER_MAX
7492 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7493#endif
7494#ifdef _SC_TTY_NAME_MAX
7495 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7496#endif
7497#ifdef _SC_TZNAME_MAX
7498 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7499#endif
7500#ifdef _SC_T_IOV_MAX
7501 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7502#endif
7503#ifdef _SC_UCHAR_MAX
7504 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7505#endif
7506#ifdef _SC_UINT_MAX
7507 {"SC_UINT_MAX", _SC_UINT_MAX},
7508#endif
7509#ifdef _SC_UIO_MAXIOV
7510 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7511#endif
7512#ifdef _SC_ULONG_MAX
7513 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7514#endif
7515#ifdef _SC_USHRT_MAX
7516 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7517#endif
7518#ifdef _SC_VERSION
7519 {"SC_VERSION", _SC_VERSION},
7520#endif
7521#ifdef _SC_WORD_BIT
7522 {"SC_WORD_BIT", _SC_WORD_BIT},
7523#endif
7524#ifdef _SC_XBS5_ILP32_OFF32
7525 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7526#endif
7527#ifdef _SC_XBS5_ILP32_OFFBIG
7528 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7529#endif
7530#ifdef _SC_XBS5_LP64_OFF64
7531 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7532#endif
7533#ifdef _SC_XBS5_LPBIG_OFFBIG
7534 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7535#endif
7536#ifdef _SC_XOPEN_CRYPT
7537 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7538#endif
7539#ifdef _SC_XOPEN_ENH_I18N
7540 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7541#endif
7542#ifdef _SC_XOPEN_LEGACY
7543 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7544#endif
7545#ifdef _SC_XOPEN_REALTIME
7546 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7547#endif
7548#ifdef _SC_XOPEN_REALTIME_THREADS
7549 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7550#endif
7551#ifdef _SC_XOPEN_SHM
7552 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7553#endif
7554#ifdef _SC_XOPEN_UNIX
7555 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7556#endif
7557#ifdef _SC_XOPEN_VERSION
7558 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7559#endif
7560#ifdef _SC_XOPEN_XCU_VERSION
7561 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7562#endif
7563#ifdef _SC_XOPEN_XPG2
7564 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7565#endif
7566#ifdef _SC_XOPEN_XPG3
7567 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7568#endif
7569#ifdef _SC_XOPEN_XPG4
7570 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7571#endif
7572};
7573
7574static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007575conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007576{
7577 return conv_confname(arg, valuep, posix_constants_sysconf,
7578 sizeof(posix_constants_sysconf)
7579 / sizeof(struct constdef));
7580}
7581
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007582PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007583"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007584Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007585
7586static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007587posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007588{
7589 PyObject *result = NULL;
7590 int name;
7591
7592 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7593 int value;
7594
7595 errno = 0;
7596 value = sysconf(name);
7597 if (value == -1 && errno != 0)
7598 posix_error();
7599 else
7600 result = PyInt_FromLong(value);
7601 }
7602 return result;
7603}
7604#endif
7605
7606
Fred Drakebec628d1999-12-15 18:31:10 +00007607/* This code is used to ensure that the tables of configuration value names
7608 * are in sorted order as required by conv_confname(), and also to build the
7609 * the exported dictionaries that are used to publish information about the
7610 * names available on the host platform.
7611 *
7612 * Sorting the table at runtime ensures that the table is properly ordered
7613 * when used, even for platforms we're not able to test on. It also makes
7614 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007615 */
Fred Drakebec628d1999-12-15 18:31:10 +00007616
7617static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007618cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007619{
7620 const struct constdef *c1 =
7621 (const struct constdef *) v1;
7622 const struct constdef *c2 =
7623 (const struct constdef *) v2;
7624
7625 return strcmp(c1->name, c2->name);
7626}
7627
7628static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007629setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007630 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007631{
Fred Drakebec628d1999-12-15 18:31:10 +00007632 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007633 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007634
7635 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7636 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007637 if (d == NULL)
7638 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007639
Barry Warsaw3155db32000-04-13 15:20:40 +00007640 for (i=0; i < tablesize; ++i) {
7641 PyObject *o = PyInt_FromLong(table[i].value);
7642 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7643 Py_XDECREF(o);
7644 Py_DECREF(d);
7645 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007646 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007647 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007648 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007649 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007650}
7651
Fred Drakebec628d1999-12-15 18:31:10 +00007652/* Return -1 on failure, 0 on success. */
7653static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007654setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007655{
7656#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007657 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007658 sizeof(posix_constants_pathconf)
7659 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007660 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007661 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007662#endif
7663#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007664 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007665 sizeof(posix_constants_confstr)
7666 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007667 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007668 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007669#endif
7670#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007671 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007672 sizeof(posix_constants_sysconf)
7673 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007674 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007675 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007676#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007677 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007678}
Fred Draked86ed291999-12-15 15:34:33 +00007679
7680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007681PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007682"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007683Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007684in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007685
7686static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007687posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007688{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007689 abort();
7690 /*NOTREACHED*/
7691 Py_FatalError("abort() called from Python code didn't abort!");
7692 return NULL;
7693}
Fred Drakebec628d1999-12-15 18:31:10 +00007694
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007695#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007696PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007697"startfile(filepath [, operation]) - Start a file with its associated\n\
7698application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007699\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007700When \"operation\" is not specified or \"open\", this acts like\n\
7701double-clicking the file in Explorer, or giving the file name as an\n\
7702argument to the DOS \"start\" command: the file is opened with whatever\n\
7703application (if any) its extension is associated.\n\
7704When another \"operation\" is given, it specifies what should be done with\n\
7705the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007706\n\
7707startfile returns as soon as the associated application is launched.\n\
7708There is no option to wait for the application to close, and no way\n\
7709to retrieve the application's exit status.\n\
7710\n\
7711The filepath is relative to the current directory. If you want to use\n\
7712an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007713the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007714
7715static PyObject *
7716win32_startfile(PyObject *self, PyObject *args)
7717{
7718 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00007719 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007720 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007721#ifdef Py_WIN_WIDE_FILENAMES
7722 if (unicode_file_names()) {
7723 PyObject *unipath, *woperation = NULL;
7724 if (!PyArg_ParseTuple(args, "U|s:startfile",
7725 &unipath, &operation)) {
7726 PyErr_Clear();
7727 goto normal;
7728 }
7729
7730
7731 if (operation) {
7732 woperation = PyUnicode_DecodeASCII(operation,
7733 strlen(operation), NULL);
7734 if (!woperation) {
7735 PyErr_Clear();
7736 operation = NULL;
7737 goto normal;
7738 }
7739 }
7740
7741 Py_BEGIN_ALLOW_THREADS
7742 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7743 PyUnicode_AS_UNICODE(unipath),
7744 NULL, NULL, SW_SHOWNORMAL);
7745 Py_END_ALLOW_THREADS
7746
7747 Py_XDECREF(woperation);
7748 if (rc <= (HINSTANCE)32) {
7749 PyObject *errval = win32_error_unicode("startfile",
7750 PyUnicode_AS_UNICODE(unipath));
7751 return errval;
7752 }
7753 Py_INCREF(Py_None);
7754 return Py_None;
7755 }
7756#endif
7757
7758normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00007759 if (!PyArg_ParseTuple(args, "et|s:startfile",
7760 Py_FileSystemDefaultEncoding, &filepath,
7761 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00007762 return NULL;
7763 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00007764 rc = ShellExecute((HWND)0, operation, filepath,
7765 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007766 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00007767 if (rc <= (HINSTANCE)32) {
7768 PyObject *errval = win32_error("startfile", filepath);
7769 PyMem_Free(filepath);
7770 return errval;
7771 }
7772 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007773 Py_INCREF(Py_None);
7774 return Py_None;
7775}
7776#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007777
Martin v. Löwis438b5342002-12-27 10:16:42 +00007778#ifdef HAVE_GETLOADAVG
7779PyDoc_STRVAR(posix_getloadavg__doc__,
7780"getloadavg() -> (float, float, float)\n\n\
7781Return the number of processes in the system run queue averaged over\n\
7782the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7783was unobtainable");
7784
7785static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007786posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007787{
7788 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007789 if (getloadavg(loadavg, 3)!=3) {
7790 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7791 return NULL;
7792 } else
7793 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
7794}
7795#endif
7796
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007797#ifdef MS_WINDOWS
7798
7799PyDoc_STRVAR(win32_urandom__doc__,
7800"urandom(n) -> str\n\n\
7801Return a string of n random bytes suitable for cryptographic use.");
7802
7803typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7804 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7805 DWORD dwFlags );
7806typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7807 BYTE *pbBuffer );
7808
7809static CRYPTGENRANDOM pCryptGenRandom = NULL;
7810static HCRYPTPROV hCryptProv = 0;
7811
Tim Peters4ad82172004-08-30 17:02:04 +00007812static PyObject*
7813win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007814{
Tim Petersd3115382004-08-30 17:36:46 +00007815 int howMany;
7816 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007817
Tim Peters4ad82172004-08-30 17:02:04 +00007818 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00007819 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00007820 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00007821 if (howMany < 0)
7822 return PyErr_Format(PyExc_ValueError,
7823 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007824
Tim Peters4ad82172004-08-30 17:02:04 +00007825 if (hCryptProv == 0) {
7826 HINSTANCE hAdvAPI32 = NULL;
7827 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007828
Tim Peters4ad82172004-08-30 17:02:04 +00007829 /* Obtain handle to the DLL containing CryptoAPI
7830 This should not fail */
7831 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7832 if(hAdvAPI32 == NULL)
7833 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007834
Tim Peters4ad82172004-08-30 17:02:04 +00007835 /* Obtain pointers to the CryptoAPI functions
7836 This will fail on some early versions of Win95 */
7837 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7838 hAdvAPI32,
7839 "CryptAcquireContextA");
7840 if (pCryptAcquireContext == NULL)
7841 return PyErr_Format(PyExc_NotImplementedError,
7842 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007843
Tim Peters4ad82172004-08-30 17:02:04 +00007844 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7845 hAdvAPI32, "CryptGenRandom");
7846 if (pCryptAcquireContext == NULL)
7847 return PyErr_Format(PyExc_NotImplementedError,
7848 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007849
Tim Peters4ad82172004-08-30 17:02:04 +00007850 /* Acquire context */
7851 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7852 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7853 return win32_error("CryptAcquireContext", NULL);
7854 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007855
Tim Peters4ad82172004-08-30 17:02:04 +00007856 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00007857 result = PyString_FromStringAndSize(NULL, howMany);
7858 if (result != NULL) {
7859 /* Get random data */
7860 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7861 PyString_AS_STRING(result))) {
7862 Py_DECREF(result);
7863 return win32_error("CryptGenRandom", NULL);
7864 }
Tim Peters4ad82172004-08-30 17:02:04 +00007865 }
Tim Petersd3115382004-08-30 17:36:46 +00007866 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007867}
7868#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007869
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007870static PyMethodDef posix_methods[] = {
7871 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7872#ifdef HAVE_TTYNAME
7873 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7874#endif
7875 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
7876 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007877#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007878 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007879#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007880#ifdef HAVE_LCHOWN
7881 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7882#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007883#ifdef HAVE_CHROOT
7884 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7885#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007886#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007887 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007888#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007889#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00007890 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00007891#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00007892 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007893#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00007894#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007895#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007896 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007897#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007898 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7899 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7900 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007901#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007902 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007903#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007904#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007905 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007906#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007907 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7908 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7909 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007910 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007911#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007912 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007913#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007914#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007915 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007916#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007917 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007918#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007919 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007920#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007921 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7922 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7923 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007924#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007925 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007926#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007927 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007928#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007929 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7930 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007931#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007932#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007933 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7934 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007935#if defined(PYOS_OS2)
7936 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7937 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7938#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007939#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007940#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007941 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007942#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007943#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007944 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007945#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007946#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007947 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007948#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007949#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007950 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007951#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007952#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007953 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007954#endif /* HAVE_GETEGID */
7955#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007956 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007957#endif /* HAVE_GETEUID */
7958#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007959 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007960#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007961#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007962 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007963#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007964 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007965#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007966 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007967#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007968#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007969 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007970#endif /* HAVE_GETPPID */
7971#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007972 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007973#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007974#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007975 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007976#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007977#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007978 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007979#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007980#ifdef HAVE_KILLPG
7981 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7982#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007983#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007984 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007985#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007986#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007987 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007988#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007989 {"popen2", win32_popen2, METH_VARARGS},
7990 {"popen3", win32_popen3, METH_VARARGS},
7991 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00007992 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007993#else
7994#if defined(PYOS_OS2) && defined(PYCC_GCC)
7995 {"popen2", os2emx_popen2, METH_VARARGS},
7996 {"popen3", os2emx_popen3, METH_VARARGS},
7997 {"popen4", os2emx_popen4, METH_VARARGS},
7998#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007999#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008000#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008001#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008002 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008003#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008004#ifdef HAVE_SETEUID
8005 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8006#endif /* HAVE_SETEUID */
8007#ifdef HAVE_SETEGID
8008 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8009#endif /* HAVE_SETEGID */
8010#ifdef HAVE_SETREUID
8011 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8012#endif /* HAVE_SETREUID */
8013#ifdef HAVE_SETREGID
8014 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8015#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008016#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008017 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008018#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008019#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00008020 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008021#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008022#ifdef HAVE_GETPGID
8023 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8024#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008025#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008026 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008027#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008028#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00008029 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008030#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008031#ifdef HAVE_WAIT3
8032 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8033#endif /* HAVE_WAIT3 */
8034#ifdef HAVE_WAIT4
8035 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8036#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008037#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008038 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008039#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008040#ifdef HAVE_GETSID
8041 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8042#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008043#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00008044 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008045#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008046#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008047 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008048#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008049#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008050 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008051#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008052#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008053 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008054#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008055 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8056 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8057 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8058 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8059 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8060 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8061 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8062 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8063 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00008064 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008065#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00008066 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008067#endif
8068#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008069 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008070#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008071#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008072 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8073#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008074#ifdef HAVE_DEVICE_MACROS
8075 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8076 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8077 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8078#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008079#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008080 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008081#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008082#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008083 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008084#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008085#ifdef HAVE_UNSETENV
8086 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8087#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00008088#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008089 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00008090#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00008091#ifdef HAVE_FCHDIR
8092 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8093#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008094#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008095 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008096#endif
8097#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008098 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008099#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008100#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008101#ifdef WCOREDUMP
8102 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8103#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008104#ifdef WIFCONTINUED
8105 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8106#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008107#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008108 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008109#endif /* WIFSTOPPED */
8110#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008111 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008112#endif /* WIFSIGNALED */
8113#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008114 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008115#endif /* WIFEXITED */
8116#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008117 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008118#endif /* WEXITSTATUS */
8119#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008120 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008121#endif /* WTERMSIG */
8122#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008123 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008124#endif /* WSTOPSIG */
8125#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008126#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008127 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008128#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008129#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008130 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008131#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008132#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00008133 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008134#endif
8135#ifdef HAVE_TEMPNAM
8136 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8137#endif
8138#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00008139 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008140#endif
Fred Drakec9680921999-12-13 16:37:25 +00008141#ifdef HAVE_CONFSTR
8142 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8143#endif
8144#ifdef HAVE_SYSCONF
8145 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8146#endif
8147#ifdef HAVE_FPATHCONF
8148 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8149#endif
8150#ifdef HAVE_PATHCONF
8151 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8152#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008153 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008154#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00008155 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8156#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008157#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00008158 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008159#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008160 #ifdef MS_WINDOWS
8161 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8162 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008163 {NULL, NULL} /* Sentinel */
8164};
8165
8166
Barry Warsaw4a342091996-12-19 23:50:02 +00008167static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008168ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008169{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008170 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008171}
8172
Guido van Rossumd48f2521997-12-05 22:19:34 +00008173#if defined(PYOS_OS2)
8174/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008175static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008176{
8177 APIRET rc;
8178 ULONG values[QSV_MAX+1];
8179 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008180 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008181
8182 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008183 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008184 Py_END_ALLOW_THREADS
8185
8186 if (rc != NO_ERROR) {
8187 os2_error(rc);
8188 return -1;
8189 }
8190
Fred Drake4d1e64b2002-04-15 19:40:07 +00008191 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8192 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8193 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8194 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8195 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8196 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8197 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008198
8199 switch (values[QSV_VERSION_MINOR]) {
8200 case 0: ver = "2.00"; break;
8201 case 10: ver = "2.10"; break;
8202 case 11: ver = "2.11"; break;
8203 case 30: ver = "3.00"; break;
8204 case 40: ver = "4.00"; break;
8205 case 50: ver = "5.00"; break;
8206 default:
Tim Peters885d4572001-11-28 20:27:42 +00008207 PyOS_snprintf(tmp, sizeof(tmp),
8208 "%d-%d", values[QSV_VERSION_MAJOR],
8209 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008210 ver = &tmp[0];
8211 }
8212
8213 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008214 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008215 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008216
8217 /* Add Indicator of Which Drive was Used to Boot the System */
8218 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8219 tmp[1] = ':';
8220 tmp[2] = '\0';
8221
Fred Drake4d1e64b2002-04-15 19:40:07 +00008222 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008223}
8224#endif
8225
Barry Warsaw4a342091996-12-19 23:50:02 +00008226static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008227all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008228{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008229#ifdef F_OK
8230 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008231#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008232#ifdef R_OK
8233 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008234#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008235#ifdef W_OK
8236 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008237#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008238#ifdef X_OK
8239 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008240#endif
Fred Drakec9680921999-12-13 16:37:25 +00008241#ifdef NGROUPS_MAX
8242 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8243#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008244#ifdef TMP_MAX
8245 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8246#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008247#ifdef WCONTINUED
8248 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8249#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008250#ifdef WNOHANG
8251 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008252#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008253#ifdef WUNTRACED
8254 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8255#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008256#ifdef O_RDONLY
8257 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8258#endif
8259#ifdef O_WRONLY
8260 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8261#endif
8262#ifdef O_RDWR
8263 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8264#endif
8265#ifdef O_NDELAY
8266 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8267#endif
8268#ifdef O_NONBLOCK
8269 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8270#endif
8271#ifdef O_APPEND
8272 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8273#endif
8274#ifdef O_DSYNC
8275 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8276#endif
8277#ifdef O_RSYNC
8278 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8279#endif
8280#ifdef O_SYNC
8281 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8282#endif
8283#ifdef O_NOCTTY
8284 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8285#endif
8286#ifdef O_CREAT
8287 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8288#endif
8289#ifdef O_EXCL
8290 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8291#endif
8292#ifdef O_TRUNC
8293 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8294#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008295#ifdef O_BINARY
8296 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8297#endif
8298#ifdef O_TEXT
8299 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8300#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008301#ifdef O_LARGEFILE
8302 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8303#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008304#ifdef O_SHLOCK
8305 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8306#endif
8307#ifdef O_EXLOCK
8308 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8309#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008310
Tim Peters5aa91602002-01-30 05:46:57 +00008311/* MS Windows */
8312#ifdef O_NOINHERIT
8313 /* Don't inherit in child processes. */
8314 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8315#endif
8316#ifdef _O_SHORT_LIVED
8317 /* Optimize for short life (keep in memory). */
8318 /* MS forgot to define this one with a non-underscore form too. */
8319 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8320#endif
8321#ifdef O_TEMPORARY
8322 /* Automatically delete when last handle is closed. */
8323 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8324#endif
8325#ifdef O_RANDOM
8326 /* Optimize for random access. */
8327 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8328#endif
8329#ifdef O_SEQUENTIAL
8330 /* Optimize for sequential access. */
8331 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8332#endif
8333
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008334/* GNU extensions. */
8335#ifdef O_DIRECT
8336 /* Direct disk access. */
8337 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8338#endif
8339#ifdef O_DIRECTORY
8340 /* Must be a directory. */
8341 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8342#endif
8343#ifdef O_NOFOLLOW
8344 /* Do not follow links. */
8345 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8346#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008347
Barry Warsaw5676bd12003-01-07 20:57:09 +00008348 /* These come from sysexits.h */
8349#ifdef EX_OK
8350 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008351#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008352#ifdef EX_USAGE
8353 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008354#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008355#ifdef EX_DATAERR
8356 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008357#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008358#ifdef EX_NOINPUT
8359 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008360#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008361#ifdef EX_NOUSER
8362 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008363#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008364#ifdef EX_NOHOST
8365 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008366#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008367#ifdef EX_UNAVAILABLE
8368 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008369#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008370#ifdef EX_SOFTWARE
8371 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008372#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008373#ifdef EX_OSERR
8374 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008375#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008376#ifdef EX_OSFILE
8377 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008378#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008379#ifdef EX_CANTCREAT
8380 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008381#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008382#ifdef EX_IOERR
8383 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008384#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008385#ifdef EX_TEMPFAIL
8386 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008387#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008388#ifdef EX_PROTOCOL
8389 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008390#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008391#ifdef EX_NOPERM
8392 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008393#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008394#ifdef EX_CONFIG
8395 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008396#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008397#ifdef EX_NOTFOUND
8398 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008399#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008400
Guido van Rossum246bc171999-02-01 23:54:31 +00008401#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008402#if defined(PYOS_OS2) && defined(PYCC_GCC)
8403 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8404 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8405 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8406 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8407 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8408 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8409 if (ins(d, "P_PM", (long)P_PM)) return -1;
8410 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8411 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8412 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8413 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8414 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8415 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8416 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8417 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8418 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8419 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8420 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8421 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8422 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8423#else
Guido van Rossum7d385291999-02-16 19:38:04 +00008424 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8425 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8426 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8427 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8428 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008429#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008430#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008431
Guido van Rossumd48f2521997-12-05 22:19:34 +00008432#if defined(PYOS_OS2)
8433 if (insertvalues(d)) return -1;
8434#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008435 return 0;
8436}
8437
8438
Tim Peters5aa91602002-01-30 05:46:57 +00008439#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008440#define INITFUNC initnt
8441#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008442
8443#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008444#define INITFUNC initos2
8445#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008446
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008447#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008448#define INITFUNC initposix
8449#define MODNAME "posix"
8450#endif
8451
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008452PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008453INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008454{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008455 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008456
Fred Drake4d1e64b2002-04-15 19:40:07 +00008457 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008458 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008459 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00008460 if (m == NULL)
8461 return;
Tim Peters5aa91602002-01-30 05:46:57 +00008462
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008463 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008464 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00008465 Py_XINCREF(v);
8466 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008467 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00008468 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008469
Fred Drake4d1e64b2002-04-15 19:40:07 +00008470 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00008471 return;
8472
Fred Drake4d1e64b2002-04-15 19:40:07 +00008473 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00008474 return;
8475
Fred Drake4d1e64b2002-04-15 19:40:07 +00008476 Py_INCREF(PyExc_OSError);
8477 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008478
Guido van Rossumb3d39562000-01-31 18:41:26 +00008479#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00008480 if (posix_putenv_garbage == NULL)
8481 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008482#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008483
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008484 if (!initialized) {
8485 stat_result_desc.name = MODNAME ".stat_result";
8486 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8487 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8488 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8489 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8490 structseq_new = StatResultType.tp_new;
8491 StatResultType.tp_new = statresult_new;
8492
8493 statvfs_result_desc.name = MODNAME ".statvfs_result";
8494 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
8495 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008496 Py_INCREF((PyObject*) &StatResultType);
8497 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00008498 Py_INCREF((PyObject*) &StatVFSResultType);
8499 PyModule_AddObject(m, "statvfs_result",
8500 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008501 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008502
8503#ifdef __APPLE__
8504 /*
8505 * Step 2 of weak-linking support on Mac OS X.
8506 *
8507 * The code below removes functions that are not available on the
8508 * currently active platform.
8509 *
8510 * This block allow one to use a python binary that was build on
8511 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8512 * OSX 10.4.
8513 */
8514#ifdef HAVE_FSTATVFS
8515 if (fstatvfs == NULL) {
8516 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8517 return;
8518 }
8519 }
8520#endif /* HAVE_FSTATVFS */
8521
8522#ifdef HAVE_STATVFS
8523 if (statvfs == NULL) {
8524 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8525 return;
8526 }
8527 }
8528#endif /* HAVE_STATVFS */
8529
8530# ifdef HAVE_LCHOWN
8531 if (lchown == NULL) {
8532 if (PyObject_DelAttrString(m, "lchown") == -1) {
8533 return;
8534 }
8535 }
8536#endif /* HAVE_LCHOWN */
8537
8538
8539#endif /* __APPLE__ */
8540
Guido van Rossumb6775db1994-08-01 11:34:53 +00008541}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008542
8543#ifdef __cplusplus
8544}
8545#endif
8546