blob: 6e4925b5f5729a4be3c013ba0180ea9353186f65 [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
Ronald Oussorend06b6f22006-04-23 11:59:25 +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
Anthony Baxterac6bd462006-04-13 02:06:09 +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
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000067#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/types.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000069#endif /* HAVE_SYS_TYPES_H */
70
71#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000072#include <sys/stat.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000073#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000074
Guido van Rossum36bc6801995-06-14 22:54:23 +000075#ifdef HAVE_SYS_WAIT_H
76#include <sys/wait.h> /* For WNOHANG */
77#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000078
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000079#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000080#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000081#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000082
Guido van Rossumb6775db1994-08-01 11:34:53 +000083#ifdef HAVE_FCNTL_H
84#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000085#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000086
Guido van Rossuma6535fd2001-10-18 19:44:10 +000087#ifdef HAVE_GRP_H
88#include <grp.h>
89#endif
90
Barry Warsaw5676bd12003-01-07 20:57:09 +000091#ifdef HAVE_SYSEXITS_H
92#include <sysexits.h>
93#endif /* HAVE_SYSEXITS_H */
94
Anthony Baxter8a560de2004-10-13 15:30:56 +000095#ifdef HAVE_SYS_LOADAVG_H
96#include <sys/loadavg.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
107#define HAVE_SYSTEM 1
108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
114#ifdef __BORLANDC__ /* Borland compiler */
115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
119#define HAVE_POPEN 1
120#define HAVE_SYSTEM 1
121#define HAVE_WAIT 1
122#else
123#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000124#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000125#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_EXECV 1
127#define HAVE_PIPE 1
128#define HAVE_POPEN 1
129#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000130#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000131#define HAVE_FSYNC 1
132#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000133#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000134#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
135/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136#else /* all other compilers */
137/* Unix functions that the configure script doesn't check for */
138#define HAVE_EXECV 1
139#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000140#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
141#define HAVE_FORK1 1
142#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000143#define HAVE_GETCWD 1
144#define HAVE_GETEGID 1
145#define HAVE_GETEUID 1
146#define HAVE_GETGID 1
147#define HAVE_GETPPID 1
148#define HAVE_GETUID 1
149#define HAVE_KILL 1
150#define HAVE_OPENDIR 1
151#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000152#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000154#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000155#define HAVE_SYSTEM 1
156#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000157#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000158#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#endif /* _MSC_VER */
160#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000161#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000162#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000163
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000164#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000165
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000166#if defined(__sgi)&&_COMPILER_VERSION>=700
167/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
168 (default) */
169extern char *ctermid_r(char *);
170#endif
171
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000172#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000173#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000175#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000176#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000177extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000180#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#endif
182#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000183extern int chdir(char *);
184extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000185#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000186extern int chdir(const char *);
187extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000188#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000189#ifdef __BORLANDC__
190extern int chmod(const char *, int);
191#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000192extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000193#endif
Christian Heimes36281872007-11-30 21:11:28 +0000194/*#ifdef HAVE_FCHMOD
195extern int fchmod(int, mode_t);
196#endif*/
197/*#ifdef HAVE_LCHMOD
198extern int lchmod(const char *, mode_t);
199#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000200extern int chown(const char *, uid_t, gid_t);
201extern char *getcwd(char *, int);
202extern char *strerror(int);
203extern int link(const char *, const char *);
204extern int rename(const char *, const char *);
205extern int stat(const char *, struct stat *);
206extern int unlink(const char *);
207extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000209extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000210#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000213#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000214#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000215
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000216#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218#ifdef HAVE_UTIME_H
219#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000220#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000222#ifdef HAVE_SYS_UTIME_H
223#include <sys/utime.h>
224#define HAVE_UTIME_H /* pretend we do for the rest of this file */
225#endif /* HAVE_SYS_UTIME_H */
226
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#ifdef HAVE_SYS_TIMES_H
228#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000229#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230
231#ifdef HAVE_SYS_PARAM_H
232#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000233#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234
235#ifdef HAVE_SYS_UTSNAME_H
236#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000237#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000239#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000241#define NAMLEN(dirent) strlen((dirent)->d_name)
242#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000243#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000244#include <direct.h>
245#define NAMLEN(dirent) strlen((dirent)->d_name)
246#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000248#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000249#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000255#endif
256#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000258#endif
259#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000261#ifdef _MSC_VER
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000262#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <direct.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000264#endif
265#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#include <io.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000267#endif
268#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269#include <process.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000270#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000271#include "osdefs.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000273#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000274#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000275#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000276#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277
Guido van Rossumd48f2521997-12-05 22:19:34 +0000278#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000280#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281
Tim Petersbc2e10e2002-03-03 23:17:02 +0000282#ifndef MAXPATHLEN
Thomas Wouters1ddba602006-04-25 15:29:46 +0000283#if defined(PATH_MAX) && PATH_MAX > 1024
284#define MAXPATHLEN PATH_MAX
285#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000286#define MAXPATHLEN 1024
Thomas Wouters1ddba602006-04-25 15:29:46 +0000287#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000288#endif /* MAXPATHLEN */
289
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000290#ifdef UNION_WAIT
291/* Emulate some macros on systems that have a union instead of macros */
292
293#ifndef WIFEXITED
294#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
295#endif
296
297#ifndef WEXITSTATUS
298#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
299#endif
300
301#ifndef WTERMSIG
302#define WTERMSIG(u_wait) ((u_wait).w_termsig)
303#endif
304
Neal Norwitzd5a37542006-03-20 06:48:34 +0000305#define WAIT_TYPE union wait
306#define WAIT_STATUS_INT(s) (s.w_status)
307
308#else /* !UNION_WAIT */
309#define WAIT_TYPE int
310#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000311#endif /* UNION_WAIT */
312
Greg Wardb48bc172000-03-01 21:51:56 +0000313/* Don't use the "_r" form if we don't need it (also, won't have a
314 prototype for it, at least on Solaris -- maybe others as well?). */
315#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
316#define USE_CTERMID_R
317#endif
318
319#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
320#define USE_TMPNAM_R
321#endif
322
Fred Drake699f3522000-06-29 21:12:41 +0000323/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000324#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000325#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000326# define STAT win32_stat
327# define FSTAT win32_fstat
328# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000329#else
330# define STAT stat
331# define FSTAT fstat
332# define STRUCT_STAT struct stat
333#endif
334
Tim Peters11b23062003-04-23 02:39:17 +0000335#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000336#include <sys/mkdev.h>
337#else
338#if defined(MAJOR_IN_SYSMACROS)
339#include <sys/sysmacros.h>
340#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000341#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
342#include <sys/mkdev.h>
343#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000344#endif
Fred Drake699f3522000-06-29 21:12:41 +0000345
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000347#ifdef WITH_NEXT_FRAMEWORK
348/* On Darwin/MacOSX a shared library or framework has no access to
349** environ directly, we must obtain it with _NSGetEnviron().
350*/
351#include <crt_externs.h>
352static char **environ;
353#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000355#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356
Barry Warsaw53699e91996-12-10 23:23:01 +0000357static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000358convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359{
Barry Warsaw53699e91996-12-10 23:23:01 +0000360 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000362 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363 if (d == NULL)
364 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000365#ifdef WITH_NEXT_FRAMEWORK
366 if (environ == NULL)
367 environ = *_NSGetEnviron();
368#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369 if (environ == NULL)
370 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000371 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000373 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000374 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375 char *p = strchr(*e, '=');
376 if (p == NULL)
377 continue;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000378 k = PyString_FromStringAndSize(*e, (int)(p-*e));
Guido van Rossum6a619f41999-08-03 19:41:10 +0000379 if (k == NULL) {
380 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000382 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000383 v = PyString_FromString(p+1);
Guido van Rossum6a619f41999-08-03 19:41:10 +0000384 if (v == NULL) {
385 PyErr_Clear();
386 Py_DECREF(k);
387 continue;
388 }
389 if (PyDict_GetItem(d, k) == NULL) {
390 if (PyDict_SetItem(d, k, v) != 0)
391 PyErr_Clear();
392 }
393 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000394 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000396#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000397 {
398 APIRET rc;
399 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
400
401 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000402 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000403 PyObject *v = PyString_FromString(buffer);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000404 PyDict_SetItemString(d, "BEGINLIBPATH", v);
405 Py_DECREF(v);
406 }
407 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
408 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000409 PyObject *v = PyString_FromString(buffer);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000410 PyDict_SetItemString(d, "ENDLIBPATH", v);
411 Py_DECREF(v);
412 }
413 }
414#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415 return d;
416}
417
418
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419/* Set a POSIX-specific error from errno, and return NULL */
420
Barry Warsawd58d7641998-07-23 16:14:40 +0000421static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000422posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000423{
Barry Warsawca74da41999-02-09 19:31:45 +0000424 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425}
Barry Warsawd58d7641998-07-23 16:14:40 +0000426static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000427posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000428{
Barry Warsawca74da41999-02-09 19:31:45 +0000429 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000430}
431
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000432#ifdef Py_WIN_WIDE_FILENAMES
433static PyObject *
434posix_error_with_unicode_filename(Py_UNICODE* name)
435{
436 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
437}
438#endif /* Py_WIN_WIDE_FILENAMES */
439
440
Mark Hammondef8b6542001-05-13 08:04:26 +0000441static PyObject *
442posix_error_with_allocated_filename(char* name)
443{
444 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
445 PyMem_Free(name);
446 return rc;
447}
448
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000449#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000450static PyObject *
451win32_error(char* function, char* filename)
452{
Mark Hammond33a6da92000-08-15 00:46:38 +0000453 /* XXX We should pass the function name along in the future.
454 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000455 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000456 Windows error object, which is non-trivial.
457 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000458 errno = GetLastError();
459 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000460 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000461 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000462 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000463}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000464
465#ifdef Py_WIN_WIDE_FILENAMES
466static PyObject *
467win32_error_unicode(char* function, Py_UNICODE* filename)
468{
469 /* XXX - see win32_error for comments on 'function' */
470 errno = GetLastError();
471 if (filename)
472 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
473 else
474 return PyErr_SetFromWindowsErr(errno);
475}
476
477static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
478{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000479}
480
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000481static int
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000482convert_to_unicode(PyObject **param)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000483{
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000484 if (PyUnicode_CheckExact(*param))
485 Py_INCREF(*param);
486 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000487 /* For a Unicode subtype that's not a Unicode object,
488 return a true Unicode object with the same data. */
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000489 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
490 PyUnicode_GET_SIZE(*param));
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000491 else
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000492 *param = PyUnicode_FromEncodedObject(*param,
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000493 Py_FileSystemDefaultEncoding,
494 "strict");
495 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000496}
497
498#endif /* Py_WIN_WIDE_FILENAMES */
499
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000500#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000501
Guido van Rossumd48f2521997-12-05 22:19:34 +0000502#if defined(PYOS_OS2)
503/**********************************************************************
504 * Helper Function to Trim and Format OS/2 Messages
505 **********************************************************************/
506 static void
507os2_formatmsg(char *msgbuf, int msglen, char *reason)
508{
509 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
510
511 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
512 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
513
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000514 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000515 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
516 }
517
518 /* Add Optional Reason Text */
519 if (reason) {
520 strcat(msgbuf, " : ");
521 strcat(msgbuf, reason);
522 }
523}
524
525/**********************************************************************
526 * Decode an OS/2 Operating System Error Code
527 *
528 * A convenience function to lookup an OS/2 error code and return a
529 * text message we can use to raise a Python exception.
530 *
531 * Notes:
532 * The messages for errors returned from the OS/2 kernel reside in
533 * the file OSO001.MSG in the \OS2 directory hierarchy.
534 *
535 **********************************************************************/
536 static char *
537os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
538{
539 APIRET rc;
540 ULONG msglen;
541
542 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
543 Py_BEGIN_ALLOW_THREADS
544 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
545 errorcode, "oso001.msg", &msglen);
546 Py_END_ALLOW_THREADS
547
548 if (rc == NO_ERROR)
549 os2_formatmsg(msgbuf, msglen, reason);
550 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000551 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000552 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000553
554 return msgbuf;
555}
556
557/* Set an OS/2-specific error and return NULL. OS/2 kernel
558 errors are not in a global variable e.g. 'errno' nor are
559 they congruent with posix error numbers. */
560
561static PyObject * os2_error(int code)
562{
563 char text[1024];
564 PyObject *v;
565
566 os2_strerror(text, sizeof(text), code, "");
567
568 v = Py_BuildValue("(is)", code, text);
569 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000570 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000571 Py_DECREF(v);
572 }
573 return NULL; /* Signal to Python that an Exception is Pending */
574}
575
576#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577
578/* POSIX generic methods */
579
Barry Warsaw53699e91996-12-10 23:23:01 +0000580static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000581posix_fildes(PyObject *fdobj, int (*func)(int))
582{
583 int fd;
584 int res;
585 fd = PyObject_AsFileDescriptor(fdobj);
586 if (fd < 0)
587 return NULL;
588 Py_BEGIN_ALLOW_THREADS
589 res = (*func)(fd);
590 Py_END_ALLOW_THREADS
591 if (res < 0)
592 return posix_error();
593 Py_INCREF(Py_None);
594 return Py_None;
595}
Guido van Rossum21142a01999-01-08 21:05:37 +0000596
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000597#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000598static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000599unicode_file_names(void)
600{
601 static int canusewide = -1;
602 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000603 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000604 the Windows NT family. */
605 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
606 }
607 return canusewide;
608}
609#endif
Tim Peters11b23062003-04-23 02:39:17 +0000610
Guido van Rossum21142a01999-01-08 21:05:37 +0000611static PyObject *
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000612posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000613{
Mark Hammondef8b6542001-05-13 08:04:26 +0000614 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000615 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000616 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000617 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000618 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000619 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000620 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000621 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000622 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000623 return posix_error_with_allocated_filename(path1);
624 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000625 Py_INCREF(Py_None);
626 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000627}
628
Barry Warsaw53699e91996-12-10 23:23:01 +0000629static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000630posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000631 char *format,
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000632 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000633{
Mark Hammondef8b6542001-05-13 08:04:26 +0000634 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000635 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000636 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000637 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000638 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000640 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000641 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000642 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000643 PyMem_Free(path1);
644 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000645 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000646 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000648 Py_INCREF(Py_None);
649 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000650}
651
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000652#ifdef Py_WIN_WIDE_FILENAMES
653static PyObject*
654win32_1str(PyObject* args, char* func,
655 char* format, BOOL (__stdcall *funcA)(LPCSTR),
656 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
657{
658 PyObject *uni;
659 char *ansi;
660 BOOL result;
661 if (unicode_file_names()) {
662 if (!PyArg_ParseTuple(args, wformat, &uni))
663 PyErr_Clear();
664 else {
665 Py_BEGIN_ALLOW_THREADS
666 result = funcW(PyUnicode_AsUnicode(uni));
667 Py_END_ALLOW_THREADS
668 if (!result)
669 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
670 Py_INCREF(Py_None);
671 return Py_None;
672 }
673 }
674 if (!PyArg_ParseTuple(args, format, &ansi))
675 return NULL;
676 Py_BEGIN_ALLOW_THREADS
677 result = funcA(ansi);
678 Py_END_ALLOW_THREADS
679 if (!result)
680 return win32_error(func, ansi);
681 Py_INCREF(Py_None);
682 return Py_None;
683
684}
685
686/* This is a reimplementation of the C library's chdir function,
687 but one that produces Win32 errors instead of DOS error codes.
688 chdir is essentially a wrapper around SetCurrentDirectory; however,
689 it also needs to set "magic" environment variables indicating
690 the per-drive current directory, which are of the form =<drive>: */
691BOOL __stdcall
692win32_chdir(LPCSTR path)
693{
694 char new_path[MAX_PATH+1];
695 int result;
696 char env[4] = "=x:";
697
698 if(!SetCurrentDirectoryA(path))
699 return FALSE;
700 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
701 if (!result)
702 return FALSE;
703 /* In the ANSI API, there should not be any paths longer
704 than MAX_PATH. */
705 assert(result <= MAX_PATH+1);
706 if (strncmp(new_path, "\\\\", 2) == 0 ||
707 strncmp(new_path, "//", 2) == 0)
708 /* UNC path, nothing to do. */
709 return TRUE;
710 env[1] = new_path[0];
711 return SetEnvironmentVariableA(env, new_path);
712}
713
714/* The Unicode version differs from the ANSI version
715 since the current directory might exceed MAX_PATH characters */
716BOOL __stdcall
717win32_wchdir(LPCWSTR path)
718{
719 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
720 int result;
721 wchar_t env[4] = L"=x:";
722
723 if(!SetCurrentDirectoryW(path))
724 return FALSE;
725 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
726 if (!result)
727 return FALSE;
728 if (result > MAX_PATH+1) {
Hirokazu Yamamoto10a018c2008-10-09 10:00:30 +0000729 new_path = malloc(result * sizeof(wchar_t));
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000730 if (!new_path) {
731 SetLastError(ERROR_OUTOFMEMORY);
732 return FALSE;
733 }
Hirokazu Yamamoto10a018c2008-10-09 10:00:30 +0000734 result = GetCurrentDirectoryW(result, new_path);
735 if (!result)
736 return FALSE;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000737 }
738 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
739 wcsncmp(new_path, L"//", 2) == 0)
740 /* UNC path, nothing to do. */
741 return TRUE;
742 env[1] = new_path[0];
743 result = SetEnvironmentVariableW(env, new_path);
744 if (new_path != _new_path)
745 free(new_path);
746 return result;
747}
748#endif
749
Martin v. Löwis14694662006-02-03 12:54:16 +0000750#ifdef MS_WINDOWS
751/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
752 - time stamps are restricted to second resolution
753 - file modification times suffer from forth-and-back conversions between
754 UTC and local time
755 Therefore, we implement our own stat, based on the Win32 API directly.
756*/
757#define HAVE_STAT_NSEC 1
758
759struct win32_stat{
760 int st_dev;
761 __int64 st_ino;
762 unsigned short st_mode;
763 int st_nlink;
764 int st_uid;
765 int st_gid;
766 int st_rdev;
767 __int64 st_size;
768 int st_atime;
769 int st_atime_nsec;
770 int st_mtime;
771 int st_mtime_nsec;
772 int st_ctime;
773 int st_ctime_nsec;
774};
775
776static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
777
778static void
779FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
780{
Martin v. Löwis77c176d2006-05-12 17:22:04 +0000781 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
782 /* Cannot simply cast and dereference in_ptr,
783 since it might not be aligned properly */
784 __int64 in;
785 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000786 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
787 /* XXX Win32 supports time stamps past 2038; we currently don't */
788 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
789}
790
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000791static void
792time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
793{
794 /* XXX endianness */
795 __int64 out;
796 out = time_in + secs_between_epochs;
Martin v. Löwisf43893a2006-10-09 20:44:25 +0000797 out = out * 10000000 + nsec_in / 100;
Martin v. Löwis77c176d2006-05-12 17:22:04 +0000798 memcpy(out_ptr, &out, sizeof(out));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000799}
800
Martin v. Löwis14694662006-02-03 12:54:16 +0000801/* Below, we *know* that ugo+r is 0444 */
802#if _S_IREAD != 0400
803#error Unsupported C library
804#endif
805static int
806attributes_to_mode(DWORD attr)
807{
808 int m = 0;
809 if (attr & FILE_ATTRIBUTE_DIRECTORY)
810 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
811 else
812 m |= _S_IFREG;
813 if (attr & FILE_ATTRIBUTE_READONLY)
814 m |= 0444;
815 else
816 m |= 0666;
817 return m;
818}
819
820static int
821attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
822{
823 memset(result, 0, sizeof(*result));
824 result->st_mode = attributes_to_mode(info->dwFileAttributes);
825 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
826 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
827 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
828 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
829
830 return 0;
831}
832
Martin v. Löwis012bc722006-10-15 09:43:39 +0000833/* Emulate GetFileAttributesEx[AW] on Windows 95 */
834static int checked = 0;
835static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
836static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
837static void
838check_gfax()
839{
840 HINSTANCE hKernel32;
841 if (checked)
842 return;
843 checked = 1;
844 hKernel32 = GetModuleHandle("KERNEL32");
845 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
846 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
847}
848
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000849static BOOL
850attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
851{
852 HANDLE hFindFile;
853 WIN32_FIND_DATAA FileData;
854 hFindFile = FindFirstFileA(pszFile, &FileData);
855 if (hFindFile == INVALID_HANDLE_VALUE)
856 return FALSE;
857 FindClose(hFindFile);
858 pfad->dwFileAttributes = FileData.dwFileAttributes;
859 pfad->ftCreationTime = FileData.ftCreationTime;
860 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
861 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
862 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
863 pfad->nFileSizeLow = FileData.nFileSizeLow;
864 return TRUE;
865}
866
867static BOOL
868attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
869{
870 HANDLE hFindFile;
871 WIN32_FIND_DATAW FileData;
872 hFindFile = FindFirstFileW(pszFile, &FileData);
873 if (hFindFile == INVALID_HANDLE_VALUE)
874 return FALSE;
875 FindClose(hFindFile);
876 pfad->dwFileAttributes = FileData.dwFileAttributes;
877 pfad->ftCreationTime = FileData.ftCreationTime;
878 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
879 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
880 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
881 pfad->nFileSizeLow = FileData.nFileSizeLow;
882 return TRUE;
883}
884
Martin v. Löwis012bc722006-10-15 09:43:39 +0000885static BOOL WINAPI
886Py_GetFileAttributesExA(LPCSTR pszFile,
887 GET_FILEEX_INFO_LEVELS level,
888 LPVOID pv)
889{
890 BOOL result;
Martin v. Löwis012bc722006-10-15 09:43:39 +0000891 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
892 /* First try to use the system's implementation, if that is
893 available and either succeeds to gives an error other than
894 that it isn't implemented. */
895 check_gfax();
896 if (gfaxa) {
897 result = gfaxa(pszFile, level, pv);
898 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
899 return result;
900 }
901 /* It's either not present, or not implemented.
902 Emulate using FindFirstFile. */
903 if (level != GetFileExInfoStandard) {
904 SetLastError(ERROR_INVALID_PARAMETER);
905 return FALSE;
906 }
907 /* Use GetFileAttributes to validate that the file name
908 does not contain wildcards (which FindFirstFile would
909 accept). */
910 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
911 return FALSE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000912 return attributes_from_dir(pszFile, pfad);
Martin v. Löwis012bc722006-10-15 09:43:39 +0000913}
914
915static BOOL WINAPI
916Py_GetFileAttributesExW(LPCWSTR pszFile,
917 GET_FILEEX_INFO_LEVELS level,
918 LPVOID pv)
919{
920 BOOL result;
Martin v. Löwis012bc722006-10-15 09:43:39 +0000921 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
922 /* First try to use the system's implementation, if that is
923 available and either succeeds to gives an error other than
924 that it isn't implemented. */
925 check_gfax();
926 if (gfaxa) {
927 result = gfaxw(pszFile, level, pv);
928 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
929 return result;
930 }
931 /* It's either not present, or not implemented.
932 Emulate using FindFirstFile. */
933 if (level != GetFileExInfoStandard) {
934 SetLastError(ERROR_INVALID_PARAMETER);
935 return FALSE;
936 }
937 /* Use GetFileAttributes to validate that the file name
938 does not contain wildcards (which FindFirstFile would
939 accept). */
940 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
941 return FALSE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000942 return attributes_from_dir_w(pszFile, pfad);
Martin v. Löwis012bc722006-10-15 09:43:39 +0000943}
944
Martin v. Löwis14694662006-02-03 12:54:16 +0000945static int
946win32_stat(const char* path, struct win32_stat *result)
947{
948 WIN32_FILE_ATTRIBUTE_DATA info;
949 int code;
950 char *dot;
951 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis012bc722006-10-15 09:43:39 +0000952 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000953 if (GetLastError() != ERROR_SHARING_VIOLATION) {
954 /* Protocol violation: we explicitly clear errno, instead of
955 setting it to a POSIX error. Callers should use GetLastError. */
956 errno = 0;
957 return -1;
958 } else {
959 /* Could not get attributes on open file. Fall back to
960 reading the directory. */
961 if (!attributes_from_dir(path, &info)) {
962 /* Very strange. This should not fail now */
963 errno = 0;
964 return -1;
965 }
966 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000967 }
968 code = attribute_data_to_stat(&info, result);
969 if (code != 0)
970 return code;
971 /* Set S_IFEXEC if it is an .exe, .bat, ... */
972 dot = strrchr(path, '.');
973 if (dot) {
974 if (stricmp(dot, ".bat") == 0 ||
975 stricmp(dot, ".cmd") == 0 ||
976 stricmp(dot, ".exe") == 0 ||
977 stricmp(dot, ".com") == 0)
978 result->st_mode |= 0111;
979 }
980 return code;
981}
982
983static int
984win32_wstat(const wchar_t* path, struct win32_stat *result)
985{
986 int code;
987 const wchar_t *dot;
988 WIN32_FILE_ATTRIBUTE_DATA info;
989 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis012bc722006-10-15 09:43:39 +0000990 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000991 if (GetLastError() != ERROR_SHARING_VIOLATION) {
992 /* Protocol violation: we explicitly clear errno, instead of
993 setting it to a POSIX error. Callers should use GetLastError. */
994 errno = 0;
995 return -1;
996 } else {
997 /* Could not get attributes on open file. Fall back to
998 reading the directory. */
999 if (!attributes_from_dir_w(path, &info)) {
1000 /* Very strange. This should not fail now */
1001 errno = 0;
1002 return -1;
1003 }
1004 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001005 }
1006 code = attribute_data_to_stat(&info, result);
1007 if (code < 0)
1008 return code;
1009 /* Set IFEXEC if it is an .exe, .bat, ... */
1010 dot = wcsrchr(path, '.');
1011 if (dot) {
1012 if (_wcsicmp(dot, L".bat") == 0 ||
1013 _wcsicmp(dot, L".cmd") == 0 ||
1014 _wcsicmp(dot, L".exe") == 0 ||
1015 _wcsicmp(dot, L".com") == 0)
1016 result->st_mode |= 0111;
1017 }
1018 return code;
1019}
1020
1021static int
1022win32_fstat(int file_number, struct win32_stat *result)
1023{
1024 BY_HANDLE_FILE_INFORMATION info;
1025 HANDLE h;
1026 int type;
1027
1028 h = (HANDLE)_get_osfhandle(file_number);
1029
1030 /* Protocol violation: we explicitly clear errno, instead of
1031 setting it to a POSIX error. Callers should use GetLastError. */
1032 errno = 0;
1033
1034 if (h == INVALID_HANDLE_VALUE) {
1035 /* This is really a C library error (invalid file handle).
1036 We set the Win32 error to the closes one matching. */
1037 SetLastError(ERROR_INVALID_HANDLE);
1038 return -1;
1039 }
1040 memset(result, 0, sizeof(*result));
1041
1042 type = GetFileType(h);
1043 if (type == FILE_TYPE_UNKNOWN) {
1044 DWORD error = GetLastError();
1045 if (error != 0) {
1046 return -1;
1047 }
1048 /* else: valid but unknown file */
1049 }
1050
1051 if (type != FILE_TYPE_DISK) {
1052 if (type == FILE_TYPE_CHAR)
1053 result->st_mode = _S_IFCHR;
1054 else if (type == FILE_TYPE_PIPE)
1055 result->st_mode = _S_IFIFO;
1056 return 0;
1057 }
1058
1059 if (!GetFileInformationByHandle(h, &info)) {
1060 return -1;
1061 }
1062
1063 /* similar to stat() */
1064 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1065 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1066 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1067 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1068 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1069 /* specific to fstat() */
1070 result->st_nlink = info.nNumberOfLinks;
1071 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1072 return 0;
1073}
1074
1075#endif /* MS_WINDOWS */
1076
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001077PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001078"stat_result: Result from stat or lstat.\n\n\
1079This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001080 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001081or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1082\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001083Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1084or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001085\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001086See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001087
1088static PyStructSequence_Field stat_result_fields[] = {
1089 {"st_mode", "protection bits"},
1090 {"st_ino", "inode"},
1091 {"st_dev", "device"},
1092 {"st_nlink", "number of hard links"},
1093 {"st_uid", "user ID of owner"},
1094 {"st_gid", "group ID of owner"},
1095 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001096 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1097 {NULL, "integer time of last access"},
1098 {NULL, "integer time of last modification"},
1099 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001100 {"st_atime", "time of last access"},
1101 {"st_mtime", "time of last modification"},
1102 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001103#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001104 {"st_blksize", "blocksize for filesystem I/O"},
1105#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001106#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001107 {"st_blocks", "number of blocks allocated"},
1108#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001109#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001110 {"st_rdev", "device type (if inode device)"},
1111#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001112#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1113 {"st_flags", "user defined flags for file"},
1114#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001115#ifdef HAVE_STRUCT_STAT_ST_GEN
1116 {"st_gen", "generation number"},
1117#endif
1118#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1119 {"st_birthtime", "time of creation"},
1120#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001121 {0}
1122};
1123
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001124#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001125#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001126#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001127#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001128#endif
1129
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001130#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001131#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1132#else
1133#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1134#endif
1135
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001136#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001137#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1138#else
1139#define ST_RDEV_IDX ST_BLOCKS_IDX
1140#endif
1141
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001142#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1143#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1144#else
1145#define ST_FLAGS_IDX ST_RDEV_IDX
1146#endif
1147
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001148#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001149#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001150#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001151#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001152#endif
1153
1154#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1155#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1156#else
1157#define ST_BIRTHTIME_IDX ST_GEN_IDX
1158#endif
1159
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001160static PyStructSequence_Desc stat_result_desc = {
1161 "stat_result", /* name */
1162 stat_result__doc__, /* doc */
1163 stat_result_fields,
1164 10
1165};
1166
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001167PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001168"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1169This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001170 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001171or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001172\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001173See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001174
1175static PyStructSequence_Field statvfs_result_fields[] = {
1176 {"f_bsize", },
1177 {"f_frsize", },
1178 {"f_blocks", },
1179 {"f_bfree", },
1180 {"f_bavail", },
1181 {"f_files", },
1182 {"f_ffree", },
1183 {"f_favail", },
1184 {"f_flag", },
1185 {"f_namemax",},
1186 {0}
1187};
1188
1189static PyStructSequence_Desc statvfs_result_desc = {
1190 "statvfs_result", /* name */
1191 statvfs_result__doc__, /* doc */
1192 statvfs_result_fields,
1193 10
1194};
1195
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00001196static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001197static PyTypeObject StatResultType;
1198static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001199static newfunc structseq_new;
1200
1201static PyObject *
1202statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1203{
1204 PyStructSequence *result;
1205 int i;
1206
1207 result = (PyStructSequence*)structseq_new(type, args, kwds);
1208 if (!result)
1209 return NULL;
1210 /* If we have been initialized from a tuple,
1211 st_?time might be set to None. Initialize it
1212 from the int slots. */
1213 for (i = 7; i <= 9; i++) {
1214 if (result->ob_item[i+3] == Py_None) {
1215 Py_DECREF(Py_None);
1216 Py_INCREF(result->ob_item[i]);
1217 result->ob_item[i+3] = result->ob_item[i];
1218 }
1219 }
1220 return (PyObject*)result;
1221}
1222
1223
1224
1225/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001226static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001227
1228PyDoc_STRVAR(stat_float_times__doc__,
1229"stat_float_times([newval]) -> oldval\n\n\
1230Determine whether os.[lf]stat represents time stamps as float objects.\n\
1231If newval is True, future calls to stat() return floats, if it is False,\n\
1232future calls return ints. \n\
1233If newval is omitted, return the current setting.\n");
1234
1235static PyObject*
1236stat_float_times(PyObject* self, PyObject *args)
1237{
1238 int newval = -1;
1239 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1240 return NULL;
1241 if (newval == -1)
1242 /* Return old value */
1243 return PyBool_FromLong(_stat_float_times);
1244 _stat_float_times = newval;
1245 Py_INCREF(Py_None);
1246 return Py_None;
1247}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001248
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001249static void
1250fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1251{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001252 PyObject *fval,*ival;
1253#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001254 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001255#else
1256 ival = PyInt_FromLong((long)sec);
1257#endif
Neal Norwitze0a81af2006-08-12 01:50:38 +00001258 if (!ival)
1259 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001260 if (_stat_float_times) {
1261 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1262 } else {
1263 fval = ival;
1264 Py_INCREF(fval);
1265 }
1266 PyStructSequence_SET_ITEM(v, index, ival);
1267 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001268}
1269
Tim Peters5aa91602002-01-30 05:46:57 +00001270/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001271 (used by posix_stat() and posix_fstat()) */
1272static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001273_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001274{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001275 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001276 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001277 if (v == NULL)
1278 return NULL;
1279
Martin v. Löwis14694662006-02-03 12:54:16 +00001280 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001281#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001282 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001283 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001284#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001285 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001286#endif
1287#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001288 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001289 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001290#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001291 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001292#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001293 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1294 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1295 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001296#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001297 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001298 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001299#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001300 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001301#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001302
Martin v. Löwis14694662006-02-03 12:54:16 +00001303#if defined(HAVE_STAT_TV_NSEC)
1304 ansec = st->st_atim.tv_nsec;
1305 mnsec = st->st_mtim.tv_nsec;
1306 cnsec = st->st_ctim.tv_nsec;
1307#elif defined(HAVE_STAT_TV_NSEC2)
1308 ansec = st->st_atimespec.tv_nsec;
1309 mnsec = st->st_mtimespec.tv_nsec;
1310 cnsec = st->st_ctimespec.tv_nsec;
1311#elif defined(HAVE_STAT_NSEC)
1312 ansec = st->st_atime_nsec;
1313 mnsec = st->st_mtime_nsec;
1314 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001315#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001316 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001317#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001318 fill_time(v, 7, st->st_atime, ansec);
1319 fill_time(v, 8, st->st_mtime, mnsec);
1320 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001321
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001322#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001323 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001324 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001325#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001326#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001327 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001328 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001329#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001330#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001331 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001332 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001333#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001334#ifdef HAVE_STRUCT_STAT_ST_GEN
1335 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001336 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001337#endif
1338#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1339 {
1340 PyObject *val;
1341 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001342 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001343#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001344 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001345#else
1346 bnsec = 0;
1347#endif
1348 if (_stat_float_times) {
1349 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1350 } else {
1351 val = PyInt_FromLong((long)bsec);
1352 }
1353 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1354 val);
1355 }
1356#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001357#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1358 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001359 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001360#endif
Fred Drake699f3522000-06-29 21:12:41 +00001361
1362 if (PyErr_Occurred()) {
1363 Py_DECREF(v);
1364 return NULL;
1365 }
1366
1367 return v;
1368}
1369
Martin v. Löwisd8948722004-06-02 09:57:56 +00001370#ifdef MS_WINDOWS
1371
1372/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1373 where / can be used in place of \ and the trailing slash is optional.
1374 Both SERVER and SHARE must have at least one character.
1375*/
1376
1377#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1378#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001379#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001380#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001381#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001382
Tim Peters4ad82172004-08-30 17:02:04 +00001383static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001384IsUNCRootA(char *path, int pathlen)
1385{
1386 #define ISSLASH ISSLASHA
1387
1388 int i, share;
1389
1390 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1391 /* minimum UNCRoot is \\x\y */
1392 return FALSE;
1393 for (i = 2; i < pathlen ; i++)
1394 if (ISSLASH(path[i])) break;
1395 if (i == 2 || i == pathlen)
1396 /* do not allow \\\SHARE or \\SERVER */
1397 return FALSE;
1398 share = i+1;
1399 for (i = share; i < pathlen; i++)
1400 if (ISSLASH(path[i])) break;
1401 return (i != share && (i == pathlen || i == pathlen-1));
1402
1403 #undef ISSLASH
1404}
1405
1406#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001407static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001408IsUNCRootW(Py_UNICODE *path, int pathlen)
1409{
1410 #define ISSLASH ISSLASHW
1411
1412 int i, share;
1413
1414 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1415 /* minimum UNCRoot is \\x\y */
1416 return FALSE;
1417 for (i = 2; i < pathlen ; i++)
1418 if (ISSLASH(path[i])) break;
1419 if (i == 2 || i == pathlen)
1420 /* do not allow \\\SHARE or \\SERVER */
1421 return FALSE;
1422 share = i+1;
1423 for (i = share; i < pathlen; i++)
1424 if (ISSLASH(path[i])) break;
1425 return (i != share && (i == pathlen || i == pathlen-1));
1426
1427 #undef ISSLASH
1428}
1429#endif /* Py_WIN_WIDE_FILENAMES */
1430#endif /* MS_WINDOWS */
1431
Barry Warsaw53699e91996-12-10 23:23:01 +00001432static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001433posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001434 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001435#ifdef __VMS
1436 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1437#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001438 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001439#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001440 char *wformat,
1441 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001442{
Fred Drake699f3522000-06-29 21:12:41 +00001443 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001444 char *path = NULL; /* pass this to stat; do not free() it */
1445 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001446 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001447 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001448
1449#ifdef Py_WIN_WIDE_FILENAMES
1450 /* If on wide-character-capable OS see if argument
1451 is Unicode and if so use wide API. */
1452 if (unicode_file_names()) {
1453 PyUnicodeObject *po;
1454 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001455 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1456
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001457 Py_BEGIN_ALLOW_THREADS
1458 /* PyUnicode_AS_UNICODE result OK without
1459 thread lock as it is a simple dereference. */
1460 res = wstatfunc(wpath, &st);
1461 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001462
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001463 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001464 return win32_error_unicode("stat", wpath);
1465 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001466 }
1467 /* Drop the argument parsing error as narrow strings
1468 are also valid. */
1469 PyErr_Clear();
1470 }
1471#endif
1472
Tim Peters5aa91602002-01-30 05:46:57 +00001473 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001474 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001475 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001476 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001477
Barry Warsaw53699e91996-12-10 23:23:01 +00001478 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001479 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001480 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001481
1482 if (res != 0) {
1483#ifdef MS_WINDOWS
1484 result = win32_error("stat", pathfree);
1485#else
1486 result = posix_error_with_filename(pathfree);
1487#endif
1488 }
1489 else
1490 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001491
Tim Peters500bd032001-12-19 19:05:01 +00001492 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001493 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001494}
1495
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001496/* POSIX methods */
1497
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001498PyDoc_STRVAR(posix_access__doc__,
Georg Brandl7a284472007-01-27 19:38:50 +00001499"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001500Use the real uid/gid to test for access to a path. Note that most\n\
1501operations will use the effective uid/gid, therefore this routine can\n\
1502be used in a suid/sgid environment to test if the invoking user has the\n\
1503specified access to the path. The mode argument can be F_OK to test\n\
1504existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001505
1506static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001507posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001508{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001509 char *path;
1510 int mode;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001511
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001512#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001513 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001514 if (unicode_file_names()) {
1515 PyUnicodeObject *po;
1516 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1517 Py_BEGIN_ALLOW_THREADS
1518 /* PyUnicode_AS_UNICODE OK without thread lock as
1519 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001520 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001521 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001522 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001523 }
1524 /* Drop the argument parsing error as narrow strings
1525 are also valid. */
1526 PyErr_Clear();
1527 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001528 if (!PyArg_ParseTuple(args, "eti:access",
1529 Py_FileSystemDefaultEncoding, &path, &mode))
1530 return 0;
1531 Py_BEGIN_ALLOW_THREADS
1532 attr = GetFileAttributesA(path);
1533 Py_END_ALLOW_THREADS
1534 PyMem_Free(path);
1535finish:
1536 if (attr == 0xFFFFFFFF)
1537 /* File does not exist, or cannot read attributes */
1538 return PyBool_FromLong(0);
1539 /* Access is possible if either write access wasn't requested, or
Martin v. Löwis7b3cc062007-12-03 23:09:04 +00001540 the file isn't read-only, or if it's a directory, as there are
1541 no read-only directories on Windows. */
1542 return PyBool_FromLong(!(mode & 2)
1543 || !(attr & FILE_ATTRIBUTE_READONLY)
1544 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001545#else
1546 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001547 if (!PyArg_ParseTuple(args, "eti:access",
1548 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001549 return NULL;
1550 Py_BEGIN_ALLOW_THREADS
1551 res = access(path, mode);
1552 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001553 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001554 return PyBool_FromLong(res == 0);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001555#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001556}
1557
Guido van Rossumd371ff11999-01-25 16:12:23 +00001558#ifndef F_OK
1559#define F_OK 0
1560#endif
1561#ifndef R_OK
1562#define R_OK 4
1563#endif
1564#ifndef W_OK
1565#define W_OK 2
1566#endif
1567#ifndef X_OK
1568#define X_OK 1
1569#endif
1570
1571#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001572PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001573"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001574Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001575
1576static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001577posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001578{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001579 int id;
1580 char *ret;
1581
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001582 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001583 return NULL;
1584
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001585#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001586 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001587 if (id == 0) {
1588 ret = ttyname();
1589 }
1590 else {
1591 ret = NULL;
1592 }
1593#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001594 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001595#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001596 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001597 return posix_error();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001598 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001599}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001600#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001601
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001602#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001603PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001604"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001605Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001606
1607static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001608posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001609{
1610 char *ret;
1611 char buffer[L_ctermid];
1612
Greg Wardb48bc172000-03-01 21:51:56 +00001613#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001614 ret = ctermid_r(buffer);
1615#else
1616 ret = ctermid(buffer);
1617#endif
1618 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001619 return posix_error();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001620 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001621}
1622#endif
1623
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001624PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001625"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001626Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001627
Barry Warsaw53699e91996-12-10 23:23:01 +00001628static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001629posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001630{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001631#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001632 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001633#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001634 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001635#elif defined(__VMS)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001636 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001637#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001638 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001639#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001640}
1641
Fred Drake4d1e64b2002-04-15 19:40:07 +00001642#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001643PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001644"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001645Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001646opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001647
1648static PyObject *
1649posix_fchdir(PyObject *self, PyObject *fdobj)
1650{
1651 return posix_fildes(fdobj, fchdir);
1652}
1653#endif /* HAVE_FCHDIR */
1654
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001655
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001656PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001657"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001658Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001659
Barry Warsaw53699e91996-12-10 23:23:01 +00001660static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001661posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001662{
Mark Hammondef8b6542001-05-13 08:04:26 +00001663 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001664 int i;
1665 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001666#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001667 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001668 if (unicode_file_names()) {
1669 PyUnicodeObject *po;
1670 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1671 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001672 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1673 if (attr != 0xFFFFFFFF) {
1674 if (i & _S_IWRITE)
1675 attr &= ~FILE_ATTRIBUTE_READONLY;
1676 else
1677 attr |= FILE_ATTRIBUTE_READONLY;
1678 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1679 }
1680 else
1681 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001682 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001683 if (!res)
1684 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001685 PyUnicode_AS_UNICODE(po));
1686 Py_INCREF(Py_None);
1687 return Py_None;
1688 }
1689 /* Drop the argument parsing error as narrow strings
1690 are also valid. */
1691 PyErr_Clear();
1692 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001693 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1694 &path, &i))
1695 return NULL;
1696 Py_BEGIN_ALLOW_THREADS
1697 attr = GetFileAttributesA(path);
1698 if (attr != 0xFFFFFFFF) {
1699 if (i & _S_IWRITE)
1700 attr &= ~FILE_ATTRIBUTE_READONLY;
1701 else
1702 attr |= FILE_ATTRIBUTE_READONLY;
1703 res = SetFileAttributesA(path, attr);
1704 }
1705 else
1706 res = 0;
1707 Py_END_ALLOW_THREADS
1708 if (!res) {
1709 win32_error("chmod", path);
1710 PyMem_Free(path);
1711 return NULL;
1712 }
Martin v. Löwis9f485bc2006-05-08 05:25:56 +00001713 PyMem_Free(path);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001714 Py_INCREF(Py_None);
1715 return Py_None;
1716#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001717 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001718 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001719 return NULL;
1720 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001721 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001722 Py_END_ALLOW_THREADS
1723 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001724 return posix_error_with_allocated_filename(path);
1725 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001726 Py_INCREF(Py_None);
1727 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001728#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001729}
1730
Christian Heimes36281872007-11-30 21:11:28 +00001731#ifdef HAVE_FCHMOD
1732PyDoc_STRVAR(posix_fchmod__doc__,
1733"fchmod(fd, mode)\n\n\
1734Change the access permissions of the file given by file\n\
1735descriptor fd.");
1736
1737static PyObject *
1738posix_fchmod(PyObject *self, PyObject *args)
1739{
1740 int fd, mode, res;
1741 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1742 return NULL;
1743 Py_BEGIN_ALLOW_THREADS
1744 res = fchmod(fd, mode);
1745 Py_END_ALLOW_THREADS
1746 if (res < 0)
1747 return posix_error();
1748 Py_RETURN_NONE;
1749}
1750#endif /* HAVE_FCHMOD */
1751
1752#ifdef HAVE_LCHMOD
1753PyDoc_STRVAR(posix_lchmod__doc__,
1754"lchmod(path, mode)\n\n\
1755Change the access permissions of a file. If path is a symlink, this\n\
1756affects the link itself rather than the target.");
1757
1758static PyObject *
1759posix_lchmod(PyObject *self, PyObject *args)
1760{
1761 char *path = NULL;
1762 int i;
1763 int res;
1764 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1765 &path, &i))
1766 return NULL;
1767 Py_BEGIN_ALLOW_THREADS
1768 res = lchmod(path, i);
1769 Py_END_ALLOW_THREADS
1770 if (res < 0)
1771 return posix_error_with_allocated_filename(path);
1772 PyMem_Free(path);
1773 Py_RETURN_NONE;
1774}
1775#endif /* HAVE_LCHMOD */
1776
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001777
Martin v. Löwis382abef2007-02-19 10:55:19 +00001778#ifdef HAVE_CHFLAGS
1779PyDoc_STRVAR(posix_chflags__doc__,
1780"chflags(path, flags)\n\n\
1781Set file flags.");
1782
1783static PyObject *
1784posix_chflags(PyObject *self, PyObject *args)
1785{
1786 char *path;
1787 unsigned long flags;
1788 int res;
1789 if (!PyArg_ParseTuple(args, "etk:chflags",
1790 Py_FileSystemDefaultEncoding, &path, &flags))
1791 return NULL;
1792 Py_BEGIN_ALLOW_THREADS
1793 res = chflags(path, flags);
1794 Py_END_ALLOW_THREADS
1795 if (res < 0)
1796 return posix_error_with_allocated_filename(path);
1797 PyMem_Free(path);
1798 Py_INCREF(Py_None);
1799 return Py_None;
1800}
1801#endif /* HAVE_CHFLAGS */
1802
1803#ifdef HAVE_LCHFLAGS
1804PyDoc_STRVAR(posix_lchflags__doc__,
1805"lchflags(path, flags)\n\n\
1806Set file flags.\n\
1807This function will not follow symbolic links.");
1808
1809static PyObject *
1810posix_lchflags(PyObject *self, PyObject *args)
1811{
1812 char *path;
1813 unsigned long flags;
1814 int res;
1815 if (!PyArg_ParseTuple(args, "etk:lchflags",
1816 Py_FileSystemDefaultEncoding, &path, &flags))
1817 return NULL;
1818 Py_BEGIN_ALLOW_THREADS
1819 res = lchflags(path, flags);
1820 Py_END_ALLOW_THREADS
1821 if (res < 0)
1822 return posix_error_with_allocated_filename(path);
1823 PyMem_Free(path);
1824 Py_INCREF(Py_None);
1825 return Py_None;
1826}
1827#endif /* HAVE_LCHFLAGS */
1828
Martin v. Löwis244edc82001-10-04 22:44:26 +00001829#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001830PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001831"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001832Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001833
1834static PyObject *
1835posix_chroot(PyObject *self, PyObject *args)
1836{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001837 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001838}
1839#endif
1840
Guido van Rossum21142a01999-01-08 21:05:37 +00001841#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001842PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001843"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001844force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001845
1846static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001847posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001848{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001849 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001850}
1851#endif /* HAVE_FSYNC */
1852
1853#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001854
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001855#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001856extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1857#endif
1858
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001859PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001860"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001861force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001862 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001863
1864static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001865posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001866{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001867 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001868}
1869#endif /* HAVE_FDATASYNC */
1870
1871
Fredrik Lundh10723342000-07-10 16:38:09 +00001872#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001873PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001874"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001875Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001876
Barry Warsaw53699e91996-12-10 23:23:01 +00001877static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001878posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001879{
Mark Hammondef8b6542001-05-13 08:04:26 +00001880 char *path = NULL;
Gregory P. Smithf48da8f2008-03-18 19:05:32 +00001881 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001882 int res;
Gregory P. Smithf48da8f2008-03-18 19:05:32 +00001883 if (!PyArg_ParseTuple(args, "etll:chown",
Tim Peters5aa91602002-01-30 05:46:57 +00001884 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001885 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001886 return NULL;
1887 Py_BEGIN_ALLOW_THREADS
1888 res = chown(path, (uid_t) uid, (gid_t) gid);
1889 Py_END_ALLOW_THREADS
1890 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001891 return posix_error_with_allocated_filename(path);
1892 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001893 Py_INCREF(Py_None);
1894 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001895}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001896#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001897
Christian Heimes36281872007-11-30 21:11:28 +00001898#ifdef HAVE_FCHOWN
1899PyDoc_STRVAR(posix_fchown__doc__,
1900"fchown(fd, uid, gid)\n\n\
1901Change the owner and group id of the file given by file descriptor\n\
1902fd to the numeric uid and gid.");
1903
1904static PyObject *
1905posix_fchown(PyObject *self, PyObject *args)
1906{
1907 int fd, uid, gid;
1908 int res;
1909 if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid))
1910 return NULL;
1911 Py_BEGIN_ALLOW_THREADS
1912 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1913 Py_END_ALLOW_THREADS
1914 if (res < 0)
1915 return posix_error();
1916 Py_RETURN_NONE;
1917}
1918#endif /* HAVE_FCHOWN */
1919
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001920#ifdef HAVE_LCHOWN
1921PyDoc_STRVAR(posix_lchown__doc__,
1922"lchown(path, uid, gid)\n\n\
1923Change the owner and group id of path to the numeric uid and gid.\n\
1924This function will not follow symbolic links.");
1925
1926static PyObject *
1927posix_lchown(PyObject *self, PyObject *args)
1928{
1929 char *path = NULL;
1930 int uid, gid;
1931 int res;
1932 if (!PyArg_ParseTuple(args, "etii:lchown",
1933 Py_FileSystemDefaultEncoding, &path,
1934 &uid, &gid))
1935 return NULL;
1936 Py_BEGIN_ALLOW_THREADS
1937 res = lchown(path, (uid_t) uid, (gid_t) gid);
1938 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001939 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001940 return posix_error_with_allocated_filename(path);
1941 PyMem_Free(path);
1942 Py_INCREF(Py_None);
1943 return Py_None;
1944}
1945#endif /* HAVE_LCHOWN */
1946
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001947
Guido van Rossum36bc6801995-06-14 22:54:23 +00001948#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001949PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001950"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001951Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001952
Barry Warsaw53699e91996-12-10 23:23:01 +00001953static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001954posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001955{
Facundo Batista5596b0c2008-06-22 13:36:20 +00001956 int bufsize_incr = 1024;
1957 int bufsize = 0;
1958 char *tmpbuf = NULL;
1959 char *res = NULL;
1960 PyObject *dynamic_return;
Neal Norwitze241ce82003-02-17 18:17:05 +00001961
Barry Warsaw53699e91996-12-10 23:23:01 +00001962 Py_BEGIN_ALLOW_THREADS
Facundo Batista5596b0c2008-06-22 13:36:20 +00001963 do {
1964 bufsize = bufsize + bufsize_incr;
1965 tmpbuf = malloc(bufsize);
1966 if (tmpbuf == NULL) {
1967 break;
1968 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001969#if defined(PYOS_OS2) && defined(PYCC_GCC)
Facundo Batista5596b0c2008-06-22 13:36:20 +00001970 res = _getcwd2(tmpbuf, bufsize);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001971#else
Facundo Batista5596b0c2008-06-22 13:36:20 +00001972 res = getcwd(tmpbuf, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001973#endif
Facundo Batista5596b0c2008-06-22 13:36:20 +00001974
1975 if (res == NULL) {
1976 free(tmpbuf);
1977 }
1978 } while ((res == NULL) && (errno == ERANGE));
Barry Warsaw53699e91996-12-10 23:23:01 +00001979 Py_END_ALLOW_THREADS
Facundo Batista5596b0c2008-06-22 13:36:20 +00001980
Guido van Rossumff4949e1992-08-05 19:58:53 +00001981 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001982 return posix_error();
Facundo Batista5596b0c2008-06-22 13:36:20 +00001983
1984 dynamic_return = PyString_FromString(tmpbuf);
1985 free(tmpbuf);
1986
1987 return dynamic_return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001988}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001989
Walter Dörwald3b918c32002-11-21 20:18:46 +00001990#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001991PyDoc_STRVAR(posix_getcwdu__doc__,
1992"getcwdu() -> path\n\n\
1993Return a unicode string representing the current working directory.");
1994
1995static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001996posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001997{
1998 char buf[1026];
1999 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002000
2001#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002002 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002003 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002004 wchar_t wbuf[1026];
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002005 wchar_t *wbuf2 = wbuf;
2006 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002007 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002008 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2009 /* If the buffer is large enough, len does not include the
2010 terminating \0. If the buffer is too small, len includes
2011 the space needed for the terminator. */
2012 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2013 wbuf2 = malloc(len * sizeof(wchar_t));
2014 if (wbuf2)
2015 len = GetCurrentDirectoryW(len, wbuf2);
2016 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002017 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002018 if (!wbuf2) {
2019 PyErr_NoMemory();
2020 return NULL;
2021 }
2022 if (!len) {
2023 if (wbuf2 != wbuf) free(wbuf2);
2024 return win32_error("getcwdu", NULL);
2025 }
2026 resobj = PyUnicode_FromWideChar(wbuf2, len);
2027 if (wbuf2 != wbuf) free(wbuf2);
2028 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002029 }
2030#endif
2031
2032 Py_BEGIN_ALLOW_THREADS
2033#if defined(PYOS_OS2) && defined(PYCC_GCC)
2034 res = _getcwd2(buf, sizeof buf);
2035#else
2036 res = getcwd(buf, sizeof buf);
2037#endif
2038 Py_END_ALLOW_THREADS
2039 if (res == NULL)
2040 return posix_error();
2041 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
2042}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002043#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00002044#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002045
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002046
Guido van Rossumb6775db1994-08-01 11:34:53 +00002047#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002048PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002049"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002050Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002051
Barry Warsaw53699e91996-12-10 23:23:01 +00002052static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002053posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002054{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00002055 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002056}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002057#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002058
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002059
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002060PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002061"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002062Return a list containing the names of the entries in the directory.\n\
2063\n\
2064 path: path of directory to list\n\
2065\n\
2066The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002067entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002068
Barry Warsaw53699e91996-12-10 23:23:01 +00002069static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002070posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002071{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002072 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002073 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002074#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002075
Barry Warsaw53699e91996-12-10 23:23:01 +00002076 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002077 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002078 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002079 WIN32_FIND_DATA FileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002080 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002081 char *bufptr = namebuf;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002082 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002083
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002084#ifdef Py_WIN_WIDE_FILENAMES
2085 /* If on wide-character-capable OS see if argument
2086 is Unicode and if so use wide API. */
2087 if (unicode_file_names()) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002088 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002089 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2090 WIN32_FIND_DATAW wFileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002091 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002092 Py_UNICODE wch;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002093 /* Overallocate for \\*.*\0 */
2094 len = PyUnicode_GET_SIZE(po);
2095 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2096 if (!wnamebuf) {
2097 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002098 return NULL;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002099 }
2100 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2101 wch = len > 0 ? wnamebuf[len-1] : '\0';
2102 if (wch != L'/' && wch != L'\\' && wch != L':')
2103 wnamebuf[len++] = L'\\';
2104 wcscpy(wnamebuf + len, L"*.*");
2105 if ((d = PyList_New(0)) == NULL) {
2106 free(wnamebuf);
2107 return NULL;
2108 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002109 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2110 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002111 int error = GetLastError();
2112 if (error == ERROR_FILE_NOT_FOUND) {
2113 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002114 return d;
2115 }
2116 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002117 win32_error_unicode("FindFirstFileW", wnamebuf);
2118 free(wnamebuf);
2119 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002120 }
2121 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002122 /* Skip over . and .. */
2123 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2124 wcscmp(wFileData.cFileName, L"..") != 0) {
2125 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2126 if (v == NULL) {
2127 Py_DECREF(d);
2128 d = NULL;
2129 break;
2130 }
2131 if (PyList_Append(d, v) != 0) {
2132 Py_DECREF(v);
2133 Py_DECREF(d);
2134 d = NULL;
2135 break;
2136 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002137 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002138 }
Georg Brandl622927b2006-03-07 12:48:03 +00002139 Py_BEGIN_ALLOW_THREADS
2140 result = FindNextFileW(hFindFile, &wFileData);
2141 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00002142 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2143 it got to the end of the directory. */
2144 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2145 Py_DECREF(d);
2146 win32_error_unicode("FindNextFileW", wnamebuf);
2147 FindClose(hFindFile);
2148 free(wnamebuf);
2149 return NULL;
2150 }
Georg Brandl622927b2006-03-07 12:48:03 +00002151 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002152
2153 if (FindClose(hFindFile) == FALSE) {
2154 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002155 win32_error_unicode("FindClose", wnamebuf);
2156 free(wnamebuf);
2157 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002158 }
Martin v. Löwise3edaea2006-05-15 05:51:36 +00002159 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002160 return d;
2161 }
2162 /* Drop the argument parsing error as narrow strings
2163 are also valid. */
2164 PyErr_Clear();
2165 }
2166#endif
2167
Tim Peters5aa91602002-01-30 05:46:57 +00002168 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002169 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002170 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002171 if (len > 0) {
2172 char ch = namebuf[len-1];
2173 if (ch != SEP && ch != ALTSEP && ch != ':')
2174 namebuf[len++] = '/';
2175 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002176 strcpy(namebuf + len, "*.*");
2177
Barry Warsaw53699e91996-12-10 23:23:01 +00002178 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002179 return NULL;
2180
2181 hFindFile = FindFirstFile(namebuf, &FileData);
2182 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002183 int error = GetLastError();
2184 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002185 return d;
2186 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002187 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002188 }
2189 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002190 /* Skip over . and .. */
2191 if (strcmp(FileData.cFileName, ".") != 0 &&
2192 strcmp(FileData.cFileName, "..") != 0) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002193 v = PyString_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002194 if (v == NULL) {
2195 Py_DECREF(d);
2196 d = NULL;
2197 break;
2198 }
2199 if (PyList_Append(d, v) != 0) {
2200 Py_DECREF(v);
2201 Py_DECREF(d);
2202 d = NULL;
2203 break;
2204 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002205 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002206 }
Georg Brandl622927b2006-03-07 12:48:03 +00002207 Py_BEGIN_ALLOW_THREADS
2208 result = FindNextFile(hFindFile, &FileData);
2209 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00002210 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2211 it got to the end of the directory. */
2212 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2213 Py_DECREF(d);
2214 win32_error("FindNextFile", namebuf);
2215 FindClose(hFindFile);
2216 return NULL;
2217 }
Georg Brandl622927b2006-03-07 12:48:03 +00002218 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002219
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002220 if (FindClose(hFindFile) == FALSE) {
2221 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002222 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002223 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002224
2225 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002226
Tim Peters0bb44a42000-09-15 07:44:49 +00002227#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002228
2229#ifndef MAX_PATH
2230#define MAX_PATH CCHMAXPATH
2231#endif
2232 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002233 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002234 PyObject *d, *v;
2235 char namebuf[MAX_PATH+5];
2236 HDIR hdir = 1;
2237 ULONG srchcnt = 1;
2238 FILEFINDBUF3 ep;
2239 APIRET rc;
2240
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002241 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002242 return NULL;
2243 if (len >= MAX_PATH) {
2244 PyErr_SetString(PyExc_ValueError, "path too long");
2245 return NULL;
2246 }
2247 strcpy(namebuf, name);
2248 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002249 if (*pt == ALTSEP)
2250 *pt = SEP;
2251 if (namebuf[len-1] != SEP)
2252 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002253 strcpy(namebuf + len, "*.*");
2254
2255 if ((d = PyList_New(0)) == NULL)
2256 return NULL;
2257
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002258 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2259 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002260 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002261 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2262 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2263 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002264
2265 if (rc != NO_ERROR) {
2266 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002267 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002268 }
2269
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002270 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002271 do {
2272 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002273 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002274 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002275
2276 strcpy(namebuf, ep.achName);
2277
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002278 /* Leave Case of Name Alone -- In Native Form */
2279 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002280
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002281 v = PyString_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002282 if (v == NULL) {
2283 Py_DECREF(d);
2284 d = NULL;
2285 break;
2286 }
2287 if (PyList_Append(d, v) != 0) {
2288 Py_DECREF(v);
2289 Py_DECREF(d);
2290 d = NULL;
2291 break;
2292 }
2293 Py_DECREF(v);
2294 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2295 }
2296
2297 return d;
2298#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002299
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002300 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002301 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002302 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002303 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002304 int arg_is_unicode = 1;
2305
Georg Brandl05e89b82006-04-11 07:04:06 +00002306 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002307 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2308 arg_is_unicode = 0;
2309 PyErr_Clear();
2310 }
2311 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002312 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002313 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002314 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002315 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002316 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002317 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002318 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002319 return NULL;
2320 }
Georg Brandl622927b2006-03-07 12:48:03 +00002321 for (;;) {
Georg Brandl86cbf812008-07-16 21:31:41 +00002322 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002323 Py_BEGIN_ALLOW_THREADS
2324 ep = readdir(dirp);
2325 Py_END_ALLOW_THREADS
Georg Brandl86cbf812008-07-16 21:31:41 +00002326 if (ep == NULL) {
2327 if (errno == 0) {
2328 break;
2329 } else {
2330 closedir(dirp);
2331 Py_DECREF(d);
2332 return posix_error_with_allocated_filename(name);
2333 }
2334 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002335 if (ep->d_name[0] == '.' &&
2336 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002337 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002338 continue;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002339 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002340 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002341 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002342 d = NULL;
2343 break;
2344 }
Just van Rossum46c97842003-02-25 21:42:15 +00002345#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00002346 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002347 PyObject *w;
2348
2349 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002350 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002351 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002352 if (w != NULL) {
2353 Py_DECREF(v);
2354 v = w;
2355 }
2356 else {
2357 /* fall back to the original byte string, as
2358 discussed in patch #683592 */
2359 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002360 }
Just van Rossum46c97842003-02-25 21:42:15 +00002361 }
2362#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002363 if (PyList_Append(d, v) != 0) {
2364 Py_DECREF(v);
2365 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002366 d = NULL;
2367 break;
2368 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002369 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002370 }
2371 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002372 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002373
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002374 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002375
Tim Peters0bb44a42000-09-15 07:44:49 +00002376#endif /* which OS */
2377} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002378
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002379#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002380/* A helper function for abspath on win32 */
2381static PyObject *
2382posix__getfullpathname(PyObject *self, PyObject *args)
2383{
2384 /* assume encoded strings wont more than double no of chars */
2385 char inbuf[MAX_PATH*2];
2386 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002387 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002388 char outbuf[MAX_PATH*2];
2389 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002390#ifdef Py_WIN_WIDE_FILENAMES
2391 if (unicode_file_names()) {
2392 PyUnicodeObject *po;
2393 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2394 Py_UNICODE woutbuf[MAX_PATH*2];
2395 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002396 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002397 sizeof(woutbuf)/sizeof(woutbuf[0]),
2398 woutbuf, &wtemp))
2399 return win32_error("GetFullPathName", "");
2400 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2401 }
2402 /* Drop the argument parsing error as narrow strings
2403 are also valid. */
2404 PyErr_Clear();
2405 }
2406#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002407 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2408 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002409 &insize))
2410 return NULL;
2411 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2412 outbuf, &temp))
2413 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002414 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2415 return PyUnicode_Decode(outbuf, strlen(outbuf),
2416 Py_FileSystemDefaultEncoding, NULL);
2417 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002418 return PyString_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002419} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002420#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002421
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002422PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002423"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002424Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002425
Barry Warsaw53699e91996-12-10 23:23:01 +00002426static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002427posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002428{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002429 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002430 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002431 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002432
2433#ifdef Py_WIN_WIDE_FILENAMES
2434 if (unicode_file_names()) {
2435 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002436 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002437 Py_BEGIN_ALLOW_THREADS
2438 /* PyUnicode_AS_UNICODE OK without thread lock as
2439 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002440 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002441 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002442 if (!res)
2443 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002444 Py_INCREF(Py_None);
2445 return Py_None;
2446 }
2447 /* Drop the argument parsing error as narrow strings
2448 are also valid. */
2449 PyErr_Clear();
2450 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002451 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2452 Py_FileSystemDefaultEncoding, &path, &mode))
2453 return NULL;
2454 Py_BEGIN_ALLOW_THREADS
2455 /* PyUnicode_AS_UNICODE OK without thread lock as
2456 it is a simple dereference. */
2457 res = CreateDirectoryA(path, NULL);
2458 Py_END_ALLOW_THREADS
2459 if (!res) {
2460 win32_error("mkdir", path);
2461 PyMem_Free(path);
2462 return NULL;
2463 }
2464 PyMem_Free(path);
2465 Py_INCREF(Py_None);
2466 return Py_None;
2467#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002468
Tim Peters5aa91602002-01-30 05:46:57 +00002469 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002470 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002471 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002472 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002473#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002474 res = mkdir(path);
2475#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002476 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002477#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002478 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002479 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002480 return posix_error_with_allocated_filename(path);
2481 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002482 Py_INCREF(Py_None);
2483 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002484#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002485}
2486
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002487
Neal Norwitz1818ed72006-03-26 00:29:48 +00002488/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2489#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002490#include <sys/resource.h>
2491#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002492
Neal Norwitz1818ed72006-03-26 00:29:48 +00002493
2494#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002495PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002496"nice(inc) -> new_priority\n\n\
2497Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002498
Barry Warsaw53699e91996-12-10 23:23:01 +00002499static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002500posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002501{
2502 int increment, value;
2503
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002504 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002505 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002506
2507 /* There are two flavours of 'nice': one that returns the new
2508 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002509 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2510 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002511
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002512 If we are of the nice family that returns the new priority, we
2513 need to clear errno before the call, and check if errno is filled
2514 before calling posix_error() on a returnvalue of -1, because the
2515 -1 may be the actual new priority! */
2516
2517 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002518 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002519#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002520 if (value == 0)
2521 value = getpriority(PRIO_PROCESS, 0);
2522#endif
2523 if (value == -1 && errno != 0)
2524 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002525 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002526 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002527}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002528#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002529
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002530PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002531"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002532Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002533
Barry Warsaw53699e91996-12-10 23:23:01 +00002534static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002535posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002536{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002537#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002538 PyObject *o1, *o2;
2539 char *p1, *p2;
2540 BOOL result;
2541 if (unicode_file_names()) {
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +00002542 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2543 goto error;
2544 if (!convert_to_unicode(&o1))
2545 goto error;
2546 if (!convert_to_unicode(&o2)) {
2547 Py_DECREF(o1);
2548 goto error;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002549 }
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +00002550 Py_BEGIN_ALLOW_THREADS
2551 result = MoveFileW(PyUnicode_AsUnicode(o1),
2552 PyUnicode_AsUnicode(o2));
2553 Py_END_ALLOW_THREADS
2554 Py_DECREF(o1);
2555 Py_DECREF(o2);
2556 if (!result)
2557 return win32_error("rename", NULL);
2558 Py_INCREF(Py_None);
2559 return Py_None;
2560error:
2561 PyErr_Clear();
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002562 }
2563 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2564 return NULL;
2565 Py_BEGIN_ALLOW_THREADS
2566 result = MoveFileA(p1, p2);
2567 Py_END_ALLOW_THREADS
2568 if (!result)
2569 return win32_error("rename", NULL);
2570 Py_INCREF(Py_None);
2571 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002572#else
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00002573 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002574#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002575}
2576
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002577
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002578PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002579"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002580Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002581
Barry Warsaw53699e91996-12-10 23:23:01 +00002582static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002583posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002584{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002585#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002586 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002587#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002588 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002589#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002590}
2591
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002592
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002593PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002594"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002595Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002596
Barry Warsaw53699e91996-12-10 23:23:01 +00002597static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002598posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002599{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002600#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002601 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002602#else
2603 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2604#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002605}
2606
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002607
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002608#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002609PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002610"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002611Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002612
Barry Warsaw53699e91996-12-10 23:23:01 +00002613static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002614posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002615{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002616 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002617 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002618 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002619 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002620 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002621 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002622 Py_END_ALLOW_THREADS
2623 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002624}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002625#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002626
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002627
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002628PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002629"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002630Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002631
Barry Warsaw53699e91996-12-10 23:23:01 +00002632static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002633posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002634{
2635 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002636 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002637 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002638 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002639 if (i < 0)
2640 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002641 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002642}
2643
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002644
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002645PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002646"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002647Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002648
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002649PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002650"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002651Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002652
Barry Warsaw53699e91996-12-10 23:23:01 +00002653static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002654posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002655{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002656#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002657 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002658#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002659 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002660#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002661}
2662
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002663
Guido van Rossumb6775db1994-08-01 11:34:53 +00002664#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002665PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002666"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002667Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002668
Barry Warsaw53699e91996-12-10 23:23:01 +00002669static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002670posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002671{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002672 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002673 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002674
Barry Warsaw53699e91996-12-10 23:23:01 +00002675 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002676 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002677 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002678 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002679 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002680 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002681 u.sysname,
2682 u.nodename,
2683 u.release,
2684 u.version,
2685 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002686}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002687#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002688
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002689static int
2690extract_time(PyObject *t, long* sec, long* usec)
2691{
2692 long intval;
2693 if (PyFloat_Check(t)) {
2694 double tval = PyFloat_AsDouble(t);
Christian Heimese93237d2007-12-19 02:37:44 +00002695 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002696 if (!intobj)
2697 return -1;
2698 intval = PyInt_AsLong(intobj);
2699 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002700 if (intval == -1 && PyErr_Occurred())
2701 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002702 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002703 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002704 if (*usec < 0)
2705 /* If rounding gave us a negative number,
2706 truncate. */
2707 *usec = 0;
2708 return 0;
2709 }
2710 intval = PyInt_AsLong(t);
2711 if (intval == -1 && PyErr_Occurred())
2712 return -1;
2713 *sec = intval;
2714 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002715 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002716}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002717
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002718PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002719"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002720utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002721Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002722second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002723
Barry Warsaw53699e91996-12-10 23:23:01 +00002724static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002725posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002726{
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002727#ifdef Py_WIN_WIDE_FILENAMES
2728 PyObject *arg;
2729 PyUnicodeObject *obwpath;
2730 wchar_t *wpath = NULL;
2731 char *apath = NULL;
2732 HANDLE hFile;
2733 long atimesec, mtimesec, ausec, musec;
2734 FILETIME atime, mtime;
2735 PyObject *result = NULL;
2736
2737 if (unicode_file_names()) {
2738 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2739 wpath = PyUnicode_AS_UNICODE(obwpath);
2740 Py_BEGIN_ALLOW_THREADS
2741 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Martin v. Löwis18aaa562006-10-15 08:43:33 +00002742 NULL, OPEN_EXISTING,
2743 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002744 Py_END_ALLOW_THREADS
2745 if (hFile == INVALID_HANDLE_VALUE)
2746 return win32_error_unicode("utime", wpath);
2747 } else
2748 /* Drop the argument parsing error as narrow strings
2749 are also valid. */
2750 PyErr_Clear();
2751 }
2752 if (!wpath) {
2753 if (!PyArg_ParseTuple(args, "etO:utime",
2754 Py_FileSystemDefaultEncoding, &apath, &arg))
2755 return NULL;
2756 Py_BEGIN_ALLOW_THREADS
2757 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Martin v. Löwis18aaa562006-10-15 08:43:33 +00002758 NULL, OPEN_EXISTING,
2759 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002760 Py_END_ALLOW_THREADS
2761 if (hFile == INVALID_HANDLE_VALUE) {
2762 win32_error("utime", apath);
2763 PyMem_Free(apath);
2764 return NULL;
2765 }
2766 PyMem_Free(apath);
2767 }
2768
2769 if (arg == Py_None) {
2770 SYSTEMTIME now;
2771 GetSystemTime(&now);
2772 if (!SystemTimeToFileTime(&now, &mtime) ||
2773 !SystemTimeToFileTime(&now, &atime)) {
2774 win32_error("utime", NULL);
2775 goto done;
2776 }
2777 }
2778 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2779 PyErr_SetString(PyExc_TypeError,
2780 "utime() arg 2 must be a tuple (atime, mtime)");
2781 goto done;
2782 }
2783 else {
2784 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2785 &atimesec, &ausec) == -1)
2786 goto done;
Martin v. Löwisf43893a2006-10-09 20:44:25 +00002787 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002788 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2789 &mtimesec, &musec) == -1)
2790 goto done;
Martin v. Löwisf43893a2006-10-09 20:44:25 +00002791 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002792 }
2793 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2794 /* Avoid putting the file name into the error here,
2795 as that may confuse the user into believing that
2796 something is wrong with the file, when it also
2797 could be the time stamp that gives a problem. */
2798 win32_error("utime", NULL);
2799 }
2800 Py_INCREF(Py_None);
2801 result = Py_None;
2802done:
2803 CloseHandle(hFile);
2804 return result;
2805#else /* Py_WIN_WIDE_FILENAMES */
2806
Neal Norwitz2adf2102004-06-09 01:46:02 +00002807 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002808 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002809 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002810 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002811
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002812#if defined(HAVE_UTIMES)
2813 struct timeval buf[2];
2814#define ATIME buf[0].tv_sec
2815#define MTIME buf[1].tv_sec
2816#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002817/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002818 struct utimbuf buf;
2819#define ATIME buf.actime
2820#define MTIME buf.modtime
2821#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002822#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002823 time_t buf[2];
2824#define ATIME buf[0]
2825#define MTIME buf[1]
2826#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002827#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002828
Mark Hammond817c9292003-12-03 01:22:38 +00002829
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002830 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002831 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002832 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002833 if (arg == Py_None) {
2834 /* optional time values not given */
2835 Py_BEGIN_ALLOW_THREADS
2836 res = utime(path, NULL);
2837 Py_END_ALLOW_THREADS
2838 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002839 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002840 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002841 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002842 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002843 return NULL;
2844 }
2845 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002846 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002847 &atime, &ausec) == -1) {
2848 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002849 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002850 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002851 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002852 &mtime, &musec) == -1) {
2853 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002854 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002855 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002856 ATIME = atime;
2857 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002858#ifdef HAVE_UTIMES
2859 buf[0].tv_usec = ausec;
2860 buf[1].tv_usec = musec;
2861 Py_BEGIN_ALLOW_THREADS
2862 res = utimes(path, buf);
2863 Py_END_ALLOW_THREADS
2864#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002865 Py_BEGIN_ALLOW_THREADS
2866 res = utime(path, UTIME_ARG);
2867 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002868#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002869 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002870 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002871 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002872 }
Neal Norwitz96652712004-06-06 20:40:27 +00002873 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002874 Py_INCREF(Py_None);
2875 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002876#undef UTIME_ARG
2877#undef ATIME
2878#undef MTIME
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002879#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002880}
2881
Guido van Rossum85e3b011991-06-03 12:42:10 +00002882
Guido van Rossum3b066191991-06-04 19:40:25 +00002883/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002884
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002885PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002886"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002887Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002888
Barry Warsaw53699e91996-12-10 23:23:01 +00002889static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002890posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002891{
2892 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002893 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002894 return NULL;
2895 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002896 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002897}
2898
Martin v. Löwis114619e2002-10-07 06:44:21 +00002899#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2900static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002901free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002902{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002903 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002904 for (i = 0; i < count; i++)
2905 PyMem_Free(array[i]);
2906 PyMem_DEL(array);
2907}
2908#endif
2909
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002910
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002911#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002912PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002913"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002914Execute an executable path with arguments, replacing current process.\n\
2915\n\
2916 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002917 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002918
Barry Warsaw53699e91996-12-10 23:23:01 +00002919static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002920posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002921{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002922 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002923 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002924 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002925 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002926 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002927
Guido van Rossum89b33251993-10-22 14:26:06 +00002928 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002929 argv is a list or tuple of strings. */
2930
Martin v. Löwis114619e2002-10-07 06:44:21 +00002931 if (!PyArg_ParseTuple(args, "etO:execv",
2932 Py_FileSystemDefaultEncoding,
2933 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002934 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002935 if (PyList_Check(argv)) {
2936 argc = PyList_Size(argv);
2937 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002938 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002939 else if (PyTuple_Check(argv)) {
2940 argc = PyTuple_Size(argv);
2941 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002942 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002943 else {
Fred Drake661ea262000-10-24 19:57:45 +00002944 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002945 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002946 return NULL;
2947 }
2948
Barry Warsaw53699e91996-12-10 23:23:01 +00002949 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002950 if (argvlist == NULL) {
2951 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002952 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002953 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002954 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002955 if (!PyArg_Parse((*getitem)(argv, i), "et",
2956 Py_FileSystemDefaultEncoding,
2957 &argvlist[i])) {
2958 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002959 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002960 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002961 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002962 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002963
Guido van Rossum85e3b011991-06-03 12:42:10 +00002964 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002965 }
2966 argvlist[argc] = NULL;
2967
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002968 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002969
Guido van Rossum85e3b011991-06-03 12:42:10 +00002970 /* If we get here it's definitely an error */
2971
Martin v. Löwis114619e2002-10-07 06:44:21 +00002972 free_string_array(argvlist, argc);
2973 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002974 return posix_error();
2975}
2976
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002977
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002978PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002979"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002980Execute a path with arguments and environment, replacing current process.\n\
2981\n\
2982 path: path of executable file\n\
2983 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002984 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002985
Barry Warsaw53699e91996-12-10 23:23:01 +00002986static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002987posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002988{
2989 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002990 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002991 char **argvlist;
2992 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002993 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002994 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002995 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00002996 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002997
2998 /* execve has three arguments: (path, argv, env), where
2999 argv is a list or tuple of strings and env is a dictionary
3000 like posix.environ. */
3001
Martin v. Löwis114619e2002-10-07 06:44:21 +00003002 if (!PyArg_ParseTuple(args, "etOO:execve",
3003 Py_FileSystemDefaultEncoding,
3004 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003005 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003006 if (PyList_Check(argv)) {
3007 argc = PyList_Size(argv);
3008 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003009 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003010 else if (PyTuple_Check(argv)) {
3011 argc = PyTuple_Size(argv);
3012 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003013 }
3014 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003015 PyErr_SetString(PyExc_TypeError,
3016 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003017 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003018 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003019 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003020 PyErr_SetString(PyExc_TypeError,
3021 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003022 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003023 }
3024
Barry Warsaw53699e91996-12-10 23:23:01 +00003025 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003026 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003027 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003028 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003029 }
3030 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003031 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00003032 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00003033 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00003034 &argvlist[i]))
3035 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003036 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003037 goto fail_1;
3038 }
3039 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003040 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003041 argvlist[argc] = NULL;
3042
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003043 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003044 if (i < 0)
3045 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003046 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003047 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003048 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003049 goto fail_1;
3050 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003051 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003052 keys = PyMapping_Keys(env);
3053 vals = PyMapping_Values(env);
3054 if (!keys || !vals)
3055 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003056 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3057 PyErr_SetString(PyExc_TypeError,
3058 "execve(): env.keys() or env.values() is not a list");
3059 goto fail_2;
3060 }
Tim Peters5aa91602002-01-30 05:46:57 +00003061
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003062 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003063 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003064 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003065
3066 key = PyList_GetItem(keys, pos);
3067 val = PyList_GetItem(vals, pos);
3068 if (!key || !val)
3069 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003070
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003071 if (!PyArg_Parse(
3072 key,
3073 "s;execve() arg 3 contains a non-string key",
3074 &k) ||
3075 !PyArg_Parse(
3076 val,
3077 "s;execve() arg 3 contains a non-string value",
3078 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003079 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003080 goto fail_2;
3081 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003082
3083#if defined(PYOS_OS2)
3084 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3085 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3086#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003087 len = PyString_Size(key) + PyString_Size(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003088 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003089 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003090 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003091 goto fail_2;
3092 }
Tim Petersc8996f52001-12-03 20:41:00 +00003093 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003094 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003095#if defined(PYOS_OS2)
3096 }
3097#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003098 }
3099 envlist[envc] = 0;
3100
3101 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003102
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003103 /* If we get here it's definitely an error */
3104
3105 (void) posix_error();
3106
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003107 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003108 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003109 PyMem_DEL(envlist[envc]);
3110 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003111 fail_1:
3112 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003113 Py_XDECREF(vals);
3114 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003115 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003116 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003117 return NULL;
3118}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003119#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003120
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003121
Guido van Rossuma1065681999-01-25 23:20:23 +00003122#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003123PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003124"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003125Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003126\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003127 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003128 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003129 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003130
3131static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003132posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003133{
3134 char *path;
3135 PyObject *argv;
3136 char **argvlist;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003137 int mode, i;
3138 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003139 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003140 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003141
3142 /* spawnv has three arguments: (mode, path, argv), where
3143 argv is a list or tuple of strings. */
3144
Martin v. Löwis114619e2002-10-07 06:44:21 +00003145 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3146 Py_FileSystemDefaultEncoding,
3147 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003148 return NULL;
3149 if (PyList_Check(argv)) {
3150 argc = PyList_Size(argv);
3151 getitem = PyList_GetItem;
3152 }
3153 else if (PyTuple_Check(argv)) {
3154 argc = PyTuple_Size(argv);
3155 getitem = PyTuple_GetItem;
3156 }
3157 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003158 PyErr_SetString(PyExc_TypeError,
3159 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003160 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003161 return NULL;
3162 }
3163
3164 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003165 if (argvlist == NULL) {
3166 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003167 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003168 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003169 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003170 if (!PyArg_Parse((*getitem)(argv, i), "et",
3171 Py_FileSystemDefaultEncoding,
3172 &argvlist[i])) {
3173 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003174 PyErr_SetString(
3175 PyExc_TypeError,
3176 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003177 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003178 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003179 }
3180 }
3181 argvlist[argc] = NULL;
3182
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003183#if defined(PYOS_OS2) && defined(PYCC_GCC)
3184 Py_BEGIN_ALLOW_THREADS
3185 spawnval = spawnv(mode, path, argvlist);
3186 Py_END_ALLOW_THREADS
3187#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003188 if (mode == _OLD_P_OVERLAY)
3189 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003190
Tim Peters25059d32001-12-07 20:35:43 +00003191 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003192 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003193 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003194#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003195
Martin v. Löwis114619e2002-10-07 06:44:21 +00003196 free_string_array(argvlist, argc);
3197 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003198
Fred Drake699f3522000-06-29 21:12:41 +00003199 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003200 return posix_error();
3201 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003202#if SIZEOF_LONG == SIZEOF_VOID_P
3203 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003204#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003205 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003206#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003207}
3208
3209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003210PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003211"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003212Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003213\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003214 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003215 path: path of executable file\n\
3216 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003217 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003218
3219static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003220posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003221{
3222 char *path;
3223 PyObject *argv, *env;
3224 char **argvlist;
3225 char **envlist;
3226 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003227 int mode, pos, envc;
3228 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003229 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003230 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003231 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003232
3233 /* spawnve has four arguments: (mode, path, argv, env), where
3234 argv is a list or tuple of strings and env is a dictionary
3235 like posix.environ. */
3236
Martin v. Löwis114619e2002-10-07 06:44:21 +00003237 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3238 Py_FileSystemDefaultEncoding,
3239 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003240 return NULL;
3241 if (PyList_Check(argv)) {
3242 argc = PyList_Size(argv);
3243 getitem = PyList_GetItem;
3244 }
3245 else if (PyTuple_Check(argv)) {
3246 argc = PyTuple_Size(argv);
3247 getitem = PyTuple_GetItem;
3248 }
3249 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003250 PyErr_SetString(PyExc_TypeError,
3251 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003252 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003253 }
3254 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003255 PyErr_SetString(PyExc_TypeError,
3256 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003257 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003258 }
3259
3260 argvlist = PyMem_NEW(char *, argc+1);
3261 if (argvlist == NULL) {
3262 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003263 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003264 }
3265 for (i = 0; i < argc; i++) {
3266 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003267 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003268 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003269 &argvlist[i]))
3270 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003271 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003272 goto fail_1;
3273 }
3274 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003275 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003276 argvlist[argc] = NULL;
3277
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003278 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003279 if (i < 0)
3280 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003281 envlist = PyMem_NEW(char *, i + 1);
3282 if (envlist == NULL) {
3283 PyErr_NoMemory();
3284 goto fail_1;
3285 }
3286 envc = 0;
3287 keys = PyMapping_Keys(env);
3288 vals = PyMapping_Values(env);
3289 if (!keys || !vals)
3290 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003291 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3292 PyErr_SetString(PyExc_TypeError,
3293 "spawnve(): env.keys() or env.values() is not a list");
3294 goto fail_2;
3295 }
Tim Peters5aa91602002-01-30 05:46:57 +00003296
Guido van Rossuma1065681999-01-25 23:20:23 +00003297 for (pos = 0; pos < i; pos++) {
3298 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003299 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003300
3301 key = PyList_GetItem(keys, pos);
3302 val = PyList_GetItem(vals, pos);
3303 if (!key || !val)
3304 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003305
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003306 if (!PyArg_Parse(
3307 key,
3308 "s;spawnve() arg 3 contains a non-string key",
3309 &k) ||
3310 !PyArg_Parse(
3311 val,
3312 "s;spawnve() arg 3 contains a non-string value",
3313 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003314 {
3315 goto fail_2;
3316 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003317 len = PyString_Size(key) + PyString_Size(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003318 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003319 if (p == NULL) {
3320 PyErr_NoMemory();
3321 goto fail_2;
3322 }
Tim Petersc8996f52001-12-03 20:41:00 +00003323 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003324 envlist[envc++] = p;
3325 }
3326 envlist[envc] = 0;
3327
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003328#if defined(PYOS_OS2) && defined(PYCC_GCC)
3329 Py_BEGIN_ALLOW_THREADS
3330 spawnval = spawnve(mode, path, argvlist, envlist);
3331 Py_END_ALLOW_THREADS
3332#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003333 if (mode == _OLD_P_OVERLAY)
3334 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003335
3336 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003337 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003338 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003339#endif
Tim Peters25059d32001-12-07 20:35:43 +00003340
Fred Drake699f3522000-06-29 21:12:41 +00003341 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003342 (void) posix_error();
3343 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003344#if SIZEOF_LONG == SIZEOF_VOID_P
3345 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003346#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003347 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003348#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003349
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003350 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003351 while (--envc >= 0)
3352 PyMem_DEL(envlist[envc]);
3353 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003354 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003355 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003356 Py_XDECREF(vals);
3357 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003358 fail_0:
3359 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003360 return res;
3361}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003362
3363/* OS/2 supports spawnvp & spawnvpe natively */
3364#if defined(PYOS_OS2)
3365PyDoc_STRVAR(posix_spawnvp__doc__,
3366"spawnvp(mode, file, args)\n\n\
3367Execute the program 'file' in a new process, using the environment\n\
3368search path to find the file.\n\
3369\n\
3370 mode: mode of process creation\n\
3371 file: executable file name\n\
3372 args: tuple or list of strings");
3373
3374static PyObject *
3375posix_spawnvp(PyObject *self, PyObject *args)
3376{
3377 char *path;
3378 PyObject *argv;
3379 char **argvlist;
3380 int mode, i, argc;
3381 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003382 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003383
3384 /* spawnvp has three arguments: (mode, path, argv), where
3385 argv is a list or tuple of strings. */
3386
3387 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3388 Py_FileSystemDefaultEncoding,
3389 &path, &argv))
3390 return NULL;
3391 if (PyList_Check(argv)) {
3392 argc = PyList_Size(argv);
3393 getitem = PyList_GetItem;
3394 }
3395 else if (PyTuple_Check(argv)) {
3396 argc = PyTuple_Size(argv);
3397 getitem = PyTuple_GetItem;
3398 }
3399 else {
3400 PyErr_SetString(PyExc_TypeError,
3401 "spawnvp() arg 2 must be a tuple or list");
3402 PyMem_Free(path);
3403 return NULL;
3404 }
3405
3406 argvlist = PyMem_NEW(char *, argc+1);
3407 if (argvlist == NULL) {
3408 PyMem_Free(path);
3409 return PyErr_NoMemory();
3410 }
3411 for (i = 0; i < argc; i++) {
3412 if (!PyArg_Parse((*getitem)(argv, i), "et",
3413 Py_FileSystemDefaultEncoding,
3414 &argvlist[i])) {
3415 free_string_array(argvlist, i);
3416 PyErr_SetString(
3417 PyExc_TypeError,
3418 "spawnvp() arg 2 must contain only strings");
3419 PyMem_Free(path);
3420 return NULL;
3421 }
3422 }
3423 argvlist[argc] = NULL;
3424
3425 Py_BEGIN_ALLOW_THREADS
3426#if defined(PYCC_GCC)
3427 spawnval = spawnvp(mode, path, argvlist);
3428#else
3429 spawnval = _spawnvp(mode, path, argvlist);
3430#endif
3431 Py_END_ALLOW_THREADS
3432
3433 free_string_array(argvlist, argc);
3434 PyMem_Free(path);
3435
3436 if (spawnval == -1)
3437 return posix_error();
3438 else
3439 return Py_BuildValue("l", (long) spawnval);
3440}
3441
3442
3443PyDoc_STRVAR(posix_spawnvpe__doc__,
3444"spawnvpe(mode, file, args, env)\n\n\
3445Execute the program 'file' in a new process, using the environment\n\
3446search path to find the file.\n\
3447\n\
3448 mode: mode of process creation\n\
3449 file: executable file name\n\
3450 args: tuple or list of arguments\n\
3451 env: dictionary of strings mapping to strings");
3452
3453static PyObject *
3454posix_spawnvpe(PyObject *self, PyObject *args)
3455{
3456 char *path;
3457 PyObject *argv, *env;
3458 char **argvlist;
3459 char **envlist;
3460 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3461 int mode, i, pos, argc, envc;
3462 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003463 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003464 int lastarg = 0;
3465
3466 /* spawnvpe has four arguments: (mode, path, argv, env), where
3467 argv is a list or tuple of strings and env is a dictionary
3468 like posix.environ. */
3469
3470 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3471 Py_FileSystemDefaultEncoding,
3472 &path, &argv, &env))
3473 return NULL;
3474 if (PyList_Check(argv)) {
3475 argc = PyList_Size(argv);
3476 getitem = PyList_GetItem;
3477 }
3478 else if (PyTuple_Check(argv)) {
3479 argc = PyTuple_Size(argv);
3480 getitem = PyTuple_GetItem;
3481 }
3482 else {
3483 PyErr_SetString(PyExc_TypeError,
3484 "spawnvpe() arg 2 must be a tuple or list");
3485 goto fail_0;
3486 }
3487 if (!PyMapping_Check(env)) {
3488 PyErr_SetString(PyExc_TypeError,
3489 "spawnvpe() arg 3 must be a mapping object");
3490 goto fail_0;
3491 }
3492
3493 argvlist = PyMem_NEW(char *, argc+1);
3494 if (argvlist == NULL) {
3495 PyErr_NoMemory();
3496 goto fail_0;
3497 }
3498 for (i = 0; i < argc; i++) {
3499 if (!PyArg_Parse((*getitem)(argv, i),
3500 "et;spawnvpe() arg 2 must contain only strings",
3501 Py_FileSystemDefaultEncoding,
3502 &argvlist[i]))
3503 {
3504 lastarg = i;
3505 goto fail_1;
3506 }
3507 }
3508 lastarg = argc;
3509 argvlist[argc] = NULL;
3510
3511 i = PyMapping_Size(env);
3512 if (i < 0)
3513 goto fail_1;
3514 envlist = PyMem_NEW(char *, i + 1);
3515 if (envlist == NULL) {
3516 PyErr_NoMemory();
3517 goto fail_1;
3518 }
3519 envc = 0;
3520 keys = PyMapping_Keys(env);
3521 vals = PyMapping_Values(env);
3522 if (!keys || !vals)
3523 goto fail_2;
3524 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3525 PyErr_SetString(PyExc_TypeError,
3526 "spawnvpe(): env.keys() or env.values() is not a list");
3527 goto fail_2;
3528 }
3529
3530 for (pos = 0; pos < i; pos++) {
3531 char *p, *k, *v;
3532 size_t len;
3533
3534 key = PyList_GetItem(keys, pos);
3535 val = PyList_GetItem(vals, pos);
3536 if (!key || !val)
3537 goto fail_2;
3538
3539 if (!PyArg_Parse(
3540 key,
3541 "s;spawnvpe() arg 3 contains a non-string key",
3542 &k) ||
3543 !PyArg_Parse(
3544 val,
3545 "s;spawnvpe() arg 3 contains a non-string value",
3546 &v))
3547 {
3548 goto fail_2;
3549 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003550 len = PyString_Size(key) + PyString_Size(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003551 p = PyMem_NEW(char, len);
3552 if (p == NULL) {
3553 PyErr_NoMemory();
3554 goto fail_2;
3555 }
3556 PyOS_snprintf(p, len, "%s=%s", k, v);
3557 envlist[envc++] = p;
3558 }
3559 envlist[envc] = 0;
3560
3561 Py_BEGIN_ALLOW_THREADS
3562#if defined(PYCC_GCC)
Andrew MacIntyre94dcf0d2008-02-03 07:07:31 +00003563 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003564#else
Andrew MacIntyre94dcf0d2008-02-03 07:07:31 +00003565 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003566#endif
3567 Py_END_ALLOW_THREADS
3568
3569 if (spawnval == -1)
3570 (void) posix_error();
3571 else
3572 res = Py_BuildValue("l", (long) spawnval);
3573
3574 fail_2:
3575 while (--envc >= 0)
3576 PyMem_DEL(envlist[envc]);
3577 PyMem_DEL(envlist);
3578 fail_1:
3579 free_string_array(argvlist, lastarg);
3580 Py_XDECREF(vals);
3581 Py_XDECREF(keys);
3582 fail_0:
3583 PyMem_Free(path);
3584 return res;
3585}
3586#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003587#endif /* HAVE_SPAWNV */
3588
3589
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003590#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003591PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003592"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003593Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3594\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003595Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003596
3597static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003598posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003599{
Christian Heimes951cc0f2008-01-31 23:08:23 +00003600 pid_t pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003601 if (pid == -1)
3602 return posix_error();
Gregory P. Smithfb7a50f2008-07-14 06:06:48 +00003603 if (pid == 0)
3604 PyOS_AfterFork();
Christian Heimes951cc0f2008-01-31 23:08:23 +00003605 return PyInt_FromLong(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003606}
3607#endif
3608
3609
Guido van Rossumad0ee831995-03-01 10:34:45 +00003610#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003611PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003612"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003613Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003614Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003615
Barry Warsaw53699e91996-12-10 23:23:01 +00003616static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003617posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003618{
Christian Heimes951cc0f2008-01-31 23:08:23 +00003619 pid_t pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003620 if (pid == -1)
3621 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003622 if (pid == 0)
3623 PyOS_AfterFork();
Christian Heimes951cc0f2008-01-31 23:08:23 +00003624 return PyInt_FromLong(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003625}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003626#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003627
Neal Norwitzb59798b2003-03-21 01:43:31 +00003628/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003629/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3630#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003631#define DEV_PTY_FILE "/dev/ptc"
3632#define HAVE_DEV_PTMX
3633#else
3634#define DEV_PTY_FILE "/dev/ptmx"
3635#endif
3636
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003637#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003638#ifdef HAVE_PTY_H
3639#include <pty.h>
3640#else
3641#ifdef HAVE_LIBUTIL_H
3642#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003643#endif /* HAVE_LIBUTIL_H */
3644#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003645#ifdef HAVE_STROPTS_H
3646#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003647#endif
3648#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003649
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003650#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003651PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003652"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003653Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003654
3655static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003656posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003657{
3658 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003659#ifndef HAVE_OPENPTY
3660 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003661#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003662#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003663 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003664#ifdef sun
Neal Norwitz84a98e02006-04-10 07:44:23 +00003665 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003666#endif
3667#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003668
Thomas Wouters70c21a12000-07-14 14:28:33 +00003669#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003670 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3671 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003672#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003673 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3674 if (slave_name == NULL)
3675 return posix_error();
3676
3677 slave_fd = open(slave_name, O_RDWR);
3678 if (slave_fd < 0)
3679 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003680#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003681 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003682 if (master_fd < 0)
3683 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003684 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003685 /* change permission of slave */
3686 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003687 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003688 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003689 }
3690 /* unlock slave */
3691 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003692 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003693 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003694 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003695 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003696 slave_name = ptsname(master_fd); /* get name of slave */
3697 if (slave_name == NULL)
3698 return posix_error();
3699 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3700 if (slave_fd < 0)
3701 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003702#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003703 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3704 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003705#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003706 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003707#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003708#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003709#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003710
Fred Drake8cef4cf2000-06-28 16:40:38 +00003711 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003712
Fred Drake8cef4cf2000-06-28 16:40:38 +00003713}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003714#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003715
3716#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003717PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003718"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003719Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3720Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003721To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003722
3723static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003724posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003725{
Christian Heimes951cc0f2008-01-31 23:08:23 +00003726 int master_fd = -1;
3727 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003728
Fred Drake8cef4cf2000-06-28 16:40:38 +00003729 pid = forkpty(&master_fd, NULL, NULL, NULL);
3730 if (pid == -1)
3731 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003732 if (pid == 0)
3733 PyOS_AfterFork();
Christian Heimes951cc0f2008-01-31 23:08:23 +00003734 return Py_BuildValue("(li)", pid, master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003735}
3736#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003737
Guido van Rossumad0ee831995-03-01 10:34:45 +00003738#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003739PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003740"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003741Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003742
Barry Warsaw53699e91996-12-10 23:23:01 +00003743static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003744posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003745{
Barry Warsaw53699e91996-12-10 23:23:01 +00003746 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003747}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003748#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003749
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003750
Guido van Rossumad0ee831995-03-01 10:34:45 +00003751#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003752PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003753"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003754Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003755
Barry Warsaw53699e91996-12-10 23:23:01 +00003756static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003757posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003758{
Barry Warsaw53699e91996-12-10 23:23:01 +00003759 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003760}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003761#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003762
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003763
Guido van Rossumad0ee831995-03-01 10:34:45 +00003764#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003765PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003766"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003767Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003768
Barry Warsaw53699e91996-12-10 23:23:01 +00003769static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003770posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003771{
Barry Warsaw53699e91996-12-10 23:23:01 +00003772 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003773}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003774#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003775
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003777PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003778"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003779Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003780
Barry Warsaw53699e91996-12-10 23:23:01 +00003781static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003782posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003783{
Barry Warsaw53699e91996-12-10 23:23:01 +00003784 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003785}
3786
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003787
Fred Drakec9680921999-12-13 16:37:25 +00003788#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003789PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003790"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003791Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003792
3793static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003794posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003795{
3796 PyObject *result = NULL;
3797
Fred Drakec9680921999-12-13 16:37:25 +00003798#ifdef NGROUPS_MAX
3799#define MAX_GROUPS NGROUPS_MAX
3800#else
3801 /* defined to be 16 on Solaris7, so this should be a small number */
3802#define MAX_GROUPS 64
3803#endif
3804 gid_t grouplist[MAX_GROUPS];
3805 int n;
3806
3807 n = getgroups(MAX_GROUPS, grouplist);
3808 if (n < 0)
3809 posix_error();
3810 else {
3811 result = PyList_New(n);
3812 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003813 int i;
3814 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003815 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003816 if (o == NULL) {
3817 Py_DECREF(result);
3818 result = NULL;
3819 break;
3820 }
3821 PyList_SET_ITEM(result, i, o);
3822 }
3823 }
3824 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003825
Fred Drakec9680921999-12-13 16:37:25 +00003826 return result;
3827}
3828#endif
3829
Martin v. Löwis606edc12002-06-13 21:09:11 +00003830#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003831PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003832"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003833Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003834
3835static PyObject *
3836posix_getpgid(PyObject *self, PyObject *args)
3837{
3838 int pid, pgid;
3839 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3840 return NULL;
3841 pgid = getpgid(pid);
3842 if (pgid < 0)
3843 return posix_error();
3844 return PyInt_FromLong((long)pgid);
3845}
3846#endif /* HAVE_GETPGID */
3847
3848
Guido van Rossumb6775db1994-08-01 11:34:53 +00003849#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003850PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003851"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003852Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003853
Barry Warsaw53699e91996-12-10 23:23:01 +00003854static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003855posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003856{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003857#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003858 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003859#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003860 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003861#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003862}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003863#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003864
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003865
Guido van Rossumb6775db1994-08-01 11:34:53 +00003866#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003867PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003868"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003869Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003870
Barry Warsaw53699e91996-12-10 23:23:01 +00003871static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003872posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003873{
Guido van Rossum64933891994-10-20 21:56:42 +00003874#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003875 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003876#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003877 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003878#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003879 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003880 Py_INCREF(Py_None);
3881 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003882}
3883
Guido van Rossumb6775db1994-08-01 11:34:53 +00003884#endif /* HAVE_SETPGRP */
3885
Guido van Rossumad0ee831995-03-01 10:34:45 +00003886#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003887PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003888"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003889Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003890
Barry Warsaw53699e91996-12-10 23:23:01 +00003891static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003892posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003893{
Christian Heimesd491d712008-02-01 18:49:26 +00003894 return PyInt_FromLong(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003895}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003896#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003897
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003898
Fred Drake12c6e2d1999-12-14 21:25:03 +00003899#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003900PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003901"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003902Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003903
3904static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003905posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003906{
Neal Norwitze241ce82003-02-17 18:17:05 +00003907 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003908 char *name;
3909 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003910
Fred Drakea30680b2000-12-06 21:24:28 +00003911 errno = 0;
3912 name = getlogin();
3913 if (name == NULL) {
3914 if (errno)
3915 posix_error();
3916 else
3917 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003918 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003919 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003920 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003921 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003922 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003923
Fred Drake12c6e2d1999-12-14 21:25:03 +00003924 return result;
3925}
3926#endif
3927
Guido van Rossumad0ee831995-03-01 10:34:45 +00003928#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003929PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003930"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003931Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003932
Barry Warsaw53699e91996-12-10 23:23:01 +00003933static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003934posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003935{
Barry Warsaw53699e91996-12-10 23:23:01 +00003936 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003937}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003938#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003939
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003940
Guido van Rossumad0ee831995-03-01 10:34:45 +00003941#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003942PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003943"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003944Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003945
Barry Warsaw53699e91996-12-10 23:23:01 +00003946static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003947posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003948{
Christian Heimesd491d712008-02-01 18:49:26 +00003949 pid_t pid;
3950 int sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003951 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003952 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003953#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003954 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3955 APIRET rc;
3956 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003957 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003958
3959 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3960 APIRET rc;
3961 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003962 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003963
3964 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003965 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003966#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003967 if (kill(pid, sig) == -1)
3968 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003969#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003970 Py_INCREF(Py_None);
3971 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003972}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003973#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003974
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003975#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003976PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003977"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003978Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003979
3980static PyObject *
3981posix_killpg(PyObject *self, PyObject *args)
3982{
3983 int pgid, sig;
3984 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3985 return NULL;
3986 if (killpg(pgid, sig) == -1)
3987 return posix_error();
3988 Py_INCREF(Py_None);
3989 return Py_None;
3990}
3991#endif
3992
Guido van Rossumc0125471996-06-28 18:55:32 +00003993#ifdef HAVE_PLOCK
3994
3995#ifdef HAVE_SYS_LOCK_H
3996#include <sys/lock.h>
3997#endif
3998
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003999PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004000"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004001Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004002
Barry Warsaw53699e91996-12-10 23:23:01 +00004003static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004004posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004005{
4006 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004007 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004008 return NULL;
4009 if (plock(op) == -1)
4010 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004011 Py_INCREF(Py_None);
4012 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004013}
4014#endif
4015
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004016
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004017#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004018PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004019"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004020Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004021
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004022#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004023#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004024static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004025async_system(const char *command)
4026{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004027 char errormsg[256], args[1024];
4028 RESULTCODES rcodes;
4029 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004030
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004031 char *shell = getenv("COMSPEC");
4032 if (!shell)
4033 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004034
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004035 /* avoid overflowing the argument buffer */
4036 if (strlen(shell) + 3 + strlen(command) >= 1024)
4037 return ERROR_NOT_ENOUGH_MEMORY
4038
4039 args[0] = '\0';
4040 strcat(args, shell);
4041 strcat(args, "/c ");
4042 strcat(args, command);
4043
4044 /* execute asynchronously, inheriting the environment */
4045 rc = DosExecPgm(errormsg,
4046 sizeof(errormsg),
4047 EXEC_ASYNC,
4048 args,
4049 NULL,
4050 &rcodes,
4051 shell);
4052 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004053}
4054
Guido van Rossumd48f2521997-12-05 22:19:34 +00004055static FILE *
4056popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004057{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004058 int oldfd, tgtfd;
4059 HFILE pipeh[2];
4060 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004061
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004062 /* mode determines which of stdin or stdout is reconnected to
4063 * the pipe to the child
4064 */
4065 if (strchr(mode, 'r') != NULL) {
4066 tgt_fd = 1; /* stdout */
4067 } else if (strchr(mode, 'w')) {
4068 tgt_fd = 0; /* stdin */
4069 } else {
4070 *err = ERROR_INVALID_ACCESS;
4071 return NULL;
4072 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004073
Andrew MacIntyrea3be2582004-12-18 09:51:05 +00004074 /* setup the pipe */
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004075 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
4076 *err = rc;
4077 return NULL;
4078 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004079
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004080 /* prevent other threads accessing stdio */
4081 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004082
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004083 /* reconnect stdio and execute child */
4084 oldfd = dup(tgtfd);
4085 close(tgtfd);
4086 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
4087 DosClose(pipeh[tgtfd]);
4088 rc = async_system(command);
4089 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004090
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004091 /* restore stdio */
4092 dup2(oldfd, tgtfd);
4093 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004094
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004095 /* allow other threads access to stdio */
4096 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004097
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004098 /* if execution of child was successful return file stream */
4099 if (rc == NO_ERROR)
4100 return fdopen(pipeh[1 - tgtfd], mode);
4101 else {
4102 DosClose(pipeh[1 - tgtfd]);
4103 *err = rc;
4104 return NULL;
4105 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004106}
4107
4108static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004109posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004110{
4111 char *name;
4112 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00004113 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004114 FILE *fp;
4115 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004116 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004117 return NULL;
4118 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00004119 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004120 Py_END_ALLOW_THREADS
4121 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004122 return os2_error(err);
4123
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004124 f = PyFile_FromFile(fp, name, mode, fclose);
4125 if (f != NULL)
4126 PyFile_SetBufSize(f, bufsize);
4127 return f;
4128}
4129
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004130#elif defined(PYCC_GCC)
4131
4132/* standard posix version of popen() support */
4133static PyObject *
4134posix_popen(PyObject *self, PyObject *args)
4135{
4136 char *name;
4137 char *mode = "r";
4138 int bufsize = -1;
4139 FILE *fp;
4140 PyObject *f;
4141 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4142 return NULL;
4143 Py_BEGIN_ALLOW_THREADS
4144 fp = popen(name, mode);
4145 Py_END_ALLOW_THREADS
4146 if (fp == NULL)
4147 return posix_error();
4148 f = PyFile_FromFile(fp, name, mode, pclose);
4149 if (f != NULL)
4150 PyFile_SetBufSize(f, bufsize);
4151 return f;
4152}
4153
4154/* fork() under OS/2 has lots'o'warts
4155 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4156 * most of this code is a ripoff of the win32 code, but using the
4157 * capabilities of EMX's C library routines
4158 */
4159
4160/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4161#define POPEN_1 1
4162#define POPEN_2 2
4163#define POPEN_3 3
4164#define POPEN_4 4
4165
4166static PyObject *_PyPopen(char *, int, int, int);
4167static int _PyPclose(FILE *file);
4168
4169/*
4170 * Internal dictionary mapping popen* file pointers to process handles,
4171 * for use when retrieving the process exit code. See _PyPclose() below
4172 * for more information on this dictionary's use.
4173 */
4174static PyObject *_PyPopenProcs = NULL;
4175
4176/* os2emx version of popen2()
4177 *
4178 * The result of this function is a pipe (file) connected to the
4179 * process's stdin, and a pipe connected to the process's stdout.
4180 */
4181
4182static PyObject *
4183os2emx_popen2(PyObject *self, PyObject *args)
4184{
4185 PyObject *f;
4186 int tm=0;
4187
4188 char *cmdstring;
4189 char *mode = "t";
4190 int bufsize = -1;
4191 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4192 return NULL;
4193
4194 if (*mode == 't')
4195 tm = O_TEXT;
4196 else if (*mode != 'b') {
4197 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4198 return NULL;
4199 } else
4200 tm = O_BINARY;
4201
4202 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
4203
4204 return f;
4205}
4206
4207/*
4208 * Variation on os2emx.popen2
4209 *
4210 * The result of this function is 3 pipes - the process's stdin,
4211 * stdout and stderr
4212 */
4213
4214static PyObject *
4215os2emx_popen3(PyObject *self, PyObject *args)
4216{
4217 PyObject *f;
4218 int tm = 0;
4219
4220 char *cmdstring;
4221 char *mode = "t";
4222 int bufsize = -1;
4223 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4224 return NULL;
4225
4226 if (*mode == 't')
4227 tm = O_TEXT;
4228 else if (*mode != 'b') {
4229 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4230 return NULL;
4231 } else
4232 tm = O_BINARY;
4233
4234 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
4235
4236 return f;
4237}
4238
4239/*
4240 * Variation on os2emx.popen2
4241 *
Tim Peters11b23062003-04-23 02:39:17 +00004242 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004243 * and stdout+stderr combined as a single pipe.
4244 */
4245
4246static PyObject *
4247os2emx_popen4(PyObject *self, PyObject *args)
4248{
4249 PyObject *f;
4250 int tm = 0;
4251
4252 char *cmdstring;
4253 char *mode = "t";
4254 int bufsize = -1;
4255 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4256 return NULL;
4257
4258 if (*mode == 't')
4259 tm = O_TEXT;
4260 else if (*mode != 'b') {
4261 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4262 return NULL;
4263 } else
4264 tm = O_BINARY;
4265
4266 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
4267
4268 return f;
4269}
4270
4271/* a couple of structures for convenient handling of multiple
4272 * file handles and pipes
4273 */
4274struct file_ref
4275{
4276 int handle;
4277 int flags;
4278};
4279
4280struct pipe_ref
4281{
4282 int rd;
4283 int wr;
4284};
4285
4286/* The following code is derived from the win32 code */
4287
4288static PyObject *
4289_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4290{
4291 struct file_ref stdio[3];
4292 struct pipe_ref p_fd[3];
4293 FILE *p_s[3];
Christian Heimesd491d712008-02-01 18:49:26 +00004294 int file_count, i, pipe_err;
4295 pid_t pipe_pid;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004296 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4297 PyObject *f, *p_f[3];
4298
4299 /* file modes for subsequent fdopen's on pipe handles */
4300 if (mode == O_TEXT)
4301 {
4302 rd_mode = "rt";
4303 wr_mode = "wt";
4304 }
4305 else
4306 {
4307 rd_mode = "rb";
4308 wr_mode = "wb";
4309 }
4310
4311 /* prepare shell references */
4312 if ((shell = getenv("EMXSHELL")) == NULL)
4313 if ((shell = getenv("COMSPEC")) == NULL)
4314 {
4315 errno = ENOENT;
4316 return posix_error();
4317 }
4318
4319 sh_name = _getname(shell);
4320 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4321 opt = "/c";
4322 else
4323 opt = "-c";
4324
4325 /* save current stdio fds + their flags, and set not inheritable */
4326 i = pipe_err = 0;
4327 while (pipe_err >= 0 && i < 3)
4328 {
4329 pipe_err = stdio[i].handle = dup(i);
4330 stdio[i].flags = fcntl(i, F_GETFD, 0);
4331 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4332 i++;
4333 }
4334 if (pipe_err < 0)
4335 {
4336 /* didn't get them all saved - clean up and bail out */
4337 int saved_err = errno;
4338 while (i-- > 0)
4339 {
4340 close(stdio[i].handle);
4341 }
4342 errno = saved_err;
4343 return posix_error();
4344 }
4345
4346 /* create pipe ends */
4347 file_count = 2;
4348 if (n == POPEN_3)
4349 file_count = 3;
4350 i = pipe_err = 0;
4351 while ((pipe_err == 0) && (i < file_count))
4352 pipe_err = pipe((int *)&p_fd[i++]);
4353 if (pipe_err < 0)
4354 {
4355 /* didn't get them all made - clean up and bail out */
4356 while (i-- > 0)
4357 {
4358 close(p_fd[i].wr);
4359 close(p_fd[i].rd);
4360 }
4361 errno = EPIPE;
4362 return posix_error();
4363 }
4364
4365 /* change the actual standard IO streams over temporarily,
4366 * making the retained pipe ends non-inheritable
4367 */
4368 pipe_err = 0;
4369
4370 /* - stdin */
4371 if (dup2(p_fd[0].rd, 0) == 0)
4372 {
4373 close(p_fd[0].rd);
4374 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4375 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4376 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4377 {
4378 close(p_fd[0].wr);
4379 pipe_err = -1;
4380 }
4381 }
4382 else
4383 {
4384 pipe_err = -1;
4385 }
4386
4387 /* - stdout */
4388 if (pipe_err == 0)
4389 {
4390 if (dup2(p_fd[1].wr, 1) == 1)
4391 {
4392 close(p_fd[1].wr);
4393 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4394 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4395 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4396 {
4397 close(p_fd[1].rd);
4398 pipe_err = -1;
4399 }
4400 }
4401 else
4402 {
4403 pipe_err = -1;
4404 }
4405 }
4406
4407 /* - stderr, as required */
4408 if (pipe_err == 0)
4409 switch (n)
4410 {
4411 case POPEN_3:
4412 {
4413 if (dup2(p_fd[2].wr, 2) == 2)
4414 {
4415 close(p_fd[2].wr);
4416 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4417 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4418 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4419 {
4420 close(p_fd[2].rd);
4421 pipe_err = -1;
4422 }
4423 }
4424 else
4425 {
4426 pipe_err = -1;
4427 }
4428 break;
4429 }
4430
4431 case POPEN_4:
4432 {
4433 if (dup2(1, 2) != 2)
4434 {
4435 pipe_err = -1;
4436 }
4437 break;
4438 }
4439 }
4440
4441 /* spawn the child process */
4442 if (pipe_err == 0)
4443 {
4444 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4445 if (pipe_pid == -1)
4446 {
4447 pipe_err = -1;
4448 }
4449 else
4450 {
4451 /* save the PID into the FILE structure
4452 * NOTE: this implementation doesn't actually
4453 * take advantage of this, but do it for
4454 * completeness - AIM Apr01
4455 */
4456 for (i = 0; i < file_count; i++)
4457 p_s[i]->_pid = pipe_pid;
4458 }
4459 }
4460
4461 /* reset standard IO to normal */
4462 for (i = 0; i < 3; i++)
4463 {
4464 dup2(stdio[i].handle, i);
4465 fcntl(i, F_SETFD, stdio[i].flags);
4466 close(stdio[i].handle);
4467 }
4468
4469 /* if any remnant problems, clean up and bail out */
4470 if (pipe_err < 0)
4471 {
4472 for (i = 0; i < 3; i++)
4473 {
4474 close(p_fd[i].rd);
4475 close(p_fd[i].wr);
4476 }
4477 errno = EPIPE;
4478 return posix_error_with_filename(cmdstring);
4479 }
4480
4481 /* build tuple of file objects to return */
4482 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4483 PyFile_SetBufSize(p_f[0], bufsize);
4484 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4485 PyFile_SetBufSize(p_f[1], bufsize);
4486 if (n == POPEN_3)
4487 {
4488 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4489 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004490 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004491 }
4492 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004493 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004494
4495 /*
4496 * Insert the files we've created into the process dictionary
4497 * all referencing the list with the process handle and the
4498 * initial number of files (see description below in _PyPclose).
4499 * Since if _PyPclose later tried to wait on a process when all
4500 * handles weren't closed, it could create a deadlock with the
4501 * child, we spend some energy here to try to ensure that we
4502 * either insert all file handles into the dictionary or none
4503 * at all. It's a little clumsy with the various popen modes
4504 * and variable number of files involved.
4505 */
4506 if (!_PyPopenProcs)
4507 {
4508 _PyPopenProcs = PyDict_New();
4509 }
4510
4511 if (_PyPopenProcs)
4512 {
4513 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4514 int ins_rc[3];
4515
4516 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4517 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4518
4519 procObj = PyList_New(2);
4520 pidObj = PyInt_FromLong((long) pipe_pid);
4521 intObj = PyInt_FromLong((long) file_count);
4522
4523 if (procObj && pidObj && intObj)
4524 {
4525 PyList_SetItem(procObj, 0, pidObj);
4526 PyList_SetItem(procObj, 1, intObj);
4527
4528 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4529 if (fileObj[0])
4530 {
4531 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4532 fileObj[0],
4533 procObj);
4534 }
4535 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4536 if (fileObj[1])
4537 {
4538 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4539 fileObj[1],
4540 procObj);
4541 }
4542 if (file_count >= 3)
4543 {
4544 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4545 if (fileObj[2])
4546 {
4547 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4548 fileObj[2],
4549 procObj);
4550 }
4551 }
4552
4553 if (ins_rc[0] < 0 || !fileObj[0] ||
4554 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4555 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4556 {
4557 /* Something failed - remove any dictionary
4558 * entries that did make it.
4559 */
4560 if (!ins_rc[0] && fileObj[0])
4561 {
4562 PyDict_DelItem(_PyPopenProcs,
4563 fileObj[0]);
4564 }
4565 if (!ins_rc[1] && fileObj[1])
4566 {
4567 PyDict_DelItem(_PyPopenProcs,
4568 fileObj[1]);
4569 }
4570 if (!ins_rc[2] && fileObj[2])
4571 {
4572 PyDict_DelItem(_PyPopenProcs,
4573 fileObj[2]);
4574 }
4575 }
4576 }
Tim Peters11b23062003-04-23 02:39:17 +00004577
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004578 /*
4579 * Clean up our localized references for the dictionary keys
4580 * and value since PyDict_SetItem will Py_INCREF any copies
4581 * that got placed in the dictionary.
4582 */
4583 Py_XDECREF(procObj);
4584 Py_XDECREF(fileObj[0]);
4585 Py_XDECREF(fileObj[1]);
4586 Py_XDECREF(fileObj[2]);
4587 }
4588
4589 /* Child is launched. */
4590 return f;
4591}
4592
4593/*
4594 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4595 * exit code for the child process and return as a result of the close.
4596 *
4597 * This function uses the _PyPopenProcs dictionary in order to map the
4598 * input file pointer to information about the process that was
4599 * originally created by the popen* call that created the file pointer.
4600 * The dictionary uses the file pointer as a key (with one entry
4601 * inserted for each file returned by the original popen* call) and a
4602 * single list object as the value for all files from a single call.
4603 * The list object contains the Win32 process handle at [0], and a file
4604 * count at [1], which is initialized to the total number of file
4605 * handles using that list.
4606 *
4607 * This function closes whichever handle it is passed, and decrements
4608 * the file count in the dictionary for the process handle pointed to
4609 * by this file. On the last close (when the file count reaches zero),
4610 * this function will wait for the child process and then return its
4611 * exit code as the result of the close() operation. This permits the
4612 * files to be closed in any order - it is always the close() of the
4613 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004614 *
4615 * NOTE: This function is currently called with the GIL released.
4616 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004617 */
4618
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004619static int _PyPclose(FILE *file)
4620{
4621 int result;
4622 int exit_code;
Christian Heimesd491d712008-02-01 18:49:26 +00004623 pid_t pipe_pid;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004624 PyObject *procObj, *pidObj, *intObj, *fileObj;
4625 int file_count;
4626#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004627 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004628#endif
4629
4630 /* Close the file handle first, to ensure it can't block the
4631 * child from exiting if it's the last handle.
4632 */
4633 result = fclose(file);
4634
4635#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004636 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004637#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004638 if (_PyPopenProcs)
4639 {
4640 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4641 (procObj = PyDict_GetItem(_PyPopenProcs,
4642 fileObj)) != NULL &&
4643 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4644 (intObj = PyList_GetItem(procObj,1)) != NULL)
4645 {
4646 pipe_pid = (int) PyInt_AsLong(pidObj);
4647 file_count = (int) PyInt_AsLong(intObj);
4648
4649 if (file_count > 1)
4650 {
4651 /* Still other files referencing process */
4652 file_count--;
4653 PyList_SetItem(procObj,1,
4654 PyInt_FromLong((long) file_count));
4655 }
4656 else
4657 {
4658 /* Last file for this process */
4659 if (result != EOF &&
4660 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4661 {
4662 /* extract exit status */
4663 if (WIFEXITED(exit_code))
4664 {
4665 result = WEXITSTATUS(exit_code);
4666 }
4667 else
4668 {
4669 errno = EPIPE;
4670 result = -1;
4671 }
4672 }
4673 else
4674 {
4675 /* Indicate failure - this will cause the file object
4676 * to raise an I/O error and translate the last
4677 * error code from errno. We do have a problem with
4678 * last errors that overlap the normal errno table,
4679 * but that's a consistent problem with the file object.
4680 */
4681 result = -1;
4682 }
4683 }
4684
4685 /* Remove this file pointer from dictionary */
4686 PyDict_DelItem(_PyPopenProcs, fileObj);
4687
4688 if (PyDict_Size(_PyPopenProcs) == 0)
4689 {
4690 Py_DECREF(_PyPopenProcs);
4691 _PyPopenProcs = NULL;
4692 }
4693
4694 } /* if object retrieval ok */
4695
4696 Py_XDECREF(fileObj);
4697 } /* if _PyPopenProcs */
4698
4699#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004700 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004701#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004702 return result;
4703}
4704
4705#endif /* PYCC_??? */
4706
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004707#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004708
4709/*
4710 * Portable 'popen' replacement for Win32.
4711 *
4712 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4713 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004714 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004715 */
4716
4717#include <malloc.h>
4718#include <io.h>
4719#include <fcntl.h>
4720
4721/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4722#define POPEN_1 1
4723#define POPEN_2 2
4724#define POPEN_3 3
4725#define POPEN_4 4
4726
4727static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004728static int _PyPclose(FILE *file);
4729
4730/*
4731 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004732 * for use when retrieving the process exit code. See _PyPclose() below
4733 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004734 */
4735static PyObject *_PyPopenProcs = NULL;
4736
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004737
4738/* popen that works from a GUI.
4739 *
4740 * The result of this function is a pipe (file) connected to the
4741 * processes stdin or stdout, depending on the requested mode.
4742 */
4743
4744static PyObject *
4745posix_popen(PyObject *self, PyObject *args)
4746{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00004747 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004748 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004749
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004750 char *cmdstring;
4751 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004752 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004753 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004754 return NULL;
4755
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004756 if (*mode == 'r')
4757 tm = _O_RDONLY;
4758 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00004759 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004760 return NULL;
4761 } else
4762 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004763
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004764 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004765 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004766 return NULL;
4767 }
4768
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004769 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004770 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004771 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004772 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004773 else
4774 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4775
4776 return f;
4777}
4778
4779/* Variation on win32pipe.popen
4780 *
4781 * The result of this function is a pipe (file) connected to the
4782 * process's stdin, and a pipe connected to the process's stdout.
4783 */
4784
4785static PyObject *
4786win32_popen2(PyObject *self, PyObject *args)
4787{
4788 PyObject *f;
4789 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004790
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004791 char *cmdstring;
4792 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004793 int bufsize = -1;
4794 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004795 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004796
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004797 if (*mode == 't')
4798 tm = _O_TEXT;
4799 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004800 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004801 return NULL;
4802 } else
4803 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004804
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004805 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004806 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004807 return NULL;
4808 }
4809
4810 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004811
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004812 return f;
4813}
4814
4815/*
4816 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004817 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004818 * The result of this function is 3 pipes - the process's stdin,
4819 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004820 */
4821
4822static PyObject *
4823win32_popen3(PyObject *self, PyObject *args)
4824{
4825 PyObject *f;
4826 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004827
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004828 char *cmdstring;
4829 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004830 int bufsize = -1;
4831 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004832 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004833
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004834 if (*mode == 't')
4835 tm = _O_TEXT;
4836 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004837 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004838 return NULL;
4839 } else
4840 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004841
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004842 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004843 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004844 return NULL;
4845 }
4846
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004847 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004848
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004849 return f;
4850}
4851
4852/*
4853 * Variation on win32pipe.popen
4854 *
Tim Peters5aa91602002-01-30 05:46:57 +00004855 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004856 * and stdout+stderr combined as a single pipe.
4857 */
4858
4859static PyObject *
4860win32_popen4(PyObject *self, PyObject *args)
4861{
4862 PyObject *f;
4863 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004864
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004865 char *cmdstring;
4866 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004867 int bufsize = -1;
4868 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004869 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004870
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004871 if (*mode == 't')
4872 tm = _O_TEXT;
4873 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004874 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004875 return NULL;
4876 } else
4877 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004878
4879 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004880 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004881 return NULL;
4882 }
4883
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004884 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004885
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004886 return f;
4887}
4888
Mark Hammond08501372001-01-31 07:30:29 +00004889static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004890_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004891 HANDLE hStdin,
4892 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004893 HANDLE hStderr,
4894 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004895{
4896 PROCESS_INFORMATION piProcInfo;
4897 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004898 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004899 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004900 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004901 int i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004902 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004903
4904 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004905 char *comshell;
4906
Tim Peters92e4dd82002-10-05 01:47:34 +00004907 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004908 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
Martin v. Löwis18e16552006-02-15 17:27:45 +00004909 /* x < i, so x fits into an integer */
4910 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00004911
4912 /* Explicitly check if we are using COMMAND.COM. If we are
4913 * then use the w9xpopen hack.
4914 */
4915 comshell = s1 + x;
4916 while (comshell >= s1 && *comshell != '\\')
4917 --comshell;
4918 ++comshell;
4919
4920 if (GetVersion() < 0x80000000 &&
4921 _stricmp(comshell, "command.com") != 0) {
4922 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004923 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004924 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004925 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004926 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004927 }
4928 else {
4929 /*
Tim Peters402d5982001-08-27 06:37:48 +00004930 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4931 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004932 */
Mark Hammond08501372001-01-31 07:30:29 +00004933 char modulepath[_MAX_PATH];
4934 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004935 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
Martin v. Löwis26fd9602006-04-22 11:15:41 +00004936 for (x = i = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004937 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004938 x = i+1;
4939 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004940 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004941 strncat(modulepath,
4942 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004943 (sizeof(modulepath)/sizeof(modulepath[0]))
4944 -strlen(modulepath));
4945 if (stat(modulepath, &statinfo) != 0) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004946 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
Tim Peters5aa91602002-01-30 05:46:57 +00004947 /* Eeek - file-not-found - possibly an embedding
4948 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004949 */
Tim Peters5aa91602002-01-30 05:46:57 +00004950 strncpy(modulepath,
4951 Py_GetExecPrefix(),
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004952 mplen);
4953 modulepath[mplen-1] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004954 if (modulepath[strlen(modulepath)-1] != '\\')
4955 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004956 strncat(modulepath,
4957 szConsoleSpawn,
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004958 mplen-strlen(modulepath));
Mark Hammond08501372001-01-31 07:30:29 +00004959 /* No where else to look - raise an easily identifiable
4960 error, rather than leaving Windows to report
4961 "file not found" - as the user is probably blissfully
4962 unaware this shim EXE is used, and it will confuse them.
4963 (well, it confused me for a while ;-)
4964 */
4965 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004966 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004967 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004968 "for popen to work with your shell "
4969 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004970 szConsoleSpawn);
4971 return FALSE;
4972 }
4973 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004974 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004975 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004976 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004977
Tim Peters92e4dd82002-10-05 01:47:34 +00004978 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004979 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004980 /* To maintain correct argument passing semantics,
4981 we pass the command-line as it stands, and allow
4982 quoting to be applied. w9xpopen.exe will then
4983 use its argv vector, and re-quote the necessary
4984 args for the ultimate child process.
4985 */
Tim Peters75cdad52001-11-28 22:07:30 +00004986 PyOS_snprintf(
4987 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004988 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004989 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004990 s1,
4991 s3,
4992 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004993 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004994 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004995 dialog:
4996 "Your program accessed mem currently in use at xxx"
4997 and a hopeful warning about the stability of your
4998 system.
4999 Cost is Ctrl+C wont kill children, but anyone
5000 who cares can have a go!
5001 */
5002 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005003 }
5004 }
5005
5006 /* Could be an else here to try cmd.exe / command.com in the path
5007 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00005008 else {
Tim Peters402d5982001-08-27 06:37:48 +00005009 PyErr_SetString(PyExc_RuntimeError,
5010 "Cannot locate a COMSPEC environment variable to "
5011 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00005012 return FALSE;
5013 }
Tim Peters5aa91602002-01-30 05:46:57 +00005014
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005015 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
5016 siStartInfo.cb = sizeof(STARTUPINFO);
5017 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
5018 siStartInfo.hStdInput = hStdin;
5019 siStartInfo.hStdOutput = hStdout;
5020 siStartInfo.hStdError = hStderr;
5021 siStartInfo.wShowWindow = SW_HIDE;
5022
5023 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005024 s2,
5025 NULL,
5026 NULL,
5027 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005028 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005029 NULL,
5030 NULL,
5031 &siStartInfo,
5032 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005033 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005034 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005035
Mark Hammondb37a3732000-08-14 04:47:33 +00005036 /* Return process handle */
5037 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005038 return TRUE;
5039 }
Tim Peters402d5982001-08-27 06:37:48 +00005040 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005041 return FALSE;
5042}
5043
5044/* The following code is based off of KB: Q190351 */
5045
5046static PyObject *
5047_PyPopen(char *cmdstring, int mode, int n)
5048{
5049 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
5050 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00005051 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00005052
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005053 SECURITY_ATTRIBUTES saAttr;
5054 BOOL fSuccess;
5055 int fd1, fd2, fd3;
5056 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00005057 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005058 PyObject *f;
5059
5060 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5061 saAttr.bInheritHandle = TRUE;
5062 saAttr.lpSecurityDescriptor = NULL;
5063
5064 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
5065 return win32_error("CreatePipe", NULL);
5066
5067 /* Create new output read handle and the input write handle. Set
5068 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00005069 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005070 * being created. */
5071 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005072 GetCurrentProcess(), &hChildStdinWrDup, 0,
5073 FALSE,
5074 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005075 if (!fSuccess)
5076 return win32_error("DuplicateHandle", NULL);
5077
5078 /* Close the inheritable version of ChildStdin
5079 that we're using. */
5080 CloseHandle(hChildStdinWr);
5081
5082 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
5083 return win32_error("CreatePipe", NULL);
5084
5085 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005086 GetCurrentProcess(), &hChildStdoutRdDup, 0,
5087 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005088 if (!fSuccess)
5089 return win32_error("DuplicateHandle", NULL);
5090
5091 /* Close the inheritable version of ChildStdout
5092 that we're using. */
5093 CloseHandle(hChildStdoutRd);
5094
5095 if (n != POPEN_4) {
5096 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
5097 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005098 fSuccess = DuplicateHandle(GetCurrentProcess(),
5099 hChildStderrRd,
5100 GetCurrentProcess(),
5101 &hChildStderrRdDup, 0,
5102 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005103 if (!fSuccess)
5104 return win32_error("DuplicateHandle", NULL);
5105 /* Close the inheritable version of ChildStdErr that we're using. */
5106 CloseHandle(hChildStderrRd);
5107 }
Tim Peters5aa91602002-01-30 05:46:57 +00005108
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005109 switch (n) {
5110 case POPEN_1:
5111 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5112 case _O_WRONLY | _O_TEXT:
5113 /* Case for writing to child Stdin in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005114 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005115 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005116 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005117 PyFile_SetBufSize(f, 0);
5118 /* We don't care about these pipes anymore, so close them. */
5119 CloseHandle(hChildStdoutRdDup);
5120 CloseHandle(hChildStderrRdDup);
5121 break;
5122
5123 case _O_RDONLY | _O_TEXT:
5124 /* Case for reading from child Stdout in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005125 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005126 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005127 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005128 PyFile_SetBufSize(f, 0);
5129 /* We don't care about these pipes anymore, so close them. */
5130 CloseHandle(hChildStdinWrDup);
5131 CloseHandle(hChildStderrRdDup);
5132 break;
5133
5134 case _O_RDONLY | _O_BINARY:
5135 /* Case for readinig from child Stdout in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005136 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005137 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005138 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005139 PyFile_SetBufSize(f, 0);
5140 /* We don't care about these pipes anymore, so close them. */
5141 CloseHandle(hChildStdinWrDup);
5142 CloseHandle(hChildStderrRdDup);
5143 break;
5144
5145 case _O_WRONLY | _O_BINARY:
5146 /* Case for writing to child Stdin in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005147 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005148 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005149 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005150 PyFile_SetBufSize(f, 0);
5151 /* We don't care about these pipes anymore, so close them. */
5152 CloseHandle(hChildStdoutRdDup);
5153 CloseHandle(hChildStderrRdDup);
5154 break;
5155 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005156 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005157 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005158
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005159 case POPEN_2:
5160 case POPEN_4:
5161 {
5162 char *m1, *m2;
5163 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005164
Tim Peters7dca21e2002-08-19 00:42:29 +00005165 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005166 m1 = "r";
5167 m2 = "w";
5168 } else {
5169 m1 = "rb";
5170 m2 = "wb";
5171 }
5172
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005173 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005174 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005175 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005176 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005177 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005178 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00005179 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005180 PyFile_SetBufSize(p2, 0);
5181
5182 if (n != 4)
5183 CloseHandle(hChildStderrRdDup);
5184
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005185 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00005186 Py_XDECREF(p1);
5187 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00005188 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005189 break;
5190 }
Tim Peters5aa91602002-01-30 05:46:57 +00005191
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005192 case POPEN_3:
5193 {
5194 char *m1, *m2;
5195 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005196
Tim Peters7dca21e2002-08-19 00:42:29 +00005197 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005198 m1 = "r";
5199 m2 = "w";
5200 } else {
5201 m1 = "rb";
5202 m2 = "wb";
5203 }
5204
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005205 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005206 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005207 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005208 f2 = _fdopen(fd2, m1);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005209 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005210 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005211 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00005212 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5213 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005214 PyFile_SetBufSize(p1, 0);
5215 PyFile_SetBufSize(p2, 0);
5216 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005217 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00005218 Py_XDECREF(p1);
5219 Py_XDECREF(p2);
5220 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00005221 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005222 break;
5223 }
5224 }
5225
5226 if (n == POPEN_4) {
5227 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005228 hChildStdinRd,
5229 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005230 hChildStdoutWr,
5231 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005232 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005233 }
5234 else {
5235 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005236 hChildStdinRd,
5237 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005238 hChildStderrWr,
5239 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005240 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005241 }
5242
Mark Hammondb37a3732000-08-14 04:47:33 +00005243 /*
5244 * Insert the files we've created into the process dictionary
5245 * all referencing the list with the process handle and the
5246 * initial number of files (see description below in _PyPclose).
5247 * Since if _PyPclose later tried to wait on a process when all
5248 * handles weren't closed, it could create a deadlock with the
5249 * child, we spend some energy here to try to ensure that we
5250 * either insert all file handles into the dictionary or none
5251 * at all. It's a little clumsy with the various popen modes
5252 * and variable number of files involved.
5253 */
5254 if (!_PyPopenProcs) {
5255 _PyPopenProcs = PyDict_New();
5256 }
5257
5258 if (_PyPopenProcs) {
5259 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5260 int ins_rc[3];
5261
5262 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5263 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
5264
5265 procObj = PyList_New(2);
5266 hProcessObj = PyLong_FromVoidPtr(hProcess);
5267 intObj = PyInt_FromLong(file_count);
5268
5269 if (procObj && hProcessObj && intObj) {
5270 PyList_SetItem(procObj,0,hProcessObj);
5271 PyList_SetItem(procObj,1,intObj);
5272
5273 fileObj[0] = PyLong_FromVoidPtr(f1);
5274 if (fileObj[0]) {
5275 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5276 fileObj[0],
5277 procObj);
5278 }
5279 if (file_count >= 2) {
5280 fileObj[1] = PyLong_FromVoidPtr(f2);
5281 if (fileObj[1]) {
5282 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5283 fileObj[1],
5284 procObj);
5285 }
5286 }
5287 if (file_count >= 3) {
5288 fileObj[2] = PyLong_FromVoidPtr(f3);
5289 if (fileObj[2]) {
5290 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5291 fileObj[2],
5292 procObj);
5293 }
5294 }
5295
5296 if (ins_rc[0] < 0 || !fileObj[0] ||
5297 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5298 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5299 /* Something failed - remove any dictionary
5300 * entries that did make it.
5301 */
5302 if (!ins_rc[0] && fileObj[0]) {
5303 PyDict_DelItem(_PyPopenProcs,
5304 fileObj[0]);
5305 }
5306 if (!ins_rc[1] && fileObj[1]) {
5307 PyDict_DelItem(_PyPopenProcs,
5308 fileObj[1]);
5309 }
5310 if (!ins_rc[2] && fileObj[2]) {
5311 PyDict_DelItem(_PyPopenProcs,
5312 fileObj[2]);
5313 }
5314 }
5315 }
Tim Peters5aa91602002-01-30 05:46:57 +00005316
Mark Hammondb37a3732000-08-14 04:47:33 +00005317 /*
5318 * Clean up our localized references for the dictionary keys
5319 * and value since PyDict_SetItem will Py_INCREF any copies
5320 * that got placed in the dictionary.
5321 */
5322 Py_XDECREF(procObj);
5323 Py_XDECREF(fileObj[0]);
5324 Py_XDECREF(fileObj[1]);
5325 Py_XDECREF(fileObj[2]);
5326 }
5327
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005328 /* Child is launched. Close the parents copy of those pipe
5329 * handles that only the child should have open. You need to
5330 * make sure that no handles to the write end of the output pipe
5331 * are maintained in this process or else the pipe will not close
5332 * when the child process exits and the ReadFile will hang. */
5333
5334 if (!CloseHandle(hChildStdinRd))
5335 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005336
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005337 if (!CloseHandle(hChildStdoutWr))
5338 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005339
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005340 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5341 return win32_error("CloseHandle", NULL);
5342
5343 return f;
5344}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005345
5346/*
5347 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5348 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005349 *
5350 * This function uses the _PyPopenProcs dictionary in order to map the
5351 * input file pointer to information about the process that was
5352 * originally created by the popen* call that created the file pointer.
5353 * The dictionary uses the file pointer as a key (with one entry
5354 * inserted for each file returned by the original popen* call) and a
5355 * single list object as the value for all files from a single call.
5356 * The list object contains the Win32 process handle at [0], and a file
5357 * count at [1], which is initialized to the total number of file
5358 * handles using that list.
5359 *
5360 * This function closes whichever handle it is passed, and decrements
5361 * the file count in the dictionary for the process handle pointed to
5362 * by this file. On the last close (when the file count reaches zero),
5363 * this function will wait for the child process and then return its
5364 * exit code as the result of the close() operation. This permits the
5365 * files to be closed in any order - it is always the close() of the
5366 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005367 *
5368 * NOTE: This function is currently called with the GIL released.
5369 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005370 */
Tim Peters736aa322000-09-01 06:51:24 +00005371
Fredrik Lundh56055a42000-07-23 19:47:12 +00005372static int _PyPclose(FILE *file)
5373{
Fredrik Lundh20318932000-07-26 17:29:12 +00005374 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005375 DWORD exit_code;
5376 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00005377 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5378 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005379#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005380 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005381#endif
5382
Fredrik Lundh20318932000-07-26 17:29:12 +00005383 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00005384 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00005385 */
5386 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005387#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005388 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005389#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005390 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005391 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5392 (procObj = PyDict_GetItem(_PyPopenProcs,
5393 fileObj)) != NULL &&
5394 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5395 (intObj = PyList_GetItem(procObj,1)) != NULL) {
5396
5397 hProcess = PyLong_AsVoidPtr(hProcessObj);
5398 file_count = PyInt_AsLong(intObj);
5399
5400 if (file_count > 1) {
5401 /* Still other files referencing process */
5402 file_count--;
5403 PyList_SetItem(procObj,1,
5404 PyInt_FromLong(file_count));
5405 } else {
5406 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00005407 if (result != EOF &&
5408 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5409 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00005410 /* Possible truncation here in 16-bit environments, but
5411 * real exit codes are just the lower byte in any event.
5412 */
5413 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005414 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00005415 /* Indicate failure - this will cause the file object
5416 * to raise an I/O error and translate the last Win32
5417 * error code from errno. We do have a problem with
5418 * last errors that overlap the normal errno table,
5419 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005420 */
Fredrik Lundh20318932000-07-26 17:29:12 +00005421 if (result != EOF) {
5422 /* If the error wasn't from the fclose(), then
5423 * set errno for the file object error handling.
5424 */
5425 errno = GetLastError();
5426 }
5427 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005428 }
5429
5430 /* Free up the native handle at this point */
5431 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00005432 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005433
Mark Hammondb37a3732000-08-14 04:47:33 +00005434 /* Remove this file pointer from dictionary */
5435 PyDict_DelItem(_PyPopenProcs, fileObj);
5436
5437 if (PyDict_Size(_PyPopenProcs) == 0) {
5438 Py_DECREF(_PyPopenProcs);
5439 _PyPopenProcs = NULL;
5440 }
5441
5442 } /* if object retrieval ok */
5443
5444 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005445 } /* if _PyPopenProcs */
5446
Tim Peters736aa322000-09-01 06:51:24 +00005447#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005448 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005449#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005450 return result;
5451}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005452
5453#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005454static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005455posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005456{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005457 char *name;
5458 char *mode = "r";
5459 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00005460 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005461 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005462 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00005463 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00005464 /* Strip mode of binary or text modifiers */
5465 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5466 mode = "r";
5467 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5468 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00005469 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005470 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005471 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00005472 if (fp == NULL)
5473 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005474 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005475 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005476 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005477 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005478}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005479
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005480#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005481#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005482
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005483
Guido van Rossumb6775db1994-08-01 11:34:53 +00005484#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005485PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005486"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005487Set the current process's user id.");
5488
Barry Warsaw53699e91996-12-10 23:23:01 +00005489static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005490posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005491{
5492 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005493 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005494 return NULL;
5495 if (setuid(uid) < 0)
5496 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005497 Py_INCREF(Py_None);
5498 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005499}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005500#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005501
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005502
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005503#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005504PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005505"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005506Set the current process's effective user id.");
5507
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005508static PyObject *
5509posix_seteuid (PyObject *self, PyObject *args)
5510{
5511 int euid;
5512 if (!PyArg_ParseTuple(args, "i", &euid)) {
5513 return NULL;
5514 } else if (seteuid(euid) < 0) {
5515 return posix_error();
5516 } else {
5517 Py_INCREF(Py_None);
5518 return Py_None;
5519 }
5520}
5521#endif /* HAVE_SETEUID */
5522
5523#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005524PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005525"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005526Set the current process's effective group id.");
5527
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005528static PyObject *
5529posix_setegid (PyObject *self, PyObject *args)
5530{
5531 int egid;
5532 if (!PyArg_ParseTuple(args, "i", &egid)) {
5533 return NULL;
5534 } else if (setegid(egid) < 0) {
5535 return posix_error();
5536 } else {
5537 Py_INCREF(Py_None);
5538 return Py_None;
5539 }
5540}
5541#endif /* HAVE_SETEGID */
5542
5543#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005544PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005545"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005546Set the current process's real and effective user ids.");
5547
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005548static PyObject *
5549posix_setreuid (PyObject *self, PyObject *args)
5550{
5551 int ruid, euid;
5552 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
5553 return NULL;
5554 } else if (setreuid(ruid, euid) < 0) {
5555 return posix_error();
5556 } else {
5557 Py_INCREF(Py_None);
5558 return Py_None;
5559 }
5560}
5561#endif /* HAVE_SETREUID */
5562
5563#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005564PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005565"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005566Set the current process's real and effective group ids.");
5567
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005568static PyObject *
5569posix_setregid (PyObject *self, PyObject *args)
5570{
5571 int rgid, egid;
5572 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
5573 return NULL;
5574 } else if (setregid(rgid, egid) < 0) {
5575 return posix_error();
5576 } else {
5577 Py_INCREF(Py_None);
5578 return Py_None;
5579 }
5580}
5581#endif /* HAVE_SETREGID */
5582
Guido van Rossumb6775db1994-08-01 11:34:53 +00005583#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005584PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005585"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005586Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005587
Barry Warsaw53699e91996-12-10 23:23:01 +00005588static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005589posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005590{
5591 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005592 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005593 return NULL;
5594 if (setgid(gid) < 0)
5595 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005596 Py_INCREF(Py_None);
5597 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005598}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005599#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005600
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005601#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005602PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005603"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005604Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005605
5606static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005607posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005608{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005609 int i, len;
5610 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005611
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005612 if (!PySequence_Check(groups)) {
5613 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5614 return NULL;
5615 }
5616 len = PySequence_Size(groups);
5617 if (len > MAX_GROUPS) {
5618 PyErr_SetString(PyExc_ValueError, "too many groups");
5619 return NULL;
5620 }
5621 for(i = 0; i < len; i++) {
5622 PyObject *elem;
5623 elem = PySequence_GetItem(groups, i);
5624 if (!elem)
5625 return NULL;
5626 if (!PyInt_Check(elem)) {
Georg Brandla13c2442005-11-22 19:30:31 +00005627 if (!PyLong_Check(elem)) {
5628 PyErr_SetString(PyExc_TypeError,
5629 "groups must be integers");
5630 Py_DECREF(elem);
5631 return NULL;
5632 } else {
5633 unsigned long x = PyLong_AsUnsignedLong(elem);
5634 if (PyErr_Occurred()) {
5635 PyErr_SetString(PyExc_TypeError,
5636 "group id too big");
5637 Py_DECREF(elem);
5638 return NULL;
5639 }
5640 grouplist[i] = x;
5641 /* read back the value to see if it fitted in gid_t */
5642 if (grouplist[i] != x) {
5643 PyErr_SetString(PyExc_TypeError,
5644 "group id too big");
5645 Py_DECREF(elem);
5646 return NULL;
5647 }
5648 }
5649 } else {
5650 long x = PyInt_AsLong(elem);
5651 grouplist[i] = x;
5652 if (grouplist[i] != x) {
5653 PyErr_SetString(PyExc_TypeError,
5654 "group id too big");
5655 Py_DECREF(elem);
5656 return NULL;
5657 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005658 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005659 Py_DECREF(elem);
5660 }
5661
5662 if (setgroups(len, grouplist) < 0)
5663 return posix_error();
5664 Py_INCREF(Py_None);
5665 return Py_None;
5666}
5667#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005668
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005669#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00005670static PyObject *
Christian Heimesd491d712008-02-01 18:49:26 +00005671wait_helper(pid_t pid, int status, struct rusage *ru)
Neal Norwitz05a45592006-03-20 06:30:08 +00005672{
5673 PyObject *result;
5674 static PyObject *struct_rusage;
5675
5676 if (pid == -1)
5677 return posix_error();
5678
5679 if (struct_rusage == NULL) {
Christian Heimes000a0742008-01-03 22:16:32 +00005680 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Neal Norwitz05a45592006-03-20 06:30:08 +00005681 if (m == NULL)
5682 return NULL;
5683 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5684 Py_DECREF(m);
5685 if (struct_rusage == NULL)
5686 return NULL;
5687 }
5688
5689 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5690 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5691 if (!result)
5692 return NULL;
5693
5694#ifndef doubletime
5695#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5696#endif
5697
5698 PyStructSequence_SET_ITEM(result, 0,
5699 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5700 PyStructSequence_SET_ITEM(result, 1,
5701 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5702#define SET_INT(result, index, value)\
5703 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5704 SET_INT(result, 2, ru->ru_maxrss);
5705 SET_INT(result, 3, ru->ru_ixrss);
5706 SET_INT(result, 4, ru->ru_idrss);
5707 SET_INT(result, 5, ru->ru_isrss);
5708 SET_INT(result, 6, ru->ru_minflt);
5709 SET_INT(result, 7, ru->ru_majflt);
5710 SET_INT(result, 8, ru->ru_nswap);
5711 SET_INT(result, 9, ru->ru_inblock);
5712 SET_INT(result, 10, ru->ru_oublock);
5713 SET_INT(result, 11, ru->ru_msgsnd);
5714 SET_INT(result, 12, ru->ru_msgrcv);
5715 SET_INT(result, 13, ru->ru_nsignals);
5716 SET_INT(result, 14, ru->ru_nvcsw);
5717 SET_INT(result, 15, ru->ru_nivcsw);
5718#undef SET_INT
5719
5720 if (PyErr_Occurred()) {
5721 Py_DECREF(result);
5722 return NULL;
5723 }
5724
Neal Norwitz9b00a562006-03-20 08:47:12 +00005725 return Py_BuildValue("iiN", pid, status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00005726}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005727#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00005728
5729#ifdef HAVE_WAIT3
5730PyDoc_STRVAR(posix_wait3__doc__,
5731"wait3(options) -> (pid, status, rusage)\n\n\
5732Wait for completion of a child process.");
5733
5734static PyObject *
5735posix_wait3(PyObject *self, PyObject *args)
5736{
Christian Heimesd491d712008-02-01 18:49:26 +00005737 pid_t pid;
5738 int options;
Neal Norwitz05a45592006-03-20 06:30:08 +00005739 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005740 WAIT_TYPE status;
5741 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005742
5743 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5744 return NULL;
5745
5746 Py_BEGIN_ALLOW_THREADS
5747 pid = wait3(&status, options, &ru);
5748 Py_END_ALLOW_THREADS
5749
Neal Norwitzd5a37542006-03-20 06:48:34 +00005750 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005751}
5752#endif /* HAVE_WAIT3 */
5753
5754#ifdef HAVE_WAIT4
5755PyDoc_STRVAR(posix_wait4__doc__,
5756"wait4(pid, options) -> (pid, status, rusage)\n\n\
5757Wait for completion of a given child process.");
5758
5759static PyObject *
5760posix_wait4(PyObject *self, PyObject *args)
5761{
Christian Heimesd491d712008-02-01 18:49:26 +00005762 pid_t pid;
5763 int options;
Neal Norwitz05a45592006-03-20 06:30:08 +00005764 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005765 WAIT_TYPE status;
5766 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005767
5768 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
5769 return NULL;
5770
5771 Py_BEGIN_ALLOW_THREADS
5772 pid = wait4(pid, &status, options, &ru);
5773 Py_END_ALLOW_THREADS
5774
Neal Norwitzd5a37542006-03-20 06:48:34 +00005775 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005776}
5777#endif /* HAVE_WAIT4 */
5778
Guido van Rossumb6775db1994-08-01 11:34:53 +00005779#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005780PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005781"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005782Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005783
Barry Warsaw53699e91996-12-10 23:23:01 +00005784static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005785posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005786{
Christian Heimesd491d712008-02-01 18:49:26 +00005787 pid_t pid;
5788 int options;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005789 WAIT_TYPE status;
5790 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005791
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005792 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00005793 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005794 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00005795 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00005796 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00005797 if (pid == -1)
5798 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005799
5800 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005801}
5802
Tim Petersab034fa2002-02-01 11:27:43 +00005803#elif defined(HAVE_CWAIT)
5804
5805/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005806PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005807"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005808"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005809
5810static PyObject *
5811posix_waitpid(PyObject *self, PyObject *args)
5812{
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005813 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005814 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005815
5816 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5817 return NULL;
5818 Py_BEGIN_ALLOW_THREADS
5819 pid = _cwait(&status, pid, options);
5820 Py_END_ALLOW_THREADS
5821 if (pid == -1)
5822 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005823
5824 /* shift the status left a byte so this is more like the POSIX waitpid */
5825 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005826}
5827#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005828
Guido van Rossumad0ee831995-03-01 10:34:45 +00005829#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005830PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005831"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005832Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005833
Barry Warsaw53699e91996-12-10 23:23:01 +00005834static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005835posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005836{
Christian Heimesd491d712008-02-01 18:49:26 +00005837 pid_t pid;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005838 WAIT_TYPE status;
5839 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005840
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005841 Py_BEGIN_ALLOW_THREADS
5842 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00005843 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00005844 if (pid == -1)
5845 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005846
5847 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005848}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005849#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005850
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005851
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005852PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005853"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005854Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005855
Barry Warsaw53699e91996-12-10 23:23:01 +00005856static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005857posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005858{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005859#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005860 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005861#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005862#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00005863 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005864#else
5865 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5866#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005867#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005868}
5869
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005870
Guido van Rossumb6775db1994-08-01 11:34:53 +00005871#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005872PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005873"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005874Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005875
Barry Warsaw53699e91996-12-10 23:23:01 +00005876static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005877posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005878{
Ronald Oussoren10168f22006-10-22 10:45:18 +00005879 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00005880 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005881 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005882 int n;
Ronald Oussoren10168f22006-10-22 10:45:18 +00005883#ifdef Py_USING_UNICODE
5884 int arg_is_unicode = 0;
5885#endif
5886
5887 if (!PyArg_ParseTuple(args, "et:readlink",
5888 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005889 return NULL;
Ronald Oussoren10168f22006-10-22 10:45:18 +00005890#ifdef Py_USING_UNICODE
5891 v = PySequence_GetItem(args, 0);
Neal Norwitz91a57212007-08-12 17:11:13 +00005892 if (v == NULL) {
5893 PyMem_Free(path);
5894 return NULL;
5895 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00005896
5897 if (PyUnicode_Check(v)) {
5898 arg_is_unicode = 1;
5899 }
5900 Py_DECREF(v);
5901#endif
5902
Barry Warsaw53699e91996-12-10 23:23:01 +00005903 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00005904 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00005905 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005906 if (n < 0)
Neal Norwitz91a57212007-08-12 17:11:13 +00005907 return posix_error_with_allocated_filename(path);
Ronald Oussoren10168f22006-10-22 10:45:18 +00005908
Neal Norwitz91a57212007-08-12 17:11:13 +00005909 PyMem_Free(path);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005910 v = PyString_FromStringAndSize(buf, n);
Ronald Oussoren10168f22006-10-22 10:45:18 +00005911#ifdef Py_USING_UNICODE
5912 if (arg_is_unicode) {
5913 PyObject *w;
5914
5915 w = PyUnicode_FromEncodedObject(v,
5916 Py_FileSystemDefaultEncoding,
5917 "strict");
5918 if (w != NULL) {
5919 Py_DECREF(v);
5920 v = w;
5921 }
5922 else {
5923 /* fall back to the original byte string, as
5924 discussed in patch #683592 */
5925 PyErr_Clear();
5926 }
5927 }
5928#endif
5929 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005930}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005931#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005932
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005933
Guido van Rossumb6775db1994-08-01 11:34:53 +00005934#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005935PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005936"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005937Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005938
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005939static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005940posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005941{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00005942 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005943}
5944#endif /* HAVE_SYMLINK */
5945
5946
5947#ifdef HAVE_TIMES
5948#ifndef HZ
5949#define HZ 60 /* Universal constant :-) */
5950#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005951
Guido van Rossumd48f2521997-12-05 22:19:34 +00005952#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5953static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005954system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005955{
5956 ULONG value = 0;
5957
5958 Py_BEGIN_ALLOW_THREADS
5959 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5960 Py_END_ALLOW_THREADS
5961
5962 return value;
5963}
5964
5965static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005966posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005967{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005968 /* Currently Only Uptime is Provided -- Others Later */
5969 return Py_BuildValue("ddddd",
5970 (double)0 /* t.tms_utime / HZ */,
5971 (double)0 /* t.tms_stime / HZ */,
5972 (double)0 /* t.tms_cutime / HZ */,
5973 (double)0 /* t.tms_cstime / HZ */,
5974 (double)system_uptime() / 1000);
5975}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005976#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005977static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005978posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005979{
5980 struct tms t;
5981 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005982 errno = 0;
5983 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005984 if (c == (clock_t) -1)
5985 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005986 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005987 (double)t.tms_utime / HZ,
5988 (double)t.tms_stime / HZ,
5989 (double)t.tms_cutime / HZ,
5990 (double)t.tms_cstime / HZ,
5991 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005992}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005993#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005994#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005995
5996
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005997#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005998#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005999static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006000posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006001{
6002 FILETIME create, exit, kernel, user;
6003 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006004 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00006005 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6006 /* The fields of a FILETIME structure are the hi and lo part
6007 of a 64-bit value expressed in 100 nanosecond units.
6008 1e7 is one second in such units; 1e-7 the inverse.
6009 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6010 */
Barry Warsaw53699e91996-12-10 23:23:01 +00006011 return Py_BuildValue(
6012 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00006013 (double)(user.dwHighDateTime*429.4967296 +
6014 user.dwLowDateTime*1e-7),
Georg Brandl0a40ffb2008-02-13 07:20:22 +00006015 (double)(kernel.dwHighDateTime*429.4967296 +
6016 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00006017 (double)0,
6018 (double)0,
6019 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006020}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006021#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006022
6023#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006024PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006025"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006026Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006027#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006028
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006029
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006030#ifdef HAVE_GETSID
6031PyDoc_STRVAR(posix_getsid__doc__,
6032"getsid(pid) -> sid\n\n\
6033Call the system call getsid().");
6034
6035static PyObject *
6036posix_getsid(PyObject *self, PyObject *args)
6037{
Christian Heimesd491d712008-02-01 18:49:26 +00006038 pid_t pid;
6039 int sid;
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006040 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
6041 return NULL;
6042 sid = getsid(pid);
6043 if (sid < 0)
6044 return posix_error();
6045 return PyInt_FromLong((long)sid);
6046}
6047#endif /* HAVE_GETSID */
6048
6049
Guido van Rossumb6775db1994-08-01 11:34:53 +00006050#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006051PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006052"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006053Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006054
Barry Warsaw53699e91996-12-10 23:23:01 +00006055static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006056posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006057{
Guido van Rossum687dd131993-05-17 08:34:16 +00006058 if (setsid() < 0)
6059 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006060 Py_INCREF(Py_None);
6061 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006062}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006063#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006064
Guido van Rossumb6775db1994-08-01 11:34:53 +00006065#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006066PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006067"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006068Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006069
Barry Warsaw53699e91996-12-10 23:23:01 +00006070static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006071posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006072{
Christian Heimesd491d712008-02-01 18:49:26 +00006073 pid_t pid;
6074 int pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006075 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00006076 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006077 if (setpgid(pid, pgrp) < 0)
6078 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006079 Py_INCREF(Py_None);
6080 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006081}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006082#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006083
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006084
Guido van Rossumb6775db1994-08-01 11:34:53 +00006085#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006086PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006087"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006088Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006089
Barry Warsaw53699e91996-12-10 23:23:01 +00006090static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006091posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006092{
Christian Heimese6a80742008-02-03 19:51:13 +00006093 int fd;
6094 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006095 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00006096 return NULL;
6097 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006098 if (pgid < 0)
6099 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006100 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006101}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006102#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006103
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006104
Guido van Rossumb6775db1994-08-01 11:34:53 +00006105#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006106PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006107"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006108Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006109
Barry Warsaw53699e91996-12-10 23:23:01 +00006110static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006111posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006112{
6113 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006114 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00006115 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006116 if (tcsetpgrp(fd, pgid) < 0)
6117 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00006118 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00006119 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006120}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006121#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006122
Guido van Rossum687dd131993-05-17 08:34:16 +00006123/* Functions acting on file descriptors */
6124
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006125PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006126"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006127Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006128
Barry Warsaw53699e91996-12-10 23:23:01 +00006129static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006130posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006131{
Mark Hammondef8b6542001-05-13 08:04:26 +00006132 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006133 int flag;
6134 int mode = 0777;
6135 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006136
6137#ifdef MS_WINDOWS
6138 if (unicode_file_names()) {
6139 PyUnicodeObject *po;
6140 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6141 Py_BEGIN_ALLOW_THREADS
6142 /* PyUnicode_AS_UNICODE OK without thread
6143 lock as it is a simple dereference. */
6144 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6145 Py_END_ALLOW_THREADS
6146 if (fd < 0)
6147 return posix_error();
6148 return PyInt_FromLong((long)fd);
6149 }
6150 /* Drop the argument parsing error as narrow strings
6151 are also valid. */
6152 PyErr_Clear();
6153 }
6154#endif
6155
Tim Peters5aa91602002-01-30 05:46:57 +00006156 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00006157 Py_FileSystemDefaultEncoding, &file,
6158 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00006159 return NULL;
6160
Barry Warsaw53699e91996-12-10 23:23:01 +00006161 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006162 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00006163 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006164 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00006165 return posix_error_with_allocated_filename(file);
6166 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00006167 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006168}
6169
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006170
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006171PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006172"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006173Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006174
Barry Warsaw53699e91996-12-10 23:23:01 +00006175static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006176posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006177{
6178 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006179 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006180 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006181 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006182 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006183 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006184 if (res < 0)
6185 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006186 Py_INCREF(Py_None);
6187 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006188}
6189
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006190
Georg Brandl309501a2008-01-19 20:22:13 +00006191PyDoc_STRVAR(posix_closerange__doc__,
6192"closerange(fd_low, fd_high)\n\n\
6193Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6194
6195static PyObject *
6196posix_closerange(PyObject *self, PyObject *args)
6197{
6198 int fd_from, fd_to, i;
6199 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6200 return NULL;
6201 Py_BEGIN_ALLOW_THREADS
6202 for (i = fd_from; i < fd_to; i++)
6203 close(i);
6204 Py_END_ALLOW_THREADS
6205 Py_RETURN_NONE;
6206}
6207
6208
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006209PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006210"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006211Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006212
Barry Warsaw53699e91996-12-10 23:23:01 +00006213static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006214posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006215{
6216 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006217 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006218 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006219 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006220 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006221 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006222 if (fd < 0)
6223 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006224 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006225}
6226
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006227
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006228PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006229"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006230Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006231
Barry Warsaw53699e91996-12-10 23:23:01 +00006232static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006233posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006234{
6235 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006236 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00006237 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006238 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006239 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00006240 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006241 if (res < 0)
6242 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006243 Py_INCREF(Py_None);
6244 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006245}
6246
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006247
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006248PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006249"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006250Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006251
Barry Warsaw53699e91996-12-10 23:23:01 +00006252static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006253posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006254{
6255 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006256#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006257 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006258#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00006259 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006260#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006261 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006262 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00006263 return NULL;
6264#ifdef SEEK_SET
6265 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6266 switch (how) {
6267 case 0: how = SEEK_SET; break;
6268 case 1: how = SEEK_CUR; break;
6269 case 2: how = SEEK_END; break;
6270 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006271#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006272
6273#if !defined(HAVE_LARGEFILE_SUPPORT)
6274 pos = PyInt_AsLong(posobj);
6275#else
6276 pos = PyLong_Check(posobj) ?
6277 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
6278#endif
6279 if (PyErr_Occurred())
6280 return NULL;
6281
Barry Warsaw53699e91996-12-10 23:23:01 +00006282 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006283#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00006284 res = _lseeki64(fd, pos, how);
6285#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006286 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006287#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006288 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006289 if (res < 0)
6290 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006291
6292#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00006293 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006294#else
6295 return PyLong_FromLongLong(res);
6296#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006297}
6298
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006299
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006300PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006301"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006302Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006303
Barry Warsaw53699e91996-12-10 23:23:01 +00006304static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006305posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006306{
Guido van Rossum8bac5461996-06-11 18:38:48 +00006307 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00006308 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006309 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006310 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00006311 if (size < 0) {
6312 errno = EINVAL;
6313 return posix_error();
6314 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006315 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006316 if (buffer == NULL)
6317 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006318 Py_BEGIN_ALLOW_THREADS
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006319 n = read(fd, PyString_AsString(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00006320 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00006321 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006322 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00006323 return posix_error();
6324 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00006325 if (n != size)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006326 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00006327 return buffer;
6328}
6329
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006330
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006331PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006332"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006333Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006334
Barry Warsaw53699e91996-12-10 23:23:01 +00006335static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006336posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006337{
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00006338 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006339 int fd;
6340 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006341
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00006342 if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00006343 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006344 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00006345 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00006346 Py_END_ALLOW_THREADS
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00006347 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00006348 if (size < 0)
6349 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006350 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006351}
6352
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006353
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006354PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006355"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006356Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006357
Barry Warsaw53699e91996-12-10 23:23:01 +00006358static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006359posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006360{
6361 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00006362 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00006363 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006364 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006365 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006366#ifdef __VMS
6367 /* on OpenVMS we must ensure that all bytes are written to the file */
6368 fsync(fd);
6369#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006370 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00006371 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00006372 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00006373 if (res != 0) {
6374#ifdef MS_WINDOWS
6375 return win32_error("fstat", NULL);
6376#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006377 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006378#endif
6379 }
Tim Peters5aa91602002-01-30 05:46:57 +00006380
Martin v. Löwis14694662006-02-03 12:54:16 +00006381 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006382}
6383
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006384
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006385PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006386"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006387Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006388
Barry Warsaw53699e91996-12-10 23:23:01 +00006389static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006390posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006391{
Guido van Rossum687dd131993-05-17 08:34:16 +00006392 int fd;
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006393 char *orgmode = "r";
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006394 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00006395 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00006396 PyObject *f;
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006397 char *mode;
6398 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00006399 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006400
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006401 /* Sanitize mode. See fileobject.c */
6402 mode = PyMem_MALLOC(strlen(orgmode)+3);
6403 if (!mode) {
6404 PyErr_NoMemory();
6405 return NULL;
6406 }
6407 strcpy(mode, orgmode);
6408 if (_PyFile_SanitizeMode(mode)) {
6409 PyMem_FREE(mode);
Thomas Heller1f043e22002-11-07 16:00:59 +00006410 return NULL;
6411 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006412 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006413#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Georg Brandl54a188a2006-03-31 20:00:11 +00006414 if (mode[0] == 'a') {
6415 /* try to make sure the O_APPEND flag is set */
6416 int flags;
6417 flags = fcntl(fd, F_GETFL);
6418 if (flags != -1)
6419 fcntl(fd, F_SETFL, flags | O_APPEND);
6420 fp = fdopen(fd, mode);
Thomas Wouters2a9a6b02006-03-31 22:38:19 +00006421 if (fp == NULL && flags != -1)
Georg Brandl54a188a2006-03-31 20:00:11 +00006422 /* restore old mode if fdopen failed */
6423 fcntl(fd, F_SETFL, flags);
6424 } else {
6425 fp = fdopen(fd, mode);
6426 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006427#else
6428 fp = fdopen(fd, mode);
6429#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006430 Py_END_ALLOW_THREADS
Neal Norwitzd83eb312007-05-02 04:47:55 +00006431 PyMem_FREE(mode);
Guido van Rossum687dd131993-05-17 08:34:16 +00006432 if (fp == NULL)
6433 return posix_error();
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006434 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006435 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00006436 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006437 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006438}
6439
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006440PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006441"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006442Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006443connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006444
6445static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006446posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006447{
6448 int fd;
6449 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6450 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006451 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006452}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006453
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006454#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006455PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006456"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006457Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006458
Barry Warsaw53699e91996-12-10 23:23:01 +00006459static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006460posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006461{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006462#if defined(PYOS_OS2)
6463 HFILE read, write;
6464 APIRET rc;
6465
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006466 Py_BEGIN_ALLOW_THREADS
6467 rc = DosCreatePipe( &read, &write, 4096);
6468 Py_END_ALLOW_THREADS
6469 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006470 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006471
6472 return Py_BuildValue("(ii)", read, write);
6473#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006474#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00006475 int fds[2];
6476 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00006477 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006478 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00006479 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006480 if (res != 0)
6481 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006482 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006483#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00006484 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006485 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00006486 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00006487 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006488 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00006489 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00006490 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006491 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00006492 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6493 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006494 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006495#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006496#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006497}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006498#endif /* HAVE_PIPE */
6499
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006500
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006501#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006502PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006503"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006504Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006505
Barry Warsaw53699e91996-12-10 23:23:01 +00006506static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006507posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006508{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006509 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006510 int mode = 0666;
6511 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006512 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006513 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006514 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006515 res = mkfifo(filename, mode);
6516 Py_END_ALLOW_THREADS
6517 if (res < 0)
6518 return posix_error();
6519 Py_INCREF(Py_None);
6520 return Py_None;
6521}
6522#endif
6523
6524
Neal Norwitz11690112002-07-30 01:08:28 +00006525#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006526PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006527"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006528Create a filesystem node (file, device special file or named pipe)\n\
6529named filename. mode specifies both the permissions to use and the\n\
6530type of node to be created, being combined (bitwise OR) with one of\n\
6531S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006532device defines the newly created device special file (probably using\n\
6533os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006534
6535
6536static PyObject *
6537posix_mknod(PyObject *self, PyObject *args)
6538{
6539 char *filename;
6540 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006541 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006542 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00006543 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006544 return NULL;
6545 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006546 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00006547 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006548 if (res < 0)
6549 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006550 Py_INCREF(Py_None);
6551 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006552}
6553#endif
6554
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006555#ifdef HAVE_DEVICE_MACROS
6556PyDoc_STRVAR(posix_major__doc__,
6557"major(device) -> major number\n\
6558Extracts a device major number from a raw device number.");
6559
6560static PyObject *
6561posix_major(PyObject *self, PyObject *args)
6562{
6563 int device;
6564 if (!PyArg_ParseTuple(args, "i:major", &device))
6565 return NULL;
6566 return PyInt_FromLong((long)major(device));
6567}
6568
6569PyDoc_STRVAR(posix_minor__doc__,
6570"minor(device) -> minor number\n\
6571Extracts a device minor number from a raw device number.");
6572
6573static PyObject *
6574posix_minor(PyObject *self, PyObject *args)
6575{
6576 int device;
6577 if (!PyArg_ParseTuple(args, "i:minor", &device))
6578 return NULL;
6579 return PyInt_FromLong((long)minor(device));
6580}
6581
6582PyDoc_STRVAR(posix_makedev__doc__,
6583"makedev(major, minor) -> device number\n\
6584Composes a raw device number from the major and minor device numbers.");
6585
6586static PyObject *
6587posix_makedev(PyObject *self, PyObject *args)
6588{
6589 int major, minor;
6590 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6591 return NULL;
6592 return PyInt_FromLong((long)makedev(major, minor));
6593}
6594#endif /* device macros */
6595
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006596
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006597#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006598PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006599"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006600Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006601
Barry Warsaw53699e91996-12-10 23:23:01 +00006602static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006603posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006604{
6605 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006606 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006607 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006608 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006609
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006610 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006611 return NULL;
6612
6613#if !defined(HAVE_LARGEFILE_SUPPORT)
6614 length = PyInt_AsLong(lenobj);
6615#else
6616 length = PyLong_Check(lenobj) ?
6617 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6618#endif
6619 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006620 return NULL;
6621
Barry Warsaw53699e91996-12-10 23:23:01 +00006622 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006623 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00006624 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006625 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006626 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006627 return NULL;
6628 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006629 Py_INCREF(Py_None);
6630 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006631}
6632#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006633
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006634#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006635PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006636"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006637Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006638
Fred Drake762e2061999-08-26 17:23:54 +00006639/* Save putenv() parameters as values here, so we can collect them when they
6640 * get re-set with another call for the same key. */
6641static PyObject *posix_putenv_garbage;
6642
Tim Peters5aa91602002-01-30 05:46:57 +00006643static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006644posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006645{
6646 char *s1, *s2;
Anthony Baxter64182fe2006-04-11 12:14:09 +00006647 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00006648 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00006649 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006650
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006651 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006652 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006653
6654#if defined(PYOS_OS2)
6655 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6656 APIRET rc;
6657
Guido van Rossumd48f2521997-12-05 22:19:34 +00006658 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6659 if (rc != NO_ERROR)
6660 return os2_error(rc);
6661
6662 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6663 APIRET rc;
6664
Guido van Rossumd48f2521997-12-05 22:19:34 +00006665 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6666 if (rc != NO_ERROR)
6667 return os2_error(rc);
6668 } else {
6669#endif
6670
Fred Drake762e2061999-08-26 17:23:54 +00006671 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00006672 len = strlen(s1) + strlen(s2) + 2;
6673 /* len includes space for a trailing \0; the size arg to
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006674 PyString_FromStringAndSize does not count that */
6675 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00006676 if (newstr == NULL)
6677 return PyErr_NoMemory();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006678 newenv = PyString_AS_STRING(newstr);
Anthony Baxter64182fe2006-04-11 12:14:09 +00006679 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6680 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00006681 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006682 posix_error();
6683 return NULL;
6684 }
Fred Drake762e2061999-08-26 17:23:54 +00006685 /* Install the first arg and newstr in posix_putenv_garbage;
6686 * this will cause previous value to be collected. This has to
6687 * happen after the real putenv() call because the old value
6688 * was still accessible until then. */
6689 if (PyDict_SetItem(posix_putenv_garbage,
6690 PyTuple_GET_ITEM(args, 0), newstr)) {
6691 /* really not much we can do; just leak */
6692 PyErr_Clear();
6693 }
6694 else {
6695 Py_DECREF(newstr);
6696 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006697
6698#if defined(PYOS_OS2)
6699 }
6700#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006701 Py_INCREF(Py_None);
6702 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006703}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006704#endif /* putenv */
6705
Guido van Rossumc524d952001-10-19 01:31:59 +00006706#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006707PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006708"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006709Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006710
6711static PyObject *
6712posix_unsetenv(PyObject *self, PyObject *args)
6713{
6714 char *s1;
6715
6716 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6717 return NULL;
6718
6719 unsetenv(s1);
6720
6721 /* Remove the key from posix_putenv_garbage;
6722 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00006723 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00006724 * old value was still accessible until then.
6725 */
6726 if (PyDict_DelItem(posix_putenv_garbage,
6727 PyTuple_GET_ITEM(args, 0))) {
6728 /* really not much we can do; just leak */
6729 PyErr_Clear();
6730 }
6731
6732 Py_INCREF(Py_None);
6733 return Py_None;
6734}
6735#endif /* unsetenv */
6736
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006737PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006738"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006739Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006740
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006741static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006742posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006743{
6744 int code;
6745 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006746 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00006747 return NULL;
6748 message = strerror(code);
6749 if (message == NULL) {
6750 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00006751 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006752 return NULL;
6753 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006754 return PyString_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006755}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006756
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006757
Guido van Rossumc9641791998-08-04 15:26:23 +00006758#ifdef HAVE_SYS_WAIT_H
6759
Fred Drake106c1a02002-04-23 15:58:02 +00006760#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006761PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006762"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006763Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006764
6765static PyObject *
6766posix_WCOREDUMP(PyObject *self, PyObject *args)
6767{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006768 WAIT_TYPE status;
6769 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006770
Neal Norwitzd5a37542006-03-20 06:48:34 +00006771 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006772 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006773
6774 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006775}
6776#endif /* WCOREDUMP */
6777
6778#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006779PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006780"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006781Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006782job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006783
6784static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006785posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006786{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006787 WAIT_TYPE status;
6788 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006789
Neal Norwitzd5a37542006-03-20 06:48:34 +00006790 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006791 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006792
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006793 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006794}
6795#endif /* WIFCONTINUED */
6796
Guido van Rossumc9641791998-08-04 15:26:23 +00006797#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006798PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006799"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006800Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006801
6802static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006803posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006804{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006805 WAIT_TYPE status;
6806 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006807
Neal Norwitzd5a37542006-03-20 06:48:34 +00006808 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006809 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006810
Fred Drake106c1a02002-04-23 15:58:02 +00006811 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006812}
6813#endif /* WIFSTOPPED */
6814
6815#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006816PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006817"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006818Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006819
6820static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006821posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006822{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006823 WAIT_TYPE status;
6824 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006825
Neal Norwitzd5a37542006-03-20 06:48:34 +00006826 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006827 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006828
Fred Drake106c1a02002-04-23 15:58:02 +00006829 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006830}
6831#endif /* WIFSIGNALED */
6832
6833#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006834PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006835"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006836Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006837system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006838
6839static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006840posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006841{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006842 WAIT_TYPE status;
6843 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006844
Neal Norwitzd5a37542006-03-20 06:48:34 +00006845 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006846 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006847
Fred Drake106c1a02002-04-23 15:58:02 +00006848 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006849}
6850#endif /* WIFEXITED */
6851
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006852#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006853PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006854"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006855Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006856
6857static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006858posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006859{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006860 WAIT_TYPE status;
6861 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006862
Neal Norwitzd5a37542006-03-20 06:48:34 +00006863 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006864 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006865
Guido van Rossumc9641791998-08-04 15:26:23 +00006866 return Py_BuildValue("i", WEXITSTATUS(status));
6867}
6868#endif /* WEXITSTATUS */
6869
6870#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006871PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006872"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006873Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006874value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006875
6876static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006877posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006878{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006879 WAIT_TYPE status;
6880 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006881
Neal Norwitzd5a37542006-03-20 06:48:34 +00006882 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006883 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006884
Guido van Rossumc9641791998-08-04 15:26:23 +00006885 return Py_BuildValue("i", WTERMSIG(status));
6886}
6887#endif /* WTERMSIG */
6888
6889#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006890PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006891"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006892Return the signal that stopped the process that provided\n\
6893the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006894
6895static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006896posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006897{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006898 WAIT_TYPE status;
6899 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006900
Neal Norwitzd5a37542006-03-20 06:48:34 +00006901 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006902 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006903
Guido van Rossumc9641791998-08-04 15:26:23 +00006904 return Py_BuildValue("i", WSTOPSIG(status));
6905}
6906#endif /* WSTOPSIG */
6907
6908#endif /* HAVE_SYS_WAIT_H */
6909
6910
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006911#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006912#ifdef _SCO_DS
6913/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6914 needed definitions in sys/statvfs.h */
6915#define _SVID3
6916#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006917#include <sys/statvfs.h>
6918
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006919static PyObject*
6920_pystatvfs_fromstructstatvfs(struct statvfs st) {
6921 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6922 if (v == NULL)
6923 return NULL;
6924
6925#if !defined(HAVE_LARGEFILE_SUPPORT)
6926 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6927 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6928 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6929 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6930 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6931 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6932 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6933 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6934 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6935 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6936#else
6937 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6938 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006939 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006940 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006941 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006942 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006943 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006944 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006945 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006946 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006947 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006948 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006949 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006950 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006951 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6952 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6953#endif
6954
6955 return v;
6956}
6957
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006958PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006959"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006960Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006961
6962static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006963posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006964{
6965 int fd, res;
6966 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006967
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006968 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006969 return NULL;
6970 Py_BEGIN_ALLOW_THREADS
6971 res = fstatvfs(fd, &st);
6972 Py_END_ALLOW_THREADS
6973 if (res != 0)
6974 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006975
6976 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006977}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006978#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006979
6980
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006981#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006982#include <sys/statvfs.h>
6983
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006984PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006985"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006986Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006987
6988static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006989posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006990{
6991 char *path;
6992 int res;
6993 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006994 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006995 return NULL;
6996 Py_BEGIN_ALLOW_THREADS
6997 res = statvfs(path, &st);
6998 Py_END_ALLOW_THREADS
6999 if (res != 0)
7000 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007001
7002 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007003}
7004#endif /* HAVE_STATVFS */
7005
7006
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007007#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007008PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007009"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007010Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00007011The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007012or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007013
7014static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007015posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007016{
7017 PyObject *result = NULL;
7018 char *dir = NULL;
7019 char *pfx = NULL;
7020 char *name;
7021
7022 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
7023 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007024
7025 if (PyErr_Warn(PyExc_RuntimeWarning,
7026 "tempnam is a potential security risk to your program") < 0)
7027 return NULL;
7028
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007029#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00007030 name = _tempnam(dir, pfx);
7031#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007032 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00007033#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007034 if (name == NULL)
7035 return PyErr_NoMemory();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007036 result = PyString_FromString(name);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007037 free(name);
7038 return result;
7039}
Guido van Rossumd371ff11999-01-25 16:12:23 +00007040#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007041
7042
7043#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007044PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007045"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007046Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007047
7048static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007049posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007050{
7051 FILE *fp;
7052
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007053 fp = tmpfile();
7054 if (fp == NULL)
7055 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00007056 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007057}
7058#endif
7059
7060
7061#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007062PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007063"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007064Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007065
7066static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007067posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007068{
7069 char buffer[L_tmpnam];
7070 char *name;
7071
Skip Montanaro95618b52001-08-18 18:52:10 +00007072 if (PyErr_Warn(PyExc_RuntimeWarning,
7073 "tmpnam is a potential security risk to your program") < 0)
7074 return NULL;
7075
Greg Wardb48bc172000-03-01 21:51:56 +00007076#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007077 name = tmpnam_r(buffer);
7078#else
7079 name = tmpnam(buffer);
7080#endif
7081 if (name == NULL) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00007082 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00007083#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007084 "unexpected NULL from tmpnam_r"
7085#else
7086 "unexpected NULL from tmpnam"
7087#endif
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00007088 );
7089 PyErr_SetObject(PyExc_OSError, err);
7090 Py_XDECREF(err);
7091 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007092 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007093 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007094}
7095#endif
7096
7097
Fred Drakec9680921999-12-13 16:37:25 +00007098/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7099 * It maps strings representing configuration variable names to
7100 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007101 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007102 * rarely-used constants. There are three separate tables that use
7103 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007104 *
7105 * This code is always included, even if none of the interfaces that
7106 * need it are included. The #if hackery needed to avoid it would be
7107 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007108 */
7109struct constdef {
7110 char *name;
7111 long value;
7112};
7113
Fred Drake12c6e2d1999-12-14 21:25:03 +00007114static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007115conv_confname(PyObject *arg, int *valuep, struct constdef *table,
7116 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007117{
7118 if (PyInt_Check(arg)) {
7119 *valuep = PyInt_AS_LONG(arg);
7120 return 1;
7121 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007122 if (PyString_Check(arg)) {
Fred Drake12c6e2d1999-12-14 21:25:03 +00007123 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00007124 size_t lo = 0;
7125 size_t mid;
7126 size_t hi = tablesize;
7127 int cmp;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007128 char *confname = PyString_AS_STRING(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007129 while (lo < hi) {
7130 mid = (lo + hi) / 2;
7131 cmp = strcmp(confname, table[mid].name);
7132 if (cmp < 0)
7133 hi = mid;
7134 else if (cmp > 0)
7135 lo = mid + 1;
7136 else {
7137 *valuep = table[mid].value;
7138 return 1;
7139 }
7140 }
7141 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
7142 }
7143 else
7144 PyErr_SetString(PyExc_TypeError,
7145 "configuration names must be strings or integers");
7146 return 0;
7147}
7148
7149
7150#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7151static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007152#ifdef _PC_ABI_AIO_XFER_MAX
7153 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
7154#endif
7155#ifdef _PC_ABI_ASYNC_IO
7156 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
7157#endif
Fred Drakec9680921999-12-13 16:37:25 +00007158#ifdef _PC_ASYNC_IO
7159 {"PC_ASYNC_IO", _PC_ASYNC_IO},
7160#endif
7161#ifdef _PC_CHOWN_RESTRICTED
7162 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
7163#endif
7164#ifdef _PC_FILESIZEBITS
7165 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
7166#endif
7167#ifdef _PC_LAST
7168 {"PC_LAST", _PC_LAST},
7169#endif
7170#ifdef _PC_LINK_MAX
7171 {"PC_LINK_MAX", _PC_LINK_MAX},
7172#endif
7173#ifdef _PC_MAX_CANON
7174 {"PC_MAX_CANON", _PC_MAX_CANON},
7175#endif
7176#ifdef _PC_MAX_INPUT
7177 {"PC_MAX_INPUT", _PC_MAX_INPUT},
7178#endif
7179#ifdef _PC_NAME_MAX
7180 {"PC_NAME_MAX", _PC_NAME_MAX},
7181#endif
7182#ifdef _PC_NO_TRUNC
7183 {"PC_NO_TRUNC", _PC_NO_TRUNC},
7184#endif
7185#ifdef _PC_PATH_MAX
7186 {"PC_PATH_MAX", _PC_PATH_MAX},
7187#endif
7188#ifdef _PC_PIPE_BUF
7189 {"PC_PIPE_BUF", _PC_PIPE_BUF},
7190#endif
7191#ifdef _PC_PRIO_IO
7192 {"PC_PRIO_IO", _PC_PRIO_IO},
7193#endif
7194#ifdef _PC_SOCK_MAXBUF
7195 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
7196#endif
7197#ifdef _PC_SYNC_IO
7198 {"PC_SYNC_IO", _PC_SYNC_IO},
7199#endif
7200#ifdef _PC_VDISABLE
7201 {"PC_VDISABLE", _PC_VDISABLE},
7202#endif
7203};
7204
Fred Drakec9680921999-12-13 16:37:25 +00007205static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007206conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007207{
7208 return conv_confname(arg, valuep, posix_constants_pathconf,
7209 sizeof(posix_constants_pathconf)
7210 / sizeof(struct constdef));
7211}
7212#endif
7213
7214#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007215PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007216"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007217Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007218If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007219
7220static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007221posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007222{
7223 PyObject *result = NULL;
7224 int name, fd;
7225
Fred Drake12c6e2d1999-12-14 21:25:03 +00007226 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7227 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00007228 long limit;
7229
7230 errno = 0;
7231 limit = fpathconf(fd, name);
7232 if (limit == -1 && errno != 0)
7233 posix_error();
7234 else
7235 result = PyInt_FromLong(limit);
7236 }
7237 return result;
7238}
7239#endif
7240
7241
7242#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007243PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007244"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007245Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007246If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007247
7248static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007249posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007250{
7251 PyObject *result = NULL;
7252 int name;
7253 char *path;
7254
7255 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7256 conv_path_confname, &name)) {
7257 long limit;
7258
7259 errno = 0;
7260 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007261 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00007262 if (errno == EINVAL)
7263 /* could be a path or name problem */
7264 posix_error();
7265 else
7266 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007267 }
Fred Drakec9680921999-12-13 16:37:25 +00007268 else
7269 result = PyInt_FromLong(limit);
7270 }
7271 return result;
7272}
7273#endif
7274
7275#ifdef HAVE_CONFSTR
7276static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007277#ifdef _CS_ARCHITECTURE
7278 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
7279#endif
7280#ifdef _CS_HOSTNAME
7281 {"CS_HOSTNAME", _CS_HOSTNAME},
7282#endif
7283#ifdef _CS_HW_PROVIDER
7284 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
7285#endif
7286#ifdef _CS_HW_SERIAL
7287 {"CS_HW_SERIAL", _CS_HW_SERIAL},
7288#endif
7289#ifdef _CS_INITTAB_NAME
7290 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
7291#endif
Fred Drakec9680921999-12-13 16:37:25 +00007292#ifdef _CS_LFS64_CFLAGS
7293 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
7294#endif
7295#ifdef _CS_LFS64_LDFLAGS
7296 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
7297#endif
7298#ifdef _CS_LFS64_LIBS
7299 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
7300#endif
7301#ifdef _CS_LFS64_LINTFLAGS
7302 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
7303#endif
7304#ifdef _CS_LFS_CFLAGS
7305 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
7306#endif
7307#ifdef _CS_LFS_LDFLAGS
7308 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
7309#endif
7310#ifdef _CS_LFS_LIBS
7311 {"CS_LFS_LIBS", _CS_LFS_LIBS},
7312#endif
7313#ifdef _CS_LFS_LINTFLAGS
7314 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
7315#endif
Fred Draked86ed291999-12-15 15:34:33 +00007316#ifdef _CS_MACHINE
7317 {"CS_MACHINE", _CS_MACHINE},
7318#endif
Fred Drakec9680921999-12-13 16:37:25 +00007319#ifdef _CS_PATH
7320 {"CS_PATH", _CS_PATH},
7321#endif
Fred Draked86ed291999-12-15 15:34:33 +00007322#ifdef _CS_RELEASE
7323 {"CS_RELEASE", _CS_RELEASE},
7324#endif
7325#ifdef _CS_SRPC_DOMAIN
7326 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
7327#endif
7328#ifdef _CS_SYSNAME
7329 {"CS_SYSNAME", _CS_SYSNAME},
7330#endif
7331#ifdef _CS_VERSION
7332 {"CS_VERSION", _CS_VERSION},
7333#endif
Fred Drakec9680921999-12-13 16:37:25 +00007334#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7335 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
7336#endif
7337#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7338 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
7339#endif
7340#ifdef _CS_XBS5_ILP32_OFF32_LIBS
7341 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
7342#endif
7343#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7344 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
7345#endif
7346#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7347 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
7348#endif
7349#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7350 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
7351#endif
7352#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7353 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
7354#endif
7355#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7356 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
7357#endif
7358#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7359 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
7360#endif
7361#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7362 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
7363#endif
7364#ifdef _CS_XBS5_LP64_OFF64_LIBS
7365 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
7366#endif
7367#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7368 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
7369#endif
7370#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7371 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
7372#endif
7373#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7374 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
7375#endif
7376#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7377 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
7378#endif
7379#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7380 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
7381#endif
Fred Draked86ed291999-12-15 15:34:33 +00007382#ifdef _MIPS_CS_AVAIL_PROCESSORS
7383 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
7384#endif
7385#ifdef _MIPS_CS_BASE
7386 {"MIPS_CS_BASE", _MIPS_CS_BASE},
7387#endif
7388#ifdef _MIPS_CS_HOSTID
7389 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
7390#endif
7391#ifdef _MIPS_CS_HW_NAME
7392 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7393#endif
7394#ifdef _MIPS_CS_NUM_PROCESSORS
7395 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7396#endif
7397#ifdef _MIPS_CS_OSREL_MAJ
7398 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7399#endif
7400#ifdef _MIPS_CS_OSREL_MIN
7401 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7402#endif
7403#ifdef _MIPS_CS_OSREL_PATCH
7404 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7405#endif
7406#ifdef _MIPS_CS_OS_NAME
7407 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7408#endif
7409#ifdef _MIPS_CS_OS_PROVIDER
7410 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7411#endif
7412#ifdef _MIPS_CS_PROCESSORS
7413 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7414#endif
7415#ifdef _MIPS_CS_SERIAL
7416 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7417#endif
7418#ifdef _MIPS_CS_VENDOR
7419 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7420#endif
Fred Drakec9680921999-12-13 16:37:25 +00007421};
7422
7423static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007424conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007425{
7426 return conv_confname(arg, valuep, posix_constants_confstr,
7427 sizeof(posix_constants_confstr)
7428 / sizeof(struct constdef));
7429}
7430
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007431PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007432"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007433Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007434
7435static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007436posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007437{
7438 PyObject *result = NULL;
7439 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007440 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007441
7442 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Skip Montanarodd527fc2006-04-18 00:49:49 +00007443 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007444
Fred Drakec9680921999-12-13 16:37:25 +00007445 errno = 0;
Skip Montanarodd527fc2006-04-18 00:49:49 +00007446 len = confstr(name, buffer, sizeof(buffer));
Skip Montanaro94785ef2006-04-20 01:29:48 +00007447 if (len == 0) {
7448 if (errno) {
7449 posix_error();
7450 }
7451 else {
7452 result = Py_None;
7453 Py_INCREF(Py_None);
7454 }
Fred Drakec9680921999-12-13 16:37:25 +00007455 }
7456 else {
Neal Norwitz0d21b1e2006-04-20 06:44:42 +00007457 if ((unsigned int)len >= sizeof(buffer)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007458 result = PyString_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007459 if (result != NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007460 confstr(name, PyString_AS_STRING(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00007461 }
7462 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007463 result = PyString_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007464 }
7465 }
7466 return result;
7467}
7468#endif
7469
7470
7471#ifdef HAVE_SYSCONF
7472static struct constdef posix_constants_sysconf[] = {
7473#ifdef _SC_2_CHAR_TERM
7474 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7475#endif
7476#ifdef _SC_2_C_BIND
7477 {"SC_2_C_BIND", _SC_2_C_BIND},
7478#endif
7479#ifdef _SC_2_C_DEV
7480 {"SC_2_C_DEV", _SC_2_C_DEV},
7481#endif
7482#ifdef _SC_2_C_VERSION
7483 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7484#endif
7485#ifdef _SC_2_FORT_DEV
7486 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7487#endif
7488#ifdef _SC_2_FORT_RUN
7489 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7490#endif
7491#ifdef _SC_2_LOCALEDEF
7492 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7493#endif
7494#ifdef _SC_2_SW_DEV
7495 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7496#endif
7497#ifdef _SC_2_UPE
7498 {"SC_2_UPE", _SC_2_UPE},
7499#endif
7500#ifdef _SC_2_VERSION
7501 {"SC_2_VERSION", _SC_2_VERSION},
7502#endif
Fred Draked86ed291999-12-15 15:34:33 +00007503#ifdef _SC_ABI_ASYNCHRONOUS_IO
7504 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7505#endif
7506#ifdef _SC_ACL
7507 {"SC_ACL", _SC_ACL},
7508#endif
Fred Drakec9680921999-12-13 16:37:25 +00007509#ifdef _SC_AIO_LISTIO_MAX
7510 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7511#endif
Fred Drakec9680921999-12-13 16:37:25 +00007512#ifdef _SC_AIO_MAX
7513 {"SC_AIO_MAX", _SC_AIO_MAX},
7514#endif
7515#ifdef _SC_AIO_PRIO_DELTA_MAX
7516 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7517#endif
7518#ifdef _SC_ARG_MAX
7519 {"SC_ARG_MAX", _SC_ARG_MAX},
7520#endif
7521#ifdef _SC_ASYNCHRONOUS_IO
7522 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7523#endif
7524#ifdef _SC_ATEXIT_MAX
7525 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7526#endif
Fred Draked86ed291999-12-15 15:34:33 +00007527#ifdef _SC_AUDIT
7528 {"SC_AUDIT", _SC_AUDIT},
7529#endif
Fred Drakec9680921999-12-13 16:37:25 +00007530#ifdef _SC_AVPHYS_PAGES
7531 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7532#endif
7533#ifdef _SC_BC_BASE_MAX
7534 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7535#endif
7536#ifdef _SC_BC_DIM_MAX
7537 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7538#endif
7539#ifdef _SC_BC_SCALE_MAX
7540 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7541#endif
7542#ifdef _SC_BC_STRING_MAX
7543 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7544#endif
Fred Draked86ed291999-12-15 15:34:33 +00007545#ifdef _SC_CAP
7546 {"SC_CAP", _SC_CAP},
7547#endif
Fred Drakec9680921999-12-13 16:37:25 +00007548#ifdef _SC_CHARCLASS_NAME_MAX
7549 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7550#endif
7551#ifdef _SC_CHAR_BIT
7552 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7553#endif
7554#ifdef _SC_CHAR_MAX
7555 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7556#endif
7557#ifdef _SC_CHAR_MIN
7558 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7559#endif
7560#ifdef _SC_CHILD_MAX
7561 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7562#endif
7563#ifdef _SC_CLK_TCK
7564 {"SC_CLK_TCK", _SC_CLK_TCK},
7565#endif
7566#ifdef _SC_COHER_BLKSZ
7567 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7568#endif
7569#ifdef _SC_COLL_WEIGHTS_MAX
7570 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7571#endif
7572#ifdef _SC_DCACHE_ASSOC
7573 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7574#endif
7575#ifdef _SC_DCACHE_BLKSZ
7576 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7577#endif
7578#ifdef _SC_DCACHE_LINESZ
7579 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7580#endif
7581#ifdef _SC_DCACHE_SZ
7582 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7583#endif
7584#ifdef _SC_DCACHE_TBLKSZ
7585 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7586#endif
7587#ifdef _SC_DELAYTIMER_MAX
7588 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7589#endif
7590#ifdef _SC_EQUIV_CLASS_MAX
7591 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7592#endif
7593#ifdef _SC_EXPR_NEST_MAX
7594 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7595#endif
7596#ifdef _SC_FSYNC
7597 {"SC_FSYNC", _SC_FSYNC},
7598#endif
7599#ifdef _SC_GETGR_R_SIZE_MAX
7600 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7601#endif
7602#ifdef _SC_GETPW_R_SIZE_MAX
7603 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7604#endif
7605#ifdef _SC_ICACHE_ASSOC
7606 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7607#endif
7608#ifdef _SC_ICACHE_BLKSZ
7609 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7610#endif
7611#ifdef _SC_ICACHE_LINESZ
7612 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7613#endif
7614#ifdef _SC_ICACHE_SZ
7615 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7616#endif
Fred Draked86ed291999-12-15 15:34:33 +00007617#ifdef _SC_INF
7618 {"SC_INF", _SC_INF},
7619#endif
Fred Drakec9680921999-12-13 16:37:25 +00007620#ifdef _SC_INT_MAX
7621 {"SC_INT_MAX", _SC_INT_MAX},
7622#endif
7623#ifdef _SC_INT_MIN
7624 {"SC_INT_MIN", _SC_INT_MIN},
7625#endif
7626#ifdef _SC_IOV_MAX
7627 {"SC_IOV_MAX", _SC_IOV_MAX},
7628#endif
Fred Draked86ed291999-12-15 15:34:33 +00007629#ifdef _SC_IP_SECOPTS
7630 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7631#endif
Fred Drakec9680921999-12-13 16:37:25 +00007632#ifdef _SC_JOB_CONTROL
7633 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7634#endif
Fred Draked86ed291999-12-15 15:34:33 +00007635#ifdef _SC_KERN_POINTERS
7636 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7637#endif
7638#ifdef _SC_KERN_SIM
7639 {"SC_KERN_SIM", _SC_KERN_SIM},
7640#endif
Fred Drakec9680921999-12-13 16:37:25 +00007641#ifdef _SC_LINE_MAX
7642 {"SC_LINE_MAX", _SC_LINE_MAX},
7643#endif
7644#ifdef _SC_LOGIN_NAME_MAX
7645 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7646#endif
7647#ifdef _SC_LOGNAME_MAX
7648 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7649#endif
7650#ifdef _SC_LONG_BIT
7651 {"SC_LONG_BIT", _SC_LONG_BIT},
7652#endif
Fred Draked86ed291999-12-15 15:34:33 +00007653#ifdef _SC_MAC
7654 {"SC_MAC", _SC_MAC},
7655#endif
Fred Drakec9680921999-12-13 16:37:25 +00007656#ifdef _SC_MAPPED_FILES
7657 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7658#endif
7659#ifdef _SC_MAXPID
7660 {"SC_MAXPID", _SC_MAXPID},
7661#endif
7662#ifdef _SC_MB_LEN_MAX
7663 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7664#endif
7665#ifdef _SC_MEMLOCK
7666 {"SC_MEMLOCK", _SC_MEMLOCK},
7667#endif
7668#ifdef _SC_MEMLOCK_RANGE
7669 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7670#endif
7671#ifdef _SC_MEMORY_PROTECTION
7672 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7673#endif
7674#ifdef _SC_MESSAGE_PASSING
7675 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7676#endif
Fred Draked86ed291999-12-15 15:34:33 +00007677#ifdef _SC_MMAP_FIXED_ALIGNMENT
7678 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7679#endif
Fred Drakec9680921999-12-13 16:37:25 +00007680#ifdef _SC_MQ_OPEN_MAX
7681 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7682#endif
7683#ifdef _SC_MQ_PRIO_MAX
7684 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7685#endif
Fred Draked86ed291999-12-15 15:34:33 +00007686#ifdef _SC_NACLS_MAX
7687 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7688#endif
Fred Drakec9680921999-12-13 16:37:25 +00007689#ifdef _SC_NGROUPS_MAX
7690 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7691#endif
7692#ifdef _SC_NL_ARGMAX
7693 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7694#endif
7695#ifdef _SC_NL_LANGMAX
7696 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7697#endif
7698#ifdef _SC_NL_MSGMAX
7699 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7700#endif
7701#ifdef _SC_NL_NMAX
7702 {"SC_NL_NMAX", _SC_NL_NMAX},
7703#endif
7704#ifdef _SC_NL_SETMAX
7705 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7706#endif
7707#ifdef _SC_NL_TEXTMAX
7708 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7709#endif
7710#ifdef _SC_NPROCESSORS_CONF
7711 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7712#endif
7713#ifdef _SC_NPROCESSORS_ONLN
7714 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7715#endif
Fred Draked86ed291999-12-15 15:34:33 +00007716#ifdef _SC_NPROC_CONF
7717 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7718#endif
7719#ifdef _SC_NPROC_ONLN
7720 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7721#endif
Fred Drakec9680921999-12-13 16:37:25 +00007722#ifdef _SC_NZERO
7723 {"SC_NZERO", _SC_NZERO},
7724#endif
7725#ifdef _SC_OPEN_MAX
7726 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7727#endif
7728#ifdef _SC_PAGESIZE
7729 {"SC_PAGESIZE", _SC_PAGESIZE},
7730#endif
7731#ifdef _SC_PAGE_SIZE
7732 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7733#endif
7734#ifdef _SC_PASS_MAX
7735 {"SC_PASS_MAX", _SC_PASS_MAX},
7736#endif
7737#ifdef _SC_PHYS_PAGES
7738 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7739#endif
7740#ifdef _SC_PII
7741 {"SC_PII", _SC_PII},
7742#endif
7743#ifdef _SC_PII_INTERNET
7744 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7745#endif
7746#ifdef _SC_PII_INTERNET_DGRAM
7747 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7748#endif
7749#ifdef _SC_PII_INTERNET_STREAM
7750 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7751#endif
7752#ifdef _SC_PII_OSI
7753 {"SC_PII_OSI", _SC_PII_OSI},
7754#endif
7755#ifdef _SC_PII_OSI_CLTS
7756 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7757#endif
7758#ifdef _SC_PII_OSI_COTS
7759 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7760#endif
7761#ifdef _SC_PII_OSI_M
7762 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7763#endif
7764#ifdef _SC_PII_SOCKET
7765 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7766#endif
7767#ifdef _SC_PII_XTI
7768 {"SC_PII_XTI", _SC_PII_XTI},
7769#endif
7770#ifdef _SC_POLL
7771 {"SC_POLL", _SC_POLL},
7772#endif
7773#ifdef _SC_PRIORITIZED_IO
7774 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7775#endif
7776#ifdef _SC_PRIORITY_SCHEDULING
7777 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7778#endif
7779#ifdef _SC_REALTIME_SIGNALS
7780 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7781#endif
7782#ifdef _SC_RE_DUP_MAX
7783 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7784#endif
7785#ifdef _SC_RTSIG_MAX
7786 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7787#endif
7788#ifdef _SC_SAVED_IDS
7789 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7790#endif
7791#ifdef _SC_SCHAR_MAX
7792 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7793#endif
7794#ifdef _SC_SCHAR_MIN
7795 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7796#endif
7797#ifdef _SC_SELECT
7798 {"SC_SELECT", _SC_SELECT},
7799#endif
7800#ifdef _SC_SEMAPHORES
7801 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7802#endif
7803#ifdef _SC_SEM_NSEMS_MAX
7804 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7805#endif
7806#ifdef _SC_SEM_VALUE_MAX
7807 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7808#endif
7809#ifdef _SC_SHARED_MEMORY_OBJECTS
7810 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7811#endif
7812#ifdef _SC_SHRT_MAX
7813 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7814#endif
7815#ifdef _SC_SHRT_MIN
7816 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7817#endif
7818#ifdef _SC_SIGQUEUE_MAX
7819 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7820#endif
7821#ifdef _SC_SIGRT_MAX
7822 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7823#endif
7824#ifdef _SC_SIGRT_MIN
7825 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7826#endif
Fred Draked86ed291999-12-15 15:34:33 +00007827#ifdef _SC_SOFTPOWER
7828 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7829#endif
Fred Drakec9680921999-12-13 16:37:25 +00007830#ifdef _SC_SPLIT_CACHE
7831 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7832#endif
7833#ifdef _SC_SSIZE_MAX
7834 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7835#endif
7836#ifdef _SC_STACK_PROT
7837 {"SC_STACK_PROT", _SC_STACK_PROT},
7838#endif
7839#ifdef _SC_STREAM_MAX
7840 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7841#endif
7842#ifdef _SC_SYNCHRONIZED_IO
7843 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7844#endif
7845#ifdef _SC_THREADS
7846 {"SC_THREADS", _SC_THREADS},
7847#endif
7848#ifdef _SC_THREAD_ATTR_STACKADDR
7849 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7850#endif
7851#ifdef _SC_THREAD_ATTR_STACKSIZE
7852 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7853#endif
7854#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7855 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7856#endif
7857#ifdef _SC_THREAD_KEYS_MAX
7858 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7859#endif
7860#ifdef _SC_THREAD_PRIORITY_SCHEDULING
7861 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7862#endif
7863#ifdef _SC_THREAD_PRIO_INHERIT
7864 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7865#endif
7866#ifdef _SC_THREAD_PRIO_PROTECT
7867 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7868#endif
7869#ifdef _SC_THREAD_PROCESS_SHARED
7870 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7871#endif
7872#ifdef _SC_THREAD_SAFE_FUNCTIONS
7873 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7874#endif
7875#ifdef _SC_THREAD_STACK_MIN
7876 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7877#endif
7878#ifdef _SC_THREAD_THREADS_MAX
7879 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7880#endif
7881#ifdef _SC_TIMERS
7882 {"SC_TIMERS", _SC_TIMERS},
7883#endif
7884#ifdef _SC_TIMER_MAX
7885 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7886#endif
7887#ifdef _SC_TTY_NAME_MAX
7888 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7889#endif
7890#ifdef _SC_TZNAME_MAX
7891 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7892#endif
7893#ifdef _SC_T_IOV_MAX
7894 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7895#endif
7896#ifdef _SC_UCHAR_MAX
7897 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7898#endif
7899#ifdef _SC_UINT_MAX
7900 {"SC_UINT_MAX", _SC_UINT_MAX},
7901#endif
7902#ifdef _SC_UIO_MAXIOV
7903 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7904#endif
7905#ifdef _SC_ULONG_MAX
7906 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7907#endif
7908#ifdef _SC_USHRT_MAX
7909 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7910#endif
7911#ifdef _SC_VERSION
7912 {"SC_VERSION", _SC_VERSION},
7913#endif
7914#ifdef _SC_WORD_BIT
7915 {"SC_WORD_BIT", _SC_WORD_BIT},
7916#endif
7917#ifdef _SC_XBS5_ILP32_OFF32
7918 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7919#endif
7920#ifdef _SC_XBS5_ILP32_OFFBIG
7921 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7922#endif
7923#ifdef _SC_XBS5_LP64_OFF64
7924 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7925#endif
7926#ifdef _SC_XBS5_LPBIG_OFFBIG
7927 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7928#endif
7929#ifdef _SC_XOPEN_CRYPT
7930 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7931#endif
7932#ifdef _SC_XOPEN_ENH_I18N
7933 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7934#endif
7935#ifdef _SC_XOPEN_LEGACY
7936 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7937#endif
7938#ifdef _SC_XOPEN_REALTIME
7939 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7940#endif
7941#ifdef _SC_XOPEN_REALTIME_THREADS
7942 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7943#endif
7944#ifdef _SC_XOPEN_SHM
7945 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7946#endif
7947#ifdef _SC_XOPEN_UNIX
7948 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7949#endif
7950#ifdef _SC_XOPEN_VERSION
7951 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7952#endif
7953#ifdef _SC_XOPEN_XCU_VERSION
7954 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7955#endif
7956#ifdef _SC_XOPEN_XPG2
7957 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7958#endif
7959#ifdef _SC_XOPEN_XPG3
7960 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7961#endif
7962#ifdef _SC_XOPEN_XPG4
7963 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7964#endif
7965};
7966
7967static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007968conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007969{
7970 return conv_confname(arg, valuep, posix_constants_sysconf,
7971 sizeof(posix_constants_sysconf)
7972 / sizeof(struct constdef));
7973}
7974
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007975PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007976"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007977Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007978
7979static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007980posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007981{
7982 PyObject *result = NULL;
7983 int name;
7984
7985 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7986 int value;
7987
7988 errno = 0;
7989 value = sysconf(name);
7990 if (value == -1 && errno != 0)
7991 posix_error();
7992 else
7993 result = PyInt_FromLong(value);
7994 }
7995 return result;
7996}
7997#endif
7998
7999
Fred Drakebec628d1999-12-15 18:31:10 +00008000/* This code is used to ensure that the tables of configuration value names
8001 * are in sorted order as required by conv_confname(), and also to build the
8002 * the exported dictionaries that are used to publish information about the
8003 * names available on the host platform.
8004 *
8005 * Sorting the table at runtime ensures that the table is properly ordered
8006 * when used, even for platforms we're not able to test on. It also makes
8007 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008008 */
Fred Drakebec628d1999-12-15 18:31:10 +00008009
8010static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008011cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008012{
8013 const struct constdef *c1 =
8014 (const struct constdef *) v1;
8015 const struct constdef *c2 =
8016 (const struct constdef *) v2;
8017
8018 return strcmp(c1->name, c2->name);
8019}
8020
8021static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008022setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008023 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008024{
Fred Drakebec628d1999-12-15 18:31:10 +00008025 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008026 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008027
8028 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8029 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008030 if (d == NULL)
8031 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008032
Barry Warsaw3155db32000-04-13 15:20:40 +00008033 for (i=0; i < tablesize; ++i) {
8034 PyObject *o = PyInt_FromLong(table[i].value);
8035 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8036 Py_XDECREF(o);
8037 Py_DECREF(d);
8038 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008039 }
Barry Warsaw3155db32000-04-13 15:20:40 +00008040 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008041 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008042 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008043}
8044
Fred Drakebec628d1999-12-15 18:31:10 +00008045/* Return -1 on failure, 0 on success. */
8046static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008047setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008048{
8049#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008050 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008051 sizeof(posix_constants_pathconf)
8052 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008053 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00008054 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008055#endif
8056#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008057 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008058 sizeof(posix_constants_confstr)
8059 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008060 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00008061 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008062#endif
8063#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008064 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008065 sizeof(posix_constants_sysconf)
8066 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008067 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00008068 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008069#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008070 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008071}
Fred Draked86ed291999-12-15 15:34:33 +00008072
8073
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008074PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008075"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008076Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008077in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008078
8079static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008080posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008081{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008082 abort();
8083 /*NOTREACHED*/
8084 Py_FatalError("abort() called from Python code didn't abort!");
8085 return NULL;
8086}
Fred Drakebec628d1999-12-15 18:31:10 +00008087
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008088#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008089PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008090"startfile(filepath [, operation]) - Start a file with its associated\n\
8091application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008092\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008093When \"operation\" is not specified or \"open\", this acts like\n\
8094double-clicking the file in Explorer, or giving the file name as an\n\
8095argument to the DOS \"start\" command: the file is opened with whatever\n\
8096application (if any) its extension is associated.\n\
8097When another \"operation\" is given, it specifies what should be done with\n\
8098the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008099\n\
8100startfile returns as soon as the associated application is launched.\n\
8101There is no option to wait for the application to close, and no way\n\
8102to retrieve the application's exit status.\n\
8103\n\
8104The filepath is relative to the current directory. If you want to use\n\
8105an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008106the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008107
8108static PyObject *
8109win32_startfile(PyObject *self, PyObject *args)
8110{
8111 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00008112 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008113 HINSTANCE rc;
Georg Brandlad89dc82006-04-03 12:26:26 +00008114#ifdef Py_WIN_WIDE_FILENAMES
8115 if (unicode_file_names()) {
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008116 PyObject *unipath, *woperation = NULL;
Georg Brandlad89dc82006-04-03 12:26:26 +00008117 if (!PyArg_ParseTuple(args, "U|s:startfile",
8118 &unipath, &operation)) {
8119 PyErr_Clear();
8120 goto normal;
8121 }
8122
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008123
8124 if (operation) {
8125 woperation = PyUnicode_DecodeASCII(operation,
8126 strlen(operation), NULL);
8127 if (!woperation) {
8128 PyErr_Clear();
8129 operation = NULL;
8130 goto normal;
8131 }
Georg Brandlad89dc82006-04-03 12:26:26 +00008132 }
8133
8134 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008135 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
Georg Brandlad89dc82006-04-03 12:26:26 +00008136 PyUnicode_AS_UNICODE(unipath),
Georg Brandlad89dc82006-04-03 12:26:26 +00008137 NULL, NULL, SW_SHOWNORMAL);
8138 Py_END_ALLOW_THREADS
8139
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008140 Py_XDECREF(woperation);
Georg Brandlad89dc82006-04-03 12:26:26 +00008141 if (rc <= (HINSTANCE)32) {
8142 PyObject *errval = win32_error_unicode("startfile",
8143 PyUnicode_AS_UNICODE(unipath));
8144 return errval;
8145 }
8146 Py_INCREF(Py_None);
8147 return Py_None;
8148 }
8149#endif
8150
8151normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00008152 if (!PyArg_ParseTuple(args, "et|s:startfile",
8153 Py_FileSystemDefaultEncoding, &filepath,
8154 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00008155 return NULL;
8156 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00008157 rc = ShellExecute((HWND)0, operation, filepath,
8158 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00008159 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00008160 if (rc <= (HINSTANCE)32) {
8161 PyObject *errval = win32_error("startfile", filepath);
8162 PyMem_Free(filepath);
8163 return errval;
8164 }
8165 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00008166 Py_INCREF(Py_None);
8167 return Py_None;
8168}
8169#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008170
Martin v. Löwis438b5342002-12-27 10:16:42 +00008171#ifdef HAVE_GETLOADAVG
8172PyDoc_STRVAR(posix_getloadavg__doc__,
8173"getloadavg() -> (float, float, float)\n\n\
8174Return the number of processes in the system run queue averaged over\n\
8175the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8176was unobtainable");
8177
8178static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008179posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008180{
8181 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008182 if (getloadavg(loadavg, 3)!=3) {
8183 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8184 return NULL;
8185 } else
8186 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
8187}
8188#endif
8189
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008190#ifdef MS_WINDOWS
8191
8192PyDoc_STRVAR(win32_urandom__doc__,
8193"urandom(n) -> str\n\n\
8194Return a string of n random bytes suitable for cryptographic use.");
8195
8196typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8197 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8198 DWORD dwFlags );
8199typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8200 BYTE *pbBuffer );
8201
8202static CRYPTGENRANDOM pCryptGenRandom = NULL;
Martin v. Löwisaf699dd2007-09-04 09:51:57 +00008203/* This handle is never explicitly released. Instead, the operating
8204 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008205static HCRYPTPROV hCryptProv = 0;
8206
Tim Peters4ad82172004-08-30 17:02:04 +00008207static PyObject*
8208win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008209{
Tim Petersd3115382004-08-30 17:36:46 +00008210 int howMany;
8211 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008212
Tim Peters4ad82172004-08-30 17:02:04 +00008213 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00008214 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00008215 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00008216 if (howMany < 0)
8217 return PyErr_Format(PyExc_ValueError,
8218 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008219
Tim Peters4ad82172004-08-30 17:02:04 +00008220 if (hCryptProv == 0) {
8221 HINSTANCE hAdvAPI32 = NULL;
8222 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008223
Tim Peters4ad82172004-08-30 17:02:04 +00008224 /* Obtain handle to the DLL containing CryptoAPI
8225 This should not fail */
8226 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8227 if(hAdvAPI32 == NULL)
8228 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008229
Tim Peters4ad82172004-08-30 17:02:04 +00008230 /* Obtain pointers to the CryptoAPI functions
8231 This will fail on some early versions of Win95 */
8232 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8233 hAdvAPI32,
8234 "CryptAcquireContextA");
8235 if (pCryptAcquireContext == NULL)
8236 return PyErr_Format(PyExc_NotImplementedError,
8237 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008238
Tim Peters4ad82172004-08-30 17:02:04 +00008239 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8240 hAdvAPI32, "CryptGenRandom");
Georg Brandl74bb7832006-09-06 06:03:59 +00008241 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00008242 return PyErr_Format(PyExc_NotImplementedError,
8243 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008244
Tim Peters4ad82172004-08-30 17:02:04 +00008245 /* Acquire context */
8246 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8247 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8248 return win32_error("CryptAcquireContext", NULL);
8249 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008250
Tim Peters4ad82172004-08-30 17:02:04 +00008251 /* Allocate bytes */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00008252 result = PyString_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00008253 if (result != NULL) {
8254 /* Get random data */
Amaury Forgeot d'Arc74bd40d2008-07-21 21:06:46 +00008255 memset(PyString_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00008256 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00008257 PyString_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00008258 Py_DECREF(result);
8259 return win32_error("CryptGenRandom", NULL);
8260 }
Tim Peters4ad82172004-08-30 17:02:04 +00008261 }
Tim Petersd3115382004-08-30 17:36:46 +00008262 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008263}
8264#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008265
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008266#ifdef __VMS
8267/* Use openssl random routine */
8268#include <openssl/rand.h>
8269PyDoc_STRVAR(vms_urandom__doc__,
8270"urandom(n) -> str\n\n\
8271Return a string of n random bytes suitable for cryptographic use.");
8272
8273static PyObject*
8274vms_urandom(PyObject *self, PyObject *args)
8275{
8276 int howMany;
8277 PyObject* result;
8278
8279 /* Read arguments */
8280 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8281 return NULL;
8282 if (howMany < 0)
8283 return PyErr_Format(PyExc_ValueError,
8284 "negative argument not allowed");
8285
8286 /* Allocate bytes */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00008287 result = PyString_FromStringAndSize(NULL, howMany);
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008288 if (result != NULL) {
8289 /* Get random data */
8290 if (RAND_pseudo_bytes((unsigned char*)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00008291 PyString_AS_STRING(result),
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008292 howMany) < 0) {
8293 Py_DECREF(result);
8294 return PyErr_Format(PyExc_ValueError,
8295 "RAND_pseudo_bytes");
8296 }
8297 }
8298 return result;
8299}
8300#endif
8301
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008302static PyMethodDef posix_methods[] = {
8303 {"access", posix_access, METH_VARARGS, posix_access__doc__},
8304#ifdef HAVE_TTYNAME
8305 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
8306#endif
8307 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008308#ifdef HAVE_CHFLAGS
8309 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
8310#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008311 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008312#ifdef HAVE_FCHMOD
8313 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
8314#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008315#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008316 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008317#endif /* HAVE_CHOWN */
Christian Heimes36281872007-11-30 21:11:28 +00008318#ifdef HAVE_LCHMOD
8319 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
8320#endif /* HAVE_LCHMOD */
8321#ifdef HAVE_FCHOWN
8322 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
8323#endif /* HAVE_FCHOWN */
Martin v. Löwis382abef2007-02-19 10:55:19 +00008324#ifdef HAVE_LCHFLAGS
8325 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
8326#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008327#ifdef HAVE_LCHOWN
8328 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
8329#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008330#ifdef HAVE_CHROOT
8331 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
8332#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008333#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00008334 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008335#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008336#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00008337 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008338#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00008339 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008340#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008341#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008342#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008343 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008344#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008345 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8346 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8347 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008348#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008349 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008350#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008351#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008352 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008353#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008354 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8355 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8356 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00008357 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008358#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008359 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008360#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008361#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008362 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008363#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008364 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008365#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00008366 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008367#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008368 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8369 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8370 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008371#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00008372 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008373#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008374 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008375#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008376 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8377 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008378#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008379#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008380 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8381 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008382#if defined(PYOS_OS2)
8383 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8384 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
8385#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008386#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008387#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00008388 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008389#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008390#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00008391 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008392#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008393#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00008394 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008395#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008396#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00008397 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008398#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008399#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008400 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008401#endif /* HAVE_GETEGID */
8402#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008403 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008404#endif /* HAVE_GETEUID */
8405#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008406 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008407#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008408#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00008409 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008410#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008411 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008412#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008413 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008414#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008415#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00008416 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008417#endif /* HAVE_GETPPID */
8418#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008419 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008420#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008421#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00008422 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008423#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008424#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008425 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008426#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008427#ifdef HAVE_KILLPG
8428 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
8429#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008430#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008431 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008432#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008433#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008434 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008435#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008436 {"popen2", win32_popen2, METH_VARARGS},
8437 {"popen3", win32_popen3, METH_VARARGS},
8438 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00008439 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008440#else
8441#if defined(PYOS_OS2) && defined(PYCC_GCC)
8442 {"popen2", os2emx_popen2, METH_VARARGS},
8443 {"popen3", os2emx_popen3, METH_VARARGS},
8444 {"popen4", os2emx_popen4, METH_VARARGS},
8445#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008446#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008447#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008448#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008449 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008450#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008451#ifdef HAVE_SETEUID
8452 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8453#endif /* HAVE_SETEUID */
8454#ifdef HAVE_SETEGID
8455 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8456#endif /* HAVE_SETEGID */
8457#ifdef HAVE_SETREUID
8458 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8459#endif /* HAVE_SETREUID */
8460#ifdef HAVE_SETREGID
8461 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8462#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008463#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008464 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008465#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008466#ifdef HAVE_SETGROUPS
Georg Brandl96a8c392006-05-29 21:04:52 +00008467 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008468#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008469#ifdef HAVE_GETPGID
8470 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8471#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008472#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008473 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008474#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008475#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00008476 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008477#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008478#ifdef HAVE_WAIT3
8479 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8480#endif /* HAVE_WAIT3 */
8481#ifdef HAVE_WAIT4
8482 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8483#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008484#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008485 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008486#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008487#ifdef HAVE_GETSID
8488 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8489#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008490#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00008491 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008492#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008493#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008494 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008495#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008496#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008497 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008498#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008499#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008500 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008501#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008502 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8503 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Georg Brandl309501a2008-01-19 20:22:13 +00008504 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008505 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8506 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8507 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8508 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8509 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8510 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8511 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00008512 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008513#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00008514 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008515#endif
8516#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008517 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008518#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008519#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008520 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8521#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008522#ifdef HAVE_DEVICE_MACROS
8523 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8524 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8525 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8526#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008527#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008528 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008529#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008530#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008531 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008532#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008533#ifdef HAVE_UNSETENV
8534 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8535#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008536 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008537#ifdef HAVE_FCHDIR
8538 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8539#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008540#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008541 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008542#endif
8543#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008544 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008545#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008546#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008547#ifdef WCOREDUMP
8548 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8549#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008550#ifdef WIFCONTINUED
8551 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8552#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008553#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008554 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008555#endif /* WIFSTOPPED */
8556#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008557 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008558#endif /* WIFSIGNALED */
8559#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008560 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008561#endif /* WIFEXITED */
8562#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008563 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008564#endif /* WEXITSTATUS */
8565#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008566 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008567#endif /* WTERMSIG */
8568#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008569 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008570#endif /* WSTOPSIG */
8571#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008572#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008573 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008574#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008575#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008576 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008577#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008578#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00008579 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008580#endif
8581#ifdef HAVE_TEMPNAM
8582 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8583#endif
8584#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00008585 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008586#endif
Fred Drakec9680921999-12-13 16:37:25 +00008587#ifdef HAVE_CONFSTR
8588 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8589#endif
8590#ifdef HAVE_SYSCONF
8591 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8592#endif
8593#ifdef HAVE_FPATHCONF
8594 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8595#endif
8596#ifdef HAVE_PATHCONF
8597 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8598#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008599 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008600#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00008601 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8602#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008603#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00008604 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008605#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008606 #ifdef MS_WINDOWS
8607 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8608 #endif
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008609 #ifdef __VMS
8610 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
8611 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008612 {NULL, NULL} /* Sentinel */
8613};
8614
8615
Barry Warsaw4a342091996-12-19 23:50:02 +00008616static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008617ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008618{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008619 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008620}
8621
Guido van Rossumd48f2521997-12-05 22:19:34 +00008622#if defined(PYOS_OS2)
8623/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008624static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008625{
8626 APIRET rc;
8627 ULONG values[QSV_MAX+1];
8628 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008629 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008630
8631 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008632 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008633 Py_END_ALLOW_THREADS
8634
8635 if (rc != NO_ERROR) {
8636 os2_error(rc);
8637 return -1;
8638 }
8639
Fred Drake4d1e64b2002-04-15 19:40:07 +00008640 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8641 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8642 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8643 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8644 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8645 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8646 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008647
8648 switch (values[QSV_VERSION_MINOR]) {
8649 case 0: ver = "2.00"; break;
8650 case 10: ver = "2.10"; break;
8651 case 11: ver = "2.11"; break;
8652 case 30: ver = "3.00"; break;
8653 case 40: ver = "4.00"; break;
8654 case 50: ver = "5.00"; break;
8655 default:
Tim Peters885d4572001-11-28 20:27:42 +00008656 PyOS_snprintf(tmp, sizeof(tmp),
8657 "%d-%d", values[QSV_VERSION_MAJOR],
8658 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008659 ver = &tmp[0];
8660 }
8661
8662 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008663 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008664 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008665
8666 /* Add Indicator of Which Drive was Used to Boot the System */
8667 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8668 tmp[1] = ':';
8669 tmp[2] = '\0';
8670
Fred Drake4d1e64b2002-04-15 19:40:07 +00008671 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008672}
8673#endif
8674
Barry Warsaw4a342091996-12-19 23:50:02 +00008675static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008676all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008677{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008678#ifdef F_OK
8679 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008680#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008681#ifdef R_OK
8682 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008683#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008684#ifdef W_OK
8685 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008686#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008687#ifdef X_OK
8688 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008689#endif
Fred Drakec9680921999-12-13 16:37:25 +00008690#ifdef NGROUPS_MAX
8691 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8692#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008693#ifdef TMP_MAX
8694 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8695#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008696#ifdef WCONTINUED
8697 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8698#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008699#ifdef WNOHANG
8700 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008701#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008702#ifdef WUNTRACED
8703 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8704#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008705#ifdef O_RDONLY
8706 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8707#endif
8708#ifdef O_WRONLY
8709 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8710#endif
8711#ifdef O_RDWR
8712 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8713#endif
8714#ifdef O_NDELAY
8715 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8716#endif
8717#ifdef O_NONBLOCK
8718 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8719#endif
8720#ifdef O_APPEND
8721 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8722#endif
8723#ifdef O_DSYNC
8724 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8725#endif
8726#ifdef O_RSYNC
8727 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8728#endif
8729#ifdef O_SYNC
8730 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8731#endif
8732#ifdef O_NOCTTY
8733 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8734#endif
8735#ifdef O_CREAT
8736 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8737#endif
8738#ifdef O_EXCL
8739 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8740#endif
8741#ifdef O_TRUNC
8742 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8743#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008744#ifdef O_BINARY
8745 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8746#endif
8747#ifdef O_TEXT
8748 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8749#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008750#ifdef O_LARGEFILE
8751 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8752#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008753#ifdef O_SHLOCK
8754 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8755#endif
8756#ifdef O_EXLOCK
8757 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8758#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008759
Tim Peters5aa91602002-01-30 05:46:57 +00008760/* MS Windows */
8761#ifdef O_NOINHERIT
8762 /* Don't inherit in child processes. */
8763 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8764#endif
8765#ifdef _O_SHORT_LIVED
8766 /* Optimize for short life (keep in memory). */
8767 /* MS forgot to define this one with a non-underscore form too. */
8768 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8769#endif
8770#ifdef O_TEMPORARY
8771 /* Automatically delete when last handle is closed. */
8772 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8773#endif
8774#ifdef O_RANDOM
8775 /* Optimize for random access. */
8776 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8777#endif
8778#ifdef O_SEQUENTIAL
8779 /* Optimize for sequential access. */
8780 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8781#endif
8782
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008783/* GNU extensions. */
Georg Brandl5049a852008-05-16 13:10:15 +00008784#ifdef O_ASYNC
8785 /* Send a SIGIO signal whenever input or output
8786 becomes available on file descriptor */
8787 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
8788#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008789#ifdef O_DIRECT
8790 /* Direct disk access. */
8791 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8792#endif
8793#ifdef O_DIRECTORY
8794 /* Must be a directory. */
8795 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8796#endif
8797#ifdef O_NOFOLLOW
8798 /* Do not follow links. */
8799 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8800#endif
Georg Brandlb67da6e2007-11-24 13:56:09 +00008801#ifdef O_NOATIME
8802 /* Do not update the access time. */
8803 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
8804#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008805
Barry Warsaw5676bd12003-01-07 20:57:09 +00008806 /* These come from sysexits.h */
8807#ifdef EX_OK
8808 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008809#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008810#ifdef EX_USAGE
8811 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008812#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008813#ifdef EX_DATAERR
8814 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008815#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008816#ifdef EX_NOINPUT
8817 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008818#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008819#ifdef EX_NOUSER
8820 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008821#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008822#ifdef EX_NOHOST
8823 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008824#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008825#ifdef EX_UNAVAILABLE
8826 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008827#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008828#ifdef EX_SOFTWARE
8829 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008830#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008831#ifdef EX_OSERR
8832 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008833#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008834#ifdef EX_OSFILE
8835 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008836#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008837#ifdef EX_CANTCREAT
8838 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008839#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008840#ifdef EX_IOERR
8841 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008842#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008843#ifdef EX_TEMPFAIL
8844 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008845#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008846#ifdef EX_PROTOCOL
8847 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008848#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008849#ifdef EX_NOPERM
8850 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008851#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008852#ifdef EX_CONFIG
8853 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008854#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008855#ifdef EX_NOTFOUND
8856 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008857#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008858
Guido van Rossum246bc171999-02-01 23:54:31 +00008859#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008860#if defined(PYOS_OS2) && defined(PYCC_GCC)
8861 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8862 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8863 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8864 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8865 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8866 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8867 if (ins(d, "P_PM", (long)P_PM)) return -1;
8868 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8869 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8870 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8871 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8872 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8873 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8874 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8875 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8876 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8877 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8878 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8879 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8880 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8881#else
Guido van Rossum7d385291999-02-16 19:38:04 +00008882 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8883 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8884 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8885 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8886 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008887#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008888#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008889
Guido van Rossumd48f2521997-12-05 22:19:34 +00008890#if defined(PYOS_OS2)
8891 if (insertvalues(d)) return -1;
8892#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008893 return 0;
8894}
8895
8896
Tim Peters5aa91602002-01-30 05:46:57 +00008897#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008898#define INITFUNC initnt
8899#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008900
8901#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008902#define INITFUNC initos2
8903#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008904
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008905#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008906#define INITFUNC initposix
8907#define MODNAME "posix"
8908#endif
8909
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008910PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008911INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008912{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008913 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008914
Fred Drake4d1e64b2002-04-15 19:40:07 +00008915 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008916 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008917 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00008918 if (m == NULL)
8919 return;
Tim Peters5aa91602002-01-30 05:46:57 +00008920
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008921 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008922 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00008923 Py_XINCREF(v);
8924 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008925 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00008926 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008927
Fred Drake4d1e64b2002-04-15 19:40:07 +00008928 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00008929 return;
8930
Fred Drake4d1e64b2002-04-15 19:40:07 +00008931 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00008932 return;
8933
Fred Drake4d1e64b2002-04-15 19:40:07 +00008934 Py_INCREF(PyExc_OSError);
8935 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008936
Guido van Rossumb3d39562000-01-31 18:41:26 +00008937#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00008938 if (posix_putenv_garbage == NULL)
8939 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008940#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008941
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008942 if (!initialized) {
8943 stat_result_desc.name = MODNAME ".stat_result";
8944 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8945 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8946 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8947 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8948 structseq_new = StatResultType.tp_new;
8949 StatResultType.tp_new = statresult_new;
8950
8951 statvfs_result_desc.name = MODNAME ".statvfs_result";
8952 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
8953 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008954 Py_INCREF((PyObject*) &StatResultType);
8955 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00008956 Py_INCREF((PyObject*) &StatVFSResultType);
8957 PyModule_AddObject(m, "statvfs_result",
8958 (PyObject*) &StatVFSResultType);
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008959 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00008960
8961#ifdef __APPLE__
8962 /*
8963 * Step 2 of weak-linking support on Mac OS X.
8964 *
8965 * The code below removes functions that are not available on the
8966 * currently active platform.
8967 *
8968 * This block allow one to use a python binary that was build on
8969 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8970 * OSX 10.4.
8971 */
8972#ifdef HAVE_FSTATVFS
8973 if (fstatvfs == NULL) {
8974 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8975 return;
8976 }
8977 }
8978#endif /* HAVE_FSTATVFS */
8979
8980#ifdef HAVE_STATVFS
8981 if (statvfs == NULL) {
8982 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8983 return;
8984 }
8985 }
8986#endif /* HAVE_STATVFS */
8987
8988# ifdef HAVE_LCHOWN
8989 if (lchown == NULL) {
8990 if (PyObject_DelAttrString(m, "lchown") == -1) {
8991 return;
8992 }
8993 }
8994#endif /* HAVE_LCHOWN */
8995
8996
8997#endif /* __APPLE__ */
8998
Guido van Rossumb6775db1994-08-01 11:34:53 +00008999}
Anthony Baxterac6bd462006-04-13 02:06:09 +00009000
9001#ifdef __cplusplus
9002}
9003#endif
9004
Martin v. Löwis18aaa562006-10-15 08:43:33 +00009005