blob: ebdbc8d55b1547a701e6261d26b6109eca9df623 [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
Antoine Pitrou794b3fc2009-05-23 15:47:13 +0000313/* Issue #1983: pid_t can be longer than a C long on some systems */
Antoine Pitrou6c95acb2009-05-24 12:17:07 +0000314#if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
Antoine Pitrou794b3fc2009-05-23 15:47:13 +0000315#define PARSE_PID "i"
316#define PyLong_FromPid PyInt_FromLong
317#define PyLong_AsPid PyInt_AsLong
318#elif SIZEOF_PID_T == SIZEOF_LONG
319#define PARSE_PID "l"
320#define PyLong_FromPid PyInt_FromLong
321#define PyLong_AsPid PyInt_AsLong
322#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
323#define PARSE_PID "L"
324#define PyLong_FromPid PyLong_FromLongLong
325#define PyLong_AsPid PyInt_AsLongLong
326#else
327#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
Antoine Pitrou794b3fc2009-05-23 15:47:13 +0000328#endif /* SIZEOF_PID_T */
329
Greg Wardb48bc172000-03-01 21:51:56 +0000330/* Don't use the "_r" form if we don't need it (also, won't have a
331 prototype for it, at least on Solaris -- maybe others as well?). */
332#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
333#define USE_CTERMID_R
334#endif
335
336#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
337#define USE_TMPNAM_R
338#endif
339
Fred Drake699f3522000-06-29 21:12:41 +0000340/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000341#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000342#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000343# define STAT win32_stat
344# define FSTAT win32_fstat
345# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000346#else
347# define STAT stat
348# define FSTAT fstat
349# define STRUCT_STAT struct stat
350#endif
351
Tim Peters11b23062003-04-23 02:39:17 +0000352#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000353#include <sys/mkdev.h>
354#else
355#if defined(MAJOR_IN_SYSMACROS)
356#include <sys/sysmacros.h>
357#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000358#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
359#include <sys/mkdev.h>
360#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000361#endif
Fred Drake699f3522000-06-29 21:12:41 +0000362
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000364#ifdef WITH_NEXT_FRAMEWORK
365/* On Darwin/MacOSX a shared library or framework has no access to
366** environ directly, we must obtain it with _NSGetEnviron().
367*/
368#include <crt_externs.h>
369static char **environ;
370#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000372#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373
Barry Warsaw53699e91996-12-10 23:23:01 +0000374static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000375convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376{
Barry Warsaw53699e91996-12-10 23:23:01 +0000377 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000379 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000380 if (d == NULL)
381 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000382#ifdef WITH_NEXT_FRAMEWORK
383 if (environ == NULL)
384 environ = *_NSGetEnviron();
385#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386 if (environ == NULL)
387 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000388 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000390 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000391 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392 char *p = strchr(*e, '=');
393 if (p == NULL)
394 continue;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000395 k = PyString_FromStringAndSize(*e, (int)(p-*e));
Guido van Rossum6a619f41999-08-03 19:41:10 +0000396 if (k == NULL) {
397 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000399 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000400 v = PyString_FromString(p+1);
Guido van Rossum6a619f41999-08-03 19:41:10 +0000401 if (v == NULL) {
402 PyErr_Clear();
403 Py_DECREF(k);
404 continue;
405 }
406 if (PyDict_GetItem(d, k) == NULL) {
407 if (PyDict_SetItem(d, k, v) != 0)
408 PyErr_Clear();
409 }
410 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000411 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000413#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000414 {
415 APIRET rc;
416 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
417
418 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000419 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000420 PyObject *v = PyString_FromString(buffer);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000421 PyDict_SetItemString(d, "BEGINLIBPATH", v);
422 Py_DECREF(v);
423 }
424 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
425 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000426 PyObject *v = PyString_FromString(buffer);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000427 PyDict_SetItemString(d, "ENDLIBPATH", v);
428 Py_DECREF(v);
429 }
430 }
431#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000432 return d;
433}
434
435
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436/* Set a POSIX-specific error from errno, and return NULL */
437
Barry Warsawd58d7641998-07-23 16:14:40 +0000438static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000439posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000440{
Barry Warsawca74da41999-02-09 19:31:45 +0000441 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442}
Barry Warsawd58d7641998-07-23 16:14:40 +0000443static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000444posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000445{
Barry Warsawca74da41999-02-09 19:31:45 +0000446 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000447}
448
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000449#ifdef Py_WIN_WIDE_FILENAMES
450static PyObject *
451posix_error_with_unicode_filename(Py_UNICODE* name)
452{
453 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
454}
455#endif /* Py_WIN_WIDE_FILENAMES */
456
457
Mark Hammondef8b6542001-05-13 08:04:26 +0000458static PyObject *
459posix_error_with_allocated_filename(char* name)
460{
461 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
462 PyMem_Free(name);
463 return rc;
464}
465
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000466#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000467static PyObject *
468win32_error(char* function, char* filename)
469{
Mark Hammond33a6da92000-08-15 00:46:38 +0000470 /* XXX We should pass the function name along in the future.
471 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000472 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000473 Windows error object, which is non-trivial.
474 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000475 errno = GetLastError();
476 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000477 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000478 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000479 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000480}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000481
482#ifdef Py_WIN_WIDE_FILENAMES
483static PyObject *
484win32_error_unicode(char* function, Py_UNICODE* filename)
485{
486 /* XXX - see win32_error for comments on 'function' */
487 errno = GetLastError();
488 if (filename)
489 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
490 else
491 return PyErr_SetFromWindowsErr(errno);
492}
493
494static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
495{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000496}
497
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000498static int
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000499convert_to_unicode(PyObject **param)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000500{
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000501 if (PyUnicode_CheckExact(*param))
502 Py_INCREF(*param);
503 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000504 /* For a Unicode subtype that's not a Unicode object,
505 return a true Unicode object with the same data. */
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000506 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
507 PyUnicode_GET_SIZE(*param));
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000508 else
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000509 *param = PyUnicode_FromEncodedObject(*param,
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000510 Py_FileSystemDefaultEncoding,
511 "strict");
512 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000513}
514
515#endif /* Py_WIN_WIDE_FILENAMES */
516
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000517#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518
Guido van Rossumd48f2521997-12-05 22:19:34 +0000519#if defined(PYOS_OS2)
520/**********************************************************************
521 * Helper Function to Trim and Format OS/2 Messages
522 **********************************************************************/
523 static void
524os2_formatmsg(char *msgbuf, int msglen, char *reason)
525{
526 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
527
528 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
529 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
530
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000531 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000532 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
533 }
534
535 /* Add Optional Reason Text */
536 if (reason) {
537 strcat(msgbuf, " : ");
538 strcat(msgbuf, reason);
539 }
540}
541
542/**********************************************************************
543 * Decode an OS/2 Operating System Error Code
544 *
545 * A convenience function to lookup an OS/2 error code and return a
546 * text message we can use to raise a Python exception.
547 *
548 * Notes:
549 * The messages for errors returned from the OS/2 kernel reside in
550 * the file OSO001.MSG in the \OS2 directory hierarchy.
551 *
552 **********************************************************************/
553 static char *
554os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
555{
556 APIRET rc;
557 ULONG msglen;
558
559 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
560 Py_BEGIN_ALLOW_THREADS
561 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
562 errorcode, "oso001.msg", &msglen);
563 Py_END_ALLOW_THREADS
564
565 if (rc == NO_ERROR)
566 os2_formatmsg(msgbuf, msglen, reason);
567 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000568 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000569 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000570
571 return msgbuf;
572}
573
574/* Set an OS/2-specific error and return NULL. OS/2 kernel
575 errors are not in a global variable e.g. 'errno' nor are
576 they congruent with posix error numbers. */
577
578static PyObject * os2_error(int code)
579{
580 char text[1024];
581 PyObject *v;
582
583 os2_strerror(text, sizeof(text), code, "");
584
585 v = Py_BuildValue("(is)", code, text);
586 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000587 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000588 Py_DECREF(v);
589 }
590 return NULL; /* Signal to Python that an Exception is Pending */
591}
592
593#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594
595/* POSIX generic methods */
596
Barry Warsaw53699e91996-12-10 23:23:01 +0000597static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000598posix_fildes(PyObject *fdobj, int (*func)(int))
599{
600 int fd;
601 int res;
602 fd = PyObject_AsFileDescriptor(fdobj);
603 if (fd < 0)
604 return NULL;
605 Py_BEGIN_ALLOW_THREADS
606 res = (*func)(fd);
607 Py_END_ALLOW_THREADS
608 if (res < 0)
609 return posix_error();
610 Py_INCREF(Py_None);
611 return Py_None;
612}
Guido van Rossum21142a01999-01-08 21:05:37 +0000613
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000614#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000615static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000616unicode_file_names(void)
617{
618 static int canusewide = -1;
619 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000620 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000621 the Windows NT family. */
622 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
623 }
624 return canusewide;
625}
626#endif
Tim Peters11b23062003-04-23 02:39:17 +0000627
Guido van Rossum21142a01999-01-08 21:05:37 +0000628static PyObject *
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000629posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630{
Mark Hammondef8b6542001-05-13 08:04:26 +0000631 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000632 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000633 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000634 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000635 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000636 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000637 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000638 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000639 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000640 return posix_error_with_allocated_filename(path1);
641 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000642 Py_INCREF(Py_None);
643 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644}
645
Barry Warsaw53699e91996-12-10 23:23:01 +0000646static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000647posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000648 char *format,
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000649 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000650{
Mark Hammondef8b6542001-05-13 08:04:26 +0000651 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000652 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000653 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000654 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000655 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000656 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000657 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000658 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000659 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000660 PyMem_Free(path1);
661 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000662 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000663 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000664 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000665 Py_INCREF(Py_None);
666 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000667}
668
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000669#ifdef Py_WIN_WIDE_FILENAMES
670static PyObject*
671win32_1str(PyObject* args, char* func,
672 char* format, BOOL (__stdcall *funcA)(LPCSTR),
673 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
674{
675 PyObject *uni;
676 char *ansi;
677 BOOL result;
678 if (unicode_file_names()) {
679 if (!PyArg_ParseTuple(args, wformat, &uni))
680 PyErr_Clear();
681 else {
682 Py_BEGIN_ALLOW_THREADS
683 result = funcW(PyUnicode_AsUnicode(uni));
684 Py_END_ALLOW_THREADS
685 if (!result)
686 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
687 Py_INCREF(Py_None);
688 return Py_None;
689 }
690 }
691 if (!PyArg_ParseTuple(args, format, &ansi))
692 return NULL;
693 Py_BEGIN_ALLOW_THREADS
694 result = funcA(ansi);
695 Py_END_ALLOW_THREADS
696 if (!result)
697 return win32_error(func, ansi);
698 Py_INCREF(Py_None);
699 return Py_None;
700
701}
702
703/* This is a reimplementation of the C library's chdir function,
704 but one that produces Win32 errors instead of DOS error codes.
705 chdir is essentially a wrapper around SetCurrentDirectory; however,
706 it also needs to set "magic" environment variables indicating
707 the per-drive current directory, which are of the form =<drive>: */
708BOOL __stdcall
709win32_chdir(LPCSTR path)
710{
711 char new_path[MAX_PATH+1];
712 int result;
713 char env[4] = "=x:";
714
715 if(!SetCurrentDirectoryA(path))
716 return FALSE;
717 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
718 if (!result)
719 return FALSE;
720 /* In the ANSI API, there should not be any paths longer
721 than MAX_PATH. */
722 assert(result <= MAX_PATH+1);
723 if (strncmp(new_path, "\\\\", 2) == 0 ||
724 strncmp(new_path, "//", 2) == 0)
725 /* UNC path, nothing to do. */
726 return TRUE;
727 env[1] = new_path[0];
728 return SetEnvironmentVariableA(env, new_path);
729}
730
731/* The Unicode version differs from the ANSI version
732 since the current directory might exceed MAX_PATH characters */
733BOOL __stdcall
734win32_wchdir(LPCWSTR path)
735{
736 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
737 int result;
738 wchar_t env[4] = L"=x:";
739
740 if(!SetCurrentDirectoryW(path))
741 return FALSE;
742 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
743 if (!result)
744 return FALSE;
745 if (result > MAX_PATH+1) {
Hirokazu Yamamoto2c66b7c2008-10-09 18:06:58 +0000746 new_path = malloc(result * sizeof(wchar_t));
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000747 if (!new_path) {
748 SetLastError(ERROR_OUTOFMEMORY);
749 return FALSE;
750 }
Hirokazu Yamamoto2c66b7c2008-10-09 18:06:58 +0000751 result = GetCurrentDirectoryW(result, new_path);
752 if (!result) {
753 free(new_path);
754 return FALSE;
755 }
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000756 }
757 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
758 wcsncmp(new_path, L"//", 2) == 0)
759 /* UNC path, nothing to do. */
760 return TRUE;
761 env[1] = new_path[0];
762 result = SetEnvironmentVariableW(env, new_path);
763 if (new_path != _new_path)
764 free(new_path);
765 return result;
766}
767#endif
768
Martin v. Löwis14694662006-02-03 12:54:16 +0000769#ifdef MS_WINDOWS
770/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
771 - time stamps are restricted to second resolution
772 - file modification times suffer from forth-and-back conversions between
773 UTC and local time
774 Therefore, we implement our own stat, based on the Win32 API directly.
775*/
776#define HAVE_STAT_NSEC 1
777
778struct win32_stat{
779 int st_dev;
780 __int64 st_ino;
781 unsigned short st_mode;
782 int st_nlink;
783 int st_uid;
784 int st_gid;
785 int st_rdev;
786 __int64 st_size;
787 int st_atime;
788 int st_atime_nsec;
789 int st_mtime;
790 int st_mtime_nsec;
791 int st_ctime;
792 int st_ctime_nsec;
793};
794
795static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
796
797static void
798FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
799{
Martin v. Löwis77c176d2006-05-12 17:22:04 +0000800 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
801 /* Cannot simply cast and dereference in_ptr,
802 since it might not be aligned properly */
803 __int64 in;
804 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000805 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
806 /* XXX Win32 supports time stamps past 2038; we currently don't */
807 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
808}
809
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000810static void
811time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
812{
813 /* XXX endianness */
814 __int64 out;
815 out = time_in + secs_between_epochs;
Martin v. Löwisf43893a2006-10-09 20:44:25 +0000816 out = out * 10000000 + nsec_in / 100;
Martin v. Löwis77c176d2006-05-12 17:22:04 +0000817 memcpy(out_ptr, &out, sizeof(out));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000818}
819
Martin v. Löwis14694662006-02-03 12:54:16 +0000820/* Below, we *know* that ugo+r is 0444 */
821#if _S_IREAD != 0400
822#error Unsupported C library
823#endif
824static int
825attributes_to_mode(DWORD attr)
826{
827 int m = 0;
828 if (attr & FILE_ATTRIBUTE_DIRECTORY)
829 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
830 else
831 m |= _S_IFREG;
832 if (attr & FILE_ATTRIBUTE_READONLY)
833 m |= 0444;
834 else
835 m |= 0666;
836 return m;
837}
838
839static int
840attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
841{
842 memset(result, 0, sizeof(*result));
843 result->st_mode = attributes_to_mode(info->dwFileAttributes);
844 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
845 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
846 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
847 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
848
849 return 0;
850}
851
Martin v. Löwis012bc722006-10-15 09:43:39 +0000852/* Emulate GetFileAttributesEx[AW] on Windows 95 */
853static int checked = 0;
854static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
855static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
856static void
857check_gfax()
858{
859 HINSTANCE hKernel32;
860 if (checked)
861 return;
862 checked = 1;
863 hKernel32 = GetModuleHandle("KERNEL32");
864 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
865 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
866}
867
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000868static BOOL
869attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
870{
871 HANDLE hFindFile;
872 WIN32_FIND_DATAA FileData;
873 hFindFile = FindFirstFileA(pszFile, &FileData);
874 if (hFindFile == INVALID_HANDLE_VALUE)
875 return FALSE;
876 FindClose(hFindFile);
877 pfad->dwFileAttributes = FileData.dwFileAttributes;
878 pfad->ftCreationTime = FileData.ftCreationTime;
879 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
880 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
881 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
882 pfad->nFileSizeLow = FileData.nFileSizeLow;
883 return TRUE;
884}
885
886static BOOL
887attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
888{
889 HANDLE hFindFile;
890 WIN32_FIND_DATAW FileData;
891 hFindFile = FindFirstFileW(pszFile, &FileData);
892 if (hFindFile == INVALID_HANDLE_VALUE)
893 return FALSE;
894 FindClose(hFindFile);
895 pfad->dwFileAttributes = FileData.dwFileAttributes;
896 pfad->ftCreationTime = FileData.ftCreationTime;
897 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
898 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
899 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
900 pfad->nFileSizeLow = FileData.nFileSizeLow;
901 return TRUE;
902}
903
Martin v. Löwis012bc722006-10-15 09:43:39 +0000904static BOOL WINAPI
905Py_GetFileAttributesExA(LPCSTR pszFile,
906 GET_FILEEX_INFO_LEVELS level,
907 LPVOID pv)
908{
909 BOOL result;
Martin v. Löwis012bc722006-10-15 09:43:39 +0000910 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
911 /* First try to use the system's implementation, if that is
912 available and either succeeds to gives an error other than
913 that it isn't implemented. */
914 check_gfax();
915 if (gfaxa) {
916 result = gfaxa(pszFile, level, pv);
917 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
918 return result;
919 }
920 /* It's either not present, or not implemented.
921 Emulate using FindFirstFile. */
922 if (level != GetFileExInfoStandard) {
923 SetLastError(ERROR_INVALID_PARAMETER);
924 return FALSE;
925 }
926 /* Use GetFileAttributes to validate that the file name
927 does not contain wildcards (which FindFirstFile would
928 accept). */
929 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
930 return FALSE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000931 return attributes_from_dir(pszFile, pfad);
Martin v. Löwis012bc722006-10-15 09:43:39 +0000932}
933
934static BOOL WINAPI
935Py_GetFileAttributesExW(LPCWSTR pszFile,
936 GET_FILEEX_INFO_LEVELS level,
937 LPVOID pv)
938{
939 BOOL result;
Martin v. Löwis012bc722006-10-15 09:43:39 +0000940 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
941 /* First try to use the system's implementation, if that is
942 available and either succeeds to gives an error other than
943 that it isn't implemented. */
944 check_gfax();
945 if (gfaxa) {
946 result = gfaxw(pszFile, level, pv);
947 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
948 return result;
949 }
950 /* It's either not present, or not implemented.
951 Emulate using FindFirstFile. */
952 if (level != GetFileExInfoStandard) {
953 SetLastError(ERROR_INVALID_PARAMETER);
954 return FALSE;
955 }
956 /* Use GetFileAttributes to validate that the file name
957 does not contain wildcards (which FindFirstFile would
958 accept). */
959 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
960 return FALSE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000961 return attributes_from_dir_w(pszFile, pfad);
Martin v. Löwis012bc722006-10-15 09:43:39 +0000962}
963
Martin v. Löwis14694662006-02-03 12:54:16 +0000964static int
965win32_stat(const char* path, struct win32_stat *result)
966{
967 WIN32_FILE_ATTRIBUTE_DATA info;
968 int code;
969 char *dot;
970 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis012bc722006-10-15 09:43:39 +0000971 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000972 if (GetLastError() != ERROR_SHARING_VIOLATION) {
973 /* Protocol violation: we explicitly clear errno, instead of
974 setting it to a POSIX error. Callers should use GetLastError. */
975 errno = 0;
976 return -1;
977 } else {
978 /* Could not get attributes on open file. Fall back to
979 reading the directory. */
980 if (!attributes_from_dir(path, &info)) {
981 /* Very strange. This should not fail now */
982 errno = 0;
983 return -1;
984 }
985 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000986 }
987 code = attribute_data_to_stat(&info, result);
988 if (code != 0)
989 return code;
990 /* Set S_IFEXEC if it is an .exe, .bat, ... */
991 dot = strrchr(path, '.');
992 if (dot) {
993 if (stricmp(dot, ".bat") == 0 ||
994 stricmp(dot, ".cmd") == 0 ||
995 stricmp(dot, ".exe") == 0 ||
996 stricmp(dot, ".com") == 0)
997 result->st_mode |= 0111;
998 }
999 return code;
1000}
1001
1002static int
1003win32_wstat(const wchar_t* path, struct win32_stat *result)
1004{
1005 int code;
1006 const wchar_t *dot;
1007 WIN32_FILE_ATTRIBUTE_DATA info;
1008 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis012bc722006-10-15 09:43:39 +00001009 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis3bf573f2007-04-04 18:30:36 +00001010 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1011 /* Protocol violation: we explicitly clear errno, instead of
1012 setting it to a POSIX error. Callers should use GetLastError. */
1013 errno = 0;
1014 return -1;
1015 } else {
1016 /* Could not get attributes on open file. Fall back to
1017 reading the directory. */
1018 if (!attributes_from_dir_w(path, &info)) {
1019 /* Very strange. This should not fail now */
1020 errno = 0;
1021 return -1;
1022 }
1023 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001024 }
1025 code = attribute_data_to_stat(&info, result);
1026 if (code < 0)
1027 return code;
1028 /* Set IFEXEC if it is an .exe, .bat, ... */
1029 dot = wcsrchr(path, '.');
1030 if (dot) {
1031 if (_wcsicmp(dot, L".bat") == 0 ||
1032 _wcsicmp(dot, L".cmd") == 0 ||
1033 _wcsicmp(dot, L".exe") == 0 ||
1034 _wcsicmp(dot, L".com") == 0)
1035 result->st_mode |= 0111;
1036 }
1037 return code;
1038}
1039
1040static int
1041win32_fstat(int file_number, struct win32_stat *result)
1042{
1043 BY_HANDLE_FILE_INFORMATION info;
1044 HANDLE h;
1045 int type;
1046
1047 h = (HANDLE)_get_osfhandle(file_number);
1048
1049 /* Protocol violation: we explicitly clear errno, instead of
1050 setting it to a POSIX error. Callers should use GetLastError. */
1051 errno = 0;
1052
1053 if (h == INVALID_HANDLE_VALUE) {
1054 /* This is really a C library error (invalid file handle).
1055 We set the Win32 error to the closes one matching. */
1056 SetLastError(ERROR_INVALID_HANDLE);
1057 return -1;
1058 }
1059 memset(result, 0, sizeof(*result));
1060
1061 type = GetFileType(h);
1062 if (type == FILE_TYPE_UNKNOWN) {
1063 DWORD error = GetLastError();
1064 if (error != 0) {
1065 return -1;
1066 }
1067 /* else: valid but unknown file */
1068 }
1069
1070 if (type != FILE_TYPE_DISK) {
1071 if (type == FILE_TYPE_CHAR)
1072 result->st_mode = _S_IFCHR;
1073 else if (type == FILE_TYPE_PIPE)
1074 result->st_mode = _S_IFIFO;
1075 return 0;
1076 }
1077
1078 if (!GetFileInformationByHandle(h, &info)) {
1079 return -1;
1080 }
1081
1082 /* similar to stat() */
1083 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1084 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1085 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1086 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1087 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1088 /* specific to fstat() */
1089 result->st_nlink = info.nNumberOfLinks;
1090 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1091 return 0;
1092}
1093
1094#endif /* MS_WINDOWS */
1095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001096PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001097"stat_result: Result from stat or lstat.\n\n\
1098This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001099 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001100or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1101\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001102Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1103or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001104\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001105See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001106
1107static PyStructSequence_Field stat_result_fields[] = {
1108 {"st_mode", "protection bits"},
1109 {"st_ino", "inode"},
1110 {"st_dev", "device"},
1111 {"st_nlink", "number of hard links"},
1112 {"st_uid", "user ID of owner"},
1113 {"st_gid", "group ID of owner"},
1114 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001115 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1116 {NULL, "integer time of last access"},
1117 {NULL, "integer time of last modification"},
1118 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001119 {"st_atime", "time of last access"},
1120 {"st_mtime", "time of last modification"},
1121 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001122#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001123 {"st_blksize", "blocksize for filesystem I/O"},
1124#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001125#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001126 {"st_blocks", "number of blocks allocated"},
1127#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001128#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001129 {"st_rdev", "device type (if inode device)"},
1130#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001131#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1132 {"st_flags", "user defined flags for file"},
1133#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001134#ifdef HAVE_STRUCT_STAT_ST_GEN
1135 {"st_gen", "generation number"},
1136#endif
1137#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1138 {"st_birthtime", "time of creation"},
1139#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001140 {0}
1141};
1142
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001143#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001144#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001145#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001146#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001147#endif
1148
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001149#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001150#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1151#else
1152#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1153#endif
1154
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001155#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001156#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1157#else
1158#define ST_RDEV_IDX ST_BLOCKS_IDX
1159#endif
1160
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001161#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1162#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1163#else
1164#define ST_FLAGS_IDX ST_RDEV_IDX
1165#endif
1166
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001167#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001168#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001169#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001170#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001171#endif
1172
1173#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1174#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1175#else
1176#define ST_BIRTHTIME_IDX ST_GEN_IDX
1177#endif
1178
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001179static PyStructSequence_Desc stat_result_desc = {
1180 "stat_result", /* name */
1181 stat_result__doc__, /* doc */
1182 stat_result_fields,
1183 10
1184};
1185
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001186PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001187"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1188This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001189 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001190or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001191\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001192See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001193
1194static PyStructSequence_Field statvfs_result_fields[] = {
1195 {"f_bsize", },
1196 {"f_frsize", },
1197 {"f_blocks", },
1198 {"f_bfree", },
1199 {"f_bavail", },
1200 {"f_files", },
1201 {"f_ffree", },
1202 {"f_favail", },
1203 {"f_flag", },
1204 {"f_namemax",},
1205 {0}
1206};
1207
1208static PyStructSequence_Desc statvfs_result_desc = {
1209 "statvfs_result", /* name */
1210 statvfs_result__doc__, /* doc */
1211 statvfs_result_fields,
1212 10
1213};
1214
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00001215static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001216static PyTypeObject StatResultType;
1217static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001218static newfunc structseq_new;
1219
1220static PyObject *
1221statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1222{
1223 PyStructSequence *result;
1224 int i;
1225
1226 result = (PyStructSequence*)structseq_new(type, args, kwds);
1227 if (!result)
1228 return NULL;
1229 /* If we have been initialized from a tuple,
1230 st_?time might be set to None. Initialize it
1231 from the int slots. */
1232 for (i = 7; i <= 9; i++) {
1233 if (result->ob_item[i+3] == Py_None) {
1234 Py_DECREF(Py_None);
1235 Py_INCREF(result->ob_item[i]);
1236 result->ob_item[i+3] = result->ob_item[i];
1237 }
1238 }
1239 return (PyObject*)result;
1240}
1241
1242
1243
1244/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001245static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001246
1247PyDoc_STRVAR(stat_float_times__doc__,
1248"stat_float_times([newval]) -> oldval\n\n\
1249Determine whether os.[lf]stat represents time stamps as float objects.\n\
1250If newval is True, future calls to stat() return floats, if it is False,\n\
1251future calls return ints. \n\
1252If newval is omitted, return the current setting.\n");
1253
1254static PyObject*
1255stat_float_times(PyObject* self, PyObject *args)
1256{
1257 int newval = -1;
1258 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1259 return NULL;
1260 if (newval == -1)
1261 /* Return old value */
1262 return PyBool_FromLong(_stat_float_times);
1263 _stat_float_times = newval;
1264 Py_INCREF(Py_None);
1265 return Py_None;
1266}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001267
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001268static void
1269fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1270{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001271 PyObject *fval,*ival;
1272#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001273 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001274#else
1275 ival = PyInt_FromLong((long)sec);
1276#endif
Neal Norwitze0a81af2006-08-12 01:50:38 +00001277 if (!ival)
1278 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001279 if (_stat_float_times) {
1280 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1281 } else {
1282 fval = ival;
1283 Py_INCREF(fval);
1284 }
1285 PyStructSequence_SET_ITEM(v, index, ival);
1286 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001287}
1288
Tim Peters5aa91602002-01-30 05:46:57 +00001289/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001290 (used by posix_stat() and posix_fstat()) */
1291static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001292_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001293{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001294 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001295 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001296 if (v == NULL)
1297 return NULL;
1298
Martin v. Löwis14694662006-02-03 12:54:16 +00001299 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001300#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001301 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001302 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001303#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001304 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001305#endif
1306#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001307 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001308 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001309#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001310 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001311#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001312 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1313 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1314 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001315#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001316 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001317 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001318#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001319 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001320#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001321
Martin v. Löwis14694662006-02-03 12:54:16 +00001322#if defined(HAVE_STAT_TV_NSEC)
1323 ansec = st->st_atim.tv_nsec;
1324 mnsec = st->st_mtim.tv_nsec;
1325 cnsec = st->st_ctim.tv_nsec;
1326#elif defined(HAVE_STAT_TV_NSEC2)
1327 ansec = st->st_atimespec.tv_nsec;
1328 mnsec = st->st_mtimespec.tv_nsec;
1329 cnsec = st->st_ctimespec.tv_nsec;
1330#elif defined(HAVE_STAT_NSEC)
1331 ansec = st->st_atime_nsec;
1332 mnsec = st->st_mtime_nsec;
1333 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001334#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001335 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001336#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001337 fill_time(v, 7, st->st_atime, ansec);
1338 fill_time(v, 8, st->st_mtime, mnsec);
1339 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001340
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001341#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001342 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001343 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001344#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001345#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001346 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001347 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001348#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001349#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001350 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001351 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001352#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001353#ifdef HAVE_STRUCT_STAT_ST_GEN
1354 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001355 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001356#endif
1357#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1358 {
1359 PyObject *val;
1360 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001361 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001362#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001363 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001364#else
1365 bnsec = 0;
1366#endif
1367 if (_stat_float_times) {
1368 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1369 } else {
1370 val = PyInt_FromLong((long)bsec);
1371 }
1372 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1373 val);
1374 }
1375#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001376#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1377 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001378 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001379#endif
Fred Drake699f3522000-06-29 21:12:41 +00001380
1381 if (PyErr_Occurred()) {
1382 Py_DECREF(v);
1383 return NULL;
1384 }
1385
1386 return v;
1387}
1388
Martin v. Löwisd8948722004-06-02 09:57:56 +00001389#ifdef MS_WINDOWS
1390
1391/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1392 where / can be used in place of \ and the trailing slash is optional.
1393 Both SERVER and SHARE must have at least one character.
1394*/
1395
1396#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1397#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001398#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001399#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001400#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001401
Tim Peters4ad82172004-08-30 17:02:04 +00001402static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001403IsUNCRootA(char *path, int pathlen)
1404{
1405 #define ISSLASH ISSLASHA
1406
1407 int i, share;
1408
1409 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1410 /* minimum UNCRoot is \\x\y */
1411 return FALSE;
1412 for (i = 2; i < pathlen ; i++)
1413 if (ISSLASH(path[i])) break;
1414 if (i == 2 || i == pathlen)
1415 /* do not allow \\\SHARE or \\SERVER */
1416 return FALSE;
1417 share = i+1;
1418 for (i = share; i < pathlen; i++)
1419 if (ISSLASH(path[i])) break;
1420 return (i != share && (i == pathlen || i == pathlen-1));
1421
1422 #undef ISSLASH
1423}
1424
1425#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001426static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001427IsUNCRootW(Py_UNICODE *path, int pathlen)
1428{
1429 #define ISSLASH ISSLASHW
1430
1431 int i, share;
1432
1433 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1434 /* minimum UNCRoot is \\x\y */
1435 return FALSE;
1436 for (i = 2; i < pathlen ; i++)
1437 if (ISSLASH(path[i])) break;
1438 if (i == 2 || i == pathlen)
1439 /* do not allow \\\SHARE or \\SERVER */
1440 return FALSE;
1441 share = i+1;
1442 for (i = share; i < pathlen; i++)
1443 if (ISSLASH(path[i])) break;
1444 return (i != share && (i == pathlen || i == pathlen-1));
1445
1446 #undef ISSLASH
1447}
1448#endif /* Py_WIN_WIDE_FILENAMES */
1449#endif /* MS_WINDOWS */
1450
Barry Warsaw53699e91996-12-10 23:23:01 +00001451static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001452posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001453 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001454#ifdef __VMS
1455 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1456#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001457 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001458#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001459 char *wformat,
1460 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001461{
Fred Drake699f3522000-06-29 21:12:41 +00001462 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001463 char *path = NULL; /* pass this to stat; do not free() it */
1464 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001465 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001466 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001467
1468#ifdef Py_WIN_WIDE_FILENAMES
1469 /* If on wide-character-capable OS see if argument
1470 is Unicode and if so use wide API. */
1471 if (unicode_file_names()) {
1472 PyUnicodeObject *po;
1473 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001474 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1475
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001476 Py_BEGIN_ALLOW_THREADS
1477 /* PyUnicode_AS_UNICODE result OK without
1478 thread lock as it is a simple dereference. */
1479 res = wstatfunc(wpath, &st);
1480 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001481
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001482 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001483 return win32_error_unicode("stat", wpath);
1484 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001485 }
1486 /* Drop the argument parsing error as narrow strings
1487 are also valid. */
1488 PyErr_Clear();
1489 }
1490#endif
1491
Tim Peters5aa91602002-01-30 05:46:57 +00001492 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001493 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001494 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001495 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001496
Barry Warsaw53699e91996-12-10 23:23:01 +00001497 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001498 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001499 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001500
1501 if (res != 0) {
1502#ifdef MS_WINDOWS
1503 result = win32_error("stat", pathfree);
1504#else
1505 result = posix_error_with_filename(pathfree);
1506#endif
1507 }
1508 else
1509 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001510
Tim Peters500bd032001-12-19 19:05:01 +00001511 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001512 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001513}
1514
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001515/* POSIX methods */
1516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001517PyDoc_STRVAR(posix_access__doc__,
Georg Brandl7a284472007-01-27 19:38:50 +00001518"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001519Use the real uid/gid to test for access to a path. Note that most\n\
1520operations will use the effective uid/gid, therefore this routine can\n\
1521be used in a suid/sgid environment to test if the invoking user has the\n\
1522specified access to the path. The mode argument can be F_OK to test\n\
1523existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001524
1525static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001526posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001527{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001528 char *path;
1529 int mode;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001530
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001531#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001532 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001533 if (unicode_file_names()) {
1534 PyUnicodeObject *po;
1535 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1536 Py_BEGIN_ALLOW_THREADS
1537 /* PyUnicode_AS_UNICODE OK without thread lock as
1538 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001539 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001540 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001541 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001542 }
1543 /* Drop the argument parsing error as narrow strings
1544 are also valid. */
1545 PyErr_Clear();
1546 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001547 if (!PyArg_ParseTuple(args, "eti:access",
1548 Py_FileSystemDefaultEncoding, &path, &mode))
1549 return 0;
1550 Py_BEGIN_ALLOW_THREADS
1551 attr = GetFileAttributesA(path);
1552 Py_END_ALLOW_THREADS
1553 PyMem_Free(path);
1554finish:
1555 if (attr == 0xFFFFFFFF)
1556 /* File does not exist, or cannot read attributes */
1557 return PyBool_FromLong(0);
1558 /* Access is possible if either write access wasn't requested, or
Martin v. Löwis7b3cc062007-12-03 23:09:04 +00001559 the file isn't read-only, or if it's a directory, as there are
1560 no read-only directories on Windows. */
1561 return PyBool_FromLong(!(mode & 2)
1562 || !(attr & FILE_ATTRIBUTE_READONLY)
1563 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001564#else
1565 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001566 if (!PyArg_ParseTuple(args, "eti:access",
1567 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001568 return NULL;
1569 Py_BEGIN_ALLOW_THREADS
1570 res = access(path, mode);
1571 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001572 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001573 return PyBool_FromLong(res == 0);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001574#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001575}
1576
Guido van Rossumd371ff11999-01-25 16:12:23 +00001577#ifndef F_OK
1578#define F_OK 0
1579#endif
1580#ifndef R_OK
1581#define R_OK 4
1582#endif
1583#ifndef W_OK
1584#define W_OK 2
1585#endif
1586#ifndef X_OK
1587#define X_OK 1
1588#endif
1589
1590#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001591PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001592"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001593Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001594
1595static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001596posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001597{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001598 int id;
1599 char *ret;
1600
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001601 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001602 return NULL;
1603
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001604#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001605 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001606 if (id == 0) {
1607 ret = ttyname();
1608 }
1609 else {
1610 ret = NULL;
1611 }
1612#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001613 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001614#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001615 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001616 return posix_error();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001617 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001618}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001619#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001620
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001621#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001622PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001623"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001624Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001625
1626static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001627posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001628{
1629 char *ret;
1630 char buffer[L_ctermid];
1631
Greg Wardb48bc172000-03-01 21:51:56 +00001632#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001633 ret = ctermid_r(buffer);
1634#else
1635 ret = ctermid(buffer);
1636#endif
1637 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001638 return posix_error();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001639 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001640}
1641#endif
1642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001643PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001644"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001645Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001646
Barry Warsaw53699e91996-12-10 23:23:01 +00001647static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001648posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001649{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001650#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001651 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001652#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001653 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001654#elif defined(__VMS)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001655 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001656#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001657 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001658#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001659}
1660
Fred Drake4d1e64b2002-04-15 19:40:07 +00001661#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001662PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001663"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001664Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001665opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001666
1667static PyObject *
1668posix_fchdir(PyObject *self, PyObject *fdobj)
1669{
1670 return posix_fildes(fdobj, fchdir);
1671}
1672#endif /* HAVE_FCHDIR */
1673
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001674
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001675PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001676"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001677Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001678
Barry Warsaw53699e91996-12-10 23:23:01 +00001679static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001680posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001681{
Mark Hammondef8b6542001-05-13 08:04:26 +00001682 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001683 int i;
1684 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001685#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001686 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001687 if (unicode_file_names()) {
1688 PyUnicodeObject *po;
1689 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1690 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001691 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1692 if (attr != 0xFFFFFFFF) {
1693 if (i & _S_IWRITE)
1694 attr &= ~FILE_ATTRIBUTE_READONLY;
1695 else
1696 attr |= FILE_ATTRIBUTE_READONLY;
1697 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1698 }
1699 else
1700 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001701 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001702 if (!res)
1703 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001704 PyUnicode_AS_UNICODE(po));
1705 Py_INCREF(Py_None);
1706 return Py_None;
1707 }
1708 /* Drop the argument parsing error as narrow strings
1709 are also valid. */
1710 PyErr_Clear();
1711 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001712 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1713 &path, &i))
1714 return NULL;
1715 Py_BEGIN_ALLOW_THREADS
1716 attr = GetFileAttributesA(path);
1717 if (attr != 0xFFFFFFFF) {
1718 if (i & _S_IWRITE)
1719 attr &= ~FILE_ATTRIBUTE_READONLY;
1720 else
1721 attr |= FILE_ATTRIBUTE_READONLY;
1722 res = SetFileAttributesA(path, attr);
1723 }
1724 else
1725 res = 0;
1726 Py_END_ALLOW_THREADS
1727 if (!res) {
1728 win32_error("chmod", path);
1729 PyMem_Free(path);
1730 return NULL;
1731 }
Martin v. Löwis9f485bc2006-05-08 05:25:56 +00001732 PyMem_Free(path);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001733 Py_INCREF(Py_None);
1734 return Py_None;
1735#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001736 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001737 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001738 return NULL;
1739 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001740 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001741 Py_END_ALLOW_THREADS
1742 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001743 return posix_error_with_allocated_filename(path);
1744 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001745 Py_INCREF(Py_None);
1746 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001747#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001748}
1749
Christian Heimes36281872007-11-30 21:11:28 +00001750#ifdef HAVE_FCHMOD
1751PyDoc_STRVAR(posix_fchmod__doc__,
1752"fchmod(fd, mode)\n\n\
1753Change the access permissions of the file given by file\n\
1754descriptor fd.");
1755
1756static PyObject *
1757posix_fchmod(PyObject *self, PyObject *args)
1758{
1759 int fd, mode, res;
1760 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1761 return NULL;
1762 Py_BEGIN_ALLOW_THREADS
1763 res = fchmod(fd, mode);
1764 Py_END_ALLOW_THREADS
1765 if (res < 0)
1766 return posix_error();
1767 Py_RETURN_NONE;
1768}
1769#endif /* HAVE_FCHMOD */
1770
1771#ifdef HAVE_LCHMOD
1772PyDoc_STRVAR(posix_lchmod__doc__,
1773"lchmod(path, mode)\n\n\
1774Change the access permissions of a file. If path is a symlink, this\n\
1775affects the link itself rather than the target.");
1776
1777static PyObject *
1778posix_lchmod(PyObject *self, PyObject *args)
1779{
1780 char *path = NULL;
1781 int i;
1782 int res;
1783 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1784 &path, &i))
1785 return NULL;
1786 Py_BEGIN_ALLOW_THREADS
1787 res = lchmod(path, i);
1788 Py_END_ALLOW_THREADS
1789 if (res < 0)
1790 return posix_error_with_allocated_filename(path);
1791 PyMem_Free(path);
1792 Py_RETURN_NONE;
1793}
1794#endif /* HAVE_LCHMOD */
1795
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001796
Martin v. Löwis382abef2007-02-19 10:55:19 +00001797#ifdef HAVE_CHFLAGS
1798PyDoc_STRVAR(posix_chflags__doc__,
1799"chflags(path, flags)\n\n\
1800Set file flags.");
1801
1802static PyObject *
1803posix_chflags(PyObject *self, PyObject *args)
1804{
1805 char *path;
1806 unsigned long flags;
1807 int res;
1808 if (!PyArg_ParseTuple(args, "etk:chflags",
1809 Py_FileSystemDefaultEncoding, &path, &flags))
1810 return NULL;
1811 Py_BEGIN_ALLOW_THREADS
1812 res = chflags(path, flags);
1813 Py_END_ALLOW_THREADS
1814 if (res < 0)
1815 return posix_error_with_allocated_filename(path);
1816 PyMem_Free(path);
1817 Py_INCREF(Py_None);
1818 return Py_None;
1819}
1820#endif /* HAVE_CHFLAGS */
1821
1822#ifdef HAVE_LCHFLAGS
1823PyDoc_STRVAR(posix_lchflags__doc__,
1824"lchflags(path, flags)\n\n\
1825Set file flags.\n\
1826This function will not follow symbolic links.");
1827
1828static PyObject *
1829posix_lchflags(PyObject *self, PyObject *args)
1830{
1831 char *path;
1832 unsigned long flags;
1833 int res;
1834 if (!PyArg_ParseTuple(args, "etk:lchflags",
1835 Py_FileSystemDefaultEncoding, &path, &flags))
1836 return NULL;
1837 Py_BEGIN_ALLOW_THREADS
1838 res = lchflags(path, flags);
1839 Py_END_ALLOW_THREADS
1840 if (res < 0)
1841 return posix_error_with_allocated_filename(path);
1842 PyMem_Free(path);
1843 Py_INCREF(Py_None);
1844 return Py_None;
1845}
1846#endif /* HAVE_LCHFLAGS */
1847
Martin v. Löwis244edc82001-10-04 22:44:26 +00001848#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001849PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001850"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001851Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001852
1853static PyObject *
1854posix_chroot(PyObject *self, PyObject *args)
1855{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001856 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001857}
1858#endif
1859
Guido van Rossum21142a01999-01-08 21:05:37 +00001860#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001861PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001862"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001863force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001864
1865static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001866posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001867{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001868 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001869}
1870#endif /* HAVE_FSYNC */
1871
1872#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001873
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001874#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001875extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1876#endif
1877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001878PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001879"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001880force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001881 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001882
1883static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001884posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001885{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001886 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001887}
1888#endif /* HAVE_FDATASYNC */
1889
1890
Fredrik Lundh10723342000-07-10 16:38:09 +00001891#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001892PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001893"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001894Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001895
Barry Warsaw53699e91996-12-10 23:23:01 +00001896static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001897posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001898{
Mark Hammondef8b6542001-05-13 08:04:26 +00001899 char *path = NULL;
Gregory P. Smithf48da8f2008-03-18 19:05:32 +00001900 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001901 int res;
Gregory P. Smithf48da8f2008-03-18 19:05:32 +00001902 if (!PyArg_ParseTuple(args, "etll:chown",
Tim Peters5aa91602002-01-30 05:46:57 +00001903 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001904 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001905 return NULL;
1906 Py_BEGIN_ALLOW_THREADS
1907 res = chown(path, (uid_t) uid, (gid_t) gid);
1908 Py_END_ALLOW_THREADS
1909 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001910 return posix_error_with_allocated_filename(path);
1911 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001912 Py_INCREF(Py_None);
1913 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001914}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001915#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001916
Christian Heimes36281872007-11-30 21:11:28 +00001917#ifdef HAVE_FCHOWN
1918PyDoc_STRVAR(posix_fchown__doc__,
1919"fchown(fd, uid, gid)\n\n\
1920Change the owner and group id of the file given by file descriptor\n\
1921fd to the numeric uid and gid.");
1922
1923static PyObject *
1924posix_fchown(PyObject *self, PyObject *args)
1925{
1926 int fd, uid, gid;
1927 int res;
1928 if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid))
1929 return NULL;
1930 Py_BEGIN_ALLOW_THREADS
1931 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1932 Py_END_ALLOW_THREADS
1933 if (res < 0)
1934 return posix_error();
1935 Py_RETURN_NONE;
1936}
1937#endif /* HAVE_FCHOWN */
1938
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001939#ifdef HAVE_LCHOWN
1940PyDoc_STRVAR(posix_lchown__doc__,
1941"lchown(path, uid, gid)\n\n\
1942Change the owner and group id of path to the numeric uid and gid.\n\
1943This function will not follow symbolic links.");
1944
1945static PyObject *
1946posix_lchown(PyObject *self, PyObject *args)
1947{
1948 char *path = NULL;
1949 int uid, gid;
1950 int res;
1951 if (!PyArg_ParseTuple(args, "etii:lchown",
1952 Py_FileSystemDefaultEncoding, &path,
1953 &uid, &gid))
1954 return NULL;
1955 Py_BEGIN_ALLOW_THREADS
1956 res = lchown(path, (uid_t) uid, (gid_t) gid);
1957 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001958 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001959 return posix_error_with_allocated_filename(path);
1960 PyMem_Free(path);
1961 Py_INCREF(Py_None);
1962 return Py_None;
1963}
1964#endif /* HAVE_LCHOWN */
1965
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001966
Guido van Rossum36bc6801995-06-14 22:54:23 +00001967#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001968PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001969"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001970Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001971
Barry Warsaw53699e91996-12-10 23:23:01 +00001972static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001973posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001974{
Facundo Batista5596b0c2008-06-22 13:36:20 +00001975 int bufsize_incr = 1024;
1976 int bufsize = 0;
1977 char *tmpbuf = NULL;
1978 char *res = NULL;
1979 PyObject *dynamic_return;
Neal Norwitze241ce82003-02-17 18:17:05 +00001980
Barry Warsaw53699e91996-12-10 23:23:01 +00001981 Py_BEGIN_ALLOW_THREADS
Facundo Batista5596b0c2008-06-22 13:36:20 +00001982 do {
1983 bufsize = bufsize + bufsize_incr;
1984 tmpbuf = malloc(bufsize);
1985 if (tmpbuf == NULL) {
1986 break;
1987 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001988#if defined(PYOS_OS2) && defined(PYCC_GCC)
Facundo Batista5596b0c2008-06-22 13:36:20 +00001989 res = _getcwd2(tmpbuf, bufsize);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001990#else
Facundo Batista5596b0c2008-06-22 13:36:20 +00001991 res = getcwd(tmpbuf, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001992#endif
Facundo Batista5596b0c2008-06-22 13:36:20 +00001993
1994 if (res == NULL) {
1995 free(tmpbuf);
1996 }
1997 } while ((res == NULL) && (errno == ERANGE));
Barry Warsaw53699e91996-12-10 23:23:01 +00001998 Py_END_ALLOW_THREADS
Facundo Batista5596b0c2008-06-22 13:36:20 +00001999
Guido van Rossumff4949e1992-08-05 19:58:53 +00002000 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002001 return posix_error();
Facundo Batista5596b0c2008-06-22 13:36:20 +00002002
2003 dynamic_return = PyString_FromString(tmpbuf);
2004 free(tmpbuf);
2005
2006 return dynamic_return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002007}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002008
Walter Dörwald3b918c32002-11-21 20:18:46 +00002009#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002010PyDoc_STRVAR(posix_getcwdu__doc__,
2011"getcwdu() -> path\n\n\
2012Return a unicode string representing the current working directory.");
2013
2014static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002015posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002016{
2017 char buf[1026];
2018 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002019
2020#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002021 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002022 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002023 wchar_t wbuf[1026];
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002024 wchar_t *wbuf2 = wbuf;
2025 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002026 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002027 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2028 /* If the buffer is large enough, len does not include the
2029 terminating \0. If the buffer is too small, len includes
2030 the space needed for the terminator. */
2031 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2032 wbuf2 = malloc(len * sizeof(wchar_t));
2033 if (wbuf2)
2034 len = GetCurrentDirectoryW(len, wbuf2);
2035 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002036 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002037 if (!wbuf2) {
2038 PyErr_NoMemory();
2039 return NULL;
2040 }
2041 if (!len) {
2042 if (wbuf2 != wbuf) free(wbuf2);
2043 return win32_error("getcwdu", NULL);
2044 }
2045 resobj = PyUnicode_FromWideChar(wbuf2, len);
2046 if (wbuf2 != wbuf) free(wbuf2);
2047 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002048 }
2049#endif
2050
2051 Py_BEGIN_ALLOW_THREADS
2052#if defined(PYOS_OS2) && defined(PYCC_GCC)
2053 res = _getcwd2(buf, sizeof buf);
2054#else
2055 res = getcwd(buf, sizeof buf);
2056#endif
2057 Py_END_ALLOW_THREADS
2058 if (res == NULL)
2059 return posix_error();
2060 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
2061}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002062#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00002063#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002064
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002065
Guido van Rossumb6775db1994-08-01 11:34:53 +00002066#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002067PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002068"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002069Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002070
Barry Warsaw53699e91996-12-10 23:23:01 +00002071static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002072posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002073{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00002074 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002075}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002076#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002077
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002078
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002079PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002080"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002081Return a list containing the names of the entries in the directory.\n\
2082\n\
2083 path: path of directory to list\n\
2084\n\
2085The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002086entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002087
Barry Warsaw53699e91996-12-10 23:23:01 +00002088static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002089posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002090{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002091 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002092 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002093#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002094
Barry Warsaw53699e91996-12-10 23:23:01 +00002095 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002096 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002097 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002098 WIN32_FIND_DATA FileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002099 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002100 char *bufptr = namebuf;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002101 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002102
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002103#ifdef Py_WIN_WIDE_FILENAMES
2104 /* If on wide-character-capable OS see if argument
2105 is Unicode and if so use wide API. */
2106 if (unicode_file_names()) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002107 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002108 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2109 WIN32_FIND_DATAW wFileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002110 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002111 Py_UNICODE wch;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002112 /* Overallocate for \\*.*\0 */
2113 len = PyUnicode_GET_SIZE(po);
2114 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2115 if (!wnamebuf) {
2116 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002117 return NULL;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002118 }
2119 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2120 wch = len > 0 ? wnamebuf[len-1] : '\0';
2121 if (wch != L'/' && wch != L'\\' && wch != L':')
2122 wnamebuf[len++] = L'\\';
2123 wcscpy(wnamebuf + len, L"*.*");
2124 if ((d = PyList_New(0)) == NULL) {
2125 free(wnamebuf);
2126 return NULL;
2127 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002128 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2129 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002130 int error = GetLastError();
2131 if (error == ERROR_FILE_NOT_FOUND) {
2132 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002133 return d;
2134 }
2135 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002136 win32_error_unicode("FindFirstFileW", wnamebuf);
2137 free(wnamebuf);
2138 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002139 }
2140 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002141 /* Skip over . and .. */
2142 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2143 wcscmp(wFileData.cFileName, L"..") != 0) {
2144 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2145 if (v == NULL) {
2146 Py_DECREF(d);
2147 d = NULL;
2148 break;
2149 }
2150 if (PyList_Append(d, v) != 0) {
2151 Py_DECREF(v);
2152 Py_DECREF(d);
2153 d = NULL;
2154 break;
2155 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002156 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002157 }
Georg Brandl622927b2006-03-07 12:48:03 +00002158 Py_BEGIN_ALLOW_THREADS
2159 result = FindNextFileW(hFindFile, &wFileData);
2160 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00002161 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2162 it got to the end of the directory. */
2163 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2164 Py_DECREF(d);
2165 win32_error_unicode("FindNextFileW", wnamebuf);
2166 FindClose(hFindFile);
2167 free(wnamebuf);
2168 return NULL;
2169 }
Georg Brandl622927b2006-03-07 12:48:03 +00002170 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002171
2172 if (FindClose(hFindFile) == FALSE) {
2173 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002174 win32_error_unicode("FindClose", wnamebuf);
2175 free(wnamebuf);
2176 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002177 }
Martin v. Löwise3edaea2006-05-15 05:51:36 +00002178 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002179 return d;
2180 }
2181 /* Drop the argument parsing error as narrow strings
2182 are also valid. */
2183 PyErr_Clear();
2184 }
2185#endif
2186
Tim Peters5aa91602002-01-30 05:46:57 +00002187 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002188 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002189 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002190 if (len > 0) {
2191 char ch = namebuf[len-1];
2192 if (ch != SEP && ch != ALTSEP && ch != ':')
2193 namebuf[len++] = '/';
2194 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002195 strcpy(namebuf + len, "*.*");
2196
Barry Warsaw53699e91996-12-10 23:23:01 +00002197 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002198 return NULL;
2199
2200 hFindFile = FindFirstFile(namebuf, &FileData);
2201 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002202 int error = GetLastError();
2203 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002204 return d;
2205 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002206 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002207 }
2208 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002209 /* Skip over . and .. */
2210 if (strcmp(FileData.cFileName, ".") != 0 &&
2211 strcmp(FileData.cFileName, "..") != 0) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002212 v = PyString_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002213 if (v == NULL) {
2214 Py_DECREF(d);
2215 d = NULL;
2216 break;
2217 }
2218 if (PyList_Append(d, v) != 0) {
2219 Py_DECREF(v);
2220 Py_DECREF(d);
2221 d = NULL;
2222 break;
2223 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002224 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002225 }
Georg Brandl622927b2006-03-07 12:48:03 +00002226 Py_BEGIN_ALLOW_THREADS
2227 result = FindNextFile(hFindFile, &FileData);
2228 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00002229 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2230 it got to the end of the directory. */
2231 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2232 Py_DECREF(d);
2233 win32_error("FindNextFile", namebuf);
2234 FindClose(hFindFile);
2235 return NULL;
2236 }
Georg Brandl622927b2006-03-07 12:48:03 +00002237 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002238
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002239 if (FindClose(hFindFile) == FALSE) {
2240 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002241 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002242 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002243
2244 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002245
Tim Peters0bb44a42000-09-15 07:44:49 +00002246#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002247
2248#ifndef MAX_PATH
2249#define MAX_PATH CCHMAXPATH
2250#endif
2251 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002252 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002253 PyObject *d, *v;
2254 char namebuf[MAX_PATH+5];
2255 HDIR hdir = 1;
2256 ULONG srchcnt = 1;
2257 FILEFINDBUF3 ep;
2258 APIRET rc;
2259
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002260 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002261 return NULL;
2262 if (len >= MAX_PATH) {
2263 PyErr_SetString(PyExc_ValueError, "path too long");
2264 return NULL;
2265 }
2266 strcpy(namebuf, name);
2267 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002268 if (*pt == ALTSEP)
2269 *pt = SEP;
2270 if (namebuf[len-1] != SEP)
2271 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002272 strcpy(namebuf + len, "*.*");
2273
2274 if ((d = PyList_New(0)) == NULL)
2275 return NULL;
2276
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002277 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2278 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002279 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002280 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2281 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2282 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002283
2284 if (rc != NO_ERROR) {
2285 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002286 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002287 }
2288
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002289 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002290 do {
2291 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002292 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002293 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002294
2295 strcpy(namebuf, ep.achName);
2296
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002297 /* Leave Case of Name Alone -- In Native Form */
2298 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002299
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002300 v = PyString_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002301 if (v == NULL) {
2302 Py_DECREF(d);
2303 d = NULL;
2304 break;
2305 }
2306 if (PyList_Append(d, v) != 0) {
2307 Py_DECREF(v);
2308 Py_DECREF(d);
2309 d = NULL;
2310 break;
2311 }
2312 Py_DECREF(v);
2313 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2314 }
2315
2316 return d;
2317#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002318
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002319 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002320 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002321 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002322 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002323 int arg_is_unicode = 1;
2324
Georg Brandl05e89b82006-04-11 07:04:06 +00002325 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002326 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2327 arg_is_unicode = 0;
2328 PyErr_Clear();
2329 }
2330 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002331 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002332 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002333 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002334 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002335 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002336 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002337 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002338 return NULL;
2339 }
Georg Brandl622927b2006-03-07 12:48:03 +00002340 for (;;) {
Georg Brandl86cbf812008-07-16 21:31:41 +00002341 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002342 Py_BEGIN_ALLOW_THREADS
2343 ep = readdir(dirp);
2344 Py_END_ALLOW_THREADS
Georg Brandl86cbf812008-07-16 21:31:41 +00002345 if (ep == NULL) {
2346 if (errno == 0) {
2347 break;
2348 } else {
2349 closedir(dirp);
2350 Py_DECREF(d);
2351 return posix_error_with_allocated_filename(name);
2352 }
2353 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002354 if (ep->d_name[0] == '.' &&
2355 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002356 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002357 continue;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002358 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002359 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002360 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002361 d = NULL;
2362 break;
2363 }
Just van Rossum46c97842003-02-25 21:42:15 +00002364#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00002365 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002366 PyObject *w;
2367
2368 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002369 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002370 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002371 if (w != NULL) {
2372 Py_DECREF(v);
2373 v = w;
2374 }
2375 else {
2376 /* fall back to the original byte string, as
2377 discussed in patch #683592 */
2378 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002379 }
Just van Rossum46c97842003-02-25 21:42:15 +00002380 }
2381#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002382 if (PyList_Append(d, v) != 0) {
2383 Py_DECREF(v);
2384 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002385 d = NULL;
2386 break;
2387 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002388 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002389 }
2390 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002391 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002392
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002393 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002394
Tim Peters0bb44a42000-09-15 07:44:49 +00002395#endif /* which OS */
2396} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002397
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002398#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002399/* A helper function for abspath on win32 */
2400static PyObject *
2401posix__getfullpathname(PyObject *self, PyObject *args)
2402{
Jesus Cea585ad8a2009-07-02 15:37:21 +00002403 /* assume encoded strings won't more than double no of chars */
Mark Hammondef8b6542001-05-13 08:04:26 +00002404 char inbuf[MAX_PATH*2];
2405 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002406 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002407 char outbuf[MAX_PATH*2];
2408 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002409#ifdef Py_WIN_WIDE_FILENAMES
2410 if (unicode_file_names()) {
2411 PyUnicodeObject *po;
2412 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
Georg Brandlbb608a82008-12-05 08:35:09 +00002413 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2414 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002415 Py_UNICODE *wtemp;
Georg Brandlbb608a82008-12-05 08:35:09 +00002416 DWORD result;
2417 PyObject *v;
2418 result = GetFullPathNameW(wpath,
2419 sizeof(woutbuf)/sizeof(woutbuf[0]),
2420 woutbuf, &wtemp);
2421 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2422 woutbufp = malloc(result * sizeof(Py_UNICODE));
2423 if (!woutbufp)
2424 return PyErr_NoMemory();
2425 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2426 }
2427 if (result)
2428 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2429 else
2430 v = win32_error_unicode("GetFullPathNameW", wpath);
2431 if (woutbufp != woutbuf)
2432 free(woutbufp);
2433 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002434 }
2435 /* Drop the argument parsing error as narrow strings
2436 are also valid. */
2437 PyErr_Clear();
2438 }
2439#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002440 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2441 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002442 &insize))
2443 return NULL;
2444 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2445 outbuf, &temp))
2446 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002447 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2448 return PyUnicode_Decode(outbuf, strlen(outbuf),
2449 Py_FileSystemDefaultEncoding, NULL);
2450 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002451 return PyString_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002452} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002453#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002454
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002455PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002456"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002457Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002458
Barry Warsaw53699e91996-12-10 23:23:01 +00002459static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002460posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002461{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002462 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002463 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002464 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002465
2466#ifdef Py_WIN_WIDE_FILENAMES
2467 if (unicode_file_names()) {
2468 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002469 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002470 Py_BEGIN_ALLOW_THREADS
2471 /* PyUnicode_AS_UNICODE OK without thread lock as
2472 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002473 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002474 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002475 if (!res)
2476 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002477 Py_INCREF(Py_None);
2478 return Py_None;
2479 }
2480 /* Drop the argument parsing error as narrow strings
2481 are also valid. */
2482 PyErr_Clear();
2483 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002484 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2485 Py_FileSystemDefaultEncoding, &path, &mode))
2486 return NULL;
2487 Py_BEGIN_ALLOW_THREADS
2488 /* PyUnicode_AS_UNICODE OK without thread lock as
2489 it is a simple dereference. */
2490 res = CreateDirectoryA(path, NULL);
2491 Py_END_ALLOW_THREADS
2492 if (!res) {
2493 win32_error("mkdir", path);
2494 PyMem_Free(path);
2495 return NULL;
2496 }
2497 PyMem_Free(path);
2498 Py_INCREF(Py_None);
2499 return Py_None;
2500#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002501
Tim Peters5aa91602002-01-30 05:46:57 +00002502 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002503 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002504 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002505 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002506#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002507 res = mkdir(path);
2508#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002509 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002510#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002511 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002512 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002513 return posix_error_with_allocated_filename(path);
2514 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002515 Py_INCREF(Py_None);
2516 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002517#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002518}
2519
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002520
Neal Norwitz1818ed72006-03-26 00:29:48 +00002521/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2522#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002523#include <sys/resource.h>
2524#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002525
Neal Norwitz1818ed72006-03-26 00:29:48 +00002526
2527#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002528PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002529"nice(inc) -> new_priority\n\n\
2530Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002531
Barry Warsaw53699e91996-12-10 23:23:01 +00002532static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002533posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002534{
2535 int increment, value;
2536
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002537 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002538 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002539
2540 /* There are two flavours of 'nice': one that returns the new
2541 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002542 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2543 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002544
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002545 If we are of the nice family that returns the new priority, we
2546 need to clear errno before the call, and check if errno is filled
2547 before calling posix_error() on a returnvalue of -1, because the
2548 -1 may be the actual new priority! */
2549
2550 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002551 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002552#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002553 if (value == 0)
2554 value = getpriority(PRIO_PROCESS, 0);
2555#endif
2556 if (value == -1 && errno != 0)
2557 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002558 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002559 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002560}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002561#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002562
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002563PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002564"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002565Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002566
Barry Warsaw53699e91996-12-10 23:23:01 +00002567static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002568posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002569{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002570#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002571 PyObject *o1, *o2;
2572 char *p1, *p2;
2573 BOOL result;
2574 if (unicode_file_names()) {
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +00002575 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2576 goto error;
2577 if (!convert_to_unicode(&o1))
2578 goto error;
2579 if (!convert_to_unicode(&o2)) {
2580 Py_DECREF(o1);
2581 goto error;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002582 }
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +00002583 Py_BEGIN_ALLOW_THREADS
2584 result = MoveFileW(PyUnicode_AsUnicode(o1),
2585 PyUnicode_AsUnicode(o2));
2586 Py_END_ALLOW_THREADS
2587 Py_DECREF(o1);
2588 Py_DECREF(o2);
2589 if (!result)
2590 return win32_error("rename", NULL);
2591 Py_INCREF(Py_None);
2592 return Py_None;
2593error:
2594 PyErr_Clear();
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002595 }
2596 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2597 return NULL;
2598 Py_BEGIN_ALLOW_THREADS
2599 result = MoveFileA(p1, p2);
2600 Py_END_ALLOW_THREADS
2601 if (!result)
2602 return win32_error("rename", NULL);
2603 Py_INCREF(Py_None);
2604 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002605#else
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00002606 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002607#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002608}
2609
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002610
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002611PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002612"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002613Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002614
Barry Warsaw53699e91996-12-10 23:23:01 +00002615static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002616posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002617{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002618#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002619 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002620#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002621 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002622#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002623}
2624
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002625
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002626PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002627"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002628Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002629
Barry Warsaw53699e91996-12-10 23:23:01 +00002630static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002631posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002632{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002633#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002634 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002635#else
2636 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2637#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002638}
2639
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002640
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002641#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002642PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002643"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002644Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002645
Barry Warsaw53699e91996-12-10 23:23:01 +00002646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002647posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002648{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002649 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002650 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002651 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002652 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002653 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002654 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002655 Py_END_ALLOW_THREADS
2656 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002657}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002658#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002659
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002661PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002662"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002663Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002664
Barry Warsaw53699e91996-12-10 23:23:01 +00002665static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002666posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002667{
2668 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002669 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002670 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002671 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002672 if (i < 0)
2673 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002674 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002675}
2676
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002678PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002679"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002680Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002681
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002682PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002683"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002684Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002685
Barry Warsaw53699e91996-12-10 23:23:01 +00002686static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002687posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002688{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002689#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002690 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002691#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002692 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002693#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002694}
2695
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002696
Guido van Rossumb6775db1994-08-01 11:34:53 +00002697#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002698PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002699"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002700Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002701
Barry Warsaw53699e91996-12-10 23:23:01 +00002702static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002703posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002704{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002705 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002706 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002707
Barry Warsaw53699e91996-12-10 23:23:01 +00002708 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002709 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002710 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002711 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002712 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002713 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002714 u.sysname,
2715 u.nodename,
2716 u.release,
2717 u.version,
2718 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002719}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002720#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002721
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002722static int
2723extract_time(PyObject *t, long* sec, long* usec)
2724{
2725 long intval;
2726 if (PyFloat_Check(t)) {
2727 double tval = PyFloat_AsDouble(t);
Christian Heimese93237d2007-12-19 02:37:44 +00002728 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002729 if (!intobj)
2730 return -1;
2731 intval = PyInt_AsLong(intobj);
2732 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002733 if (intval == -1 && PyErr_Occurred())
2734 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002735 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002736 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002737 if (*usec < 0)
2738 /* If rounding gave us a negative number,
2739 truncate. */
2740 *usec = 0;
2741 return 0;
2742 }
2743 intval = PyInt_AsLong(t);
2744 if (intval == -1 && PyErr_Occurred())
2745 return -1;
2746 *sec = intval;
2747 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002748 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002749}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002751PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002752"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002753utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002754Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002755second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002756
Barry Warsaw53699e91996-12-10 23:23:01 +00002757static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002758posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002759{
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002760#ifdef Py_WIN_WIDE_FILENAMES
2761 PyObject *arg;
2762 PyUnicodeObject *obwpath;
2763 wchar_t *wpath = NULL;
2764 char *apath = NULL;
2765 HANDLE hFile;
2766 long atimesec, mtimesec, ausec, musec;
2767 FILETIME atime, mtime;
2768 PyObject *result = NULL;
2769
2770 if (unicode_file_names()) {
2771 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2772 wpath = PyUnicode_AS_UNICODE(obwpath);
2773 Py_BEGIN_ALLOW_THREADS
2774 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Martin v. Löwis18aaa562006-10-15 08:43:33 +00002775 NULL, OPEN_EXISTING,
2776 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002777 Py_END_ALLOW_THREADS
2778 if (hFile == INVALID_HANDLE_VALUE)
2779 return win32_error_unicode("utime", wpath);
2780 } else
2781 /* Drop the argument parsing error as narrow strings
2782 are also valid. */
2783 PyErr_Clear();
2784 }
2785 if (!wpath) {
2786 if (!PyArg_ParseTuple(args, "etO:utime",
2787 Py_FileSystemDefaultEncoding, &apath, &arg))
2788 return NULL;
2789 Py_BEGIN_ALLOW_THREADS
2790 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Martin v. Löwis18aaa562006-10-15 08:43:33 +00002791 NULL, OPEN_EXISTING,
2792 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002793 Py_END_ALLOW_THREADS
2794 if (hFile == INVALID_HANDLE_VALUE) {
2795 win32_error("utime", apath);
2796 PyMem_Free(apath);
2797 return NULL;
2798 }
2799 PyMem_Free(apath);
2800 }
2801
2802 if (arg == Py_None) {
2803 SYSTEMTIME now;
2804 GetSystemTime(&now);
2805 if (!SystemTimeToFileTime(&now, &mtime) ||
2806 !SystemTimeToFileTime(&now, &atime)) {
2807 win32_error("utime", NULL);
2808 goto done;
2809 }
2810 }
2811 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2812 PyErr_SetString(PyExc_TypeError,
2813 "utime() arg 2 must be a tuple (atime, mtime)");
2814 goto done;
2815 }
2816 else {
2817 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2818 &atimesec, &ausec) == -1)
2819 goto done;
Martin v. Löwisf43893a2006-10-09 20:44:25 +00002820 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002821 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2822 &mtimesec, &musec) == -1)
2823 goto done;
Martin v. Löwisf43893a2006-10-09 20:44:25 +00002824 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002825 }
2826 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2827 /* Avoid putting the file name into the error here,
2828 as that may confuse the user into believing that
2829 something is wrong with the file, when it also
2830 could be the time stamp that gives a problem. */
2831 win32_error("utime", NULL);
2832 }
2833 Py_INCREF(Py_None);
2834 result = Py_None;
2835done:
2836 CloseHandle(hFile);
2837 return result;
2838#else /* Py_WIN_WIDE_FILENAMES */
2839
Neal Norwitz2adf2102004-06-09 01:46:02 +00002840 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002841 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002842 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002843 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002844
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002845#if defined(HAVE_UTIMES)
2846 struct timeval buf[2];
2847#define ATIME buf[0].tv_sec
2848#define MTIME buf[1].tv_sec
2849#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002850/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002851 struct utimbuf buf;
2852#define ATIME buf.actime
2853#define MTIME buf.modtime
2854#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002855#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002856 time_t buf[2];
2857#define ATIME buf[0]
2858#define MTIME buf[1]
2859#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002860#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002861
Mark Hammond817c9292003-12-03 01:22:38 +00002862
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002863 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002864 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002865 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002866 if (arg == Py_None) {
2867 /* optional time values not given */
2868 Py_BEGIN_ALLOW_THREADS
2869 res = utime(path, NULL);
2870 Py_END_ALLOW_THREADS
2871 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002872 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002873 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002874 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002875 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002876 return NULL;
2877 }
2878 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002879 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002880 &atime, &ausec) == -1) {
2881 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002882 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002883 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002884 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002885 &mtime, &musec) == -1) {
2886 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002887 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002888 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002889 ATIME = atime;
2890 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002891#ifdef HAVE_UTIMES
2892 buf[0].tv_usec = ausec;
2893 buf[1].tv_usec = musec;
2894 Py_BEGIN_ALLOW_THREADS
2895 res = utimes(path, buf);
2896 Py_END_ALLOW_THREADS
2897#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002898 Py_BEGIN_ALLOW_THREADS
2899 res = utime(path, UTIME_ARG);
2900 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002901#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002902 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002903 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002904 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002905 }
Neal Norwitz96652712004-06-06 20:40:27 +00002906 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002907 Py_INCREF(Py_None);
2908 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002909#undef UTIME_ARG
2910#undef ATIME
2911#undef MTIME
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002912#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002913}
2914
Guido van Rossum85e3b011991-06-03 12:42:10 +00002915
Guido van Rossum3b066191991-06-04 19:40:25 +00002916/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002917
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002918PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002919"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002920Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002921
Barry Warsaw53699e91996-12-10 23:23:01 +00002922static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002923posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002924{
2925 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002926 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002927 return NULL;
2928 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002929 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002930}
2931
Martin v. Löwis114619e2002-10-07 06:44:21 +00002932#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2933static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002934free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002935{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002936 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002937 for (i = 0; i < count; i++)
2938 PyMem_Free(array[i]);
2939 PyMem_DEL(array);
2940}
2941#endif
2942
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002943
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002944#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002945PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002946"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002947Execute an executable path with arguments, replacing current process.\n\
2948\n\
2949 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002950 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002951
Barry Warsaw53699e91996-12-10 23:23:01 +00002952static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002953posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002954{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002955 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002956 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002957 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002958 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002959 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002960
Guido van Rossum89b33251993-10-22 14:26:06 +00002961 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002962 argv is a list or tuple of strings. */
2963
Martin v. Löwis114619e2002-10-07 06:44:21 +00002964 if (!PyArg_ParseTuple(args, "etO:execv",
2965 Py_FileSystemDefaultEncoding,
2966 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002967 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002968 if (PyList_Check(argv)) {
2969 argc = PyList_Size(argv);
2970 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002971 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002972 else if (PyTuple_Check(argv)) {
2973 argc = PyTuple_Size(argv);
2974 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002975 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002976 else {
Fred Drake661ea262000-10-24 19:57:45 +00002977 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002978 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002979 return NULL;
2980 }
2981
Barry Warsaw53699e91996-12-10 23:23:01 +00002982 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002983 if (argvlist == NULL) {
2984 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002985 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002986 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002987 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002988 if (!PyArg_Parse((*getitem)(argv, i), "et",
2989 Py_FileSystemDefaultEncoding,
2990 &argvlist[i])) {
2991 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002992 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002993 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002994 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002995 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002996
Guido van Rossum85e3b011991-06-03 12:42:10 +00002997 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002998 }
2999 argvlist[argc] = NULL;
3000
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003001 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003002
Guido van Rossum85e3b011991-06-03 12:42:10 +00003003 /* If we get here it's definitely an error */
3004
Martin v. Löwis114619e2002-10-07 06:44:21 +00003005 free_string_array(argvlist, argc);
3006 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003007 return posix_error();
3008}
3009
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003010
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003011PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003012"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003013Execute a path with arguments and environment, replacing current process.\n\
3014\n\
3015 path: path of executable file\n\
3016 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003017 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003018
Barry Warsaw53699e91996-12-10 23:23:01 +00003019static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003020posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003021{
3022 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003023 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003024 char **argvlist;
3025 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003026 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003027 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003028 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003029 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003030
3031 /* execve has three arguments: (path, argv, env), where
3032 argv is a list or tuple of strings and env is a dictionary
3033 like posix.environ. */
3034
Martin v. Löwis114619e2002-10-07 06:44:21 +00003035 if (!PyArg_ParseTuple(args, "etOO:execve",
3036 Py_FileSystemDefaultEncoding,
3037 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003038 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003039 if (PyList_Check(argv)) {
3040 argc = PyList_Size(argv);
3041 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003042 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003043 else if (PyTuple_Check(argv)) {
3044 argc = PyTuple_Size(argv);
3045 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003046 }
3047 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003048 PyErr_SetString(PyExc_TypeError,
3049 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003050 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003051 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003052 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003053 PyErr_SetString(PyExc_TypeError,
3054 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003055 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003056 }
3057
Barry Warsaw53699e91996-12-10 23:23:01 +00003058 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003059 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003060 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003061 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003062 }
3063 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003064 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00003065 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00003066 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00003067 &argvlist[i]))
3068 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003069 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003070 goto fail_1;
3071 }
3072 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003073 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003074 argvlist[argc] = NULL;
3075
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003076 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003077 if (i < 0)
3078 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003079 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003080 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003081 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003082 goto fail_1;
3083 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003084 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003085 keys = PyMapping_Keys(env);
3086 vals = PyMapping_Values(env);
3087 if (!keys || !vals)
3088 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003089 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3090 PyErr_SetString(PyExc_TypeError,
3091 "execve(): env.keys() or env.values() is not a list");
3092 goto fail_2;
3093 }
Tim Peters5aa91602002-01-30 05:46:57 +00003094
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003095 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003096 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003097 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003098
3099 key = PyList_GetItem(keys, pos);
3100 val = PyList_GetItem(vals, pos);
3101 if (!key || !val)
3102 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003103
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003104 if (!PyArg_Parse(
3105 key,
3106 "s;execve() arg 3 contains a non-string key",
3107 &k) ||
3108 !PyArg_Parse(
3109 val,
3110 "s;execve() arg 3 contains a non-string value",
3111 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003112 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003113 goto fail_2;
3114 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003115
3116#if defined(PYOS_OS2)
3117 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3118 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3119#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003120 len = PyString_Size(key) + PyString_Size(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003121 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003122 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003123 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003124 goto fail_2;
3125 }
Tim Petersc8996f52001-12-03 20:41:00 +00003126 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003127 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003128#if defined(PYOS_OS2)
3129 }
3130#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003131 }
3132 envlist[envc] = 0;
3133
3134 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003135
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003136 /* If we get here it's definitely an error */
3137
3138 (void) posix_error();
3139
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003140 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003141 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003142 PyMem_DEL(envlist[envc]);
3143 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003144 fail_1:
3145 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003146 Py_XDECREF(vals);
3147 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003148 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003149 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003150 return NULL;
3151}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003152#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003153
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003154
Guido van Rossuma1065681999-01-25 23:20:23 +00003155#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003156PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003157"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003158Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003159\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003160 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003161 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003162 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003163
3164static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003165posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003166{
3167 char *path;
3168 PyObject *argv;
3169 char **argvlist;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003170 int mode, i;
3171 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003172 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003173 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003174
3175 /* spawnv has three arguments: (mode, path, argv), where
3176 argv is a list or tuple of strings. */
3177
Martin v. Löwis114619e2002-10-07 06:44:21 +00003178 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3179 Py_FileSystemDefaultEncoding,
3180 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003181 return NULL;
3182 if (PyList_Check(argv)) {
3183 argc = PyList_Size(argv);
3184 getitem = PyList_GetItem;
3185 }
3186 else if (PyTuple_Check(argv)) {
3187 argc = PyTuple_Size(argv);
3188 getitem = PyTuple_GetItem;
3189 }
3190 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003191 PyErr_SetString(PyExc_TypeError,
3192 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003193 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003194 return NULL;
3195 }
3196
3197 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003198 if (argvlist == NULL) {
3199 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003200 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003201 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003202 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003203 if (!PyArg_Parse((*getitem)(argv, i), "et",
3204 Py_FileSystemDefaultEncoding,
3205 &argvlist[i])) {
3206 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003207 PyErr_SetString(
3208 PyExc_TypeError,
3209 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003210 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003211 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003212 }
3213 }
3214 argvlist[argc] = NULL;
3215
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003216#if defined(PYOS_OS2) && defined(PYCC_GCC)
3217 Py_BEGIN_ALLOW_THREADS
3218 spawnval = spawnv(mode, path, argvlist);
3219 Py_END_ALLOW_THREADS
3220#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003221 if (mode == _OLD_P_OVERLAY)
3222 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003223
Tim Peters25059d32001-12-07 20:35:43 +00003224 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003225 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003226 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003227#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003228
Martin v. Löwis114619e2002-10-07 06:44:21 +00003229 free_string_array(argvlist, argc);
3230 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003231
Fred Drake699f3522000-06-29 21:12:41 +00003232 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003233 return posix_error();
3234 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003235#if SIZEOF_LONG == SIZEOF_VOID_P
3236 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003237#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003238 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003239#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003240}
3241
3242
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003243PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003244"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003245Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003246\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003247 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003248 path: path of executable file\n\
3249 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003250 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003251
3252static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003253posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003254{
3255 char *path;
3256 PyObject *argv, *env;
3257 char **argvlist;
3258 char **envlist;
3259 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003260 int mode, pos, envc;
3261 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003262 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003263 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003264 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003265
3266 /* spawnve has four arguments: (mode, path, argv, env), where
3267 argv is a list or tuple of strings and env is a dictionary
3268 like posix.environ. */
3269
Martin v. Löwis114619e2002-10-07 06:44:21 +00003270 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3271 Py_FileSystemDefaultEncoding,
3272 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003273 return NULL;
3274 if (PyList_Check(argv)) {
3275 argc = PyList_Size(argv);
3276 getitem = PyList_GetItem;
3277 }
3278 else if (PyTuple_Check(argv)) {
3279 argc = PyTuple_Size(argv);
3280 getitem = PyTuple_GetItem;
3281 }
3282 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003283 PyErr_SetString(PyExc_TypeError,
3284 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003285 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003286 }
3287 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003288 PyErr_SetString(PyExc_TypeError,
3289 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003290 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003291 }
3292
3293 argvlist = PyMem_NEW(char *, argc+1);
3294 if (argvlist == NULL) {
3295 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003296 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003297 }
3298 for (i = 0; i < argc; i++) {
3299 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003300 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003301 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003302 &argvlist[i]))
3303 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003304 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003305 goto fail_1;
3306 }
3307 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003308 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003309 argvlist[argc] = NULL;
3310
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003311 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003312 if (i < 0)
3313 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003314 envlist = PyMem_NEW(char *, i + 1);
3315 if (envlist == NULL) {
3316 PyErr_NoMemory();
3317 goto fail_1;
3318 }
3319 envc = 0;
3320 keys = PyMapping_Keys(env);
3321 vals = PyMapping_Values(env);
3322 if (!keys || !vals)
3323 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003324 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3325 PyErr_SetString(PyExc_TypeError,
3326 "spawnve(): env.keys() or env.values() is not a list");
3327 goto fail_2;
3328 }
Tim Peters5aa91602002-01-30 05:46:57 +00003329
Guido van Rossuma1065681999-01-25 23:20:23 +00003330 for (pos = 0; pos < i; pos++) {
3331 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003332 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003333
3334 key = PyList_GetItem(keys, pos);
3335 val = PyList_GetItem(vals, pos);
3336 if (!key || !val)
3337 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003338
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003339 if (!PyArg_Parse(
3340 key,
3341 "s;spawnve() arg 3 contains a non-string key",
3342 &k) ||
3343 !PyArg_Parse(
3344 val,
3345 "s;spawnve() arg 3 contains a non-string value",
3346 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003347 {
3348 goto fail_2;
3349 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003350 len = PyString_Size(key) + PyString_Size(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003351 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003352 if (p == NULL) {
3353 PyErr_NoMemory();
3354 goto fail_2;
3355 }
Tim Petersc8996f52001-12-03 20:41:00 +00003356 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003357 envlist[envc++] = p;
3358 }
3359 envlist[envc] = 0;
3360
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003361#if defined(PYOS_OS2) && defined(PYCC_GCC)
3362 Py_BEGIN_ALLOW_THREADS
3363 spawnval = spawnve(mode, path, argvlist, envlist);
3364 Py_END_ALLOW_THREADS
3365#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003366 if (mode == _OLD_P_OVERLAY)
3367 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003368
3369 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003370 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003371 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003372#endif
Tim Peters25059d32001-12-07 20:35:43 +00003373
Fred Drake699f3522000-06-29 21:12:41 +00003374 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003375 (void) posix_error();
3376 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003377#if SIZEOF_LONG == SIZEOF_VOID_P
3378 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003379#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003380 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003381#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003382
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003383 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003384 while (--envc >= 0)
3385 PyMem_DEL(envlist[envc]);
3386 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003387 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003388 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003389 Py_XDECREF(vals);
3390 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003391 fail_0:
3392 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003393 return res;
3394}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003395
3396/* OS/2 supports spawnvp & spawnvpe natively */
3397#if defined(PYOS_OS2)
3398PyDoc_STRVAR(posix_spawnvp__doc__,
3399"spawnvp(mode, file, args)\n\n\
3400Execute the program 'file' in a new process, using the environment\n\
3401search path to find the file.\n\
3402\n\
3403 mode: mode of process creation\n\
3404 file: executable file name\n\
3405 args: tuple or list of strings");
3406
3407static PyObject *
3408posix_spawnvp(PyObject *self, PyObject *args)
3409{
3410 char *path;
3411 PyObject *argv;
3412 char **argvlist;
3413 int mode, i, argc;
3414 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003415 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003416
3417 /* spawnvp has three arguments: (mode, path, argv), where
3418 argv is a list or tuple of strings. */
3419
3420 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3421 Py_FileSystemDefaultEncoding,
3422 &path, &argv))
3423 return NULL;
3424 if (PyList_Check(argv)) {
3425 argc = PyList_Size(argv);
3426 getitem = PyList_GetItem;
3427 }
3428 else if (PyTuple_Check(argv)) {
3429 argc = PyTuple_Size(argv);
3430 getitem = PyTuple_GetItem;
3431 }
3432 else {
3433 PyErr_SetString(PyExc_TypeError,
3434 "spawnvp() arg 2 must be a tuple or list");
3435 PyMem_Free(path);
3436 return NULL;
3437 }
3438
3439 argvlist = PyMem_NEW(char *, argc+1);
3440 if (argvlist == NULL) {
3441 PyMem_Free(path);
3442 return PyErr_NoMemory();
3443 }
3444 for (i = 0; i < argc; i++) {
3445 if (!PyArg_Parse((*getitem)(argv, i), "et",
3446 Py_FileSystemDefaultEncoding,
3447 &argvlist[i])) {
3448 free_string_array(argvlist, i);
3449 PyErr_SetString(
3450 PyExc_TypeError,
3451 "spawnvp() arg 2 must contain only strings");
3452 PyMem_Free(path);
3453 return NULL;
3454 }
3455 }
3456 argvlist[argc] = NULL;
3457
3458 Py_BEGIN_ALLOW_THREADS
3459#if defined(PYCC_GCC)
3460 spawnval = spawnvp(mode, path, argvlist);
3461#else
3462 spawnval = _spawnvp(mode, path, argvlist);
3463#endif
3464 Py_END_ALLOW_THREADS
3465
3466 free_string_array(argvlist, argc);
3467 PyMem_Free(path);
3468
3469 if (spawnval == -1)
3470 return posix_error();
3471 else
3472 return Py_BuildValue("l", (long) spawnval);
3473}
3474
3475
3476PyDoc_STRVAR(posix_spawnvpe__doc__,
3477"spawnvpe(mode, file, args, env)\n\n\
3478Execute the program 'file' in a new process, using the environment\n\
3479search path to find the file.\n\
3480\n\
3481 mode: mode of process creation\n\
3482 file: executable file name\n\
3483 args: tuple or list of arguments\n\
3484 env: dictionary of strings mapping to strings");
3485
3486static PyObject *
3487posix_spawnvpe(PyObject *self, PyObject *args)
3488{
3489 char *path;
3490 PyObject *argv, *env;
3491 char **argvlist;
3492 char **envlist;
3493 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3494 int mode, i, pos, argc, envc;
3495 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003496 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003497 int lastarg = 0;
3498
3499 /* spawnvpe has four arguments: (mode, path, argv, env), where
3500 argv is a list or tuple of strings and env is a dictionary
3501 like posix.environ. */
3502
3503 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3504 Py_FileSystemDefaultEncoding,
3505 &path, &argv, &env))
3506 return NULL;
3507 if (PyList_Check(argv)) {
3508 argc = PyList_Size(argv);
3509 getitem = PyList_GetItem;
3510 }
3511 else if (PyTuple_Check(argv)) {
3512 argc = PyTuple_Size(argv);
3513 getitem = PyTuple_GetItem;
3514 }
3515 else {
3516 PyErr_SetString(PyExc_TypeError,
3517 "spawnvpe() arg 2 must be a tuple or list");
3518 goto fail_0;
3519 }
3520 if (!PyMapping_Check(env)) {
3521 PyErr_SetString(PyExc_TypeError,
3522 "spawnvpe() arg 3 must be a mapping object");
3523 goto fail_0;
3524 }
3525
3526 argvlist = PyMem_NEW(char *, argc+1);
3527 if (argvlist == NULL) {
3528 PyErr_NoMemory();
3529 goto fail_0;
3530 }
3531 for (i = 0; i < argc; i++) {
3532 if (!PyArg_Parse((*getitem)(argv, i),
3533 "et;spawnvpe() arg 2 must contain only strings",
3534 Py_FileSystemDefaultEncoding,
3535 &argvlist[i]))
3536 {
3537 lastarg = i;
3538 goto fail_1;
3539 }
3540 }
3541 lastarg = argc;
3542 argvlist[argc] = NULL;
3543
3544 i = PyMapping_Size(env);
3545 if (i < 0)
3546 goto fail_1;
3547 envlist = PyMem_NEW(char *, i + 1);
3548 if (envlist == NULL) {
3549 PyErr_NoMemory();
3550 goto fail_1;
3551 }
3552 envc = 0;
3553 keys = PyMapping_Keys(env);
3554 vals = PyMapping_Values(env);
3555 if (!keys || !vals)
3556 goto fail_2;
3557 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3558 PyErr_SetString(PyExc_TypeError,
3559 "spawnvpe(): env.keys() or env.values() is not a list");
3560 goto fail_2;
3561 }
3562
3563 for (pos = 0; pos < i; pos++) {
3564 char *p, *k, *v;
3565 size_t len;
3566
3567 key = PyList_GetItem(keys, pos);
3568 val = PyList_GetItem(vals, pos);
3569 if (!key || !val)
3570 goto fail_2;
3571
3572 if (!PyArg_Parse(
3573 key,
3574 "s;spawnvpe() arg 3 contains a non-string key",
3575 &k) ||
3576 !PyArg_Parse(
3577 val,
3578 "s;spawnvpe() arg 3 contains a non-string value",
3579 &v))
3580 {
3581 goto fail_2;
3582 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003583 len = PyString_Size(key) + PyString_Size(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003584 p = PyMem_NEW(char, len);
3585 if (p == NULL) {
3586 PyErr_NoMemory();
3587 goto fail_2;
3588 }
3589 PyOS_snprintf(p, len, "%s=%s", k, v);
3590 envlist[envc++] = p;
3591 }
3592 envlist[envc] = 0;
3593
3594 Py_BEGIN_ALLOW_THREADS
3595#if defined(PYCC_GCC)
Andrew MacIntyre94dcf0d2008-02-03 07:07:31 +00003596 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003597#else
Andrew MacIntyre94dcf0d2008-02-03 07:07:31 +00003598 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003599#endif
3600 Py_END_ALLOW_THREADS
3601
3602 if (spawnval == -1)
3603 (void) posix_error();
3604 else
3605 res = Py_BuildValue("l", (long) spawnval);
3606
3607 fail_2:
3608 while (--envc >= 0)
3609 PyMem_DEL(envlist[envc]);
3610 PyMem_DEL(envlist);
3611 fail_1:
3612 free_string_array(argvlist, lastarg);
3613 Py_XDECREF(vals);
3614 Py_XDECREF(keys);
3615 fail_0:
3616 PyMem_Free(path);
3617 return res;
3618}
3619#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003620#endif /* HAVE_SPAWNV */
3621
3622
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003623#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003624PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003625"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003626Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3627\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003628Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003629
3630static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003631posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003632{
Thomas Wouters448db212009-09-16 20:06:36 +00003633 pid_t pid;
3634 int result;
3635 _PyImport_AcquireLock();
3636 pid = fork1();
3637 result = _PyImport_ReleaseLock();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003638 if (pid == -1)
3639 return posix_error();
Gregory P. Smithfb7a50f2008-07-14 06:06:48 +00003640 if (pid == 0)
3641 PyOS_AfterFork();
Thomas Wouters448db212009-09-16 20:06:36 +00003642 if (result < 0) {
3643 /* Don't clobber the OSError if the fork failed. */
3644 PyErr_SetString(PyExc_RuntimeError,
3645 "not holding the import lock");
3646 return NULL;
3647 }
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00003648 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003649}
3650#endif
3651
3652
Guido van Rossumad0ee831995-03-01 10:34:45 +00003653#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003654PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003655"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003656Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003657Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003658
Barry Warsaw53699e91996-12-10 23:23:01 +00003659static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003660posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003661{
Thomas Wouters448db212009-09-16 20:06:36 +00003662 pid_t pid;
3663 int result;
3664 _PyImport_AcquireLock();
3665 pid = fork();
3666 result = _PyImport_ReleaseLock();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003667 if (pid == -1)
3668 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003669 if (pid == 0)
3670 PyOS_AfterFork();
Thomas Wouters448db212009-09-16 20:06:36 +00003671 if (result < 0) {
3672 /* Don't clobber the OSError if the fork failed. */
3673 PyErr_SetString(PyExc_RuntimeError,
3674 "not holding the import lock");
3675 return NULL;
3676 }
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00003677 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003678}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003679#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003680
Neal Norwitzb59798b2003-03-21 01:43:31 +00003681/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003682/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3683#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003684#define DEV_PTY_FILE "/dev/ptc"
3685#define HAVE_DEV_PTMX
3686#else
3687#define DEV_PTY_FILE "/dev/ptmx"
3688#endif
3689
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003690#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003691#ifdef HAVE_PTY_H
3692#include <pty.h>
3693#else
3694#ifdef HAVE_LIBUTIL_H
3695#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003696#endif /* HAVE_LIBUTIL_H */
3697#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003698#ifdef HAVE_STROPTS_H
3699#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003700#endif
3701#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003702
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003703#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003704PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003705"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003706Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003707
3708static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003709posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003710{
3711 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003712#ifndef HAVE_OPENPTY
3713 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003714#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003715#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003716 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003717#ifdef sun
Neal Norwitz84a98e02006-04-10 07:44:23 +00003718 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003719#endif
3720#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003721
Thomas Wouters70c21a12000-07-14 14:28:33 +00003722#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003723 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3724 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003725#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003726 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3727 if (slave_name == NULL)
3728 return posix_error();
3729
3730 slave_fd = open(slave_name, O_RDWR);
3731 if (slave_fd < 0)
3732 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003733#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003734 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003735 if (master_fd < 0)
3736 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003737 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003738 /* change permission of slave */
3739 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003740 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003741 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003742 }
3743 /* unlock slave */
3744 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003745 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003746 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003747 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003748 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003749 slave_name = ptsname(master_fd); /* get name of slave */
3750 if (slave_name == NULL)
3751 return posix_error();
3752 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3753 if (slave_fd < 0)
3754 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003755#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003756 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3757 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003758#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003759 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003760#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003761#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003762#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003763
Fred Drake8cef4cf2000-06-28 16:40:38 +00003764 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003765
Fred Drake8cef4cf2000-06-28 16:40:38 +00003766}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003767#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003768
3769#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003770PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003771"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003772Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3773Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003774To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003775
3776static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003777posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003778{
Thomas Wouters448db212009-09-16 20:06:36 +00003779 int master_fd = -1, result;
Christian Heimes951cc0f2008-01-31 23:08:23 +00003780 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003781
Thomas Wouters448db212009-09-16 20:06:36 +00003782 _PyImport_AcquireLock();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003783 pid = forkpty(&master_fd, NULL, NULL, NULL);
Thomas Wouters448db212009-09-16 20:06:36 +00003784 result = _PyImport_ReleaseLock();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003785 if (pid == -1)
3786 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003787 if (pid == 0)
3788 PyOS_AfterFork();
Thomas Wouters448db212009-09-16 20:06:36 +00003789 if (result < 0) {
3790 /* Don't clobber the OSError if the fork failed. */
3791 PyErr_SetString(PyExc_RuntimeError,
3792 "not holding the import lock");
3793 return NULL;
3794 }
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00003795 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003796}
3797#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003798
Guido van Rossumad0ee831995-03-01 10:34:45 +00003799#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003800PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003801"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003802Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003803
Barry Warsaw53699e91996-12-10 23:23:01 +00003804static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003805posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003806{
Barry Warsaw53699e91996-12-10 23:23:01 +00003807 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003808}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003809#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003810
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003811
Guido van Rossumad0ee831995-03-01 10:34:45 +00003812#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003813PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003814"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003815Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003816
Barry Warsaw53699e91996-12-10 23:23:01 +00003817static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003818posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003819{
Barry Warsaw53699e91996-12-10 23:23:01 +00003820 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003821}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003822#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003823
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003824
Guido van Rossumad0ee831995-03-01 10:34:45 +00003825#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003826PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003827"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003828Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003829
Barry Warsaw53699e91996-12-10 23:23:01 +00003830static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003831posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003832{
Barry Warsaw53699e91996-12-10 23:23:01 +00003833 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003834}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003835#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003836
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003837
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003838PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003839"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003840Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003841
Barry Warsaw53699e91996-12-10 23:23:01 +00003842static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003843posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003844{
Antoine Pitroucd1376d2009-05-23 16:19:24 +00003845 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003846}
3847
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003848
Fred Drakec9680921999-12-13 16:37:25 +00003849#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003850PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003851"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003852Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003853
3854static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003855posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003856{
3857 PyObject *result = NULL;
3858
Fred Drakec9680921999-12-13 16:37:25 +00003859#ifdef NGROUPS_MAX
3860#define MAX_GROUPS NGROUPS_MAX
3861#else
3862 /* defined to be 16 on Solaris7, so this should be a small number */
3863#define MAX_GROUPS 64
3864#endif
3865 gid_t grouplist[MAX_GROUPS];
3866 int n;
3867
3868 n = getgroups(MAX_GROUPS, grouplist);
3869 if (n < 0)
3870 posix_error();
3871 else {
3872 result = PyList_New(n);
3873 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003874 int i;
3875 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003876 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003877 if (o == NULL) {
3878 Py_DECREF(result);
3879 result = NULL;
3880 break;
3881 }
3882 PyList_SET_ITEM(result, i, o);
3883 }
3884 }
3885 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003886
Fred Drakec9680921999-12-13 16:37:25 +00003887 return result;
3888}
3889#endif
3890
Martin v. Löwis606edc12002-06-13 21:09:11 +00003891#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003892PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003893"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003894Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003895
3896static PyObject *
3897posix_getpgid(PyObject *self, PyObject *args)
3898{
Antoine Pitroucd1376d2009-05-23 16:19:24 +00003899 pid_t pid, pgid;
3900 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
Martin v. Löwis606edc12002-06-13 21:09:11 +00003901 return NULL;
3902 pgid = getpgid(pid);
3903 if (pgid < 0)
3904 return posix_error();
Antoine Pitroucd1376d2009-05-23 16:19:24 +00003905 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003906}
3907#endif /* HAVE_GETPGID */
3908
3909
Guido van Rossumb6775db1994-08-01 11:34:53 +00003910#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003911PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003912"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003913Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003914
Barry Warsaw53699e91996-12-10 23:23:01 +00003915static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003916posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003917{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003918#ifdef GETPGRP_HAVE_ARG
Antoine Pitroucd1376d2009-05-23 16:19:24 +00003919 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003920#else /* GETPGRP_HAVE_ARG */
Antoine Pitroucd1376d2009-05-23 16:19:24 +00003921 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003922#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003923}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003924#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003925
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003926
Guido van Rossumb6775db1994-08-01 11:34:53 +00003927#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003928PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003929"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003930Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003931
Barry Warsaw53699e91996-12-10 23:23:01 +00003932static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003933posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003934{
Guido van Rossum64933891994-10-20 21:56:42 +00003935#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003936 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003937#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003938 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003939#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003940 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003941 Py_INCREF(Py_None);
3942 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003943}
3944
Guido van Rossumb6775db1994-08-01 11:34:53 +00003945#endif /* HAVE_SETPGRP */
3946
Guido van Rossumad0ee831995-03-01 10:34:45 +00003947#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003948PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003949"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003950Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003951
Barry Warsaw53699e91996-12-10 23:23:01 +00003952static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003953posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003954{
Antoine Pitroucd1376d2009-05-23 16:19:24 +00003955 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003956}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003957#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003958
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003959
Fred Drake12c6e2d1999-12-14 21:25:03 +00003960#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003961PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003962"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003963Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003964
3965static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003966posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003967{
Neal Norwitze241ce82003-02-17 18:17:05 +00003968 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003969 char *name;
3970 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003971
Fred Drakea30680b2000-12-06 21:24:28 +00003972 errno = 0;
3973 name = getlogin();
3974 if (name == NULL) {
3975 if (errno)
3976 posix_error();
3977 else
3978 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003979 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003980 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003981 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003982 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003983 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003984
Fred Drake12c6e2d1999-12-14 21:25:03 +00003985 return result;
3986}
3987#endif
3988
Guido van Rossumad0ee831995-03-01 10:34:45 +00003989#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003990PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003991"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003992Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003993
Barry Warsaw53699e91996-12-10 23:23:01 +00003994static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003995posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003996{
Barry Warsaw53699e91996-12-10 23:23:01 +00003997 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003998}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003999#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004000
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004001
Guido van Rossumad0ee831995-03-01 10:34:45 +00004002#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004003PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004004"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004005Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004006
Barry Warsaw53699e91996-12-10 23:23:01 +00004007static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004008posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004009{
Christian Heimesd491d712008-02-01 18:49:26 +00004010 pid_t pid;
4011 int sig;
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00004012 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00004013 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004014#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004015 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4016 APIRET rc;
4017 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004018 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004019
4020 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4021 APIRET rc;
4022 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004023 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004024
4025 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004026 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004027#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004028 if (kill(pid, sig) == -1)
4029 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004030#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004031 Py_INCREF(Py_None);
4032 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004033}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004034#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004035
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004036#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004037PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004038"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004039Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004040
4041static PyObject *
4042posix_killpg(PyObject *self, PyObject *args)
4043{
Antoine Pitroucd1376d2009-05-23 16:19:24 +00004044 int sig;
4045 pid_t pgid;
4046 /* XXX some man pages make the `pgid` parameter an int, others
4047 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4048 take the same type. Moreover, pid_t is always at least as wide as
4049 int (else compilation of this module fails), which is safe. */
4050 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004051 return NULL;
4052 if (killpg(pgid, sig) == -1)
4053 return posix_error();
4054 Py_INCREF(Py_None);
4055 return Py_None;
4056}
4057#endif
4058
Guido van Rossumc0125471996-06-28 18:55:32 +00004059#ifdef HAVE_PLOCK
4060
4061#ifdef HAVE_SYS_LOCK_H
4062#include <sys/lock.h>
4063#endif
4064
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004065PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004066"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004067Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004068
Barry Warsaw53699e91996-12-10 23:23:01 +00004069static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004070posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004071{
4072 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004073 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004074 return NULL;
4075 if (plock(op) == -1)
4076 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004077 Py_INCREF(Py_None);
4078 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004079}
4080#endif
4081
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004082
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004083#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004084PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004085"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004086Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004087
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004088#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004089#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004090static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004091async_system(const char *command)
4092{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004093 char errormsg[256], args[1024];
4094 RESULTCODES rcodes;
4095 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004096
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004097 char *shell = getenv("COMSPEC");
4098 if (!shell)
4099 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004100
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004101 /* avoid overflowing the argument buffer */
4102 if (strlen(shell) + 3 + strlen(command) >= 1024)
4103 return ERROR_NOT_ENOUGH_MEMORY
4104
4105 args[0] = '\0';
4106 strcat(args, shell);
4107 strcat(args, "/c ");
4108 strcat(args, command);
4109
4110 /* execute asynchronously, inheriting the environment */
4111 rc = DosExecPgm(errormsg,
4112 sizeof(errormsg),
4113 EXEC_ASYNC,
4114 args,
4115 NULL,
4116 &rcodes,
4117 shell);
4118 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004119}
4120
Guido van Rossumd48f2521997-12-05 22:19:34 +00004121static FILE *
4122popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004123{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004124 int oldfd, tgtfd;
4125 HFILE pipeh[2];
4126 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004127
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004128 /* mode determines which of stdin or stdout is reconnected to
4129 * the pipe to the child
4130 */
4131 if (strchr(mode, 'r') != NULL) {
4132 tgt_fd = 1; /* stdout */
4133 } else if (strchr(mode, 'w')) {
4134 tgt_fd = 0; /* stdin */
4135 } else {
4136 *err = ERROR_INVALID_ACCESS;
4137 return NULL;
4138 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004139
Andrew MacIntyrea3be2582004-12-18 09:51:05 +00004140 /* setup the pipe */
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004141 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
4142 *err = rc;
4143 return NULL;
4144 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004145
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004146 /* prevent other threads accessing stdio */
4147 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004148
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004149 /* reconnect stdio and execute child */
4150 oldfd = dup(tgtfd);
4151 close(tgtfd);
4152 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
4153 DosClose(pipeh[tgtfd]);
4154 rc = async_system(command);
4155 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004156
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004157 /* restore stdio */
4158 dup2(oldfd, tgtfd);
4159 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004160
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004161 /* allow other threads access to stdio */
4162 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004163
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004164 /* if execution of child was successful return file stream */
4165 if (rc == NO_ERROR)
4166 return fdopen(pipeh[1 - tgtfd], mode);
4167 else {
4168 DosClose(pipeh[1 - tgtfd]);
4169 *err = rc;
4170 return NULL;
4171 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004172}
4173
4174static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004175posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004176{
4177 char *name;
4178 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00004179 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004180 FILE *fp;
4181 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004182 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004183 return NULL;
4184 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00004185 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004186 Py_END_ALLOW_THREADS
4187 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004188 return os2_error(err);
4189
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004190 f = PyFile_FromFile(fp, name, mode, fclose);
4191 if (f != NULL)
4192 PyFile_SetBufSize(f, bufsize);
4193 return f;
4194}
4195
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004196#elif defined(PYCC_GCC)
4197
4198/* standard posix version of popen() support */
4199static PyObject *
4200posix_popen(PyObject *self, PyObject *args)
4201{
4202 char *name;
4203 char *mode = "r";
4204 int bufsize = -1;
4205 FILE *fp;
4206 PyObject *f;
4207 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4208 return NULL;
4209 Py_BEGIN_ALLOW_THREADS
4210 fp = popen(name, mode);
4211 Py_END_ALLOW_THREADS
4212 if (fp == NULL)
4213 return posix_error();
4214 f = PyFile_FromFile(fp, name, mode, pclose);
4215 if (f != NULL)
4216 PyFile_SetBufSize(f, bufsize);
4217 return f;
4218}
4219
4220/* fork() under OS/2 has lots'o'warts
4221 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4222 * most of this code is a ripoff of the win32 code, but using the
4223 * capabilities of EMX's C library routines
4224 */
4225
4226/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4227#define POPEN_1 1
4228#define POPEN_2 2
4229#define POPEN_3 3
4230#define POPEN_4 4
4231
4232static PyObject *_PyPopen(char *, int, int, int);
4233static int _PyPclose(FILE *file);
4234
4235/*
4236 * Internal dictionary mapping popen* file pointers to process handles,
4237 * for use when retrieving the process exit code. See _PyPclose() below
4238 * for more information on this dictionary's use.
4239 */
4240static PyObject *_PyPopenProcs = NULL;
4241
4242/* os2emx version of popen2()
4243 *
4244 * The result of this function is a pipe (file) connected to the
4245 * process's stdin, and a pipe connected to the process's stdout.
4246 */
4247
4248static PyObject *
4249os2emx_popen2(PyObject *self, PyObject *args)
4250{
4251 PyObject *f;
4252 int tm=0;
4253
4254 char *cmdstring;
4255 char *mode = "t";
4256 int bufsize = -1;
4257 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4258 return NULL;
4259
4260 if (*mode == 't')
4261 tm = O_TEXT;
4262 else if (*mode != 'b') {
4263 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4264 return NULL;
4265 } else
4266 tm = O_BINARY;
4267
4268 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
4269
4270 return f;
4271}
4272
4273/*
4274 * Variation on os2emx.popen2
4275 *
4276 * The result of this function is 3 pipes - the process's stdin,
4277 * stdout and stderr
4278 */
4279
4280static PyObject *
4281os2emx_popen3(PyObject *self, PyObject *args)
4282{
4283 PyObject *f;
4284 int tm = 0;
4285
4286 char *cmdstring;
4287 char *mode = "t";
4288 int bufsize = -1;
4289 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4290 return NULL;
4291
4292 if (*mode == 't')
4293 tm = O_TEXT;
4294 else if (*mode != 'b') {
4295 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4296 return NULL;
4297 } else
4298 tm = O_BINARY;
4299
4300 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
4301
4302 return f;
4303}
4304
4305/*
4306 * Variation on os2emx.popen2
4307 *
Tim Peters11b23062003-04-23 02:39:17 +00004308 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004309 * and stdout+stderr combined as a single pipe.
4310 */
4311
4312static PyObject *
4313os2emx_popen4(PyObject *self, PyObject *args)
4314{
4315 PyObject *f;
4316 int tm = 0;
4317
4318 char *cmdstring;
4319 char *mode = "t";
4320 int bufsize = -1;
4321 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4322 return NULL;
4323
4324 if (*mode == 't')
4325 tm = O_TEXT;
4326 else if (*mode != 'b') {
4327 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4328 return NULL;
4329 } else
4330 tm = O_BINARY;
4331
4332 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
4333
4334 return f;
4335}
4336
4337/* a couple of structures for convenient handling of multiple
4338 * file handles and pipes
4339 */
4340struct file_ref
4341{
4342 int handle;
4343 int flags;
4344};
4345
4346struct pipe_ref
4347{
4348 int rd;
4349 int wr;
4350};
4351
4352/* The following code is derived from the win32 code */
4353
4354static PyObject *
4355_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4356{
4357 struct file_ref stdio[3];
4358 struct pipe_ref p_fd[3];
4359 FILE *p_s[3];
Christian Heimesd491d712008-02-01 18:49:26 +00004360 int file_count, i, pipe_err;
4361 pid_t pipe_pid;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004362 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4363 PyObject *f, *p_f[3];
4364
4365 /* file modes for subsequent fdopen's on pipe handles */
4366 if (mode == O_TEXT)
4367 {
4368 rd_mode = "rt";
4369 wr_mode = "wt";
4370 }
4371 else
4372 {
4373 rd_mode = "rb";
4374 wr_mode = "wb";
4375 }
4376
4377 /* prepare shell references */
4378 if ((shell = getenv("EMXSHELL")) == NULL)
4379 if ((shell = getenv("COMSPEC")) == NULL)
4380 {
4381 errno = ENOENT;
4382 return posix_error();
4383 }
4384
4385 sh_name = _getname(shell);
4386 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4387 opt = "/c";
4388 else
4389 opt = "-c";
4390
4391 /* save current stdio fds + their flags, and set not inheritable */
4392 i = pipe_err = 0;
4393 while (pipe_err >= 0 && i < 3)
4394 {
4395 pipe_err = stdio[i].handle = dup(i);
4396 stdio[i].flags = fcntl(i, F_GETFD, 0);
4397 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4398 i++;
4399 }
4400 if (pipe_err < 0)
4401 {
4402 /* didn't get them all saved - clean up and bail out */
4403 int saved_err = errno;
4404 while (i-- > 0)
4405 {
4406 close(stdio[i].handle);
4407 }
4408 errno = saved_err;
4409 return posix_error();
4410 }
4411
4412 /* create pipe ends */
4413 file_count = 2;
4414 if (n == POPEN_3)
4415 file_count = 3;
4416 i = pipe_err = 0;
4417 while ((pipe_err == 0) && (i < file_count))
4418 pipe_err = pipe((int *)&p_fd[i++]);
4419 if (pipe_err < 0)
4420 {
4421 /* didn't get them all made - clean up and bail out */
4422 while (i-- > 0)
4423 {
4424 close(p_fd[i].wr);
4425 close(p_fd[i].rd);
4426 }
4427 errno = EPIPE;
4428 return posix_error();
4429 }
4430
4431 /* change the actual standard IO streams over temporarily,
4432 * making the retained pipe ends non-inheritable
4433 */
4434 pipe_err = 0;
4435
4436 /* - stdin */
4437 if (dup2(p_fd[0].rd, 0) == 0)
4438 {
4439 close(p_fd[0].rd);
4440 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4441 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4442 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4443 {
4444 close(p_fd[0].wr);
4445 pipe_err = -1;
4446 }
4447 }
4448 else
4449 {
4450 pipe_err = -1;
4451 }
4452
4453 /* - stdout */
4454 if (pipe_err == 0)
4455 {
4456 if (dup2(p_fd[1].wr, 1) == 1)
4457 {
4458 close(p_fd[1].wr);
4459 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4460 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4461 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4462 {
4463 close(p_fd[1].rd);
4464 pipe_err = -1;
4465 }
4466 }
4467 else
4468 {
4469 pipe_err = -1;
4470 }
4471 }
4472
4473 /* - stderr, as required */
4474 if (pipe_err == 0)
4475 switch (n)
4476 {
4477 case POPEN_3:
4478 {
4479 if (dup2(p_fd[2].wr, 2) == 2)
4480 {
4481 close(p_fd[2].wr);
4482 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4483 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4484 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4485 {
4486 close(p_fd[2].rd);
4487 pipe_err = -1;
4488 }
4489 }
4490 else
4491 {
4492 pipe_err = -1;
4493 }
4494 break;
4495 }
4496
4497 case POPEN_4:
4498 {
4499 if (dup2(1, 2) != 2)
4500 {
4501 pipe_err = -1;
4502 }
4503 break;
4504 }
4505 }
4506
4507 /* spawn the child process */
4508 if (pipe_err == 0)
4509 {
4510 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4511 if (pipe_pid == -1)
4512 {
4513 pipe_err = -1;
4514 }
4515 else
4516 {
4517 /* save the PID into the FILE structure
4518 * NOTE: this implementation doesn't actually
4519 * take advantage of this, but do it for
4520 * completeness - AIM Apr01
4521 */
4522 for (i = 0; i < file_count; i++)
4523 p_s[i]->_pid = pipe_pid;
4524 }
4525 }
4526
4527 /* reset standard IO to normal */
4528 for (i = 0; i < 3; i++)
4529 {
4530 dup2(stdio[i].handle, i);
4531 fcntl(i, F_SETFD, stdio[i].flags);
4532 close(stdio[i].handle);
4533 }
4534
4535 /* if any remnant problems, clean up and bail out */
4536 if (pipe_err < 0)
4537 {
4538 for (i = 0; i < 3; i++)
4539 {
4540 close(p_fd[i].rd);
4541 close(p_fd[i].wr);
4542 }
4543 errno = EPIPE;
4544 return posix_error_with_filename(cmdstring);
4545 }
4546
4547 /* build tuple of file objects to return */
4548 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4549 PyFile_SetBufSize(p_f[0], bufsize);
4550 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4551 PyFile_SetBufSize(p_f[1], bufsize);
4552 if (n == POPEN_3)
4553 {
4554 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4555 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004556 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004557 }
4558 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004559 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004560
4561 /*
4562 * Insert the files we've created into the process dictionary
4563 * all referencing the list with the process handle and the
4564 * initial number of files (see description below in _PyPclose).
4565 * Since if _PyPclose later tried to wait on a process when all
4566 * handles weren't closed, it could create a deadlock with the
4567 * child, we spend some energy here to try to ensure that we
4568 * either insert all file handles into the dictionary or none
4569 * at all. It's a little clumsy with the various popen modes
4570 * and variable number of files involved.
4571 */
4572 if (!_PyPopenProcs)
4573 {
4574 _PyPopenProcs = PyDict_New();
4575 }
4576
4577 if (_PyPopenProcs)
4578 {
4579 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4580 int ins_rc[3];
4581
4582 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4583 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4584
4585 procObj = PyList_New(2);
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00004586 pidObj = PyLong_FromPid(pipe_pid);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004587 intObj = PyInt_FromLong((long) file_count);
4588
4589 if (procObj && pidObj && intObj)
4590 {
4591 PyList_SetItem(procObj, 0, pidObj);
4592 PyList_SetItem(procObj, 1, intObj);
4593
4594 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4595 if (fileObj[0])
4596 {
4597 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4598 fileObj[0],
4599 procObj);
4600 }
4601 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4602 if (fileObj[1])
4603 {
4604 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4605 fileObj[1],
4606 procObj);
4607 }
4608 if (file_count >= 3)
4609 {
4610 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4611 if (fileObj[2])
4612 {
4613 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4614 fileObj[2],
4615 procObj);
4616 }
4617 }
4618
4619 if (ins_rc[0] < 0 || !fileObj[0] ||
4620 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4621 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4622 {
4623 /* Something failed - remove any dictionary
4624 * entries that did make it.
4625 */
4626 if (!ins_rc[0] && fileObj[0])
4627 {
4628 PyDict_DelItem(_PyPopenProcs,
4629 fileObj[0]);
4630 }
4631 if (!ins_rc[1] && fileObj[1])
4632 {
4633 PyDict_DelItem(_PyPopenProcs,
4634 fileObj[1]);
4635 }
4636 if (!ins_rc[2] && fileObj[2])
4637 {
4638 PyDict_DelItem(_PyPopenProcs,
4639 fileObj[2]);
4640 }
4641 }
4642 }
Tim Peters11b23062003-04-23 02:39:17 +00004643
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004644 /*
4645 * Clean up our localized references for the dictionary keys
4646 * and value since PyDict_SetItem will Py_INCREF any copies
4647 * that got placed in the dictionary.
4648 */
4649 Py_XDECREF(procObj);
4650 Py_XDECREF(fileObj[0]);
4651 Py_XDECREF(fileObj[1]);
4652 Py_XDECREF(fileObj[2]);
4653 }
4654
4655 /* Child is launched. */
4656 return f;
4657}
4658
4659/*
4660 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4661 * exit code for the child process and return as a result of the close.
4662 *
4663 * This function uses the _PyPopenProcs dictionary in order to map the
4664 * input file pointer to information about the process that was
4665 * originally created by the popen* call that created the file pointer.
4666 * The dictionary uses the file pointer as a key (with one entry
4667 * inserted for each file returned by the original popen* call) and a
4668 * single list object as the value for all files from a single call.
4669 * The list object contains the Win32 process handle at [0], and a file
4670 * count at [1], which is initialized to the total number of file
4671 * handles using that list.
4672 *
4673 * This function closes whichever handle it is passed, and decrements
4674 * the file count in the dictionary for the process handle pointed to
4675 * by this file. On the last close (when the file count reaches zero),
4676 * this function will wait for the child process and then return its
4677 * exit code as the result of the close() operation. This permits the
4678 * files to be closed in any order - it is always the close() of the
4679 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004680 *
4681 * NOTE: This function is currently called with the GIL released.
4682 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004683 */
4684
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004685static int _PyPclose(FILE *file)
4686{
4687 int result;
4688 int exit_code;
Christian Heimesd491d712008-02-01 18:49:26 +00004689 pid_t pipe_pid;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004690 PyObject *procObj, *pidObj, *intObj, *fileObj;
4691 int file_count;
4692#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004693 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004694#endif
4695
4696 /* Close the file handle first, to ensure it can't block the
4697 * child from exiting if it's the last handle.
4698 */
4699 result = fclose(file);
4700
4701#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004702 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004703#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004704 if (_PyPopenProcs)
4705 {
4706 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4707 (procObj = PyDict_GetItem(_PyPopenProcs,
4708 fileObj)) != NULL &&
4709 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4710 (intObj = PyList_GetItem(procObj,1)) != NULL)
4711 {
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00004712 pipe_pid = (pid_t) PyLong_AsPid(pidObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004713 file_count = (int) PyInt_AsLong(intObj);
4714
4715 if (file_count > 1)
4716 {
4717 /* Still other files referencing process */
4718 file_count--;
4719 PyList_SetItem(procObj,1,
4720 PyInt_FromLong((long) file_count));
4721 }
4722 else
4723 {
4724 /* Last file for this process */
4725 if (result != EOF &&
4726 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4727 {
4728 /* extract exit status */
4729 if (WIFEXITED(exit_code))
4730 {
4731 result = WEXITSTATUS(exit_code);
4732 }
4733 else
4734 {
4735 errno = EPIPE;
4736 result = -1;
4737 }
4738 }
4739 else
4740 {
4741 /* Indicate failure - this will cause the file object
4742 * to raise an I/O error and translate the last
4743 * error code from errno. We do have a problem with
4744 * last errors that overlap the normal errno table,
4745 * but that's a consistent problem with the file object.
4746 */
4747 result = -1;
4748 }
4749 }
4750
4751 /* Remove this file pointer from dictionary */
4752 PyDict_DelItem(_PyPopenProcs, fileObj);
4753
4754 if (PyDict_Size(_PyPopenProcs) == 0)
4755 {
4756 Py_DECREF(_PyPopenProcs);
4757 _PyPopenProcs = NULL;
4758 }
4759
4760 } /* if object retrieval ok */
4761
4762 Py_XDECREF(fileObj);
4763 } /* if _PyPopenProcs */
4764
4765#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004766 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004767#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004768 return result;
4769}
4770
4771#endif /* PYCC_??? */
4772
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004773#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004774
4775/*
4776 * Portable 'popen' replacement for Win32.
4777 *
4778 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4779 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004780 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004781 */
4782
4783#include <malloc.h>
4784#include <io.h>
4785#include <fcntl.h>
4786
4787/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4788#define POPEN_1 1
4789#define POPEN_2 2
4790#define POPEN_3 3
4791#define POPEN_4 4
4792
4793static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004794static int _PyPclose(FILE *file);
4795
4796/*
4797 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004798 * for use when retrieving the process exit code. See _PyPclose() below
4799 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004800 */
4801static PyObject *_PyPopenProcs = NULL;
4802
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004803
4804/* popen that works from a GUI.
4805 *
4806 * The result of this function is a pipe (file) connected to the
4807 * processes stdin or stdout, depending on the requested mode.
4808 */
4809
4810static PyObject *
4811posix_popen(PyObject *self, PyObject *args)
4812{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00004813 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004814 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004815
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004816 char *cmdstring;
4817 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004818 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004819 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004820 return NULL;
4821
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004822 if (*mode == 'r')
4823 tm = _O_RDONLY;
4824 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00004825 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004826 return NULL;
4827 } else
4828 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004829
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004830 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004831 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004832 return NULL;
4833 }
4834
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004835 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004836 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004837 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004838 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004839 else
4840 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4841
4842 return f;
4843}
4844
4845/* Variation on win32pipe.popen
4846 *
4847 * The result of this function is a pipe (file) connected to the
4848 * process's stdin, and a pipe connected to the process's stdout.
4849 */
4850
4851static PyObject *
4852win32_popen2(PyObject *self, PyObject *args)
4853{
4854 PyObject *f;
4855 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004856
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004857 char *cmdstring;
4858 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004859 int bufsize = -1;
4860 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004861 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004862
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004863 if (*mode == 't')
4864 tm = _O_TEXT;
4865 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004866 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004867 return NULL;
4868 } else
4869 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004870
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004871 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004872 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004873 return NULL;
4874 }
4875
4876 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004877
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004878 return f;
4879}
4880
4881/*
4882 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004883 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004884 * The result of this function is 3 pipes - the process's stdin,
4885 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004886 */
4887
4888static PyObject *
4889win32_popen3(PyObject *self, PyObject *args)
4890{
4891 PyObject *f;
4892 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004893
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004894 char *cmdstring;
4895 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004896 int bufsize = -1;
4897 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004898 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004899
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004900 if (*mode == 't')
4901 tm = _O_TEXT;
4902 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004903 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004904 return NULL;
4905 } else
4906 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004907
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004908 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004909 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004910 return NULL;
4911 }
4912
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004913 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004914
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004915 return f;
4916}
4917
4918/*
4919 * Variation on win32pipe.popen
4920 *
Tim Peters5aa91602002-01-30 05:46:57 +00004921 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004922 * and stdout+stderr combined as a single pipe.
4923 */
4924
4925static PyObject *
4926win32_popen4(PyObject *self, PyObject *args)
4927{
4928 PyObject *f;
4929 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004930
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004931 char *cmdstring;
4932 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004933 int bufsize = -1;
4934 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004935 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004936
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004937 if (*mode == 't')
4938 tm = _O_TEXT;
4939 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004940 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004941 return NULL;
4942 } else
4943 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004944
4945 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004946 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004947 return NULL;
4948 }
4949
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004950 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004951
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004952 return f;
4953}
4954
Mark Hammond08501372001-01-31 07:30:29 +00004955static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004956_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004957 HANDLE hStdin,
4958 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004959 HANDLE hStderr,
4960 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004961{
4962 PROCESS_INFORMATION piProcInfo;
4963 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004964 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004965 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004966 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004967 int i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004968 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004969
4970 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004971 char *comshell;
4972
Tim Peters92e4dd82002-10-05 01:47:34 +00004973 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004974 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
Martin v. Löwis18e16552006-02-15 17:27:45 +00004975 /* x < i, so x fits into an integer */
4976 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00004977
4978 /* Explicitly check if we are using COMMAND.COM. If we are
4979 * then use the w9xpopen hack.
4980 */
4981 comshell = s1 + x;
4982 while (comshell >= s1 && *comshell != '\\')
4983 --comshell;
4984 ++comshell;
4985
4986 if (GetVersion() < 0x80000000 &&
4987 _stricmp(comshell, "command.com") != 0) {
4988 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004989 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004990 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004991 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004992 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004993 }
4994 else {
4995 /*
Tim Peters402d5982001-08-27 06:37:48 +00004996 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4997 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004998 */
Mark Hammond08501372001-01-31 07:30:29 +00004999 char modulepath[_MAX_PATH];
5000 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005001 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
Martin v. Löwis26fd9602006-04-22 11:15:41 +00005002 for (x = i = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005003 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005004 x = i+1;
5005 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00005006 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00005007 strncat(modulepath,
5008 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00005009 (sizeof(modulepath)/sizeof(modulepath[0]))
5010 -strlen(modulepath));
5011 if (stat(modulepath, &statinfo) != 0) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00005012 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
Tim Peters5aa91602002-01-30 05:46:57 +00005013 /* Eeek - file-not-found - possibly an embedding
5014 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00005015 */
Tim Peters5aa91602002-01-30 05:46:57 +00005016 strncpy(modulepath,
5017 Py_GetExecPrefix(),
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00005018 mplen);
5019 modulepath[mplen-1] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00005020 if (modulepath[strlen(modulepath)-1] != '\\')
5021 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00005022 strncat(modulepath,
5023 szConsoleSpawn,
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00005024 mplen-strlen(modulepath));
Mark Hammond08501372001-01-31 07:30:29 +00005025 /* No where else to look - raise an easily identifiable
5026 error, rather than leaving Windows to report
5027 "file not found" - as the user is probably blissfully
5028 unaware this shim EXE is used, and it will confuse them.
5029 (well, it confused me for a while ;-)
5030 */
5031 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00005032 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00005033 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00005034 "for popen to work with your shell "
5035 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00005036 szConsoleSpawn);
5037 return FALSE;
5038 }
5039 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005040 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00005041 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005042 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00005043
Tim Peters92e4dd82002-10-05 01:47:34 +00005044 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005045 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00005046 /* To maintain correct argument passing semantics,
5047 we pass the command-line as it stands, and allow
5048 quoting to be applied. w9xpopen.exe will then
5049 use its argv vector, and re-quote the necessary
5050 args for the ultimate child process.
5051 */
Tim Peters75cdad52001-11-28 22:07:30 +00005052 PyOS_snprintf(
5053 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00005054 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005055 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005056 s1,
5057 s3,
5058 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005059 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00005060 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005061 dialog:
5062 "Your program accessed mem currently in use at xxx"
5063 and a hopeful warning about the stability of your
5064 system.
Jesus Cea585ad8a2009-07-02 15:37:21 +00005065 Cost is Ctrl+C won't kill children, but anyone
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005066 who cares can have a go!
5067 */
5068 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005069 }
5070 }
5071
5072 /* Could be an else here to try cmd.exe / command.com in the path
5073 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00005074 else {
Tim Peters402d5982001-08-27 06:37:48 +00005075 PyErr_SetString(PyExc_RuntimeError,
5076 "Cannot locate a COMSPEC environment variable to "
5077 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00005078 return FALSE;
5079 }
Tim Peters5aa91602002-01-30 05:46:57 +00005080
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005081 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
5082 siStartInfo.cb = sizeof(STARTUPINFO);
5083 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
5084 siStartInfo.hStdInput = hStdin;
5085 siStartInfo.hStdOutput = hStdout;
5086 siStartInfo.hStdError = hStderr;
5087 siStartInfo.wShowWindow = SW_HIDE;
5088
5089 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005090 s2,
5091 NULL,
5092 NULL,
5093 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005094 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005095 NULL,
5096 NULL,
5097 &siStartInfo,
5098 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005099 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005100 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005101
Mark Hammondb37a3732000-08-14 04:47:33 +00005102 /* Return process handle */
5103 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005104 return TRUE;
5105 }
Tim Peters402d5982001-08-27 06:37:48 +00005106 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005107 return FALSE;
5108}
5109
5110/* The following code is based off of KB: Q190351 */
5111
5112static PyObject *
5113_PyPopen(char *cmdstring, int mode, int n)
5114{
5115 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
5116 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00005117 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00005118
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005119 SECURITY_ATTRIBUTES saAttr;
5120 BOOL fSuccess;
5121 int fd1, fd2, fd3;
5122 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00005123 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005124 PyObject *f;
5125
5126 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5127 saAttr.bInheritHandle = TRUE;
5128 saAttr.lpSecurityDescriptor = NULL;
5129
5130 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
5131 return win32_error("CreatePipe", NULL);
5132
5133 /* Create new output read handle and the input write handle. Set
5134 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00005135 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005136 * being created. */
5137 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005138 GetCurrentProcess(), &hChildStdinWrDup, 0,
5139 FALSE,
5140 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005141 if (!fSuccess)
5142 return win32_error("DuplicateHandle", NULL);
5143
5144 /* Close the inheritable version of ChildStdin
5145 that we're using. */
5146 CloseHandle(hChildStdinWr);
5147
5148 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
5149 return win32_error("CreatePipe", NULL);
5150
5151 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005152 GetCurrentProcess(), &hChildStdoutRdDup, 0,
5153 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005154 if (!fSuccess)
5155 return win32_error("DuplicateHandle", NULL);
5156
5157 /* Close the inheritable version of ChildStdout
5158 that we're using. */
5159 CloseHandle(hChildStdoutRd);
5160
5161 if (n != POPEN_4) {
5162 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
5163 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005164 fSuccess = DuplicateHandle(GetCurrentProcess(),
5165 hChildStderrRd,
5166 GetCurrentProcess(),
5167 &hChildStderrRdDup, 0,
5168 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005169 if (!fSuccess)
5170 return win32_error("DuplicateHandle", NULL);
5171 /* Close the inheritable version of ChildStdErr that we're using. */
5172 CloseHandle(hChildStderrRd);
5173 }
Tim Peters5aa91602002-01-30 05:46:57 +00005174
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005175 switch (n) {
5176 case POPEN_1:
5177 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5178 case _O_WRONLY | _O_TEXT:
5179 /* Case for writing to child Stdin in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005180 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005181 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005182 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005183 PyFile_SetBufSize(f, 0);
5184 /* We don't care about these pipes anymore, so close them. */
5185 CloseHandle(hChildStdoutRdDup);
5186 CloseHandle(hChildStderrRdDup);
5187 break;
5188
5189 case _O_RDONLY | _O_TEXT:
5190 /* Case for reading from child Stdout in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005191 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005192 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005193 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005194 PyFile_SetBufSize(f, 0);
5195 /* We don't care about these pipes anymore, so close them. */
5196 CloseHandle(hChildStdinWrDup);
5197 CloseHandle(hChildStderrRdDup);
5198 break;
5199
5200 case _O_RDONLY | _O_BINARY:
5201 /* Case for readinig from child Stdout in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005202 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005203 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005204 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005205 PyFile_SetBufSize(f, 0);
5206 /* We don't care about these pipes anymore, so close them. */
5207 CloseHandle(hChildStdinWrDup);
5208 CloseHandle(hChildStderrRdDup);
5209 break;
5210
5211 case _O_WRONLY | _O_BINARY:
5212 /* Case for writing to child Stdin in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005213 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005214 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005215 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005216 PyFile_SetBufSize(f, 0);
5217 /* We don't care about these pipes anymore, so close them. */
5218 CloseHandle(hChildStdoutRdDup);
5219 CloseHandle(hChildStderrRdDup);
5220 break;
5221 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005222 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005223 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005224
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005225 case POPEN_2:
5226 case POPEN_4:
5227 {
5228 char *m1, *m2;
5229 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005230
Tim Peters7dca21e2002-08-19 00:42:29 +00005231 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005232 m1 = "r";
5233 m2 = "w";
5234 } else {
5235 m1 = "rb";
5236 m2 = "wb";
5237 }
5238
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005239 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005240 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005241 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005242 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005243 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005244 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00005245 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005246 PyFile_SetBufSize(p2, 0);
5247
5248 if (n != 4)
5249 CloseHandle(hChildStderrRdDup);
5250
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005251 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00005252 Py_XDECREF(p1);
5253 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00005254 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005255 break;
5256 }
Tim Peters5aa91602002-01-30 05:46:57 +00005257
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005258 case POPEN_3:
5259 {
5260 char *m1, *m2;
5261 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005262
Tim Peters7dca21e2002-08-19 00:42:29 +00005263 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005264 m1 = "r";
5265 m2 = "w";
5266 } else {
5267 m1 = "rb";
5268 m2 = "wb";
5269 }
5270
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005271 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005272 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005273 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005274 f2 = _fdopen(fd2, m1);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005275 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005276 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005277 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00005278 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5279 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005280 PyFile_SetBufSize(p1, 0);
5281 PyFile_SetBufSize(p2, 0);
5282 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005283 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00005284 Py_XDECREF(p1);
5285 Py_XDECREF(p2);
5286 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00005287 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005288 break;
5289 }
5290 }
5291
5292 if (n == POPEN_4) {
5293 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005294 hChildStdinRd,
5295 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005296 hChildStdoutWr,
5297 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005298 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005299 }
5300 else {
5301 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005302 hChildStdinRd,
5303 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005304 hChildStderrWr,
5305 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005306 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005307 }
5308
Mark Hammondb37a3732000-08-14 04:47:33 +00005309 /*
5310 * Insert the files we've created into the process dictionary
5311 * all referencing the list with the process handle and the
5312 * initial number of files (see description below in _PyPclose).
5313 * Since if _PyPclose later tried to wait on a process when all
5314 * handles weren't closed, it could create a deadlock with the
5315 * child, we spend some energy here to try to ensure that we
5316 * either insert all file handles into the dictionary or none
5317 * at all. It's a little clumsy with the various popen modes
5318 * and variable number of files involved.
5319 */
5320 if (!_PyPopenProcs) {
5321 _PyPopenProcs = PyDict_New();
5322 }
5323
5324 if (_PyPopenProcs) {
5325 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5326 int ins_rc[3];
5327
5328 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5329 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
5330
5331 procObj = PyList_New(2);
5332 hProcessObj = PyLong_FromVoidPtr(hProcess);
5333 intObj = PyInt_FromLong(file_count);
5334
5335 if (procObj && hProcessObj && intObj) {
5336 PyList_SetItem(procObj,0,hProcessObj);
5337 PyList_SetItem(procObj,1,intObj);
5338
5339 fileObj[0] = PyLong_FromVoidPtr(f1);
5340 if (fileObj[0]) {
5341 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5342 fileObj[0],
5343 procObj);
5344 }
5345 if (file_count >= 2) {
5346 fileObj[1] = PyLong_FromVoidPtr(f2);
5347 if (fileObj[1]) {
5348 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5349 fileObj[1],
5350 procObj);
5351 }
5352 }
5353 if (file_count >= 3) {
5354 fileObj[2] = PyLong_FromVoidPtr(f3);
5355 if (fileObj[2]) {
5356 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5357 fileObj[2],
5358 procObj);
5359 }
5360 }
5361
5362 if (ins_rc[0] < 0 || !fileObj[0] ||
5363 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5364 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5365 /* Something failed - remove any dictionary
5366 * entries that did make it.
5367 */
5368 if (!ins_rc[0] && fileObj[0]) {
5369 PyDict_DelItem(_PyPopenProcs,
5370 fileObj[0]);
5371 }
5372 if (!ins_rc[1] && fileObj[1]) {
5373 PyDict_DelItem(_PyPopenProcs,
5374 fileObj[1]);
5375 }
5376 if (!ins_rc[2] && fileObj[2]) {
5377 PyDict_DelItem(_PyPopenProcs,
5378 fileObj[2]);
5379 }
5380 }
5381 }
Tim Peters5aa91602002-01-30 05:46:57 +00005382
Mark Hammondb37a3732000-08-14 04:47:33 +00005383 /*
5384 * Clean up our localized references for the dictionary keys
5385 * and value since PyDict_SetItem will Py_INCREF any copies
5386 * that got placed in the dictionary.
5387 */
5388 Py_XDECREF(procObj);
5389 Py_XDECREF(fileObj[0]);
5390 Py_XDECREF(fileObj[1]);
5391 Py_XDECREF(fileObj[2]);
5392 }
5393
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005394 /* Child is launched. Close the parents copy of those pipe
5395 * handles that only the child should have open. You need to
5396 * make sure that no handles to the write end of the output pipe
5397 * are maintained in this process or else the pipe will not close
5398 * when the child process exits and the ReadFile will hang. */
5399
5400 if (!CloseHandle(hChildStdinRd))
5401 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005402
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005403 if (!CloseHandle(hChildStdoutWr))
5404 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005405
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005406 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5407 return win32_error("CloseHandle", NULL);
5408
5409 return f;
5410}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005411
5412/*
5413 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5414 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005415 *
5416 * This function uses the _PyPopenProcs dictionary in order to map the
5417 * input file pointer to information about the process that was
5418 * originally created by the popen* call that created the file pointer.
5419 * The dictionary uses the file pointer as a key (with one entry
5420 * inserted for each file returned by the original popen* call) and a
5421 * single list object as the value for all files from a single call.
5422 * The list object contains the Win32 process handle at [0], and a file
5423 * count at [1], which is initialized to the total number of file
5424 * handles using that list.
5425 *
5426 * This function closes whichever handle it is passed, and decrements
5427 * the file count in the dictionary for the process handle pointed to
5428 * by this file. On the last close (when the file count reaches zero),
5429 * this function will wait for the child process and then return its
5430 * exit code as the result of the close() operation. This permits the
5431 * files to be closed in any order - it is always the close() of the
5432 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005433 *
5434 * NOTE: This function is currently called with the GIL released.
5435 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005436 */
Tim Peters736aa322000-09-01 06:51:24 +00005437
Fredrik Lundh56055a42000-07-23 19:47:12 +00005438static int _PyPclose(FILE *file)
5439{
Fredrik Lundh20318932000-07-26 17:29:12 +00005440 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005441 DWORD exit_code;
5442 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00005443 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5444 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005445#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005446 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005447#endif
5448
Fredrik Lundh20318932000-07-26 17:29:12 +00005449 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00005450 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00005451 */
5452 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005453#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005454 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005455#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005456 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005457 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5458 (procObj = PyDict_GetItem(_PyPopenProcs,
5459 fileObj)) != NULL &&
5460 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5461 (intObj = PyList_GetItem(procObj,1)) != NULL) {
5462
5463 hProcess = PyLong_AsVoidPtr(hProcessObj);
5464 file_count = PyInt_AsLong(intObj);
5465
5466 if (file_count > 1) {
5467 /* Still other files referencing process */
5468 file_count--;
5469 PyList_SetItem(procObj,1,
5470 PyInt_FromLong(file_count));
5471 } else {
5472 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00005473 if (result != EOF &&
5474 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5475 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00005476 /* Possible truncation here in 16-bit environments, but
5477 * real exit codes are just the lower byte in any event.
5478 */
5479 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005480 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00005481 /* Indicate failure - this will cause the file object
5482 * to raise an I/O error and translate the last Win32
5483 * error code from errno. We do have a problem with
5484 * last errors that overlap the normal errno table,
5485 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005486 */
Fredrik Lundh20318932000-07-26 17:29:12 +00005487 if (result != EOF) {
5488 /* If the error wasn't from the fclose(), then
5489 * set errno for the file object error handling.
5490 */
5491 errno = GetLastError();
5492 }
5493 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005494 }
5495
5496 /* Free up the native handle at this point */
5497 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00005498 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005499
Mark Hammondb37a3732000-08-14 04:47:33 +00005500 /* Remove this file pointer from dictionary */
5501 PyDict_DelItem(_PyPopenProcs, fileObj);
5502
5503 if (PyDict_Size(_PyPopenProcs) == 0) {
5504 Py_DECREF(_PyPopenProcs);
5505 _PyPopenProcs = NULL;
5506 }
5507
5508 } /* if object retrieval ok */
5509
5510 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005511 } /* if _PyPopenProcs */
5512
Tim Peters736aa322000-09-01 06:51:24 +00005513#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005514 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005515#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005516 return result;
5517}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005518
5519#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005520static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005521posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005522{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005523 char *name;
5524 char *mode = "r";
5525 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00005526 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005527 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005528 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00005529 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00005530 /* Strip mode of binary or text modifiers */
5531 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5532 mode = "r";
5533 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5534 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00005535 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005536 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005537 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00005538 if (fp == NULL)
5539 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005540 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005541 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005542 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005543 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005544}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005545
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005546#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005547#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005548
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005549
Guido van Rossumb6775db1994-08-01 11:34:53 +00005550#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005551PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005552"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005553Set the current process's user id.");
5554
Barry Warsaw53699e91996-12-10 23:23:01 +00005555static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005556posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005557{
Gregory P. Smithdd7b6302009-04-06 06:47:37 +00005558 long uid_arg;
5559 uid_t uid;
5560 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005561 return NULL;
Gregory P. Smithdd7b6302009-04-06 06:47:37 +00005562 uid = uid_arg;
5563 if (uid != uid_arg) {
5564 PyErr_SetString(PyExc_OverflowError, "user id too big");
5565 return NULL;
5566 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005567 if (setuid(uid) < 0)
5568 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005569 Py_INCREF(Py_None);
5570 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005571}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005572#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005573
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005574
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005575#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005576PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005577"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005578Set the current process's effective user id.");
5579
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005580static PyObject *
5581posix_seteuid (PyObject *self, PyObject *args)
5582{
Gregory P. Smithdd7b6302009-04-06 06:47:37 +00005583 long euid_arg;
5584 uid_t euid;
5585 if (!PyArg_ParseTuple(args, "l", &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005586 return NULL;
Gregory P. Smithdd7b6302009-04-06 06:47:37 +00005587 euid = euid_arg;
5588 if (euid != euid_arg) {
5589 PyErr_SetString(PyExc_OverflowError, "user id too big");
5590 return NULL;
5591 }
5592 if (seteuid(euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005593 return posix_error();
5594 } else {
5595 Py_INCREF(Py_None);
5596 return Py_None;
5597 }
5598}
5599#endif /* HAVE_SETEUID */
5600
5601#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005602PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005603"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005604Set the current process's effective group id.");
5605
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005606static PyObject *
5607posix_setegid (PyObject *self, PyObject *args)
5608{
Gregory P. Smithdd7b6302009-04-06 06:47:37 +00005609 long egid_arg;
5610 gid_t egid;
5611 if (!PyArg_ParseTuple(args, "l", &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005612 return NULL;
Gregory P. Smithdd7b6302009-04-06 06:47:37 +00005613 egid = egid_arg;
5614 if (egid != egid_arg) {
5615 PyErr_SetString(PyExc_OverflowError, "group id too big");
5616 return NULL;
5617 }
5618 if (setegid(egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005619 return posix_error();
5620 } else {
5621 Py_INCREF(Py_None);
5622 return Py_None;
5623 }
5624}
5625#endif /* HAVE_SETEGID */
5626
5627#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005628PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005629"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005630Set the current process's real and effective user ids.");
5631
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005632static PyObject *
5633posix_setreuid (PyObject *self, PyObject *args)
5634{
Gregory P. Smithdd7b6302009-04-06 06:47:37 +00005635 long ruid_arg, euid_arg;
5636 uid_t ruid, euid;
5637 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005638 return NULL;
Gregory P. Smithdd7b6302009-04-06 06:47:37 +00005639 ruid = ruid_arg;
5640 euid = euid_arg;
5641 if (euid != euid_arg || ruid != ruid_arg) {
5642 PyErr_SetString(PyExc_OverflowError, "user id too big");
5643 return NULL;
5644 }
5645 if (setreuid(ruid, euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005646 return posix_error();
5647 } else {
5648 Py_INCREF(Py_None);
5649 return Py_None;
5650 }
5651}
5652#endif /* HAVE_SETREUID */
5653
5654#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005655PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005656"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005657Set the current process's real and effective group ids.");
5658
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005659static PyObject *
5660posix_setregid (PyObject *self, PyObject *args)
5661{
Gregory P. Smithdd7b6302009-04-06 06:47:37 +00005662 long rgid_arg, egid_arg;
5663 gid_t rgid, egid;
5664 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005665 return NULL;
Gregory P. Smithdd7b6302009-04-06 06:47:37 +00005666 rgid = rgid_arg;
5667 egid = egid_arg;
5668 if (egid != egid_arg || rgid != rgid_arg) {
5669 PyErr_SetString(PyExc_OverflowError, "group id too big");
5670 return NULL;
5671 }
5672 if (setregid(rgid, egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005673 return posix_error();
5674 } else {
5675 Py_INCREF(Py_None);
5676 return Py_None;
5677 }
5678}
5679#endif /* HAVE_SETREGID */
5680
Guido van Rossumb6775db1994-08-01 11:34:53 +00005681#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005682PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005683"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005684Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005685
Barry Warsaw53699e91996-12-10 23:23:01 +00005686static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005687posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005688{
Gregory P. Smithdd7b6302009-04-06 06:47:37 +00005689 long gid_arg;
5690 gid_t gid;
5691 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005692 return NULL;
Gregory P. Smithdd7b6302009-04-06 06:47:37 +00005693 gid = gid_arg;
5694 if (gid != gid_arg) {
5695 PyErr_SetString(PyExc_OverflowError, "group id too big");
5696 return NULL;
5697 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005698 if (setgid(gid) < 0)
5699 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005700 Py_INCREF(Py_None);
5701 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005702}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005703#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005704
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005705#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005706PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005707"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005708Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005709
5710static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005711posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005712{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005713 int i, len;
5714 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005715
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005716 if (!PySequence_Check(groups)) {
5717 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5718 return NULL;
5719 }
5720 len = PySequence_Size(groups);
5721 if (len > MAX_GROUPS) {
5722 PyErr_SetString(PyExc_ValueError, "too many groups");
5723 return NULL;
5724 }
5725 for(i = 0; i < len; i++) {
5726 PyObject *elem;
5727 elem = PySequence_GetItem(groups, i);
5728 if (!elem)
5729 return NULL;
5730 if (!PyInt_Check(elem)) {
Georg Brandla13c2442005-11-22 19:30:31 +00005731 if (!PyLong_Check(elem)) {
5732 PyErr_SetString(PyExc_TypeError,
5733 "groups must be integers");
5734 Py_DECREF(elem);
5735 return NULL;
5736 } else {
5737 unsigned long x = PyLong_AsUnsignedLong(elem);
5738 if (PyErr_Occurred()) {
5739 PyErr_SetString(PyExc_TypeError,
5740 "group id too big");
5741 Py_DECREF(elem);
5742 return NULL;
5743 }
5744 grouplist[i] = x;
Gregory P. Smithdd7b6302009-04-06 06:47:37 +00005745 /* read back to see if it fits in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00005746 if (grouplist[i] != x) {
5747 PyErr_SetString(PyExc_TypeError,
5748 "group id too big");
5749 Py_DECREF(elem);
5750 return NULL;
5751 }
5752 }
5753 } else {
5754 long x = PyInt_AsLong(elem);
5755 grouplist[i] = x;
5756 if (grouplist[i] != x) {
5757 PyErr_SetString(PyExc_TypeError,
5758 "group id too big");
5759 Py_DECREF(elem);
5760 return NULL;
5761 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005762 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005763 Py_DECREF(elem);
5764 }
5765
5766 if (setgroups(len, grouplist) < 0)
5767 return posix_error();
5768 Py_INCREF(Py_None);
5769 return Py_None;
5770}
5771#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005772
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005773#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00005774static PyObject *
Christian Heimesd491d712008-02-01 18:49:26 +00005775wait_helper(pid_t pid, int status, struct rusage *ru)
Neal Norwitz05a45592006-03-20 06:30:08 +00005776{
5777 PyObject *result;
5778 static PyObject *struct_rusage;
5779
5780 if (pid == -1)
5781 return posix_error();
5782
5783 if (struct_rusage == NULL) {
Christian Heimes000a0742008-01-03 22:16:32 +00005784 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Neal Norwitz05a45592006-03-20 06:30:08 +00005785 if (m == NULL)
5786 return NULL;
5787 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5788 Py_DECREF(m);
5789 if (struct_rusage == NULL)
5790 return NULL;
5791 }
5792
5793 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5794 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5795 if (!result)
5796 return NULL;
5797
5798#ifndef doubletime
5799#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5800#endif
5801
5802 PyStructSequence_SET_ITEM(result, 0,
5803 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5804 PyStructSequence_SET_ITEM(result, 1,
5805 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5806#define SET_INT(result, index, value)\
5807 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5808 SET_INT(result, 2, ru->ru_maxrss);
5809 SET_INT(result, 3, ru->ru_ixrss);
5810 SET_INT(result, 4, ru->ru_idrss);
5811 SET_INT(result, 5, ru->ru_isrss);
5812 SET_INT(result, 6, ru->ru_minflt);
5813 SET_INT(result, 7, ru->ru_majflt);
5814 SET_INT(result, 8, ru->ru_nswap);
5815 SET_INT(result, 9, ru->ru_inblock);
5816 SET_INT(result, 10, ru->ru_oublock);
5817 SET_INT(result, 11, ru->ru_msgsnd);
5818 SET_INT(result, 12, ru->ru_msgrcv);
5819 SET_INT(result, 13, ru->ru_nsignals);
5820 SET_INT(result, 14, ru->ru_nvcsw);
5821 SET_INT(result, 15, ru->ru_nivcsw);
5822#undef SET_INT
5823
5824 if (PyErr_Occurred()) {
5825 Py_DECREF(result);
5826 return NULL;
5827 }
5828
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00005829 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00005830}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005831#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00005832
5833#ifdef HAVE_WAIT3
5834PyDoc_STRVAR(posix_wait3__doc__,
5835"wait3(options) -> (pid, status, rusage)\n\n\
5836Wait for completion of a child process.");
5837
5838static PyObject *
5839posix_wait3(PyObject *self, PyObject *args)
5840{
Christian Heimesd491d712008-02-01 18:49:26 +00005841 pid_t pid;
5842 int options;
Neal Norwitz05a45592006-03-20 06:30:08 +00005843 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005844 WAIT_TYPE status;
5845 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005846
5847 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5848 return NULL;
5849
5850 Py_BEGIN_ALLOW_THREADS
5851 pid = wait3(&status, options, &ru);
5852 Py_END_ALLOW_THREADS
5853
Neal Norwitzd5a37542006-03-20 06:48:34 +00005854 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005855}
5856#endif /* HAVE_WAIT3 */
5857
5858#ifdef HAVE_WAIT4
5859PyDoc_STRVAR(posix_wait4__doc__,
5860"wait4(pid, options) -> (pid, status, rusage)\n\n\
5861Wait for completion of a given child process.");
5862
5863static PyObject *
5864posix_wait4(PyObject *self, PyObject *args)
5865{
Christian Heimesd491d712008-02-01 18:49:26 +00005866 pid_t pid;
5867 int options;
Neal Norwitz05a45592006-03-20 06:30:08 +00005868 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005869 WAIT_TYPE status;
5870 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005871
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00005872 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
Neal Norwitz05a45592006-03-20 06:30:08 +00005873 return NULL;
5874
5875 Py_BEGIN_ALLOW_THREADS
5876 pid = wait4(pid, &status, options, &ru);
5877 Py_END_ALLOW_THREADS
5878
Neal Norwitzd5a37542006-03-20 06:48:34 +00005879 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005880}
5881#endif /* HAVE_WAIT4 */
5882
Guido van Rossumb6775db1994-08-01 11:34:53 +00005883#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005884PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005885"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005886Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005887
Barry Warsaw53699e91996-12-10 23:23:01 +00005888static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005889posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005890{
Christian Heimesd491d712008-02-01 18:49:26 +00005891 pid_t pid;
5892 int options;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005893 WAIT_TYPE status;
5894 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005895
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00005896 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00005897 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005898 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00005899 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00005900 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00005901 if (pid == -1)
5902 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005903
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00005904 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005905}
5906
Tim Petersab034fa2002-02-01 11:27:43 +00005907#elif defined(HAVE_CWAIT)
5908
5909/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005910PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005911"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005912"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005913
5914static PyObject *
5915posix_waitpid(PyObject *self, PyObject *args)
5916{
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005917 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005918 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005919
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00005920 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Tim Petersab034fa2002-02-01 11:27:43 +00005921 return NULL;
5922 Py_BEGIN_ALLOW_THREADS
5923 pid = _cwait(&status, pid, options);
5924 Py_END_ALLOW_THREADS
5925 if (pid == -1)
5926 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005927
5928 /* shift the status left a byte so this is more like the POSIX waitpid */
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00005929 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005930}
5931#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005932
Guido van Rossumad0ee831995-03-01 10:34:45 +00005933#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005934PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005935"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005936Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005937
Barry Warsaw53699e91996-12-10 23:23:01 +00005938static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005939posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005940{
Christian Heimesd491d712008-02-01 18:49:26 +00005941 pid_t pid;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005942 WAIT_TYPE status;
5943 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005944
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005945 Py_BEGIN_ALLOW_THREADS
5946 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00005947 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00005948 if (pid == -1)
5949 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005950
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00005951 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005952}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005953#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005954
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005955
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005956PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005957"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005958Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005959
Barry Warsaw53699e91996-12-10 23:23:01 +00005960static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005961posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005962{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005963#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005964 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005965#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005966#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00005967 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005968#else
5969 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5970#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005971#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005972}
5973
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005974
Guido van Rossumb6775db1994-08-01 11:34:53 +00005975#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005976PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005977"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005978Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005979
Barry Warsaw53699e91996-12-10 23:23:01 +00005980static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005981posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005982{
Ronald Oussoren10168f22006-10-22 10:45:18 +00005983 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00005984 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005985 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005986 int n;
Ronald Oussoren10168f22006-10-22 10:45:18 +00005987#ifdef Py_USING_UNICODE
5988 int arg_is_unicode = 0;
5989#endif
5990
5991 if (!PyArg_ParseTuple(args, "et:readlink",
5992 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005993 return NULL;
Ronald Oussoren10168f22006-10-22 10:45:18 +00005994#ifdef Py_USING_UNICODE
5995 v = PySequence_GetItem(args, 0);
Neal Norwitz91a57212007-08-12 17:11:13 +00005996 if (v == NULL) {
5997 PyMem_Free(path);
5998 return NULL;
5999 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006000
6001 if (PyUnicode_Check(v)) {
6002 arg_is_unicode = 1;
6003 }
6004 Py_DECREF(v);
6005#endif
6006
Barry Warsaw53699e91996-12-10 23:23:01 +00006007 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00006008 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00006009 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006010 if (n < 0)
Neal Norwitz91a57212007-08-12 17:11:13 +00006011 return posix_error_with_allocated_filename(path);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006012
Neal Norwitz91a57212007-08-12 17:11:13 +00006013 PyMem_Free(path);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006014 v = PyString_FromStringAndSize(buf, n);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006015#ifdef Py_USING_UNICODE
6016 if (arg_is_unicode) {
6017 PyObject *w;
6018
6019 w = PyUnicode_FromEncodedObject(v,
6020 Py_FileSystemDefaultEncoding,
6021 "strict");
6022 if (w != NULL) {
6023 Py_DECREF(v);
6024 v = w;
6025 }
6026 else {
6027 /* fall back to the original byte string, as
6028 discussed in patch #683592 */
6029 PyErr_Clear();
6030 }
6031 }
6032#endif
6033 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006034}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006035#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006036
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006037
Guido van Rossumb6775db1994-08-01 11:34:53 +00006038#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006039PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006040"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006041Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006042
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006043static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006044posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006045{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00006046 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006047}
6048#endif /* HAVE_SYMLINK */
6049
6050
6051#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006052#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6053static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006054system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006055{
6056 ULONG value = 0;
6057
6058 Py_BEGIN_ALLOW_THREADS
6059 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6060 Py_END_ALLOW_THREADS
6061
6062 return value;
6063}
6064
6065static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006066posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006067{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006068 /* Currently Only Uptime is Provided -- Others Later */
6069 return Py_BuildValue("ddddd",
6070 (double)0 /* t.tms_utime / HZ */,
6071 (double)0 /* t.tms_stime / HZ */,
6072 (double)0 /* t.tms_cutime / HZ */,
6073 (double)0 /* t.tms_cstime / HZ */,
6074 (double)system_uptime() / 1000);
6075}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006076#else /* not OS2 */
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00006077#define NEED_TICKS_PER_SECOND
6078static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006079static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006080posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006081{
6082 struct tms t;
6083 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00006084 errno = 0;
6085 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00006086 if (c == (clock_t) -1)
6087 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006088 return Py_BuildValue("ddddd",
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00006089 (double)t.tms_utime / ticks_per_second,
6090 (double)t.tms_stime / ticks_per_second,
6091 (double)t.tms_cutime / ticks_per_second,
6092 (double)t.tms_cstime / ticks_per_second,
6093 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006094}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006095#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006096#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006097
6098
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006099#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006100#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006101static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006102posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006103{
6104 FILETIME create, exit, kernel, user;
6105 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006106 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00006107 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6108 /* The fields of a FILETIME structure are the hi and lo part
6109 of a 64-bit value expressed in 100 nanosecond units.
6110 1e7 is one second in such units; 1e-7 the inverse.
6111 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6112 */
Barry Warsaw53699e91996-12-10 23:23:01 +00006113 return Py_BuildValue(
6114 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00006115 (double)(user.dwHighDateTime*429.4967296 +
6116 user.dwLowDateTime*1e-7),
Georg Brandl0a40ffb2008-02-13 07:20:22 +00006117 (double)(kernel.dwHighDateTime*429.4967296 +
6118 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00006119 (double)0,
6120 (double)0,
6121 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006122}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006123#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006124
6125#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006126PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006127"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006128Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006129#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006130
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006131
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006132#ifdef HAVE_GETSID
6133PyDoc_STRVAR(posix_getsid__doc__,
6134"getsid(pid) -> sid\n\n\
6135Call the system call getsid().");
6136
6137static PyObject *
6138posix_getsid(PyObject *self, PyObject *args)
6139{
Christian Heimesd491d712008-02-01 18:49:26 +00006140 pid_t pid;
6141 int sid;
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00006142 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006143 return NULL;
6144 sid = getsid(pid);
6145 if (sid < 0)
6146 return posix_error();
6147 return PyInt_FromLong((long)sid);
6148}
6149#endif /* HAVE_GETSID */
6150
6151
Guido van Rossumb6775db1994-08-01 11:34:53 +00006152#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006153PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006154"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006155Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006156
Barry Warsaw53699e91996-12-10 23:23:01 +00006157static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006158posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006159{
Guido van Rossum687dd131993-05-17 08:34:16 +00006160 if (setsid() < 0)
6161 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006162 Py_INCREF(Py_None);
6163 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006164}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006165#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006166
Guido van Rossumb6775db1994-08-01 11:34:53 +00006167#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006168PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006169"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006170Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006171
Barry Warsaw53699e91996-12-10 23:23:01 +00006172static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006173posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006174{
Christian Heimesd491d712008-02-01 18:49:26 +00006175 pid_t pid;
6176 int pgrp;
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00006177 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00006178 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006179 if (setpgid(pid, pgrp) < 0)
6180 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006181 Py_INCREF(Py_None);
6182 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006183}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006184#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006185
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006186
Guido van Rossumb6775db1994-08-01 11:34:53 +00006187#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006188PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006189"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006190Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006191
Barry Warsaw53699e91996-12-10 23:23:01 +00006192static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006193posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006194{
Christian Heimese6a80742008-02-03 19:51:13 +00006195 int fd;
6196 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006197 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00006198 return NULL;
6199 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006200 if (pgid < 0)
6201 return posix_error();
Antoine Pitrou794b3fc2009-05-23 15:47:13 +00006202 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006203}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006204#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006205
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006206
Guido van Rossumb6775db1994-08-01 11:34:53 +00006207#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006208PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006209"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006210Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006211
Barry Warsaw53699e91996-12-10 23:23:01 +00006212static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006213posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006214{
Antoine Pitroucd1376d2009-05-23 16:19:24 +00006215 int fd;
6216 pid_t pgid;
6217 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00006218 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006219 if (tcsetpgrp(fd, pgid) < 0)
6220 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00006221 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00006222 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006223}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006224#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006225
Guido van Rossum687dd131993-05-17 08:34:16 +00006226/* Functions acting on file descriptors */
6227
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006228PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006229"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006230Open a file (for low level IO).");
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_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006234{
Mark Hammondef8b6542001-05-13 08:04:26 +00006235 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006236 int flag;
6237 int mode = 0777;
6238 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006239
6240#ifdef MS_WINDOWS
6241 if (unicode_file_names()) {
6242 PyUnicodeObject *po;
6243 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6244 Py_BEGIN_ALLOW_THREADS
6245 /* PyUnicode_AS_UNICODE OK without thread
6246 lock as it is a simple dereference. */
6247 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6248 Py_END_ALLOW_THREADS
6249 if (fd < 0)
6250 return posix_error();
6251 return PyInt_FromLong((long)fd);
6252 }
6253 /* Drop the argument parsing error as narrow strings
6254 are also valid. */
6255 PyErr_Clear();
6256 }
6257#endif
6258
Tim Peters5aa91602002-01-30 05:46:57 +00006259 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00006260 Py_FileSystemDefaultEncoding, &file,
6261 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00006262 return NULL;
6263
Barry Warsaw53699e91996-12-10 23:23:01 +00006264 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006265 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00006266 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006267 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00006268 return posix_error_with_allocated_filename(file);
6269 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00006270 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006271}
6272
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006273
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006274PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006275"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006276Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006277
Barry Warsaw53699e91996-12-10 23:23:01 +00006278static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006279posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006280{
6281 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006282 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006283 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006284 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006285 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006286 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006287 if (res < 0)
6288 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006289 Py_INCREF(Py_None);
6290 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006291}
6292
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006293
Georg Brandl309501a2008-01-19 20:22:13 +00006294PyDoc_STRVAR(posix_closerange__doc__,
6295"closerange(fd_low, fd_high)\n\n\
6296Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6297
6298static PyObject *
6299posix_closerange(PyObject *self, PyObject *args)
6300{
6301 int fd_from, fd_to, i;
6302 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6303 return NULL;
6304 Py_BEGIN_ALLOW_THREADS
6305 for (i = fd_from; i < fd_to; i++)
6306 close(i);
6307 Py_END_ALLOW_THREADS
6308 Py_RETURN_NONE;
6309}
6310
6311
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006312PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006313"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006314Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006315
Barry Warsaw53699e91996-12-10 23:23:01 +00006316static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006317posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006318{
6319 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006320 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006321 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006322 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006323 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006324 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006325 if (fd < 0)
6326 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006327 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006328}
6329
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006330
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006331PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006332"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006333Duplicate 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_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006337{
6338 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006339 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00006340 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006341 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006342 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00006343 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006344 if (res < 0)
6345 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006346 Py_INCREF(Py_None);
6347 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006348}
6349
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006350
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006351PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006352"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006353Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006354
Barry Warsaw53699e91996-12-10 23:23:01 +00006355static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006356posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006357{
6358 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006359#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006360 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006361#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00006362 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006363#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006364 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006365 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00006366 return NULL;
6367#ifdef SEEK_SET
6368 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6369 switch (how) {
6370 case 0: how = SEEK_SET; break;
6371 case 1: how = SEEK_CUR; break;
6372 case 2: how = SEEK_END; break;
6373 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006374#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006375
6376#if !defined(HAVE_LARGEFILE_SUPPORT)
6377 pos = PyInt_AsLong(posobj);
6378#else
6379 pos = PyLong_Check(posobj) ?
6380 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
6381#endif
6382 if (PyErr_Occurred())
6383 return NULL;
6384
Barry Warsaw53699e91996-12-10 23:23:01 +00006385 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006386#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00006387 res = _lseeki64(fd, pos, how);
6388#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006389 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006390#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006391 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006392 if (res < 0)
6393 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006394
6395#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00006396 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006397#else
6398 return PyLong_FromLongLong(res);
6399#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006400}
6401
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006402
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006403PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006404"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006405Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006406
Barry Warsaw53699e91996-12-10 23:23:01 +00006407static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006408posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006409{
Guido van Rossum8bac5461996-06-11 18:38:48 +00006410 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00006411 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006412 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006413 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00006414 if (size < 0) {
6415 errno = EINVAL;
6416 return posix_error();
6417 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006418 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006419 if (buffer == NULL)
6420 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006421 Py_BEGIN_ALLOW_THREADS
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006422 n = read(fd, PyString_AsString(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00006423 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00006424 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006425 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00006426 return posix_error();
6427 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00006428 if (n != size)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006429 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00006430 return buffer;
6431}
6432
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006433
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006434PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006435"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006436Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006437
Barry Warsaw53699e91996-12-10 23:23:01 +00006438static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006439posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006440{
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00006441 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006442 int fd;
6443 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006444
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00006445 if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00006446 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006447 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00006448 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00006449 Py_END_ALLOW_THREADS
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00006450 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00006451 if (size < 0)
6452 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006453 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006454}
6455
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006457PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006458"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006459Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006460
Barry Warsaw53699e91996-12-10 23:23:01 +00006461static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006462posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006463{
6464 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00006465 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00006466 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006467 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006468 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006469#ifdef __VMS
6470 /* on OpenVMS we must ensure that all bytes are written to the file */
6471 fsync(fd);
6472#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006473 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00006474 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00006475 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00006476 if (res != 0) {
6477#ifdef MS_WINDOWS
6478 return win32_error("fstat", NULL);
6479#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006480 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006481#endif
6482 }
Tim Peters5aa91602002-01-30 05:46:57 +00006483
Martin v. Löwis14694662006-02-03 12:54:16 +00006484 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006485}
6486
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006487
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006488PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006489"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006490Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006491
Barry Warsaw53699e91996-12-10 23:23:01 +00006492static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006493posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006494{
Guido van Rossum687dd131993-05-17 08:34:16 +00006495 int fd;
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006496 char *orgmode = "r";
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006497 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00006498 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00006499 PyObject *f;
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006500 char *mode;
6501 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00006502 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006503
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006504 /* Sanitize mode. See fileobject.c */
6505 mode = PyMem_MALLOC(strlen(orgmode)+3);
6506 if (!mode) {
6507 PyErr_NoMemory();
6508 return NULL;
6509 }
6510 strcpy(mode, orgmode);
6511 if (_PyFile_SanitizeMode(mode)) {
6512 PyMem_FREE(mode);
Thomas Heller1f043e22002-11-07 16:00:59 +00006513 return NULL;
6514 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006515 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006516#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Georg Brandl54a188a2006-03-31 20:00:11 +00006517 if (mode[0] == 'a') {
6518 /* try to make sure the O_APPEND flag is set */
6519 int flags;
6520 flags = fcntl(fd, F_GETFL);
6521 if (flags != -1)
6522 fcntl(fd, F_SETFL, flags | O_APPEND);
6523 fp = fdopen(fd, mode);
Thomas Wouters2a9a6b02006-03-31 22:38:19 +00006524 if (fp == NULL && flags != -1)
Georg Brandl54a188a2006-03-31 20:00:11 +00006525 /* restore old mode if fdopen failed */
6526 fcntl(fd, F_SETFL, flags);
6527 } else {
6528 fp = fdopen(fd, mode);
6529 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006530#else
6531 fp = fdopen(fd, mode);
6532#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006533 Py_END_ALLOW_THREADS
Neal Norwitzd83eb312007-05-02 04:47:55 +00006534 PyMem_FREE(mode);
Guido van Rossum687dd131993-05-17 08:34:16 +00006535 if (fp == NULL)
6536 return posix_error();
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006537 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006538 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00006539 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006540 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006541}
6542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006543PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006544"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006545Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006546connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006547
6548static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006549posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006550{
6551 int fd;
6552 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6553 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006554 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006555}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006556
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006557#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006558PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006559"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006560Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006561
Barry Warsaw53699e91996-12-10 23:23:01 +00006562static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006563posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006564{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006565#if defined(PYOS_OS2)
6566 HFILE read, write;
6567 APIRET rc;
6568
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006569 Py_BEGIN_ALLOW_THREADS
6570 rc = DosCreatePipe( &read, &write, 4096);
6571 Py_END_ALLOW_THREADS
6572 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006573 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006574
6575 return Py_BuildValue("(ii)", read, write);
6576#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006577#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00006578 int fds[2];
6579 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00006580 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006581 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00006582 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006583 if (res != 0)
6584 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006585 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006586#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00006587 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006588 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00006589 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00006590 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006591 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00006592 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00006593 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006594 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00006595 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6596 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006597 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006598#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006599#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006600}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006601#endif /* HAVE_PIPE */
6602
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006603
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006604#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006605PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006606"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006607Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006608
Barry Warsaw53699e91996-12-10 23:23:01 +00006609static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006610posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006611{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006612 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006613 int mode = 0666;
6614 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006615 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006616 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006617 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006618 res = mkfifo(filename, mode);
6619 Py_END_ALLOW_THREADS
6620 if (res < 0)
6621 return posix_error();
6622 Py_INCREF(Py_None);
6623 return Py_None;
6624}
6625#endif
6626
6627
Neal Norwitz11690112002-07-30 01:08:28 +00006628#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006629PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006630"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006631Create a filesystem node (file, device special file or named pipe)\n\
6632named filename. mode specifies both the permissions to use and the\n\
6633type of node to be created, being combined (bitwise OR) with one of\n\
6634S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006635device defines the newly created device special file (probably using\n\
6636os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006637
6638
6639static PyObject *
6640posix_mknod(PyObject *self, PyObject *args)
6641{
6642 char *filename;
6643 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006644 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006645 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00006646 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006647 return NULL;
6648 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006649 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00006650 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006651 if (res < 0)
6652 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006653 Py_INCREF(Py_None);
6654 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006655}
6656#endif
6657
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006658#ifdef HAVE_DEVICE_MACROS
6659PyDoc_STRVAR(posix_major__doc__,
6660"major(device) -> major number\n\
6661Extracts a device major number from a raw device number.");
6662
6663static PyObject *
6664posix_major(PyObject *self, PyObject *args)
6665{
6666 int device;
6667 if (!PyArg_ParseTuple(args, "i:major", &device))
6668 return NULL;
6669 return PyInt_FromLong((long)major(device));
6670}
6671
6672PyDoc_STRVAR(posix_minor__doc__,
6673"minor(device) -> minor number\n\
6674Extracts a device minor number from a raw device number.");
6675
6676static PyObject *
6677posix_minor(PyObject *self, PyObject *args)
6678{
6679 int device;
6680 if (!PyArg_ParseTuple(args, "i:minor", &device))
6681 return NULL;
6682 return PyInt_FromLong((long)minor(device));
6683}
6684
6685PyDoc_STRVAR(posix_makedev__doc__,
6686"makedev(major, minor) -> device number\n\
6687Composes a raw device number from the major and minor device numbers.");
6688
6689static PyObject *
6690posix_makedev(PyObject *self, PyObject *args)
6691{
6692 int major, minor;
6693 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6694 return NULL;
6695 return PyInt_FromLong((long)makedev(major, minor));
6696}
6697#endif /* device macros */
6698
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006699
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006700#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006701PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006702"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006703Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006704
Barry Warsaw53699e91996-12-10 23:23:01 +00006705static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006706posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006707{
6708 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006709 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006710 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006711 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006712
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006713 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006714 return NULL;
6715
6716#if !defined(HAVE_LARGEFILE_SUPPORT)
6717 length = PyInt_AsLong(lenobj);
6718#else
6719 length = PyLong_Check(lenobj) ?
6720 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6721#endif
6722 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006723 return NULL;
6724
Barry Warsaw53699e91996-12-10 23:23:01 +00006725 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006726 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00006727 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006728 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006729 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006730 return NULL;
6731 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006732 Py_INCREF(Py_None);
6733 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006734}
6735#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006736
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006737#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006738PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006739"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006740Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006741
Fred Drake762e2061999-08-26 17:23:54 +00006742/* Save putenv() parameters as values here, so we can collect them when they
6743 * get re-set with another call for the same key. */
6744static PyObject *posix_putenv_garbage;
6745
Tim Peters5aa91602002-01-30 05:46:57 +00006746static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006747posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006748{
6749 char *s1, *s2;
Anthony Baxter64182fe2006-04-11 12:14:09 +00006750 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00006751 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00006752 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006753
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006754 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006755 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006756
6757#if defined(PYOS_OS2)
6758 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6759 APIRET rc;
6760
Guido van Rossumd48f2521997-12-05 22:19:34 +00006761 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6762 if (rc != NO_ERROR)
6763 return os2_error(rc);
6764
6765 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6766 APIRET rc;
6767
Guido van Rossumd48f2521997-12-05 22:19:34 +00006768 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6769 if (rc != NO_ERROR)
6770 return os2_error(rc);
6771 } else {
6772#endif
6773
Fred Drake762e2061999-08-26 17:23:54 +00006774 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00006775 len = strlen(s1) + strlen(s2) + 2;
6776 /* len includes space for a trailing \0; the size arg to
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006777 PyString_FromStringAndSize does not count that */
6778 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00006779 if (newstr == NULL)
6780 return PyErr_NoMemory();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006781 newenv = PyString_AS_STRING(newstr);
Anthony Baxter64182fe2006-04-11 12:14:09 +00006782 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6783 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00006784 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006785 posix_error();
6786 return NULL;
6787 }
Fred Drake762e2061999-08-26 17:23:54 +00006788 /* Install the first arg and newstr in posix_putenv_garbage;
6789 * this will cause previous value to be collected. This has to
6790 * happen after the real putenv() call because the old value
6791 * was still accessible until then. */
6792 if (PyDict_SetItem(posix_putenv_garbage,
6793 PyTuple_GET_ITEM(args, 0), newstr)) {
6794 /* really not much we can do; just leak */
6795 PyErr_Clear();
6796 }
6797 else {
6798 Py_DECREF(newstr);
6799 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006800
6801#if defined(PYOS_OS2)
6802 }
6803#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006804 Py_INCREF(Py_None);
6805 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006806}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006807#endif /* putenv */
6808
Guido van Rossumc524d952001-10-19 01:31:59 +00006809#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006810PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006811"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006812Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006813
6814static PyObject *
6815posix_unsetenv(PyObject *self, PyObject *args)
6816{
6817 char *s1;
6818
6819 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6820 return NULL;
6821
6822 unsetenv(s1);
6823
6824 /* Remove the key from posix_putenv_garbage;
6825 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00006826 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00006827 * old value was still accessible until then.
6828 */
6829 if (PyDict_DelItem(posix_putenv_garbage,
6830 PyTuple_GET_ITEM(args, 0))) {
6831 /* really not much we can do; just leak */
6832 PyErr_Clear();
6833 }
6834
6835 Py_INCREF(Py_None);
6836 return Py_None;
6837}
6838#endif /* unsetenv */
6839
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006840PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006841"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006842Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006843
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006844static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006845posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006846{
6847 int code;
6848 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006849 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00006850 return NULL;
6851 message = strerror(code);
6852 if (message == NULL) {
6853 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00006854 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006855 return NULL;
6856 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006857 return PyString_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006858}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006859
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006860
Guido van Rossumc9641791998-08-04 15:26:23 +00006861#ifdef HAVE_SYS_WAIT_H
6862
Fred Drake106c1a02002-04-23 15:58:02 +00006863#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006864PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006865"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006866Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006867
6868static PyObject *
6869posix_WCOREDUMP(PyObject *self, PyObject *args)
6870{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006871 WAIT_TYPE status;
6872 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006873
Neal Norwitzd5a37542006-03-20 06:48:34 +00006874 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006875 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006876
6877 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006878}
6879#endif /* WCOREDUMP */
6880
6881#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006882PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006883"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006884Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006885job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006886
6887static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006888posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006889{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006890 WAIT_TYPE status;
6891 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006892
Neal Norwitzd5a37542006-03-20 06:48:34 +00006893 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006894 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006895
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006896 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006897}
6898#endif /* WIFCONTINUED */
6899
Guido van Rossumc9641791998-08-04 15:26:23 +00006900#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006901PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006902"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006903Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006904
6905static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006906posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006907{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006908 WAIT_TYPE status;
6909 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006910
Neal Norwitzd5a37542006-03-20 06:48:34 +00006911 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006912 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006913
Fred Drake106c1a02002-04-23 15:58:02 +00006914 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006915}
6916#endif /* WIFSTOPPED */
6917
6918#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006919PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006920"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006921Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006922
6923static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006924posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006925{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006926 WAIT_TYPE status;
6927 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006928
Neal Norwitzd5a37542006-03-20 06:48:34 +00006929 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006930 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006931
Fred Drake106c1a02002-04-23 15:58:02 +00006932 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006933}
6934#endif /* WIFSIGNALED */
6935
6936#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006937PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006938"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006939Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006940system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006941
6942static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006943posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006944{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006945 WAIT_TYPE status;
6946 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006947
Neal Norwitzd5a37542006-03-20 06:48:34 +00006948 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006949 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006950
Fred Drake106c1a02002-04-23 15:58:02 +00006951 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006952}
6953#endif /* WIFEXITED */
6954
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006955#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006956PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006957"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006958Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006959
6960static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006961posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006962{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006963 WAIT_TYPE status;
6964 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006965
Neal Norwitzd5a37542006-03-20 06:48:34 +00006966 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006967 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006968
Guido van Rossumc9641791998-08-04 15:26:23 +00006969 return Py_BuildValue("i", WEXITSTATUS(status));
6970}
6971#endif /* WEXITSTATUS */
6972
6973#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006974PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006975"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006976Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006977value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006978
6979static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006980posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006981{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006982 WAIT_TYPE status;
6983 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006984
Neal Norwitzd5a37542006-03-20 06:48:34 +00006985 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006986 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006987
Guido van Rossumc9641791998-08-04 15:26:23 +00006988 return Py_BuildValue("i", WTERMSIG(status));
6989}
6990#endif /* WTERMSIG */
6991
6992#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006993PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006994"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006995Return the signal that stopped the process that provided\n\
6996the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006997
6998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006999posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007000{
Neal Norwitzd5a37542006-03-20 06:48:34 +00007001 WAIT_TYPE status;
7002 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007003
Neal Norwitzd5a37542006-03-20 06:48:34 +00007004 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00007005 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007006
Guido van Rossumc9641791998-08-04 15:26:23 +00007007 return Py_BuildValue("i", WSTOPSIG(status));
7008}
7009#endif /* WSTOPSIG */
7010
7011#endif /* HAVE_SYS_WAIT_H */
7012
7013
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007014#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007015#ifdef _SCO_DS
7016/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7017 needed definitions in sys/statvfs.h */
7018#define _SVID3
7019#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007020#include <sys/statvfs.h>
7021
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007022static PyObject*
7023_pystatvfs_fromstructstatvfs(struct statvfs st) {
7024 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7025 if (v == NULL)
7026 return NULL;
7027
7028#if !defined(HAVE_LARGEFILE_SUPPORT)
7029 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7030 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7031 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
7032 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
7033 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
7034 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
7035 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
7036 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
7037 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7038 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
7039#else
7040 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7041 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00007042 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00007043 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00007044 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00007045 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007046 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00007047 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00007048 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00007049 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00007050 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00007051 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00007052 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00007053 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007054 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7055 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
7056#endif
7057
7058 return v;
7059}
7060
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007061PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007062"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007063Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007064
7065static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007066posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007067{
7068 int fd, res;
7069 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007070
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007071 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00007072 return NULL;
7073 Py_BEGIN_ALLOW_THREADS
7074 res = fstatvfs(fd, &st);
7075 Py_END_ALLOW_THREADS
7076 if (res != 0)
7077 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007078
7079 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007080}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007081#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007082
7083
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007084#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007085#include <sys/statvfs.h>
7086
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007087PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007088"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007089Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007090
7091static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007092posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007093{
7094 char *path;
7095 int res;
7096 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007097 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00007098 return NULL;
7099 Py_BEGIN_ALLOW_THREADS
7100 res = statvfs(path, &st);
7101 Py_END_ALLOW_THREADS
7102 if (res != 0)
7103 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007104
7105 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007106}
7107#endif /* HAVE_STATVFS */
7108
7109
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007110#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007111PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007112"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007113Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00007114The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007115or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007116
7117static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007118posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007119{
7120 PyObject *result = NULL;
7121 char *dir = NULL;
7122 char *pfx = NULL;
7123 char *name;
7124
7125 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
7126 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007127
7128 if (PyErr_Warn(PyExc_RuntimeWarning,
7129 "tempnam is a potential security risk to your program") < 0)
7130 return NULL;
7131
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007132#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00007133 name = _tempnam(dir, pfx);
7134#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007135 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00007136#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007137 if (name == NULL)
7138 return PyErr_NoMemory();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007139 result = PyString_FromString(name);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007140 free(name);
7141 return result;
7142}
Guido van Rossumd371ff11999-01-25 16:12:23 +00007143#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007144
7145
7146#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007147PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007148"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007149Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007150
7151static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007152posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007153{
7154 FILE *fp;
7155
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007156 fp = tmpfile();
7157 if (fp == NULL)
7158 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00007159 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007160}
7161#endif
7162
7163
7164#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007165PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007166"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007167Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007168
7169static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007170posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007171{
7172 char buffer[L_tmpnam];
7173 char *name;
7174
Skip Montanaro95618b52001-08-18 18:52:10 +00007175 if (PyErr_Warn(PyExc_RuntimeWarning,
7176 "tmpnam is a potential security risk to your program") < 0)
7177 return NULL;
7178
Greg Wardb48bc172000-03-01 21:51:56 +00007179#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007180 name = tmpnam_r(buffer);
7181#else
7182 name = tmpnam(buffer);
7183#endif
7184 if (name == NULL) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00007185 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00007186#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007187 "unexpected NULL from tmpnam_r"
7188#else
7189 "unexpected NULL from tmpnam"
7190#endif
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00007191 );
7192 PyErr_SetObject(PyExc_OSError, err);
7193 Py_XDECREF(err);
7194 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007195 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007196 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007197}
7198#endif
7199
7200
Fred Drakec9680921999-12-13 16:37:25 +00007201/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7202 * It maps strings representing configuration variable names to
7203 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007204 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007205 * rarely-used constants. There are three separate tables that use
7206 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007207 *
7208 * This code is always included, even if none of the interfaces that
7209 * need it are included. The #if hackery needed to avoid it would be
7210 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007211 */
7212struct constdef {
7213 char *name;
7214 long value;
7215};
7216
Fred Drake12c6e2d1999-12-14 21:25:03 +00007217static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007218conv_confname(PyObject *arg, int *valuep, struct constdef *table,
7219 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007220{
7221 if (PyInt_Check(arg)) {
7222 *valuep = PyInt_AS_LONG(arg);
7223 return 1;
7224 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007225 if (PyString_Check(arg)) {
Fred Drake12c6e2d1999-12-14 21:25:03 +00007226 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00007227 size_t lo = 0;
7228 size_t mid;
7229 size_t hi = tablesize;
7230 int cmp;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007231 char *confname = PyString_AS_STRING(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007232 while (lo < hi) {
7233 mid = (lo + hi) / 2;
7234 cmp = strcmp(confname, table[mid].name);
7235 if (cmp < 0)
7236 hi = mid;
7237 else if (cmp > 0)
7238 lo = mid + 1;
7239 else {
7240 *valuep = table[mid].value;
7241 return 1;
7242 }
7243 }
7244 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
7245 }
7246 else
7247 PyErr_SetString(PyExc_TypeError,
7248 "configuration names must be strings or integers");
7249 return 0;
7250}
7251
7252
7253#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7254static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007255#ifdef _PC_ABI_AIO_XFER_MAX
7256 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
7257#endif
7258#ifdef _PC_ABI_ASYNC_IO
7259 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
7260#endif
Fred Drakec9680921999-12-13 16:37:25 +00007261#ifdef _PC_ASYNC_IO
7262 {"PC_ASYNC_IO", _PC_ASYNC_IO},
7263#endif
7264#ifdef _PC_CHOWN_RESTRICTED
7265 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
7266#endif
7267#ifdef _PC_FILESIZEBITS
7268 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
7269#endif
7270#ifdef _PC_LAST
7271 {"PC_LAST", _PC_LAST},
7272#endif
7273#ifdef _PC_LINK_MAX
7274 {"PC_LINK_MAX", _PC_LINK_MAX},
7275#endif
7276#ifdef _PC_MAX_CANON
7277 {"PC_MAX_CANON", _PC_MAX_CANON},
7278#endif
7279#ifdef _PC_MAX_INPUT
7280 {"PC_MAX_INPUT", _PC_MAX_INPUT},
7281#endif
7282#ifdef _PC_NAME_MAX
7283 {"PC_NAME_MAX", _PC_NAME_MAX},
7284#endif
7285#ifdef _PC_NO_TRUNC
7286 {"PC_NO_TRUNC", _PC_NO_TRUNC},
7287#endif
7288#ifdef _PC_PATH_MAX
7289 {"PC_PATH_MAX", _PC_PATH_MAX},
7290#endif
7291#ifdef _PC_PIPE_BUF
7292 {"PC_PIPE_BUF", _PC_PIPE_BUF},
7293#endif
7294#ifdef _PC_PRIO_IO
7295 {"PC_PRIO_IO", _PC_PRIO_IO},
7296#endif
7297#ifdef _PC_SOCK_MAXBUF
7298 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
7299#endif
7300#ifdef _PC_SYNC_IO
7301 {"PC_SYNC_IO", _PC_SYNC_IO},
7302#endif
7303#ifdef _PC_VDISABLE
7304 {"PC_VDISABLE", _PC_VDISABLE},
7305#endif
7306};
7307
Fred Drakec9680921999-12-13 16:37:25 +00007308static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007309conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007310{
7311 return conv_confname(arg, valuep, posix_constants_pathconf,
7312 sizeof(posix_constants_pathconf)
7313 / sizeof(struct constdef));
7314}
7315#endif
7316
7317#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007318PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007319"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007320Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007321If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007322
7323static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007324posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007325{
7326 PyObject *result = NULL;
7327 int name, fd;
7328
Fred Drake12c6e2d1999-12-14 21:25:03 +00007329 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7330 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00007331 long limit;
7332
7333 errno = 0;
7334 limit = fpathconf(fd, name);
7335 if (limit == -1 && errno != 0)
7336 posix_error();
7337 else
7338 result = PyInt_FromLong(limit);
7339 }
7340 return result;
7341}
7342#endif
7343
7344
7345#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007346PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007347"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007348Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007349If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007350
7351static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007352posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007353{
7354 PyObject *result = NULL;
7355 int name;
7356 char *path;
7357
7358 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7359 conv_path_confname, &name)) {
7360 long limit;
7361
7362 errno = 0;
7363 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007364 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00007365 if (errno == EINVAL)
7366 /* could be a path or name problem */
7367 posix_error();
7368 else
7369 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007370 }
Fred Drakec9680921999-12-13 16:37:25 +00007371 else
7372 result = PyInt_FromLong(limit);
7373 }
7374 return result;
7375}
7376#endif
7377
7378#ifdef HAVE_CONFSTR
7379static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007380#ifdef _CS_ARCHITECTURE
7381 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
7382#endif
7383#ifdef _CS_HOSTNAME
7384 {"CS_HOSTNAME", _CS_HOSTNAME},
7385#endif
7386#ifdef _CS_HW_PROVIDER
7387 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
7388#endif
7389#ifdef _CS_HW_SERIAL
7390 {"CS_HW_SERIAL", _CS_HW_SERIAL},
7391#endif
7392#ifdef _CS_INITTAB_NAME
7393 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
7394#endif
Fred Drakec9680921999-12-13 16:37:25 +00007395#ifdef _CS_LFS64_CFLAGS
7396 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
7397#endif
7398#ifdef _CS_LFS64_LDFLAGS
7399 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
7400#endif
7401#ifdef _CS_LFS64_LIBS
7402 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
7403#endif
7404#ifdef _CS_LFS64_LINTFLAGS
7405 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
7406#endif
7407#ifdef _CS_LFS_CFLAGS
7408 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
7409#endif
7410#ifdef _CS_LFS_LDFLAGS
7411 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
7412#endif
7413#ifdef _CS_LFS_LIBS
7414 {"CS_LFS_LIBS", _CS_LFS_LIBS},
7415#endif
7416#ifdef _CS_LFS_LINTFLAGS
7417 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
7418#endif
Fred Draked86ed291999-12-15 15:34:33 +00007419#ifdef _CS_MACHINE
7420 {"CS_MACHINE", _CS_MACHINE},
7421#endif
Fred Drakec9680921999-12-13 16:37:25 +00007422#ifdef _CS_PATH
7423 {"CS_PATH", _CS_PATH},
7424#endif
Fred Draked86ed291999-12-15 15:34:33 +00007425#ifdef _CS_RELEASE
7426 {"CS_RELEASE", _CS_RELEASE},
7427#endif
7428#ifdef _CS_SRPC_DOMAIN
7429 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
7430#endif
7431#ifdef _CS_SYSNAME
7432 {"CS_SYSNAME", _CS_SYSNAME},
7433#endif
7434#ifdef _CS_VERSION
7435 {"CS_VERSION", _CS_VERSION},
7436#endif
Fred Drakec9680921999-12-13 16:37:25 +00007437#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7438 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
7439#endif
7440#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7441 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
7442#endif
7443#ifdef _CS_XBS5_ILP32_OFF32_LIBS
7444 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
7445#endif
7446#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7447 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
7448#endif
7449#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7450 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
7451#endif
7452#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7453 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
7454#endif
7455#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7456 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
7457#endif
7458#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7459 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
7460#endif
7461#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7462 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
7463#endif
7464#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7465 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
7466#endif
7467#ifdef _CS_XBS5_LP64_OFF64_LIBS
7468 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
7469#endif
7470#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7471 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
7472#endif
7473#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7474 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
7475#endif
7476#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7477 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
7478#endif
7479#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7480 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
7481#endif
7482#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7483 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
7484#endif
Fred Draked86ed291999-12-15 15:34:33 +00007485#ifdef _MIPS_CS_AVAIL_PROCESSORS
7486 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
7487#endif
7488#ifdef _MIPS_CS_BASE
7489 {"MIPS_CS_BASE", _MIPS_CS_BASE},
7490#endif
7491#ifdef _MIPS_CS_HOSTID
7492 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
7493#endif
7494#ifdef _MIPS_CS_HW_NAME
7495 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7496#endif
7497#ifdef _MIPS_CS_NUM_PROCESSORS
7498 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7499#endif
7500#ifdef _MIPS_CS_OSREL_MAJ
7501 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7502#endif
7503#ifdef _MIPS_CS_OSREL_MIN
7504 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7505#endif
7506#ifdef _MIPS_CS_OSREL_PATCH
7507 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7508#endif
7509#ifdef _MIPS_CS_OS_NAME
7510 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7511#endif
7512#ifdef _MIPS_CS_OS_PROVIDER
7513 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7514#endif
7515#ifdef _MIPS_CS_PROCESSORS
7516 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7517#endif
7518#ifdef _MIPS_CS_SERIAL
7519 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7520#endif
7521#ifdef _MIPS_CS_VENDOR
7522 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7523#endif
Fred Drakec9680921999-12-13 16:37:25 +00007524};
7525
7526static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007527conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007528{
7529 return conv_confname(arg, valuep, posix_constants_confstr,
7530 sizeof(posix_constants_confstr)
7531 / sizeof(struct constdef));
7532}
7533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007534PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007535"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007536Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007537
7538static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007539posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007540{
7541 PyObject *result = NULL;
7542 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007543 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007544
7545 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Skip Montanarodd527fc2006-04-18 00:49:49 +00007546 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007547
Fred Drakec9680921999-12-13 16:37:25 +00007548 errno = 0;
Skip Montanarodd527fc2006-04-18 00:49:49 +00007549 len = confstr(name, buffer, sizeof(buffer));
Skip Montanaro94785ef2006-04-20 01:29:48 +00007550 if (len == 0) {
7551 if (errno) {
7552 posix_error();
7553 }
7554 else {
7555 result = Py_None;
7556 Py_INCREF(Py_None);
7557 }
Fred Drakec9680921999-12-13 16:37:25 +00007558 }
7559 else {
Neal Norwitz0d21b1e2006-04-20 06:44:42 +00007560 if ((unsigned int)len >= sizeof(buffer)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007561 result = PyString_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007562 if (result != NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007563 confstr(name, PyString_AS_STRING(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00007564 }
7565 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007566 result = PyString_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007567 }
7568 }
7569 return result;
7570}
7571#endif
7572
7573
7574#ifdef HAVE_SYSCONF
7575static struct constdef posix_constants_sysconf[] = {
7576#ifdef _SC_2_CHAR_TERM
7577 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7578#endif
7579#ifdef _SC_2_C_BIND
7580 {"SC_2_C_BIND", _SC_2_C_BIND},
7581#endif
7582#ifdef _SC_2_C_DEV
7583 {"SC_2_C_DEV", _SC_2_C_DEV},
7584#endif
7585#ifdef _SC_2_C_VERSION
7586 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7587#endif
7588#ifdef _SC_2_FORT_DEV
7589 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7590#endif
7591#ifdef _SC_2_FORT_RUN
7592 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7593#endif
7594#ifdef _SC_2_LOCALEDEF
7595 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7596#endif
7597#ifdef _SC_2_SW_DEV
7598 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7599#endif
7600#ifdef _SC_2_UPE
7601 {"SC_2_UPE", _SC_2_UPE},
7602#endif
7603#ifdef _SC_2_VERSION
7604 {"SC_2_VERSION", _SC_2_VERSION},
7605#endif
Fred Draked86ed291999-12-15 15:34:33 +00007606#ifdef _SC_ABI_ASYNCHRONOUS_IO
7607 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7608#endif
7609#ifdef _SC_ACL
7610 {"SC_ACL", _SC_ACL},
7611#endif
Fred Drakec9680921999-12-13 16:37:25 +00007612#ifdef _SC_AIO_LISTIO_MAX
7613 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7614#endif
Fred Drakec9680921999-12-13 16:37:25 +00007615#ifdef _SC_AIO_MAX
7616 {"SC_AIO_MAX", _SC_AIO_MAX},
7617#endif
7618#ifdef _SC_AIO_PRIO_DELTA_MAX
7619 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7620#endif
7621#ifdef _SC_ARG_MAX
7622 {"SC_ARG_MAX", _SC_ARG_MAX},
7623#endif
7624#ifdef _SC_ASYNCHRONOUS_IO
7625 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7626#endif
7627#ifdef _SC_ATEXIT_MAX
7628 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7629#endif
Fred Draked86ed291999-12-15 15:34:33 +00007630#ifdef _SC_AUDIT
7631 {"SC_AUDIT", _SC_AUDIT},
7632#endif
Fred Drakec9680921999-12-13 16:37:25 +00007633#ifdef _SC_AVPHYS_PAGES
7634 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7635#endif
7636#ifdef _SC_BC_BASE_MAX
7637 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7638#endif
7639#ifdef _SC_BC_DIM_MAX
7640 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7641#endif
7642#ifdef _SC_BC_SCALE_MAX
7643 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7644#endif
7645#ifdef _SC_BC_STRING_MAX
7646 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7647#endif
Fred Draked86ed291999-12-15 15:34:33 +00007648#ifdef _SC_CAP
7649 {"SC_CAP", _SC_CAP},
7650#endif
Fred Drakec9680921999-12-13 16:37:25 +00007651#ifdef _SC_CHARCLASS_NAME_MAX
7652 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7653#endif
7654#ifdef _SC_CHAR_BIT
7655 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7656#endif
7657#ifdef _SC_CHAR_MAX
7658 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7659#endif
7660#ifdef _SC_CHAR_MIN
7661 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7662#endif
7663#ifdef _SC_CHILD_MAX
7664 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7665#endif
7666#ifdef _SC_CLK_TCK
7667 {"SC_CLK_TCK", _SC_CLK_TCK},
7668#endif
7669#ifdef _SC_COHER_BLKSZ
7670 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7671#endif
7672#ifdef _SC_COLL_WEIGHTS_MAX
7673 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7674#endif
7675#ifdef _SC_DCACHE_ASSOC
7676 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7677#endif
7678#ifdef _SC_DCACHE_BLKSZ
7679 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7680#endif
7681#ifdef _SC_DCACHE_LINESZ
7682 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7683#endif
7684#ifdef _SC_DCACHE_SZ
7685 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7686#endif
7687#ifdef _SC_DCACHE_TBLKSZ
7688 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7689#endif
7690#ifdef _SC_DELAYTIMER_MAX
7691 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7692#endif
7693#ifdef _SC_EQUIV_CLASS_MAX
7694 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7695#endif
7696#ifdef _SC_EXPR_NEST_MAX
7697 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7698#endif
7699#ifdef _SC_FSYNC
7700 {"SC_FSYNC", _SC_FSYNC},
7701#endif
7702#ifdef _SC_GETGR_R_SIZE_MAX
7703 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7704#endif
7705#ifdef _SC_GETPW_R_SIZE_MAX
7706 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7707#endif
7708#ifdef _SC_ICACHE_ASSOC
7709 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7710#endif
7711#ifdef _SC_ICACHE_BLKSZ
7712 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7713#endif
7714#ifdef _SC_ICACHE_LINESZ
7715 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7716#endif
7717#ifdef _SC_ICACHE_SZ
7718 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7719#endif
Fred Draked86ed291999-12-15 15:34:33 +00007720#ifdef _SC_INF
7721 {"SC_INF", _SC_INF},
7722#endif
Fred Drakec9680921999-12-13 16:37:25 +00007723#ifdef _SC_INT_MAX
7724 {"SC_INT_MAX", _SC_INT_MAX},
7725#endif
7726#ifdef _SC_INT_MIN
7727 {"SC_INT_MIN", _SC_INT_MIN},
7728#endif
7729#ifdef _SC_IOV_MAX
7730 {"SC_IOV_MAX", _SC_IOV_MAX},
7731#endif
Fred Draked86ed291999-12-15 15:34:33 +00007732#ifdef _SC_IP_SECOPTS
7733 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7734#endif
Fred Drakec9680921999-12-13 16:37:25 +00007735#ifdef _SC_JOB_CONTROL
7736 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7737#endif
Fred Draked86ed291999-12-15 15:34:33 +00007738#ifdef _SC_KERN_POINTERS
7739 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7740#endif
7741#ifdef _SC_KERN_SIM
7742 {"SC_KERN_SIM", _SC_KERN_SIM},
7743#endif
Fred Drakec9680921999-12-13 16:37:25 +00007744#ifdef _SC_LINE_MAX
7745 {"SC_LINE_MAX", _SC_LINE_MAX},
7746#endif
7747#ifdef _SC_LOGIN_NAME_MAX
7748 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7749#endif
7750#ifdef _SC_LOGNAME_MAX
7751 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7752#endif
7753#ifdef _SC_LONG_BIT
7754 {"SC_LONG_BIT", _SC_LONG_BIT},
7755#endif
Fred Draked86ed291999-12-15 15:34:33 +00007756#ifdef _SC_MAC
7757 {"SC_MAC", _SC_MAC},
7758#endif
Fred Drakec9680921999-12-13 16:37:25 +00007759#ifdef _SC_MAPPED_FILES
7760 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7761#endif
7762#ifdef _SC_MAXPID
7763 {"SC_MAXPID", _SC_MAXPID},
7764#endif
7765#ifdef _SC_MB_LEN_MAX
7766 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7767#endif
7768#ifdef _SC_MEMLOCK
7769 {"SC_MEMLOCK", _SC_MEMLOCK},
7770#endif
7771#ifdef _SC_MEMLOCK_RANGE
7772 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7773#endif
7774#ifdef _SC_MEMORY_PROTECTION
7775 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7776#endif
7777#ifdef _SC_MESSAGE_PASSING
7778 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7779#endif
Fred Draked86ed291999-12-15 15:34:33 +00007780#ifdef _SC_MMAP_FIXED_ALIGNMENT
7781 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7782#endif
Fred Drakec9680921999-12-13 16:37:25 +00007783#ifdef _SC_MQ_OPEN_MAX
7784 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7785#endif
7786#ifdef _SC_MQ_PRIO_MAX
7787 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7788#endif
Fred Draked86ed291999-12-15 15:34:33 +00007789#ifdef _SC_NACLS_MAX
7790 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7791#endif
Fred Drakec9680921999-12-13 16:37:25 +00007792#ifdef _SC_NGROUPS_MAX
7793 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7794#endif
7795#ifdef _SC_NL_ARGMAX
7796 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7797#endif
7798#ifdef _SC_NL_LANGMAX
7799 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7800#endif
7801#ifdef _SC_NL_MSGMAX
7802 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7803#endif
7804#ifdef _SC_NL_NMAX
7805 {"SC_NL_NMAX", _SC_NL_NMAX},
7806#endif
7807#ifdef _SC_NL_SETMAX
7808 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7809#endif
7810#ifdef _SC_NL_TEXTMAX
7811 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7812#endif
7813#ifdef _SC_NPROCESSORS_CONF
7814 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7815#endif
7816#ifdef _SC_NPROCESSORS_ONLN
7817 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7818#endif
Fred Draked86ed291999-12-15 15:34:33 +00007819#ifdef _SC_NPROC_CONF
7820 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7821#endif
7822#ifdef _SC_NPROC_ONLN
7823 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7824#endif
Fred Drakec9680921999-12-13 16:37:25 +00007825#ifdef _SC_NZERO
7826 {"SC_NZERO", _SC_NZERO},
7827#endif
7828#ifdef _SC_OPEN_MAX
7829 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7830#endif
7831#ifdef _SC_PAGESIZE
7832 {"SC_PAGESIZE", _SC_PAGESIZE},
7833#endif
7834#ifdef _SC_PAGE_SIZE
7835 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7836#endif
7837#ifdef _SC_PASS_MAX
7838 {"SC_PASS_MAX", _SC_PASS_MAX},
7839#endif
7840#ifdef _SC_PHYS_PAGES
7841 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7842#endif
7843#ifdef _SC_PII
7844 {"SC_PII", _SC_PII},
7845#endif
7846#ifdef _SC_PII_INTERNET
7847 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7848#endif
7849#ifdef _SC_PII_INTERNET_DGRAM
7850 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7851#endif
7852#ifdef _SC_PII_INTERNET_STREAM
7853 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7854#endif
7855#ifdef _SC_PII_OSI
7856 {"SC_PII_OSI", _SC_PII_OSI},
7857#endif
7858#ifdef _SC_PII_OSI_CLTS
7859 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7860#endif
7861#ifdef _SC_PII_OSI_COTS
7862 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7863#endif
7864#ifdef _SC_PII_OSI_M
7865 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7866#endif
7867#ifdef _SC_PII_SOCKET
7868 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7869#endif
7870#ifdef _SC_PII_XTI
7871 {"SC_PII_XTI", _SC_PII_XTI},
7872#endif
7873#ifdef _SC_POLL
7874 {"SC_POLL", _SC_POLL},
7875#endif
7876#ifdef _SC_PRIORITIZED_IO
7877 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7878#endif
7879#ifdef _SC_PRIORITY_SCHEDULING
7880 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7881#endif
7882#ifdef _SC_REALTIME_SIGNALS
7883 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7884#endif
7885#ifdef _SC_RE_DUP_MAX
7886 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7887#endif
7888#ifdef _SC_RTSIG_MAX
7889 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7890#endif
7891#ifdef _SC_SAVED_IDS
7892 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7893#endif
7894#ifdef _SC_SCHAR_MAX
7895 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7896#endif
7897#ifdef _SC_SCHAR_MIN
7898 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7899#endif
7900#ifdef _SC_SELECT
7901 {"SC_SELECT", _SC_SELECT},
7902#endif
7903#ifdef _SC_SEMAPHORES
7904 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7905#endif
7906#ifdef _SC_SEM_NSEMS_MAX
7907 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7908#endif
7909#ifdef _SC_SEM_VALUE_MAX
7910 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7911#endif
7912#ifdef _SC_SHARED_MEMORY_OBJECTS
7913 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7914#endif
7915#ifdef _SC_SHRT_MAX
7916 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7917#endif
7918#ifdef _SC_SHRT_MIN
7919 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7920#endif
7921#ifdef _SC_SIGQUEUE_MAX
7922 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7923#endif
7924#ifdef _SC_SIGRT_MAX
7925 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7926#endif
7927#ifdef _SC_SIGRT_MIN
7928 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7929#endif
Fred Draked86ed291999-12-15 15:34:33 +00007930#ifdef _SC_SOFTPOWER
7931 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7932#endif
Fred Drakec9680921999-12-13 16:37:25 +00007933#ifdef _SC_SPLIT_CACHE
7934 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7935#endif
7936#ifdef _SC_SSIZE_MAX
7937 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7938#endif
7939#ifdef _SC_STACK_PROT
7940 {"SC_STACK_PROT", _SC_STACK_PROT},
7941#endif
7942#ifdef _SC_STREAM_MAX
7943 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7944#endif
7945#ifdef _SC_SYNCHRONIZED_IO
7946 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7947#endif
7948#ifdef _SC_THREADS
7949 {"SC_THREADS", _SC_THREADS},
7950#endif
7951#ifdef _SC_THREAD_ATTR_STACKADDR
7952 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7953#endif
7954#ifdef _SC_THREAD_ATTR_STACKSIZE
7955 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7956#endif
7957#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7958 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7959#endif
7960#ifdef _SC_THREAD_KEYS_MAX
7961 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7962#endif
7963#ifdef _SC_THREAD_PRIORITY_SCHEDULING
7964 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7965#endif
7966#ifdef _SC_THREAD_PRIO_INHERIT
7967 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7968#endif
7969#ifdef _SC_THREAD_PRIO_PROTECT
7970 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7971#endif
7972#ifdef _SC_THREAD_PROCESS_SHARED
7973 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7974#endif
7975#ifdef _SC_THREAD_SAFE_FUNCTIONS
7976 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7977#endif
7978#ifdef _SC_THREAD_STACK_MIN
7979 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7980#endif
7981#ifdef _SC_THREAD_THREADS_MAX
7982 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7983#endif
7984#ifdef _SC_TIMERS
7985 {"SC_TIMERS", _SC_TIMERS},
7986#endif
7987#ifdef _SC_TIMER_MAX
7988 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7989#endif
7990#ifdef _SC_TTY_NAME_MAX
7991 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7992#endif
7993#ifdef _SC_TZNAME_MAX
7994 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7995#endif
7996#ifdef _SC_T_IOV_MAX
7997 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7998#endif
7999#ifdef _SC_UCHAR_MAX
8000 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
8001#endif
8002#ifdef _SC_UINT_MAX
8003 {"SC_UINT_MAX", _SC_UINT_MAX},
8004#endif
8005#ifdef _SC_UIO_MAXIOV
8006 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
8007#endif
8008#ifdef _SC_ULONG_MAX
8009 {"SC_ULONG_MAX", _SC_ULONG_MAX},
8010#endif
8011#ifdef _SC_USHRT_MAX
8012 {"SC_USHRT_MAX", _SC_USHRT_MAX},
8013#endif
8014#ifdef _SC_VERSION
8015 {"SC_VERSION", _SC_VERSION},
8016#endif
8017#ifdef _SC_WORD_BIT
8018 {"SC_WORD_BIT", _SC_WORD_BIT},
8019#endif
8020#ifdef _SC_XBS5_ILP32_OFF32
8021 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
8022#endif
8023#ifdef _SC_XBS5_ILP32_OFFBIG
8024 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
8025#endif
8026#ifdef _SC_XBS5_LP64_OFF64
8027 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
8028#endif
8029#ifdef _SC_XBS5_LPBIG_OFFBIG
8030 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
8031#endif
8032#ifdef _SC_XOPEN_CRYPT
8033 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
8034#endif
8035#ifdef _SC_XOPEN_ENH_I18N
8036 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
8037#endif
8038#ifdef _SC_XOPEN_LEGACY
8039 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
8040#endif
8041#ifdef _SC_XOPEN_REALTIME
8042 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
8043#endif
8044#ifdef _SC_XOPEN_REALTIME_THREADS
8045 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
8046#endif
8047#ifdef _SC_XOPEN_SHM
8048 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
8049#endif
8050#ifdef _SC_XOPEN_UNIX
8051 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
8052#endif
8053#ifdef _SC_XOPEN_VERSION
8054 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
8055#endif
8056#ifdef _SC_XOPEN_XCU_VERSION
8057 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
8058#endif
8059#ifdef _SC_XOPEN_XPG2
8060 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
8061#endif
8062#ifdef _SC_XOPEN_XPG3
8063 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
8064#endif
8065#ifdef _SC_XOPEN_XPG4
8066 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
8067#endif
8068};
8069
8070static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008071conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008072{
8073 return conv_confname(arg, valuep, posix_constants_sysconf,
8074 sizeof(posix_constants_sysconf)
8075 / sizeof(struct constdef));
8076}
8077
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008078PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008079"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008080Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008081
8082static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008083posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008084{
8085 PyObject *result = NULL;
8086 int name;
8087
8088 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8089 int value;
8090
8091 errno = 0;
8092 value = sysconf(name);
8093 if (value == -1 && errno != 0)
8094 posix_error();
8095 else
8096 result = PyInt_FromLong(value);
8097 }
8098 return result;
8099}
8100#endif
8101
8102
Fred Drakebec628d1999-12-15 18:31:10 +00008103/* This code is used to ensure that the tables of configuration value names
8104 * are in sorted order as required by conv_confname(), and also to build the
8105 * the exported dictionaries that are used to publish information about the
8106 * names available on the host platform.
8107 *
8108 * Sorting the table at runtime ensures that the table is properly ordered
8109 * when used, even for platforms we're not able to test on. It also makes
8110 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008111 */
Fred Drakebec628d1999-12-15 18:31:10 +00008112
8113static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008114cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008115{
8116 const struct constdef *c1 =
8117 (const struct constdef *) v1;
8118 const struct constdef *c2 =
8119 (const struct constdef *) v2;
8120
8121 return strcmp(c1->name, c2->name);
8122}
8123
8124static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008125setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008126 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008127{
Fred Drakebec628d1999-12-15 18:31:10 +00008128 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008129 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008130
8131 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8132 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008133 if (d == NULL)
8134 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008135
Barry Warsaw3155db32000-04-13 15:20:40 +00008136 for (i=0; i < tablesize; ++i) {
8137 PyObject *o = PyInt_FromLong(table[i].value);
8138 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8139 Py_XDECREF(o);
8140 Py_DECREF(d);
8141 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008142 }
Barry Warsaw3155db32000-04-13 15:20:40 +00008143 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008144 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008145 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008146}
8147
Fred Drakebec628d1999-12-15 18:31:10 +00008148/* Return -1 on failure, 0 on success. */
8149static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008150setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008151{
8152#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008153 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008154 sizeof(posix_constants_pathconf)
8155 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008156 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00008157 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008158#endif
8159#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008160 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008161 sizeof(posix_constants_confstr)
8162 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008163 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00008164 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008165#endif
8166#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008167 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008168 sizeof(posix_constants_sysconf)
8169 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008170 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00008171 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008172#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008173 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008174}
Fred Draked86ed291999-12-15 15:34:33 +00008175
8176
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008177PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008178"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008179Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008180in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008181
8182static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008183posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008184{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008185 abort();
8186 /*NOTREACHED*/
8187 Py_FatalError("abort() called from Python code didn't abort!");
8188 return NULL;
8189}
Fred Drakebec628d1999-12-15 18:31:10 +00008190
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008191#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008192PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008193"startfile(filepath [, operation]) - Start a file with its associated\n\
8194application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008195\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008196When \"operation\" is not specified or \"open\", this acts like\n\
8197double-clicking the file in Explorer, or giving the file name as an\n\
8198argument to the DOS \"start\" command: the file is opened with whatever\n\
8199application (if any) its extension is associated.\n\
8200When another \"operation\" is given, it specifies what should be done with\n\
8201the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008202\n\
8203startfile returns as soon as the associated application is launched.\n\
8204There is no option to wait for the application to close, and no way\n\
8205to retrieve the application's exit status.\n\
8206\n\
8207The filepath is relative to the current directory. If you want to use\n\
8208an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008209the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008210
8211static PyObject *
8212win32_startfile(PyObject *self, PyObject *args)
8213{
8214 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00008215 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008216 HINSTANCE rc;
Georg Brandlad89dc82006-04-03 12:26:26 +00008217#ifdef Py_WIN_WIDE_FILENAMES
8218 if (unicode_file_names()) {
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008219 PyObject *unipath, *woperation = NULL;
Georg Brandlad89dc82006-04-03 12:26:26 +00008220 if (!PyArg_ParseTuple(args, "U|s:startfile",
8221 &unipath, &operation)) {
8222 PyErr_Clear();
8223 goto normal;
8224 }
8225
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008226
8227 if (operation) {
8228 woperation = PyUnicode_DecodeASCII(operation,
8229 strlen(operation), NULL);
8230 if (!woperation) {
8231 PyErr_Clear();
8232 operation = NULL;
8233 goto normal;
8234 }
Georg Brandlad89dc82006-04-03 12:26:26 +00008235 }
8236
8237 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008238 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
Georg Brandlad89dc82006-04-03 12:26:26 +00008239 PyUnicode_AS_UNICODE(unipath),
Georg Brandlad89dc82006-04-03 12:26:26 +00008240 NULL, NULL, SW_SHOWNORMAL);
8241 Py_END_ALLOW_THREADS
8242
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008243 Py_XDECREF(woperation);
Georg Brandlad89dc82006-04-03 12:26:26 +00008244 if (rc <= (HINSTANCE)32) {
8245 PyObject *errval = win32_error_unicode("startfile",
8246 PyUnicode_AS_UNICODE(unipath));
8247 return errval;
8248 }
8249 Py_INCREF(Py_None);
8250 return Py_None;
8251 }
8252#endif
8253
8254normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00008255 if (!PyArg_ParseTuple(args, "et|s:startfile",
8256 Py_FileSystemDefaultEncoding, &filepath,
8257 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00008258 return NULL;
8259 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00008260 rc = ShellExecute((HWND)0, operation, filepath,
8261 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00008262 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00008263 if (rc <= (HINSTANCE)32) {
8264 PyObject *errval = win32_error("startfile", filepath);
8265 PyMem_Free(filepath);
8266 return errval;
8267 }
8268 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00008269 Py_INCREF(Py_None);
8270 return Py_None;
8271}
8272#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008273
Martin v. Löwis438b5342002-12-27 10:16:42 +00008274#ifdef HAVE_GETLOADAVG
8275PyDoc_STRVAR(posix_getloadavg__doc__,
8276"getloadavg() -> (float, float, float)\n\n\
8277Return the number of processes in the system run queue averaged over\n\
8278the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8279was unobtainable");
8280
8281static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008282posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008283{
8284 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008285 if (getloadavg(loadavg, 3)!=3) {
8286 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8287 return NULL;
8288 } else
8289 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
8290}
8291#endif
8292
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008293#ifdef MS_WINDOWS
8294
8295PyDoc_STRVAR(win32_urandom__doc__,
8296"urandom(n) -> str\n\n\
8297Return a string of n random bytes suitable for cryptographic use.");
8298
8299typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8300 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8301 DWORD dwFlags );
8302typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8303 BYTE *pbBuffer );
8304
8305static CRYPTGENRANDOM pCryptGenRandom = NULL;
Martin v. Löwisaf699dd2007-09-04 09:51:57 +00008306/* This handle is never explicitly released. Instead, the operating
8307 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008308static HCRYPTPROV hCryptProv = 0;
8309
Tim Peters4ad82172004-08-30 17:02:04 +00008310static PyObject*
8311win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008312{
Tim Petersd3115382004-08-30 17:36:46 +00008313 int howMany;
8314 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008315
Tim Peters4ad82172004-08-30 17:02:04 +00008316 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00008317 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00008318 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00008319 if (howMany < 0)
8320 return PyErr_Format(PyExc_ValueError,
8321 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008322
Tim Peters4ad82172004-08-30 17:02:04 +00008323 if (hCryptProv == 0) {
8324 HINSTANCE hAdvAPI32 = NULL;
8325 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008326
Tim Peters4ad82172004-08-30 17:02:04 +00008327 /* Obtain handle to the DLL containing CryptoAPI
8328 This should not fail */
8329 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8330 if(hAdvAPI32 == NULL)
8331 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008332
Tim Peters4ad82172004-08-30 17:02:04 +00008333 /* Obtain pointers to the CryptoAPI functions
8334 This will fail on some early versions of Win95 */
8335 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8336 hAdvAPI32,
8337 "CryptAcquireContextA");
8338 if (pCryptAcquireContext == NULL)
8339 return PyErr_Format(PyExc_NotImplementedError,
8340 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008341
Tim Peters4ad82172004-08-30 17:02:04 +00008342 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8343 hAdvAPI32, "CryptGenRandom");
Georg Brandl74bb7832006-09-06 06:03:59 +00008344 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00008345 return PyErr_Format(PyExc_NotImplementedError,
8346 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008347
Tim Peters4ad82172004-08-30 17:02:04 +00008348 /* Acquire context */
8349 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8350 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8351 return win32_error("CryptAcquireContext", NULL);
8352 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008353
Tim Peters4ad82172004-08-30 17:02:04 +00008354 /* Allocate bytes */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00008355 result = PyString_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00008356 if (result != NULL) {
8357 /* Get random data */
Amaury Forgeot d'Arc74bd40d2008-07-21 21:06:46 +00008358 memset(PyString_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00008359 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00008360 PyString_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00008361 Py_DECREF(result);
8362 return win32_error("CryptGenRandom", NULL);
8363 }
Tim Peters4ad82172004-08-30 17:02:04 +00008364 }
Tim Petersd3115382004-08-30 17:36:46 +00008365 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008366}
8367#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008368
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008369#ifdef __VMS
8370/* Use openssl random routine */
8371#include <openssl/rand.h>
8372PyDoc_STRVAR(vms_urandom__doc__,
8373"urandom(n) -> str\n\n\
8374Return a string of n random bytes suitable for cryptographic use.");
8375
8376static PyObject*
8377vms_urandom(PyObject *self, PyObject *args)
8378{
8379 int howMany;
8380 PyObject* result;
8381
8382 /* Read arguments */
8383 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8384 return NULL;
8385 if (howMany < 0)
8386 return PyErr_Format(PyExc_ValueError,
8387 "negative argument not allowed");
8388
8389 /* Allocate bytes */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00008390 result = PyString_FromStringAndSize(NULL, howMany);
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008391 if (result != NULL) {
8392 /* Get random data */
8393 if (RAND_pseudo_bytes((unsigned char*)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00008394 PyString_AS_STRING(result),
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008395 howMany) < 0) {
8396 Py_DECREF(result);
8397 return PyErr_Format(PyExc_ValueError,
8398 "RAND_pseudo_bytes");
8399 }
8400 }
8401 return result;
8402}
8403#endif
8404
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008405static PyMethodDef posix_methods[] = {
8406 {"access", posix_access, METH_VARARGS, posix_access__doc__},
8407#ifdef HAVE_TTYNAME
8408 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
8409#endif
8410 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008411#ifdef HAVE_CHFLAGS
8412 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
8413#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008414 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008415#ifdef HAVE_FCHMOD
8416 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
8417#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008418#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008419 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008420#endif /* HAVE_CHOWN */
Christian Heimes36281872007-11-30 21:11:28 +00008421#ifdef HAVE_LCHMOD
8422 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
8423#endif /* HAVE_LCHMOD */
8424#ifdef HAVE_FCHOWN
8425 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
8426#endif /* HAVE_FCHOWN */
Martin v. Löwis382abef2007-02-19 10:55:19 +00008427#ifdef HAVE_LCHFLAGS
8428 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
8429#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008430#ifdef HAVE_LCHOWN
8431 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
8432#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008433#ifdef HAVE_CHROOT
8434 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
8435#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008436#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00008437 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008438#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008439#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00008440 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008441#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00008442 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008443#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008444#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008445#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008446 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008447#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008448 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8449 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8450 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008451#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008452 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008453#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008454#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008455 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008456#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008457 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8458 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8459 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00008460 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008461#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008462 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008463#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008464#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008465 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008466#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008467 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008468#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00008469 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008470#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008471 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8472 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8473 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008474#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00008475 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008476#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008477 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008478#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008479 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8480 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008481#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008482#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008483 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8484 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008485#if defined(PYOS_OS2)
8486 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8487 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
8488#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008489#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008490#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00008491 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008492#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008493#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00008494 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008495#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008496#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00008497 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008498#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008499#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00008500 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008501#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008502#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008503 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008504#endif /* HAVE_GETEGID */
8505#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008506 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008507#endif /* HAVE_GETEUID */
8508#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008509 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008510#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008511#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00008512 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008513#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008514 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008515#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008516 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008517#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008518#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00008519 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008520#endif /* HAVE_GETPPID */
8521#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008522 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008523#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008524#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00008525 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008526#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008527#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008528 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008529#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008530#ifdef HAVE_KILLPG
8531 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
8532#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008533#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008534 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008535#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008536#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008537 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008538#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008539 {"popen2", win32_popen2, METH_VARARGS},
8540 {"popen3", win32_popen3, METH_VARARGS},
8541 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00008542 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008543#else
8544#if defined(PYOS_OS2) && defined(PYCC_GCC)
8545 {"popen2", os2emx_popen2, METH_VARARGS},
8546 {"popen3", os2emx_popen3, METH_VARARGS},
8547 {"popen4", os2emx_popen4, METH_VARARGS},
8548#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008549#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008550#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008551#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008552 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008553#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008554#ifdef HAVE_SETEUID
8555 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8556#endif /* HAVE_SETEUID */
8557#ifdef HAVE_SETEGID
8558 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8559#endif /* HAVE_SETEGID */
8560#ifdef HAVE_SETREUID
8561 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8562#endif /* HAVE_SETREUID */
8563#ifdef HAVE_SETREGID
8564 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8565#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008566#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008567 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008568#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008569#ifdef HAVE_SETGROUPS
Georg Brandl96a8c392006-05-29 21:04:52 +00008570 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008571#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008572#ifdef HAVE_GETPGID
8573 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8574#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008575#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008576 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008577#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008578#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00008579 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008580#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008581#ifdef HAVE_WAIT3
8582 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8583#endif /* HAVE_WAIT3 */
8584#ifdef HAVE_WAIT4
8585 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8586#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008587#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008588 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008589#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008590#ifdef HAVE_GETSID
8591 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8592#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008593#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00008594 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008595#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008596#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008597 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008598#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008599#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008600 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008601#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008602#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008603 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008604#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008605 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8606 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Georg Brandl309501a2008-01-19 20:22:13 +00008607 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008608 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8609 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8610 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8611 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8612 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8613 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8614 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00008615 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008616#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00008617 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008618#endif
8619#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008620 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008621#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008622#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008623 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8624#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008625#ifdef HAVE_DEVICE_MACROS
8626 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8627 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8628 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8629#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008630#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008631 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008632#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008633#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008634 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008635#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008636#ifdef HAVE_UNSETENV
8637 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8638#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008639 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008640#ifdef HAVE_FCHDIR
8641 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8642#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008643#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008644 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008645#endif
8646#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008647 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008648#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008649#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008650#ifdef WCOREDUMP
8651 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8652#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008653#ifdef WIFCONTINUED
8654 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8655#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008656#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008657 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008658#endif /* WIFSTOPPED */
8659#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008660 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008661#endif /* WIFSIGNALED */
8662#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008663 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008664#endif /* WIFEXITED */
8665#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008666 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008667#endif /* WEXITSTATUS */
8668#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008669 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008670#endif /* WTERMSIG */
8671#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008672 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008673#endif /* WSTOPSIG */
8674#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008675#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008676 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008677#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008678#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008679 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008680#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008681#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00008682 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008683#endif
8684#ifdef HAVE_TEMPNAM
8685 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8686#endif
8687#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00008688 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008689#endif
Fred Drakec9680921999-12-13 16:37:25 +00008690#ifdef HAVE_CONFSTR
8691 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8692#endif
8693#ifdef HAVE_SYSCONF
8694 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8695#endif
8696#ifdef HAVE_FPATHCONF
8697 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8698#endif
8699#ifdef HAVE_PATHCONF
8700 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8701#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008702 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008703#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00008704 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8705#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008706#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00008707 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008708#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008709 #ifdef MS_WINDOWS
8710 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8711 #endif
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008712 #ifdef __VMS
8713 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
8714 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008715 {NULL, NULL} /* Sentinel */
8716};
8717
8718
Barry Warsaw4a342091996-12-19 23:50:02 +00008719static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008720ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008721{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008722 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008723}
8724
Guido van Rossumd48f2521997-12-05 22:19:34 +00008725#if defined(PYOS_OS2)
8726/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008727static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008728{
8729 APIRET rc;
8730 ULONG values[QSV_MAX+1];
8731 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008732 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008733
8734 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008735 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008736 Py_END_ALLOW_THREADS
8737
8738 if (rc != NO_ERROR) {
8739 os2_error(rc);
8740 return -1;
8741 }
8742
Fred Drake4d1e64b2002-04-15 19:40:07 +00008743 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8744 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8745 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8746 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8747 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8748 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8749 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008750
8751 switch (values[QSV_VERSION_MINOR]) {
8752 case 0: ver = "2.00"; break;
8753 case 10: ver = "2.10"; break;
8754 case 11: ver = "2.11"; break;
8755 case 30: ver = "3.00"; break;
8756 case 40: ver = "4.00"; break;
8757 case 50: ver = "5.00"; break;
8758 default:
Tim Peters885d4572001-11-28 20:27:42 +00008759 PyOS_snprintf(tmp, sizeof(tmp),
8760 "%d-%d", values[QSV_VERSION_MAJOR],
8761 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008762 ver = &tmp[0];
8763 }
8764
8765 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008766 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008767 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008768
8769 /* Add Indicator of Which Drive was Used to Boot the System */
8770 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8771 tmp[1] = ':';
8772 tmp[2] = '\0';
8773
Fred Drake4d1e64b2002-04-15 19:40:07 +00008774 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008775}
8776#endif
8777
Barry Warsaw4a342091996-12-19 23:50:02 +00008778static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008779all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008780{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008781#ifdef F_OK
8782 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008783#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008784#ifdef R_OK
8785 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008786#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008787#ifdef W_OK
8788 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008789#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008790#ifdef X_OK
8791 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008792#endif
Fred Drakec9680921999-12-13 16:37:25 +00008793#ifdef NGROUPS_MAX
8794 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8795#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008796#ifdef TMP_MAX
8797 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8798#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008799#ifdef WCONTINUED
8800 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8801#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008802#ifdef WNOHANG
8803 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008804#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008805#ifdef WUNTRACED
8806 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8807#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008808#ifdef O_RDONLY
8809 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8810#endif
8811#ifdef O_WRONLY
8812 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8813#endif
8814#ifdef O_RDWR
8815 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8816#endif
8817#ifdef O_NDELAY
8818 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8819#endif
8820#ifdef O_NONBLOCK
8821 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8822#endif
8823#ifdef O_APPEND
8824 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8825#endif
8826#ifdef O_DSYNC
8827 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8828#endif
8829#ifdef O_RSYNC
8830 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8831#endif
8832#ifdef O_SYNC
8833 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8834#endif
8835#ifdef O_NOCTTY
8836 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8837#endif
8838#ifdef O_CREAT
8839 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8840#endif
8841#ifdef O_EXCL
8842 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8843#endif
8844#ifdef O_TRUNC
8845 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8846#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008847#ifdef O_BINARY
8848 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8849#endif
8850#ifdef O_TEXT
8851 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8852#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008853#ifdef O_LARGEFILE
8854 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8855#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008856#ifdef O_SHLOCK
8857 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8858#endif
8859#ifdef O_EXLOCK
8860 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8861#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008862
Tim Peters5aa91602002-01-30 05:46:57 +00008863/* MS Windows */
8864#ifdef O_NOINHERIT
8865 /* Don't inherit in child processes. */
8866 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8867#endif
8868#ifdef _O_SHORT_LIVED
8869 /* Optimize for short life (keep in memory). */
8870 /* MS forgot to define this one with a non-underscore form too. */
8871 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8872#endif
8873#ifdef O_TEMPORARY
8874 /* Automatically delete when last handle is closed. */
8875 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8876#endif
8877#ifdef O_RANDOM
8878 /* Optimize for random access. */
8879 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8880#endif
8881#ifdef O_SEQUENTIAL
8882 /* Optimize for sequential access. */
8883 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8884#endif
8885
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008886/* GNU extensions. */
Georg Brandl5049a852008-05-16 13:10:15 +00008887#ifdef O_ASYNC
8888 /* Send a SIGIO signal whenever input or output
8889 becomes available on file descriptor */
8890 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
8891#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008892#ifdef O_DIRECT
8893 /* Direct disk access. */
8894 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8895#endif
8896#ifdef O_DIRECTORY
8897 /* Must be a directory. */
8898 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8899#endif
8900#ifdef O_NOFOLLOW
8901 /* Do not follow links. */
8902 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8903#endif
Georg Brandlb67da6e2007-11-24 13:56:09 +00008904#ifdef O_NOATIME
8905 /* Do not update the access time. */
8906 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
8907#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008908
Barry Warsaw5676bd12003-01-07 20:57:09 +00008909 /* These come from sysexits.h */
8910#ifdef EX_OK
8911 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008912#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008913#ifdef EX_USAGE
8914 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008915#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008916#ifdef EX_DATAERR
8917 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008918#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008919#ifdef EX_NOINPUT
8920 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008921#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008922#ifdef EX_NOUSER
8923 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008924#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008925#ifdef EX_NOHOST
8926 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008927#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008928#ifdef EX_UNAVAILABLE
8929 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008930#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008931#ifdef EX_SOFTWARE
8932 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008933#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008934#ifdef EX_OSERR
8935 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008936#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008937#ifdef EX_OSFILE
8938 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008939#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008940#ifdef EX_CANTCREAT
8941 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008942#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008943#ifdef EX_IOERR
8944 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008945#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008946#ifdef EX_TEMPFAIL
8947 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008948#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008949#ifdef EX_PROTOCOL
8950 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008951#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008952#ifdef EX_NOPERM
8953 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008954#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008955#ifdef EX_CONFIG
8956 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008957#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008958#ifdef EX_NOTFOUND
8959 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008960#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008961
Guido van Rossum246bc171999-02-01 23:54:31 +00008962#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008963#if defined(PYOS_OS2) && defined(PYCC_GCC)
8964 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8965 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8966 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8967 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8968 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8969 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8970 if (ins(d, "P_PM", (long)P_PM)) return -1;
8971 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8972 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8973 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8974 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8975 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8976 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8977 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8978 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8979 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8980 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8981 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8982 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8983 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8984#else
Guido van Rossum7d385291999-02-16 19:38:04 +00008985 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8986 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8987 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8988 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8989 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008990#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008991#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008992
Guido van Rossumd48f2521997-12-05 22:19:34 +00008993#if defined(PYOS_OS2)
8994 if (insertvalues(d)) return -1;
8995#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008996 return 0;
8997}
8998
8999
Tim Peters5aa91602002-01-30 05:46:57 +00009000#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009001#define INITFUNC initnt
9002#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009003
9004#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009005#define INITFUNC initos2
9006#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009007
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009008#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009009#define INITFUNC initposix
9010#define MODNAME "posix"
9011#endif
9012
Mark Hammondfe51c6d2002-08-02 02:27:13 +00009013PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009014INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00009015{
Fred Drake4d1e64b2002-04-15 19:40:07 +00009016 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00009017
Fred Drake4d1e64b2002-04-15 19:40:07 +00009018 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009019 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00009020 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00009021 if (m == NULL)
9022 return;
Tim Peters5aa91602002-01-30 05:46:57 +00009023
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009024 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009025 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00009026 Py_XINCREF(v);
9027 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009028 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00009029 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00009030
Fred Drake4d1e64b2002-04-15 19:40:07 +00009031 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00009032 return;
9033
Fred Drake4d1e64b2002-04-15 19:40:07 +00009034 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00009035 return;
9036
Fred Drake4d1e64b2002-04-15 19:40:07 +00009037 Py_INCREF(PyExc_OSError);
9038 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00009039
Guido van Rossumb3d39562000-01-31 18:41:26 +00009040#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00009041 if (posix_putenv_garbage == NULL)
9042 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00009043#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009044
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00009045 if (!initialized) {
9046 stat_result_desc.name = MODNAME ".stat_result";
9047 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
9048 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
9049 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
9050 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
9051 structseq_new = StatResultType.tp_new;
9052 StatResultType.tp_new = statresult_new;
9053
9054 statvfs_result_desc.name = MODNAME ".statvfs_result";
9055 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00009056#ifdef NEED_TICKS_PER_SECOND
9057# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
9058 ticks_per_second = sysconf(_SC_CLK_TCK);
9059# elif defined(HZ)
9060 ticks_per_second = HZ;
9061# else
9062 ticks_per_second = 60; /* magic fallback value; may be bogus */
9063# endif
9064#endif
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00009065 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009066 Py_INCREF((PyObject*) &StatResultType);
9067 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00009068 Py_INCREF((PyObject*) &StatVFSResultType);
9069 PyModule_AddObject(m, "statvfs_result",
9070 (PyObject*) &StatVFSResultType);
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00009071 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009072
9073#ifdef __APPLE__
9074 /*
9075 * Step 2 of weak-linking support on Mac OS X.
9076 *
9077 * The code below removes functions that are not available on the
9078 * currently active platform.
9079 *
9080 * This block allow one to use a python binary that was build on
9081 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
9082 * OSX 10.4.
9083 */
9084#ifdef HAVE_FSTATVFS
9085 if (fstatvfs == NULL) {
9086 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
9087 return;
9088 }
9089 }
9090#endif /* HAVE_FSTATVFS */
9091
9092#ifdef HAVE_STATVFS
9093 if (statvfs == NULL) {
9094 if (PyObject_DelAttrString(m, "statvfs") == -1) {
9095 return;
9096 }
9097 }
9098#endif /* HAVE_STATVFS */
9099
9100# ifdef HAVE_LCHOWN
9101 if (lchown == NULL) {
9102 if (PyObject_DelAttrString(m, "lchown") == -1) {
9103 return;
9104 }
9105 }
9106#endif /* HAVE_LCHOWN */
9107
9108
9109#endif /* __APPLE__ */
9110
Guido van Rossumb6775db1994-08-01 11:34:53 +00009111}
Anthony Baxterac6bd462006-04-13 02:06:09 +00009112
9113#ifdef __cplusplus
9114}
9115#endif
9116
Martin v. Löwis18aaa562006-10-15 08:43:33 +00009117