blob: 0a84a2556b556e82c4ae8bb30c1729fad9635cd5 [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
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000194extern int chown(const char *, uid_t, gid_t);
195extern char *getcwd(char *, int);
196extern char *strerror(int);
197extern int link(const char *, const char *);
198extern int rename(const char *, const char *);
199extern int stat(const char *, struct stat *);
200extern int unlink(const char *);
201extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000203extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000204#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000206extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000207#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000209
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000210#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#ifdef HAVE_UTIME_H
213#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000216#ifdef HAVE_SYS_UTIME_H
217#include <sys/utime.h>
218#define HAVE_UTIME_H /* pretend we do for the rest of this file */
219#endif /* HAVE_SYS_UTIME_H */
220
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#ifdef HAVE_SYS_TIMES_H
222#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000223#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224
225#ifdef HAVE_SYS_PARAM_H
226#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000227#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228
229#ifdef HAVE_SYS_UTSNAME_H
230#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000231#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#define NAMLEN(dirent) strlen((dirent)->d_name)
236#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000237#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#include <direct.h>
239#define NAMLEN(dirent) strlen((dirent)->d_name)
240#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000243#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000244#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000249#endif
250#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000255#ifdef _MSC_VER
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000256#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <direct.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000258#endif
259#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <io.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000261#endif
262#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <process.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000264#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000265#include "osdefs.h"
Martin v. Löwisdc3883f2004-08-29 15:46:35 +0000266#define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000268#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000269#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000270#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000271#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272
Guido van Rossumd48f2521997-12-05 22:19:34 +0000273#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000275#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276
Tim Petersbc2e10e2002-03-03 23:17:02 +0000277#ifndef MAXPATHLEN
Thomas Wouters1ddba602006-04-25 15:29:46 +0000278#if defined(PATH_MAX) && PATH_MAX > 1024
279#define MAXPATHLEN PATH_MAX
280#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000281#define MAXPATHLEN 1024
Thomas Wouters1ddba602006-04-25 15:29:46 +0000282#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000283#endif /* MAXPATHLEN */
284
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000285#ifdef UNION_WAIT
286/* Emulate some macros on systems that have a union instead of macros */
287
288#ifndef WIFEXITED
289#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
290#endif
291
292#ifndef WEXITSTATUS
293#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
294#endif
295
296#ifndef WTERMSIG
297#define WTERMSIG(u_wait) ((u_wait).w_termsig)
298#endif
299
Neal Norwitzd5a37542006-03-20 06:48:34 +0000300#define WAIT_TYPE union wait
301#define WAIT_STATUS_INT(s) (s.w_status)
302
303#else /* !UNION_WAIT */
304#define WAIT_TYPE int
305#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000306#endif /* UNION_WAIT */
307
Greg Wardb48bc172000-03-01 21:51:56 +0000308/* Don't use the "_r" form if we don't need it (also, won't have a
309 prototype for it, at least on Solaris -- maybe others as well?). */
310#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
311#define USE_CTERMID_R
312#endif
313
314#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
315#define USE_TMPNAM_R
316#endif
317
Fred Drake699f3522000-06-29 21:12:41 +0000318/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000319#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000320#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000321# define STAT win32_stat
322# define FSTAT win32_fstat
323# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000324#else
325# define STAT stat
326# define FSTAT fstat
327# define STRUCT_STAT struct stat
328#endif
329
Tim Peters11b23062003-04-23 02:39:17 +0000330#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000331#include <sys/mkdev.h>
332#else
333#if defined(MAJOR_IN_SYSMACROS)
334#include <sys/sysmacros.h>
335#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000336#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
337#include <sys/mkdev.h>
338#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000339#endif
Fred Drake699f3522000-06-29 21:12:41 +0000340
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000342#ifdef WITH_NEXT_FRAMEWORK
343/* On Darwin/MacOSX a shared library or framework has no access to
344** environ directly, we must obtain it with _NSGetEnviron().
345*/
346#include <crt_externs.h>
347static char **environ;
348#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000350#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351
Barry Warsaw53699e91996-12-10 23:23:01 +0000352static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000353convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354{
Barry Warsaw53699e91996-12-10 23:23:01 +0000355 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000357 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358 if (d == NULL)
359 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000360#ifdef WITH_NEXT_FRAMEWORK
361 if (environ == NULL)
362 environ = *_NSGetEnviron();
363#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364 if (environ == NULL)
365 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000366 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000368 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000369 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370 char *p = strchr(*e, '=');
371 if (p == NULL)
372 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000373 k = PyString_FromStringAndSize(*e, (int)(p-*e));
374 if (k == NULL) {
375 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000377 }
378 v = PyString_FromString(p+1);
379 if (v == NULL) {
380 PyErr_Clear();
381 Py_DECREF(k);
382 continue;
383 }
384 if (PyDict_GetItem(d, k) == NULL) {
385 if (PyDict_SetItem(d, k, v) != 0)
386 PyErr_Clear();
387 }
388 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000389 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000391#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000392 {
393 APIRET rc;
394 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
395
396 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000397 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000398 PyObject *v = PyString_FromString(buffer);
399 PyDict_SetItemString(d, "BEGINLIBPATH", v);
400 Py_DECREF(v);
401 }
402 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
403 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
404 PyObject *v = PyString_FromString(buffer);
405 PyDict_SetItemString(d, "ENDLIBPATH", v);
406 Py_DECREF(v);
407 }
408 }
409#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410 return d;
411}
412
413
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414/* Set a POSIX-specific error from errno, and return NULL */
415
Barry Warsawd58d7641998-07-23 16:14:40 +0000416static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000417posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000418{
Barry Warsawca74da41999-02-09 19:31:45 +0000419 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000420}
Barry Warsawd58d7641998-07-23 16:14:40 +0000421static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000422posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000423{
Barry Warsawca74da41999-02-09 19:31:45 +0000424 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000425}
426
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000427#ifdef Py_WIN_WIDE_FILENAMES
428static PyObject *
429posix_error_with_unicode_filename(Py_UNICODE* name)
430{
431 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
432}
433#endif /* Py_WIN_WIDE_FILENAMES */
434
435
Mark Hammondef8b6542001-05-13 08:04:26 +0000436static PyObject *
437posix_error_with_allocated_filename(char* name)
438{
439 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
440 PyMem_Free(name);
441 return rc;
442}
443
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000444#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000445static PyObject *
446win32_error(char* function, char* filename)
447{
Mark Hammond33a6da92000-08-15 00:46:38 +0000448 /* XXX We should pass the function name along in the future.
449 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000450 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000451 Windows error object, which is non-trivial.
452 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000453 errno = GetLastError();
454 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000455 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000456 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000457 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000458}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000459
460#ifdef Py_WIN_WIDE_FILENAMES
461static PyObject *
462win32_error_unicode(char* function, Py_UNICODE* filename)
463{
464 /* XXX - see win32_error for comments on 'function' */
465 errno = GetLastError();
466 if (filename)
467 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
468 else
469 return PyErr_SetFromWindowsErr(errno);
470}
471
472static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
473{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000474}
475
476/* Function suitable for O& conversion */
477static int
478convert_to_unicode(PyObject *arg, void* _param)
479{
480 PyObject **param = (PyObject**)_param;
481 if (PyUnicode_CheckExact(arg)) {
482 Py_INCREF(arg);
483 *param = arg;
484 }
485 else if (PyUnicode_Check(arg)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000486 /* For a Unicode subtype that's not a Unicode object,
487 return a true Unicode object with the same data. */
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000488 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
489 PyUnicode_GET_SIZE(arg));
490 return *param != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000491 }
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000492 else
493 *param = PyUnicode_FromEncodedObject(arg,
494 Py_FileSystemDefaultEncoding,
495 "strict");
496 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000497}
498
499#endif /* Py_WIN_WIDE_FILENAMES */
500
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000501#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000502
Guido van Rossumd48f2521997-12-05 22:19:34 +0000503#if defined(PYOS_OS2)
504/**********************************************************************
505 * Helper Function to Trim and Format OS/2 Messages
506 **********************************************************************/
507 static void
508os2_formatmsg(char *msgbuf, int msglen, char *reason)
509{
510 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
511
512 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
513 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
514
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000515 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000516 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
517 }
518
519 /* Add Optional Reason Text */
520 if (reason) {
521 strcat(msgbuf, " : ");
522 strcat(msgbuf, reason);
523 }
524}
525
526/**********************************************************************
527 * Decode an OS/2 Operating System Error Code
528 *
529 * A convenience function to lookup an OS/2 error code and return a
530 * text message we can use to raise a Python exception.
531 *
532 * Notes:
533 * The messages for errors returned from the OS/2 kernel reside in
534 * the file OSO001.MSG in the \OS2 directory hierarchy.
535 *
536 **********************************************************************/
537 static char *
538os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
539{
540 APIRET rc;
541 ULONG msglen;
542
543 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
544 Py_BEGIN_ALLOW_THREADS
545 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
546 errorcode, "oso001.msg", &msglen);
547 Py_END_ALLOW_THREADS
548
549 if (rc == NO_ERROR)
550 os2_formatmsg(msgbuf, msglen, reason);
551 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000552 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000553 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000554
555 return msgbuf;
556}
557
558/* Set an OS/2-specific error and return NULL. OS/2 kernel
559 errors are not in a global variable e.g. 'errno' nor are
560 they congruent with posix error numbers. */
561
562static PyObject * os2_error(int code)
563{
564 char text[1024];
565 PyObject *v;
566
567 os2_strerror(text, sizeof(text), code, "");
568
569 v = Py_BuildValue("(is)", code, text);
570 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000571 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000572 Py_DECREF(v);
573 }
574 return NULL; /* Signal to Python that an Exception is Pending */
575}
576
577#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578
579/* POSIX generic methods */
580
Barry Warsaw53699e91996-12-10 23:23:01 +0000581static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000582posix_fildes(PyObject *fdobj, int (*func)(int))
583{
584 int fd;
585 int res;
586 fd = PyObject_AsFileDescriptor(fdobj);
587 if (fd < 0)
588 return NULL;
589 Py_BEGIN_ALLOW_THREADS
590 res = (*func)(fd);
591 Py_END_ALLOW_THREADS
592 if (res < 0)
593 return posix_error();
594 Py_INCREF(Py_None);
595 return Py_None;
596}
Guido van Rossum21142a01999-01-08 21:05:37 +0000597
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000598#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000599static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000600unicode_file_names(void)
601{
602 static int canusewide = -1;
603 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000604 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000605 the Windows NT family. */
606 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
607 }
608 return canusewide;
609}
610#endif
Tim Peters11b23062003-04-23 02:39:17 +0000611
Guido van Rossum21142a01999-01-08 21:05:37 +0000612static PyObject *
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000613posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000614{
Mark Hammondef8b6542001-05-13 08:04:26 +0000615 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000616 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000617 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000618 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000620 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000621 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000622 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000623 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000624 return posix_error_with_allocated_filename(path1);
625 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000626 Py_INCREF(Py_None);
627 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000628}
629
Barry Warsaw53699e91996-12-10 23:23:01 +0000630static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000631posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000632 char *format,
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000633 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000634{
Mark Hammondef8b6542001-05-13 08:04:26 +0000635 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000636 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000637 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000638 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000639 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000640 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000641 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000642 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000643 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000644 PyMem_Free(path1);
645 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000646 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000647 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000648 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000649 Py_INCREF(Py_None);
650 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000651}
652
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000653#ifdef Py_WIN_WIDE_FILENAMES
654static PyObject*
655win32_1str(PyObject* args, char* func,
656 char* format, BOOL (__stdcall *funcA)(LPCSTR),
657 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
658{
659 PyObject *uni;
660 char *ansi;
661 BOOL result;
662 if (unicode_file_names()) {
663 if (!PyArg_ParseTuple(args, wformat, &uni))
664 PyErr_Clear();
665 else {
666 Py_BEGIN_ALLOW_THREADS
667 result = funcW(PyUnicode_AsUnicode(uni));
668 Py_END_ALLOW_THREADS
669 if (!result)
670 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
671 Py_INCREF(Py_None);
672 return Py_None;
673 }
674 }
675 if (!PyArg_ParseTuple(args, format, &ansi))
676 return NULL;
677 Py_BEGIN_ALLOW_THREADS
678 result = funcA(ansi);
679 Py_END_ALLOW_THREADS
680 if (!result)
681 return win32_error(func, ansi);
682 Py_INCREF(Py_None);
683 return Py_None;
684
685}
686
687/* This is a reimplementation of the C library's chdir function,
688 but one that produces Win32 errors instead of DOS error codes.
689 chdir is essentially a wrapper around SetCurrentDirectory; however,
690 it also needs to set "magic" environment variables indicating
691 the per-drive current directory, which are of the form =<drive>: */
692BOOL __stdcall
693win32_chdir(LPCSTR path)
694{
695 char new_path[MAX_PATH+1];
696 int result;
697 char env[4] = "=x:";
698
699 if(!SetCurrentDirectoryA(path))
700 return FALSE;
701 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
702 if (!result)
703 return FALSE;
704 /* In the ANSI API, there should not be any paths longer
705 than MAX_PATH. */
706 assert(result <= MAX_PATH+1);
707 if (strncmp(new_path, "\\\\", 2) == 0 ||
708 strncmp(new_path, "//", 2) == 0)
709 /* UNC path, nothing to do. */
710 return TRUE;
711 env[1] = new_path[0];
712 return SetEnvironmentVariableA(env, new_path);
713}
714
715/* The Unicode version differs from the ANSI version
716 since the current directory might exceed MAX_PATH characters */
717BOOL __stdcall
718win32_wchdir(LPCWSTR path)
719{
720 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
721 int result;
722 wchar_t env[4] = L"=x:";
723
724 if(!SetCurrentDirectoryW(path))
725 return FALSE;
726 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
727 if (!result)
728 return FALSE;
729 if (result > MAX_PATH+1) {
730 new_path = malloc(result);
731 if (!new_path) {
732 SetLastError(ERROR_OUTOFMEMORY);
733 return FALSE;
734 }
735 }
736 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
737 wcsncmp(new_path, L"//", 2) == 0)
738 /* UNC path, nothing to do. */
739 return TRUE;
740 env[1] = new_path[0];
741 result = SetEnvironmentVariableW(env, new_path);
742 if (new_path != _new_path)
743 free(new_path);
744 return result;
745}
746#endif
747
Martin v. Löwis14694662006-02-03 12:54:16 +0000748#ifdef MS_WINDOWS
749/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
750 - time stamps are restricted to second resolution
751 - file modification times suffer from forth-and-back conversions between
752 UTC and local time
753 Therefore, we implement our own stat, based on the Win32 API directly.
754*/
755#define HAVE_STAT_NSEC 1
756
757struct win32_stat{
758 int st_dev;
759 __int64 st_ino;
760 unsigned short st_mode;
761 int st_nlink;
762 int st_uid;
763 int st_gid;
764 int st_rdev;
765 __int64 st_size;
766 int st_atime;
767 int st_atime_nsec;
768 int st_mtime;
769 int st_mtime_nsec;
770 int st_ctime;
771 int st_ctime_nsec;
772};
773
774static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
775
776static void
777FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
778{
Martin v. Löwis77c176d2006-05-12 17:22:04 +0000779 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
780 /* Cannot simply cast and dereference in_ptr,
781 since it might not be aligned properly */
782 __int64 in;
783 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000784 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
785 /* XXX Win32 supports time stamps past 2038; we currently don't */
786 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
787}
788
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000789static void
790time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
791{
792 /* XXX endianness */
793 __int64 out;
794 out = time_in + secs_between_epochs;
Martin v. Löwis463a42b2006-10-09 20:44:50 +0000795 out = out * 10000000 + nsec_in / 100;
Martin v. Löwis77c176d2006-05-12 17:22:04 +0000796 memcpy(out_ptr, &out, sizeof(out));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000797}
798
Martin v. Löwis14694662006-02-03 12:54:16 +0000799/* Below, we *know* that ugo+r is 0444 */
800#if _S_IREAD != 0400
801#error Unsupported C library
802#endif
803static int
804attributes_to_mode(DWORD attr)
805{
806 int m = 0;
807 if (attr & FILE_ATTRIBUTE_DIRECTORY)
808 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
809 else
810 m |= _S_IFREG;
811 if (attr & FILE_ATTRIBUTE_READONLY)
812 m |= 0444;
813 else
814 m |= 0666;
815 return m;
816}
817
818static int
819attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
820{
821 memset(result, 0, sizeof(*result));
822 result->st_mode = attributes_to_mode(info->dwFileAttributes);
823 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
824 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
825 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
826 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
827
828 return 0;
829}
830
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000831/* Emulate GetFileAttributesEx[AW] on Windows 95 */
832static int checked = 0;
833static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
834static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
835static void
836check_gfax()
837{
838 HINSTANCE hKernel32;
839 if (checked)
840 return;
841 checked = 1;
842 hKernel32 = GetModuleHandle("KERNEL32");
843 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
844 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
845}
846
847static BOOL WINAPI
848Py_GetFileAttributesExA(LPCSTR pszFile,
849 GET_FILEEX_INFO_LEVELS level,
850 LPVOID pv)
851{
852 BOOL result;
853 HANDLE hFindFile;
854 WIN32_FIND_DATAA FileData;
855 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
856 /* First try to use the system's implementation, if that is
857 available and either succeeds to gives an error other than
858 that it isn't implemented. */
859 check_gfax();
860 if (gfaxa) {
861 result = gfaxa(pszFile, level, pv);
862 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
863 return result;
864 }
865 /* It's either not present, or not implemented.
866 Emulate using FindFirstFile. */
867 if (level != GetFileExInfoStandard) {
868 SetLastError(ERROR_INVALID_PARAMETER);
869 return FALSE;
870 }
871 /* Use GetFileAttributes to validate that the file name
872 does not contain wildcards (which FindFirstFile would
873 accept). */
874 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
875 return FALSE;
876 hFindFile = FindFirstFileA(pszFile, &FileData);
877 if (hFindFile == INVALID_HANDLE_VALUE)
878 return FALSE;
879 FindClose(hFindFile);
880 pfad->dwFileAttributes = FileData.dwFileAttributes;
881 pfad->ftCreationTime = FileData.ftCreationTime;
882 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
883 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
884 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
885 pfad->nFileSizeLow = FileData.nFileSizeLow;
886 return TRUE;
887}
888
889static BOOL WINAPI
890Py_GetFileAttributesExW(LPCWSTR pszFile,
891 GET_FILEEX_INFO_LEVELS level,
892 LPVOID pv)
893{
894 BOOL result;
895 HANDLE hFindFile;
896 WIN32_FIND_DATAW FileData;
897 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
898 /* First try to use the system's implementation, if that is
899 available and either succeeds to gives an error other than
900 that it isn't implemented. */
901 check_gfax();
902 if (gfaxa) {
903 result = gfaxw(pszFile, level, pv);
904 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
905 return result;
906 }
907 /* It's either not present, or not implemented.
908 Emulate using FindFirstFile. */
909 if (level != GetFileExInfoStandard) {
910 SetLastError(ERROR_INVALID_PARAMETER);
911 return FALSE;
912 }
913 /* Use GetFileAttributes to validate that the file name
914 does not contain wildcards (which FindFirstFile would
915 accept). */
916 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
917 return FALSE;
918 hFindFile = FindFirstFileW(pszFile, &FileData);
919 if (hFindFile == INVALID_HANDLE_VALUE)
920 return FALSE;
921 FindClose(hFindFile);
922 pfad->dwFileAttributes = FileData.dwFileAttributes;
923 pfad->ftCreationTime = FileData.ftCreationTime;
924 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
925 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
926 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
927 pfad->nFileSizeLow = FileData.nFileSizeLow;
928 return TRUE;
929}
930
Martin v. Löwis14694662006-02-03 12:54:16 +0000931static int
932win32_stat(const char* path, struct win32_stat *result)
933{
934 WIN32_FILE_ATTRIBUTE_DATA info;
935 int code;
936 char *dot;
937 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000938 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis14694662006-02-03 12:54:16 +0000939 /* Protocol violation: we explicitly clear errno, instead of
940 setting it to a POSIX error. Callers should use GetLastError. */
941 errno = 0;
942 return -1;
943 }
944 code = attribute_data_to_stat(&info, result);
945 if (code != 0)
946 return code;
947 /* Set S_IFEXEC if it is an .exe, .bat, ... */
948 dot = strrchr(path, '.');
949 if (dot) {
950 if (stricmp(dot, ".bat") == 0 ||
951 stricmp(dot, ".cmd") == 0 ||
952 stricmp(dot, ".exe") == 0 ||
953 stricmp(dot, ".com") == 0)
954 result->st_mode |= 0111;
955 }
956 return code;
957}
958
959static int
960win32_wstat(const wchar_t* path, struct win32_stat *result)
961{
962 int code;
963 const wchar_t *dot;
964 WIN32_FILE_ATTRIBUTE_DATA info;
965 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000966 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis14694662006-02-03 12:54:16 +0000967 /* Protocol violation: we explicitly clear errno, instead of
968 setting it to a POSIX error. Callers should use GetLastError. */
969 errno = 0;
970 return -1;
971 }
972 code = attribute_data_to_stat(&info, result);
973 if (code < 0)
974 return code;
975 /* Set IFEXEC if it is an .exe, .bat, ... */
976 dot = wcsrchr(path, '.');
977 if (dot) {
978 if (_wcsicmp(dot, L".bat") == 0 ||
979 _wcsicmp(dot, L".cmd") == 0 ||
980 _wcsicmp(dot, L".exe") == 0 ||
981 _wcsicmp(dot, L".com") == 0)
982 result->st_mode |= 0111;
983 }
984 return code;
985}
986
987static int
988win32_fstat(int file_number, struct win32_stat *result)
989{
990 BY_HANDLE_FILE_INFORMATION info;
991 HANDLE h;
992 int type;
993
994 h = (HANDLE)_get_osfhandle(file_number);
995
996 /* Protocol violation: we explicitly clear errno, instead of
997 setting it to a POSIX error. Callers should use GetLastError. */
998 errno = 0;
999
1000 if (h == INVALID_HANDLE_VALUE) {
1001 /* This is really a C library error (invalid file handle).
1002 We set the Win32 error to the closes one matching. */
1003 SetLastError(ERROR_INVALID_HANDLE);
1004 return -1;
1005 }
1006 memset(result, 0, sizeof(*result));
1007
1008 type = GetFileType(h);
1009 if (type == FILE_TYPE_UNKNOWN) {
1010 DWORD error = GetLastError();
1011 if (error != 0) {
1012 return -1;
1013 }
1014 /* else: valid but unknown file */
1015 }
1016
1017 if (type != FILE_TYPE_DISK) {
1018 if (type == FILE_TYPE_CHAR)
1019 result->st_mode = _S_IFCHR;
1020 else if (type == FILE_TYPE_PIPE)
1021 result->st_mode = _S_IFIFO;
1022 return 0;
1023 }
1024
1025 if (!GetFileInformationByHandle(h, &info)) {
1026 return -1;
1027 }
1028
1029 /* similar to stat() */
1030 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1031 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1032 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1033 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1034 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1035 /* specific to fstat() */
1036 result->st_nlink = info.nNumberOfLinks;
1037 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1038 return 0;
1039}
1040
1041#endif /* MS_WINDOWS */
1042
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001043PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001044"stat_result: Result from stat or lstat.\n\n\
1045This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001046 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001047or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1048\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001049Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1050or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001051\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001052See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001053
1054static PyStructSequence_Field stat_result_fields[] = {
1055 {"st_mode", "protection bits"},
1056 {"st_ino", "inode"},
1057 {"st_dev", "device"},
1058 {"st_nlink", "number of hard links"},
1059 {"st_uid", "user ID of owner"},
1060 {"st_gid", "group ID of owner"},
1061 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001062 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1063 {NULL, "integer time of last access"},
1064 {NULL, "integer time of last modification"},
1065 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001066 {"st_atime", "time of last access"},
1067 {"st_mtime", "time of last modification"},
1068 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001069#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001070 {"st_blksize", "blocksize for filesystem I/O"},
1071#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001072#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001073 {"st_blocks", "number of blocks allocated"},
1074#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001075#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001076 {"st_rdev", "device type (if inode device)"},
1077#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001078#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1079 {"st_flags", "user defined flags for file"},
1080#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001081#ifdef HAVE_STRUCT_STAT_ST_GEN
1082 {"st_gen", "generation number"},
1083#endif
1084#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1085 {"st_birthtime", "time of creation"},
1086#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001087 {0}
1088};
1089
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001090#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001091#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001092#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001093#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001094#endif
1095
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001096#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001097#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1098#else
1099#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1100#endif
1101
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001102#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001103#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1104#else
1105#define ST_RDEV_IDX ST_BLOCKS_IDX
1106#endif
1107
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001108#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1109#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1110#else
1111#define ST_FLAGS_IDX ST_RDEV_IDX
1112#endif
1113
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001114#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001115#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001116#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001117#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001118#endif
1119
1120#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1121#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1122#else
1123#define ST_BIRTHTIME_IDX ST_GEN_IDX
1124#endif
1125
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001126static PyStructSequence_Desc stat_result_desc = {
1127 "stat_result", /* name */
1128 stat_result__doc__, /* doc */
1129 stat_result_fields,
1130 10
1131};
1132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001133PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001134"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1135This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001136 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001137or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001138\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001139See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001140
1141static PyStructSequence_Field statvfs_result_fields[] = {
1142 {"f_bsize", },
1143 {"f_frsize", },
1144 {"f_blocks", },
1145 {"f_bfree", },
1146 {"f_bavail", },
1147 {"f_files", },
1148 {"f_ffree", },
1149 {"f_favail", },
1150 {"f_flag", },
1151 {"f_namemax",},
1152 {0}
1153};
1154
1155static PyStructSequence_Desc statvfs_result_desc = {
1156 "statvfs_result", /* name */
1157 statvfs_result__doc__, /* doc */
1158 statvfs_result_fields,
1159 10
1160};
1161
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00001162static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001163static PyTypeObject StatResultType;
1164static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001165static newfunc structseq_new;
1166
1167static PyObject *
1168statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1169{
1170 PyStructSequence *result;
1171 int i;
1172
1173 result = (PyStructSequence*)structseq_new(type, args, kwds);
1174 if (!result)
1175 return NULL;
1176 /* If we have been initialized from a tuple,
1177 st_?time might be set to None. Initialize it
1178 from the int slots. */
1179 for (i = 7; i <= 9; i++) {
1180 if (result->ob_item[i+3] == Py_None) {
1181 Py_DECREF(Py_None);
1182 Py_INCREF(result->ob_item[i]);
1183 result->ob_item[i+3] = result->ob_item[i];
1184 }
1185 }
1186 return (PyObject*)result;
1187}
1188
1189
1190
1191/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001192static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001193
1194PyDoc_STRVAR(stat_float_times__doc__,
1195"stat_float_times([newval]) -> oldval\n\n\
1196Determine whether os.[lf]stat represents time stamps as float objects.\n\
1197If newval is True, future calls to stat() return floats, if it is False,\n\
1198future calls return ints. \n\
1199If newval is omitted, return the current setting.\n");
1200
1201static PyObject*
1202stat_float_times(PyObject* self, PyObject *args)
1203{
1204 int newval = -1;
1205 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1206 return NULL;
1207 if (newval == -1)
1208 /* Return old value */
1209 return PyBool_FromLong(_stat_float_times);
1210 _stat_float_times = newval;
1211 Py_INCREF(Py_None);
1212 return Py_None;
1213}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001214
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001215static void
1216fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1217{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001218 PyObject *fval,*ival;
1219#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001220 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001221#else
1222 ival = PyInt_FromLong((long)sec);
1223#endif
Neal Norwitze0a81af2006-08-12 01:50:38 +00001224 if (!ival)
1225 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001226 if (_stat_float_times) {
1227 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1228 } else {
1229 fval = ival;
1230 Py_INCREF(fval);
1231 }
1232 PyStructSequence_SET_ITEM(v, index, ival);
1233 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001234}
1235
Tim Peters5aa91602002-01-30 05:46:57 +00001236/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001237 (used by posix_stat() and posix_fstat()) */
1238static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001239_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001240{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001241 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001242 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001243 if (v == NULL)
1244 return NULL;
1245
Martin v. Löwis14694662006-02-03 12:54:16 +00001246 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001247#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001248 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001249 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001250#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001251 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001252#endif
1253#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001254 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001255 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001256#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001257 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001258#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001259 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1260 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1261 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001262#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001263 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001264 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001265#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001266 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001267#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001268
Martin v. Löwis14694662006-02-03 12:54:16 +00001269#if defined(HAVE_STAT_TV_NSEC)
1270 ansec = st->st_atim.tv_nsec;
1271 mnsec = st->st_mtim.tv_nsec;
1272 cnsec = st->st_ctim.tv_nsec;
1273#elif defined(HAVE_STAT_TV_NSEC2)
1274 ansec = st->st_atimespec.tv_nsec;
1275 mnsec = st->st_mtimespec.tv_nsec;
1276 cnsec = st->st_ctimespec.tv_nsec;
1277#elif defined(HAVE_STAT_NSEC)
1278 ansec = st->st_atime_nsec;
1279 mnsec = st->st_mtime_nsec;
1280 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001281#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001282 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001283#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001284 fill_time(v, 7, st->st_atime, ansec);
1285 fill_time(v, 8, st->st_mtime, mnsec);
1286 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001287
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001288#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001289 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001290 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001291#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001292#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001293 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001294 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001295#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001296#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001297 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001298 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001299#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001300#ifdef HAVE_STRUCT_STAT_ST_GEN
1301 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001302 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001303#endif
1304#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1305 {
1306 PyObject *val;
1307 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001308 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001309#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001310 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001311#else
1312 bnsec = 0;
1313#endif
1314 if (_stat_float_times) {
1315 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1316 } else {
1317 val = PyInt_FromLong((long)bsec);
1318 }
1319 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1320 val);
1321 }
1322#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001323#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1324 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001325 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001326#endif
Fred Drake699f3522000-06-29 21:12:41 +00001327
1328 if (PyErr_Occurred()) {
1329 Py_DECREF(v);
1330 return NULL;
1331 }
1332
1333 return v;
1334}
1335
Martin v. Löwisd8948722004-06-02 09:57:56 +00001336#ifdef MS_WINDOWS
1337
1338/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1339 where / can be used in place of \ and the trailing slash is optional.
1340 Both SERVER and SHARE must have at least one character.
1341*/
1342
1343#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1344#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001345#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001346#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001347#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001348
Tim Peters4ad82172004-08-30 17:02:04 +00001349static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001350IsUNCRootA(char *path, int pathlen)
1351{
1352 #define ISSLASH ISSLASHA
1353
1354 int i, share;
1355
1356 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1357 /* minimum UNCRoot is \\x\y */
1358 return FALSE;
1359 for (i = 2; i < pathlen ; i++)
1360 if (ISSLASH(path[i])) break;
1361 if (i == 2 || i == pathlen)
1362 /* do not allow \\\SHARE or \\SERVER */
1363 return FALSE;
1364 share = i+1;
1365 for (i = share; i < pathlen; i++)
1366 if (ISSLASH(path[i])) break;
1367 return (i != share && (i == pathlen || i == pathlen-1));
1368
1369 #undef ISSLASH
1370}
1371
1372#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001373static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001374IsUNCRootW(Py_UNICODE *path, int pathlen)
1375{
1376 #define ISSLASH ISSLASHW
1377
1378 int i, share;
1379
1380 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1381 /* minimum UNCRoot is \\x\y */
1382 return FALSE;
1383 for (i = 2; i < pathlen ; i++)
1384 if (ISSLASH(path[i])) break;
1385 if (i == 2 || i == pathlen)
1386 /* do not allow \\\SHARE or \\SERVER */
1387 return FALSE;
1388 share = i+1;
1389 for (i = share; i < pathlen; i++)
1390 if (ISSLASH(path[i])) break;
1391 return (i != share && (i == pathlen || i == pathlen-1));
1392
1393 #undef ISSLASH
1394}
1395#endif /* Py_WIN_WIDE_FILENAMES */
1396#endif /* MS_WINDOWS */
1397
Barry Warsaw53699e91996-12-10 23:23:01 +00001398static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001399posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001400 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001401#ifdef __VMS
1402 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1403#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001404 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001405#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001406 char *wformat,
1407 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001408{
Fred Drake699f3522000-06-29 21:12:41 +00001409 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001410 char *path = NULL; /* pass this to stat; do not free() it */
1411 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001412 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001413 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001414
1415#ifdef Py_WIN_WIDE_FILENAMES
1416 /* If on wide-character-capable OS see if argument
1417 is Unicode and if so use wide API. */
1418 if (unicode_file_names()) {
1419 PyUnicodeObject *po;
1420 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001421 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1422
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001423 Py_BEGIN_ALLOW_THREADS
1424 /* PyUnicode_AS_UNICODE result OK without
1425 thread lock as it is a simple dereference. */
1426 res = wstatfunc(wpath, &st);
1427 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001428
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001429 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001430 return win32_error_unicode("stat", wpath);
1431 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001432 }
1433 /* Drop the argument parsing error as narrow strings
1434 are also valid. */
1435 PyErr_Clear();
1436 }
1437#endif
1438
Tim Peters5aa91602002-01-30 05:46:57 +00001439 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001440 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001441 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001442 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001443
Barry Warsaw53699e91996-12-10 23:23:01 +00001444 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001445 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001446 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001447
1448 if (res != 0) {
1449#ifdef MS_WINDOWS
1450 result = win32_error("stat", pathfree);
1451#else
1452 result = posix_error_with_filename(pathfree);
1453#endif
1454 }
1455 else
1456 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001457
Tim Peters500bd032001-12-19 19:05:01 +00001458 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001459 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001460}
1461
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001462/* POSIX methods */
1463
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001464PyDoc_STRVAR(posix_access__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001465"access(path, mode) -> 1 if granted, 0 otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001466Use the real uid/gid to test for access to a path. Note that most\n\
1467operations will use the effective uid/gid, therefore this routine can\n\
1468be used in a suid/sgid environment to test if the invoking user has the\n\
1469specified access to the path. The mode argument can be F_OK to test\n\
1470existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001471
1472static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001473posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001474{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001475 char *path;
1476 int mode;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001477
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001478#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001479 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001480 if (unicode_file_names()) {
1481 PyUnicodeObject *po;
1482 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1483 Py_BEGIN_ALLOW_THREADS
1484 /* PyUnicode_AS_UNICODE OK without thread lock as
1485 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001486 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001487 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001488 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001489 }
1490 /* Drop the argument parsing error as narrow strings
1491 are also valid. */
1492 PyErr_Clear();
1493 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001494 if (!PyArg_ParseTuple(args, "eti:access",
1495 Py_FileSystemDefaultEncoding, &path, &mode))
1496 return 0;
1497 Py_BEGIN_ALLOW_THREADS
1498 attr = GetFileAttributesA(path);
1499 Py_END_ALLOW_THREADS
1500 PyMem_Free(path);
1501finish:
1502 if (attr == 0xFFFFFFFF)
1503 /* File does not exist, or cannot read attributes */
1504 return PyBool_FromLong(0);
1505 /* Access is possible if either write access wasn't requested, or
1506 the file isn't read-only. */
Martin v. Löwisee1e06d2006-07-02 18:44:00 +00001507 return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001508#else
1509 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001510 if (!PyArg_ParseTuple(args, "eti:access",
1511 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001512 return NULL;
1513 Py_BEGIN_ALLOW_THREADS
1514 res = access(path, mode);
1515 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001516 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001517 return PyBool_FromLong(res == 0);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001518#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001519}
1520
Guido van Rossumd371ff11999-01-25 16:12:23 +00001521#ifndef F_OK
1522#define F_OK 0
1523#endif
1524#ifndef R_OK
1525#define R_OK 4
1526#endif
1527#ifndef W_OK
1528#define W_OK 2
1529#endif
1530#ifndef X_OK
1531#define X_OK 1
1532#endif
1533
1534#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001535PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001536"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001537Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001538
1539static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001540posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001541{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001542 int id;
1543 char *ret;
1544
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001545 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001546 return NULL;
1547
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001548#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001549 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001550 if (id == 0) {
1551 ret = ttyname();
1552 }
1553 else {
1554 ret = NULL;
1555 }
1556#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001557 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001558#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001559 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001560 return posix_error();
1561 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001562}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001563#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001564
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001565#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001566PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001567"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001568Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001569
1570static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001571posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001572{
1573 char *ret;
1574 char buffer[L_ctermid];
1575
Greg Wardb48bc172000-03-01 21:51:56 +00001576#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001577 ret = ctermid_r(buffer);
1578#else
1579 ret = ctermid(buffer);
1580#endif
1581 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001582 return posix_error();
1583 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001584}
1585#endif
1586
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001587PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001588"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001589Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001590
Barry Warsaw53699e91996-12-10 23:23:01 +00001591static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001592posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001593{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001594#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001595 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001596#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001597 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001598#elif defined(__VMS)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001599 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001600#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001601 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001602#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001603}
1604
Fred Drake4d1e64b2002-04-15 19:40:07 +00001605#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001606PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001607"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001608Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001609opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001610
1611static PyObject *
1612posix_fchdir(PyObject *self, PyObject *fdobj)
1613{
1614 return posix_fildes(fdobj, fchdir);
1615}
1616#endif /* HAVE_FCHDIR */
1617
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001618
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001619PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001620"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001621Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001622
Barry Warsaw53699e91996-12-10 23:23:01 +00001623static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001624posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001625{
Mark Hammondef8b6542001-05-13 08:04:26 +00001626 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001627 int i;
1628 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001629#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001630 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001631 if (unicode_file_names()) {
1632 PyUnicodeObject *po;
1633 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1634 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001635 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1636 if (attr != 0xFFFFFFFF) {
1637 if (i & _S_IWRITE)
1638 attr &= ~FILE_ATTRIBUTE_READONLY;
1639 else
1640 attr |= FILE_ATTRIBUTE_READONLY;
1641 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1642 }
1643 else
1644 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001645 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001646 if (!res)
1647 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001648 PyUnicode_AS_UNICODE(po));
1649 Py_INCREF(Py_None);
1650 return Py_None;
1651 }
1652 /* Drop the argument parsing error as narrow strings
1653 are also valid. */
1654 PyErr_Clear();
1655 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001656 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1657 &path, &i))
1658 return NULL;
1659 Py_BEGIN_ALLOW_THREADS
1660 attr = GetFileAttributesA(path);
1661 if (attr != 0xFFFFFFFF) {
1662 if (i & _S_IWRITE)
1663 attr &= ~FILE_ATTRIBUTE_READONLY;
1664 else
1665 attr |= FILE_ATTRIBUTE_READONLY;
1666 res = SetFileAttributesA(path, attr);
1667 }
1668 else
1669 res = 0;
1670 Py_END_ALLOW_THREADS
1671 if (!res) {
1672 win32_error("chmod", path);
1673 PyMem_Free(path);
1674 return NULL;
1675 }
Martin v. Löwis9f485bc2006-05-08 05:25:56 +00001676 PyMem_Free(path);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001677 Py_INCREF(Py_None);
1678 return Py_None;
1679#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001680 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001681 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001682 return NULL;
1683 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001684 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001685 Py_END_ALLOW_THREADS
1686 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001687 return posix_error_with_allocated_filename(path);
1688 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001689 Py_INCREF(Py_None);
1690 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001691#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001692}
1693
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001694
Martin v. Löwis244edc82001-10-04 22:44:26 +00001695#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001696PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001697"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001698Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001699
1700static PyObject *
1701posix_chroot(PyObject *self, PyObject *args)
1702{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001703 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001704}
1705#endif
1706
Guido van Rossum21142a01999-01-08 21:05:37 +00001707#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001708PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001709"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001710force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001711
1712static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001713posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001714{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001715 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001716}
1717#endif /* HAVE_FSYNC */
1718
1719#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001720
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001721#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001722extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1723#endif
1724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001725PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001726"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001727force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001728 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001729
1730static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001731posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001732{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001733 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001734}
1735#endif /* HAVE_FDATASYNC */
1736
1737
Fredrik Lundh10723342000-07-10 16:38:09 +00001738#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001739PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001740"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001741Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001742
Barry Warsaw53699e91996-12-10 23:23:01 +00001743static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001744posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001745{
Mark Hammondef8b6542001-05-13 08:04:26 +00001746 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001747 int uid, gid;
1748 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001749 if (!PyArg_ParseTuple(args, "etii:chown",
1750 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001751 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001752 return NULL;
1753 Py_BEGIN_ALLOW_THREADS
1754 res = chown(path, (uid_t) uid, (gid_t) gid);
1755 Py_END_ALLOW_THREADS
1756 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001757 return posix_error_with_allocated_filename(path);
1758 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001759 Py_INCREF(Py_None);
1760 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001761}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001762#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001763
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001764#ifdef HAVE_LCHOWN
1765PyDoc_STRVAR(posix_lchown__doc__,
1766"lchown(path, uid, gid)\n\n\
1767Change the owner and group id of path to the numeric uid and gid.\n\
1768This function will not follow symbolic links.");
1769
1770static PyObject *
1771posix_lchown(PyObject *self, PyObject *args)
1772{
1773 char *path = NULL;
1774 int uid, gid;
1775 int res;
1776 if (!PyArg_ParseTuple(args, "etii:lchown",
1777 Py_FileSystemDefaultEncoding, &path,
1778 &uid, &gid))
1779 return NULL;
1780 Py_BEGIN_ALLOW_THREADS
1781 res = lchown(path, (uid_t) uid, (gid_t) gid);
1782 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001783 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001784 return posix_error_with_allocated_filename(path);
1785 PyMem_Free(path);
1786 Py_INCREF(Py_None);
1787 return Py_None;
1788}
1789#endif /* HAVE_LCHOWN */
1790
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001791
Guido van Rossum36bc6801995-06-14 22:54:23 +00001792#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001793PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001794"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001795Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001796
Barry Warsaw53699e91996-12-10 23:23:01 +00001797static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001798posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001799{
1800 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001801 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001802
Barry Warsaw53699e91996-12-10 23:23:01 +00001803 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001804#if defined(PYOS_OS2) && defined(PYCC_GCC)
1805 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001806#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001807 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001808#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001809 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001810 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001811 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001812 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001813}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001814
Walter Dörwald3b918c32002-11-21 20:18:46 +00001815#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001816PyDoc_STRVAR(posix_getcwdu__doc__,
1817"getcwdu() -> path\n\n\
1818Return a unicode string representing the current working directory.");
1819
1820static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001821posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001822{
1823 char buf[1026];
1824 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001825
1826#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001827 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001828 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001829 wchar_t wbuf[1026];
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001830 wchar_t *wbuf2 = wbuf;
1831 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001832 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001833 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1834 /* If the buffer is large enough, len does not include the
1835 terminating \0. If the buffer is too small, len includes
1836 the space needed for the terminator. */
1837 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1838 wbuf2 = malloc(len * sizeof(wchar_t));
1839 if (wbuf2)
1840 len = GetCurrentDirectoryW(len, wbuf2);
1841 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001842 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001843 if (!wbuf2) {
1844 PyErr_NoMemory();
1845 return NULL;
1846 }
1847 if (!len) {
1848 if (wbuf2 != wbuf) free(wbuf2);
1849 return win32_error("getcwdu", NULL);
1850 }
1851 resobj = PyUnicode_FromWideChar(wbuf2, len);
1852 if (wbuf2 != wbuf) free(wbuf2);
1853 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001854 }
1855#endif
1856
1857 Py_BEGIN_ALLOW_THREADS
1858#if defined(PYOS_OS2) && defined(PYCC_GCC)
1859 res = _getcwd2(buf, sizeof buf);
1860#else
1861 res = getcwd(buf, sizeof buf);
1862#endif
1863 Py_END_ALLOW_THREADS
1864 if (res == NULL)
1865 return posix_error();
1866 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1867}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001868#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001869#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001870
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001871
Guido van Rossumb6775db1994-08-01 11:34:53 +00001872#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001873PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001874"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001875Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001876
Barry Warsaw53699e91996-12-10 23:23:01 +00001877static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001878posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001879{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00001880 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001881}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001882#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001883
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001884
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001885PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001886"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001887Return a list containing the names of the entries in the directory.\n\
1888\n\
1889 path: path of directory to list\n\
1890\n\
1891The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001892entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001893
Barry Warsaw53699e91996-12-10 23:23:01 +00001894static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001895posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001896{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001897 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001898 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001899#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001900
Barry Warsaw53699e91996-12-10 23:23:01 +00001901 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001902 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001903 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001904 WIN32_FIND_DATA FileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001905 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00001906 char *bufptr = namebuf;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001907 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001908
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001909#ifdef Py_WIN_WIDE_FILENAMES
1910 /* If on wide-character-capable OS see if argument
1911 is Unicode and if so use wide API. */
1912 if (unicode_file_names()) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001913 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001914 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1915 WIN32_FIND_DATAW wFileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001916 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001917 Py_UNICODE wch;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001918 /* Overallocate for \\*.*\0 */
1919 len = PyUnicode_GET_SIZE(po);
1920 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
1921 if (!wnamebuf) {
1922 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001923 return NULL;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001924 }
1925 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
1926 wch = len > 0 ? wnamebuf[len-1] : '\0';
1927 if (wch != L'/' && wch != L'\\' && wch != L':')
1928 wnamebuf[len++] = L'\\';
1929 wcscpy(wnamebuf + len, L"*.*");
1930 if ((d = PyList_New(0)) == NULL) {
1931 free(wnamebuf);
1932 return NULL;
1933 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001934 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1935 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001936 int error = GetLastError();
1937 if (error == ERROR_FILE_NOT_FOUND) {
1938 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001939 return d;
1940 }
1941 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001942 win32_error_unicode("FindFirstFileW", wnamebuf);
1943 free(wnamebuf);
1944 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001945 }
1946 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001947 /* Skip over . and .. */
1948 if (wcscmp(wFileData.cFileName, L".") != 0 &&
1949 wcscmp(wFileData.cFileName, L"..") != 0) {
1950 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1951 if (v == NULL) {
1952 Py_DECREF(d);
1953 d = NULL;
1954 break;
1955 }
1956 if (PyList_Append(d, v) != 0) {
1957 Py_DECREF(v);
1958 Py_DECREF(d);
1959 d = NULL;
1960 break;
1961 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001962 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001963 }
Georg Brandl622927b2006-03-07 12:48:03 +00001964 Py_BEGIN_ALLOW_THREADS
1965 result = FindNextFileW(hFindFile, &wFileData);
1966 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00001967 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
1968 it got to the end of the directory. */
1969 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
1970 Py_DECREF(d);
1971 win32_error_unicode("FindNextFileW", wnamebuf);
1972 FindClose(hFindFile);
1973 free(wnamebuf);
1974 return NULL;
1975 }
Georg Brandl622927b2006-03-07 12:48:03 +00001976 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001977
1978 if (FindClose(hFindFile) == FALSE) {
1979 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001980 win32_error_unicode("FindClose", wnamebuf);
1981 free(wnamebuf);
1982 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001983 }
Martin v. Löwise3edaea2006-05-15 05:51:36 +00001984 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001985 return d;
1986 }
1987 /* Drop the argument parsing error as narrow strings
1988 are also valid. */
1989 PyErr_Clear();
1990 }
1991#endif
1992
Tim Peters5aa91602002-01-30 05:46:57 +00001993 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001994 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00001995 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00001996 if (len > 0) {
1997 char ch = namebuf[len-1];
1998 if (ch != SEP && ch != ALTSEP && ch != ':')
1999 namebuf[len++] = '/';
2000 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002001 strcpy(namebuf + len, "*.*");
2002
Barry Warsaw53699e91996-12-10 23:23:01 +00002003 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002004 return NULL;
2005
2006 hFindFile = FindFirstFile(namebuf, &FileData);
2007 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002008 int error = GetLastError();
2009 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002010 return d;
2011 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002012 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002013 }
2014 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002015 /* Skip over . and .. */
2016 if (strcmp(FileData.cFileName, ".") != 0 &&
2017 strcmp(FileData.cFileName, "..") != 0) {
2018 v = PyString_FromString(FileData.cFileName);
2019 if (v == NULL) {
2020 Py_DECREF(d);
2021 d = NULL;
2022 break;
2023 }
2024 if (PyList_Append(d, v) != 0) {
2025 Py_DECREF(v);
2026 Py_DECREF(d);
2027 d = NULL;
2028 break;
2029 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002030 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002031 }
Georg Brandl622927b2006-03-07 12:48:03 +00002032 Py_BEGIN_ALLOW_THREADS
2033 result = FindNextFile(hFindFile, &FileData);
2034 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00002035 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2036 it got to the end of the directory. */
2037 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2038 Py_DECREF(d);
2039 win32_error("FindNextFile", namebuf);
2040 FindClose(hFindFile);
2041 return NULL;
2042 }
Georg Brandl622927b2006-03-07 12:48:03 +00002043 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002044
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002045 if (FindClose(hFindFile) == FALSE) {
2046 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002047 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002048 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002049
2050 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002051
Tim Peters0bb44a42000-09-15 07:44:49 +00002052#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002053
2054#ifndef MAX_PATH
2055#define MAX_PATH CCHMAXPATH
2056#endif
2057 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002058 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002059 PyObject *d, *v;
2060 char namebuf[MAX_PATH+5];
2061 HDIR hdir = 1;
2062 ULONG srchcnt = 1;
2063 FILEFINDBUF3 ep;
2064 APIRET rc;
2065
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002066 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002067 return NULL;
2068 if (len >= MAX_PATH) {
2069 PyErr_SetString(PyExc_ValueError, "path too long");
2070 return NULL;
2071 }
2072 strcpy(namebuf, name);
2073 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002074 if (*pt == ALTSEP)
2075 *pt = SEP;
2076 if (namebuf[len-1] != SEP)
2077 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002078 strcpy(namebuf + len, "*.*");
2079
2080 if ((d = PyList_New(0)) == NULL)
2081 return NULL;
2082
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002083 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2084 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002085 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002086 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2087 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2088 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002089
2090 if (rc != NO_ERROR) {
2091 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002092 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002093 }
2094
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002095 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002096 do {
2097 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002098 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002099 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002100
2101 strcpy(namebuf, ep.achName);
2102
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002103 /* Leave Case of Name Alone -- In Native Form */
2104 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002105
2106 v = PyString_FromString(namebuf);
2107 if (v == NULL) {
2108 Py_DECREF(d);
2109 d = NULL;
2110 break;
2111 }
2112 if (PyList_Append(d, v) != 0) {
2113 Py_DECREF(v);
2114 Py_DECREF(d);
2115 d = NULL;
2116 break;
2117 }
2118 Py_DECREF(v);
2119 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2120 }
2121
2122 return d;
2123#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002124
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002125 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002126 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002127 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002128 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002129 int arg_is_unicode = 1;
2130
Georg Brandl05e89b82006-04-11 07:04:06 +00002131 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002132 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2133 arg_is_unicode = 0;
2134 PyErr_Clear();
2135 }
2136 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002137 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002138 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002139 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002140 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002141 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002142 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002143 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002144 return NULL;
2145 }
Georg Brandl622927b2006-03-07 12:48:03 +00002146 for (;;) {
2147 Py_BEGIN_ALLOW_THREADS
2148 ep = readdir(dirp);
2149 Py_END_ALLOW_THREADS
2150 if (ep == NULL)
2151 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002152 if (ep->d_name[0] == '.' &&
2153 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002154 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002155 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00002156 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002157 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002158 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002159 d = NULL;
2160 break;
2161 }
Just van Rossum46c97842003-02-25 21:42:15 +00002162#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00002163 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002164 PyObject *w;
2165
2166 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002167 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002168 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002169 if (w != NULL) {
2170 Py_DECREF(v);
2171 v = w;
2172 }
2173 else {
2174 /* fall back to the original byte string, as
2175 discussed in patch #683592 */
2176 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002177 }
Just van Rossum46c97842003-02-25 21:42:15 +00002178 }
2179#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002180 if (PyList_Append(d, v) != 0) {
2181 Py_DECREF(v);
2182 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002183 d = NULL;
2184 break;
2185 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002186 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002187 }
Georg Brandlbbfe4fa2006-04-11 06:47:43 +00002188 if (errno != 0 && d != NULL) {
2189 /* readdir() returned NULL and set errno */
2190 closedir(dirp);
2191 Py_DECREF(d);
2192 return posix_error_with_allocated_filename(name);
2193 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002194 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002195 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002196
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002197 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002198
Tim Peters0bb44a42000-09-15 07:44:49 +00002199#endif /* which OS */
2200} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002201
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002202#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002203/* A helper function for abspath on win32 */
2204static PyObject *
2205posix__getfullpathname(PyObject *self, PyObject *args)
2206{
2207 /* assume encoded strings wont more than double no of chars */
2208 char inbuf[MAX_PATH*2];
2209 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002210 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002211 char outbuf[MAX_PATH*2];
2212 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002213#ifdef Py_WIN_WIDE_FILENAMES
2214 if (unicode_file_names()) {
2215 PyUnicodeObject *po;
2216 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2217 Py_UNICODE woutbuf[MAX_PATH*2];
2218 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002219 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002220 sizeof(woutbuf)/sizeof(woutbuf[0]),
2221 woutbuf, &wtemp))
2222 return win32_error("GetFullPathName", "");
2223 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2224 }
2225 /* Drop the argument parsing error as narrow strings
2226 are also valid. */
2227 PyErr_Clear();
2228 }
2229#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002230 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2231 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002232 &insize))
2233 return NULL;
2234 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2235 outbuf, &temp))
2236 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002237 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2238 return PyUnicode_Decode(outbuf, strlen(outbuf),
2239 Py_FileSystemDefaultEncoding, NULL);
2240 }
Mark Hammondef8b6542001-05-13 08:04:26 +00002241 return PyString_FromString(outbuf);
2242} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002243#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002244
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002245PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002246"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002247Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002248
Barry Warsaw53699e91996-12-10 23:23:01 +00002249static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002250posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002251{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002252 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002253 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002254 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002255
2256#ifdef Py_WIN_WIDE_FILENAMES
2257 if (unicode_file_names()) {
2258 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002259 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002260 Py_BEGIN_ALLOW_THREADS
2261 /* PyUnicode_AS_UNICODE OK without thread lock as
2262 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002263 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002264 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002265 if (!res)
2266 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002267 Py_INCREF(Py_None);
2268 return Py_None;
2269 }
2270 /* Drop the argument parsing error as narrow strings
2271 are also valid. */
2272 PyErr_Clear();
2273 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002274 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2275 Py_FileSystemDefaultEncoding, &path, &mode))
2276 return NULL;
2277 Py_BEGIN_ALLOW_THREADS
2278 /* PyUnicode_AS_UNICODE OK without thread lock as
2279 it is a simple dereference. */
2280 res = CreateDirectoryA(path, NULL);
2281 Py_END_ALLOW_THREADS
2282 if (!res) {
2283 win32_error("mkdir", path);
2284 PyMem_Free(path);
2285 return NULL;
2286 }
2287 PyMem_Free(path);
2288 Py_INCREF(Py_None);
2289 return Py_None;
2290#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002291
Tim Peters5aa91602002-01-30 05:46:57 +00002292 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002293 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002294 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002295 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002296#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002297 res = mkdir(path);
2298#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002299 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002300#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002301 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002302 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002303 return posix_error_with_allocated_filename(path);
2304 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002305 Py_INCREF(Py_None);
2306 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002307#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002308}
2309
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002310
Neal Norwitz1818ed72006-03-26 00:29:48 +00002311/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2312#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002313#include <sys/resource.h>
2314#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002315
Neal Norwitz1818ed72006-03-26 00:29:48 +00002316
2317#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002318PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002319"nice(inc) -> new_priority\n\n\
2320Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002321
Barry Warsaw53699e91996-12-10 23:23:01 +00002322static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002323posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002324{
2325 int increment, value;
2326
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002327 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002328 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002329
2330 /* There are two flavours of 'nice': one that returns the new
2331 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002332 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2333 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002334
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002335 If we are of the nice family that returns the new priority, we
2336 need to clear errno before the call, and check if errno is filled
2337 before calling posix_error() on a returnvalue of -1, because the
2338 -1 may be the actual new priority! */
2339
2340 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002341 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002342#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002343 if (value == 0)
2344 value = getpriority(PRIO_PROCESS, 0);
2345#endif
2346 if (value == -1 && errno != 0)
2347 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002348 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002349 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002350}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002351#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002352
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002353PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002354"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002355Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002356
Barry Warsaw53699e91996-12-10 23:23:01 +00002357static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002358posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002359{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002360#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002361 PyObject *o1, *o2;
2362 char *p1, *p2;
2363 BOOL result;
2364 if (unicode_file_names()) {
2365 if (!PyArg_ParseTuple(args, "O&O&:rename",
2366 convert_to_unicode, &o1,
2367 convert_to_unicode, &o2))
2368 PyErr_Clear();
2369 else {
2370 Py_BEGIN_ALLOW_THREADS
2371 result = MoveFileW(PyUnicode_AsUnicode(o1),
2372 PyUnicode_AsUnicode(o2));
2373 Py_END_ALLOW_THREADS
2374 Py_DECREF(o1);
2375 Py_DECREF(o2);
2376 if (!result)
2377 return win32_error("rename", NULL);
2378 Py_INCREF(Py_None);
2379 return Py_None;
2380 }
2381 }
2382 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2383 return NULL;
2384 Py_BEGIN_ALLOW_THREADS
2385 result = MoveFileA(p1, p2);
2386 Py_END_ALLOW_THREADS
2387 if (!result)
2388 return win32_error("rename", NULL);
2389 Py_INCREF(Py_None);
2390 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002391#else
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00002392 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002393#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002394}
2395
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002397PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002398"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002399Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002400
Barry Warsaw53699e91996-12-10 23:23:01 +00002401static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002402posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002403{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002404#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002405 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002406#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002407 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002408#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002409}
2410
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002411
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002412PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002413"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002414Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002415
Barry Warsaw53699e91996-12-10 23:23:01 +00002416static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002417posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002418{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002419#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002420 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002421#else
2422 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2423#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002424}
2425
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002426
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002427#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002428PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002429"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002430Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002431
Barry Warsaw53699e91996-12-10 23:23:01 +00002432static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002433posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002434{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002435 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002436 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002437 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002438 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002439 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002440 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002441 Py_END_ALLOW_THREADS
2442 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002443}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002444#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002445
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002446
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002447PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002448"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002449Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002450
Barry Warsaw53699e91996-12-10 23:23:01 +00002451static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002452posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002453{
2454 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002455 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002456 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002457 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002458 if (i < 0)
2459 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002460 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002461}
2462
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002463
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002464PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002465"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002466Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002467
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002468PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002469"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002470Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002471
Barry Warsaw53699e91996-12-10 23:23:01 +00002472static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002473posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002474{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002475#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002476 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002477#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002478 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002479#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002480}
2481
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002482
Guido van Rossumb6775db1994-08-01 11:34:53 +00002483#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002484PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002485"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002486Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002487
Barry Warsaw53699e91996-12-10 23:23:01 +00002488static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002489posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002490{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002491 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002492 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002493
Barry Warsaw53699e91996-12-10 23:23:01 +00002494 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002495 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002496 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002497 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002498 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002499 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002500 u.sysname,
2501 u.nodename,
2502 u.release,
2503 u.version,
2504 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002505}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002506#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002507
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002508static int
2509extract_time(PyObject *t, long* sec, long* usec)
2510{
2511 long intval;
2512 if (PyFloat_Check(t)) {
2513 double tval = PyFloat_AsDouble(t);
2514 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
2515 if (!intobj)
2516 return -1;
2517 intval = PyInt_AsLong(intobj);
2518 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002519 if (intval == -1 && PyErr_Occurred())
2520 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002521 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002522 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002523 if (*usec < 0)
2524 /* If rounding gave us a negative number,
2525 truncate. */
2526 *usec = 0;
2527 return 0;
2528 }
2529 intval = PyInt_AsLong(t);
2530 if (intval == -1 && PyErr_Occurred())
2531 return -1;
2532 *sec = intval;
2533 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002534 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002535}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002536
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002537PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002538"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002539utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002540Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002541second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002542
Barry Warsaw53699e91996-12-10 23:23:01 +00002543static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002544posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002545{
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002546#ifdef Py_WIN_WIDE_FILENAMES
2547 PyObject *arg;
2548 PyUnicodeObject *obwpath;
2549 wchar_t *wpath = NULL;
2550 char *apath = NULL;
2551 HANDLE hFile;
2552 long atimesec, mtimesec, ausec, musec;
2553 FILETIME atime, mtime;
2554 PyObject *result = NULL;
2555
2556 if (unicode_file_names()) {
2557 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2558 wpath = PyUnicode_AS_UNICODE(obwpath);
2559 Py_BEGIN_ALLOW_THREADS
2560 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2561 NULL, OPEN_EXISTING, 0, NULL);
2562 Py_END_ALLOW_THREADS
2563 if (hFile == INVALID_HANDLE_VALUE)
2564 return win32_error_unicode("utime", wpath);
2565 } else
2566 /* Drop the argument parsing error as narrow strings
2567 are also valid. */
2568 PyErr_Clear();
2569 }
2570 if (!wpath) {
2571 if (!PyArg_ParseTuple(args, "etO:utime",
2572 Py_FileSystemDefaultEncoding, &apath, &arg))
2573 return NULL;
2574 Py_BEGIN_ALLOW_THREADS
2575 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2576 NULL, OPEN_EXISTING, 0, NULL);
2577 Py_END_ALLOW_THREADS
2578 if (hFile == INVALID_HANDLE_VALUE) {
2579 win32_error("utime", apath);
2580 PyMem_Free(apath);
2581 return NULL;
2582 }
2583 PyMem_Free(apath);
2584 }
2585
2586 if (arg == Py_None) {
2587 SYSTEMTIME now;
2588 GetSystemTime(&now);
2589 if (!SystemTimeToFileTime(&now, &mtime) ||
2590 !SystemTimeToFileTime(&now, &atime)) {
2591 win32_error("utime", NULL);
2592 goto done;
2593 }
2594 }
2595 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2596 PyErr_SetString(PyExc_TypeError,
2597 "utime() arg 2 must be a tuple (atime, mtime)");
2598 goto done;
2599 }
2600 else {
2601 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2602 &atimesec, &ausec) == -1)
2603 goto done;
Martin v. Löwis463a42b2006-10-09 20:44:50 +00002604 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002605 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2606 &mtimesec, &musec) == -1)
2607 goto done;
Martin v. Löwis463a42b2006-10-09 20:44:50 +00002608 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002609 }
2610 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2611 /* Avoid putting the file name into the error here,
2612 as that may confuse the user into believing that
2613 something is wrong with the file, when it also
2614 could be the time stamp that gives a problem. */
2615 win32_error("utime", NULL);
2616 }
2617 Py_INCREF(Py_None);
2618 result = Py_None;
2619done:
2620 CloseHandle(hFile);
2621 return result;
2622#else /* Py_WIN_WIDE_FILENAMES */
2623
Neal Norwitz2adf2102004-06-09 01:46:02 +00002624 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002625 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002626 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002627 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002628
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002629#if defined(HAVE_UTIMES)
2630 struct timeval buf[2];
2631#define ATIME buf[0].tv_sec
2632#define MTIME buf[1].tv_sec
2633#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002634/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002635 struct utimbuf buf;
2636#define ATIME buf.actime
2637#define MTIME buf.modtime
2638#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002639#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002640 time_t buf[2];
2641#define ATIME buf[0]
2642#define MTIME buf[1]
2643#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002644#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002645
Mark Hammond817c9292003-12-03 01:22:38 +00002646
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002647 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002648 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002649 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002650 if (arg == Py_None) {
2651 /* optional time values not given */
2652 Py_BEGIN_ALLOW_THREADS
2653 res = utime(path, NULL);
2654 Py_END_ALLOW_THREADS
2655 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002656 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002657 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002658 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002659 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002660 return NULL;
2661 }
2662 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002663 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002664 &atime, &ausec) == -1) {
2665 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002666 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002667 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002668 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002669 &mtime, &musec) == -1) {
2670 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002671 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002672 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002673 ATIME = atime;
2674 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002675#ifdef HAVE_UTIMES
2676 buf[0].tv_usec = ausec;
2677 buf[1].tv_usec = musec;
2678 Py_BEGIN_ALLOW_THREADS
2679 res = utimes(path, buf);
2680 Py_END_ALLOW_THREADS
2681#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002682 Py_BEGIN_ALLOW_THREADS
2683 res = utime(path, UTIME_ARG);
2684 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002685#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002686 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002687 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002688 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002689 }
Neal Norwitz96652712004-06-06 20:40:27 +00002690 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002691 Py_INCREF(Py_None);
2692 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002693#undef UTIME_ARG
2694#undef ATIME
2695#undef MTIME
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002696#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002697}
2698
Guido van Rossum85e3b011991-06-03 12:42:10 +00002699
Guido van Rossum3b066191991-06-04 19:40:25 +00002700/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002701
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002702PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002703"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002704Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002705
Barry Warsaw53699e91996-12-10 23:23:01 +00002706static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002707posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002708{
2709 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002710 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002711 return NULL;
2712 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002713 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002714}
2715
Martin v. Löwis114619e2002-10-07 06:44:21 +00002716#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2717static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002718free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002719{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002720 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002721 for (i = 0; i < count; i++)
2722 PyMem_Free(array[i]);
2723 PyMem_DEL(array);
2724}
2725#endif
2726
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002727
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002728#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002729PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002730"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002731Execute an executable path with arguments, replacing current process.\n\
2732\n\
2733 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002734 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002735
Barry Warsaw53699e91996-12-10 23:23:01 +00002736static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002737posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002738{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002739 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002740 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002741 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002742 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002743 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002744
Guido van Rossum89b33251993-10-22 14:26:06 +00002745 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002746 argv is a list or tuple of strings. */
2747
Martin v. Löwis114619e2002-10-07 06:44:21 +00002748 if (!PyArg_ParseTuple(args, "etO:execv",
2749 Py_FileSystemDefaultEncoding,
2750 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002751 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002752 if (PyList_Check(argv)) {
2753 argc = PyList_Size(argv);
2754 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002755 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002756 else if (PyTuple_Check(argv)) {
2757 argc = PyTuple_Size(argv);
2758 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002759 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002760 else {
Fred Drake661ea262000-10-24 19:57:45 +00002761 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002762 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002763 return NULL;
2764 }
2765
Barry Warsaw53699e91996-12-10 23:23:01 +00002766 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002767 if (argvlist == NULL) {
2768 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002769 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002770 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002771 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002772 if (!PyArg_Parse((*getitem)(argv, i), "et",
2773 Py_FileSystemDefaultEncoding,
2774 &argvlist[i])) {
2775 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002776 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002777 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002778 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002779 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002780
Guido van Rossum85e3b011991-06-03 12:42:10 +00002781 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002782 }
2783 argvlist[argc] = NULL;
2784
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002785 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002786
Guido van Rossum85e3b011991-06-03 12:42:10 +00002787 /* If we get here it's definitely an error */
2788
Martin v. Löwis114619e2002-10-07 06:44:21 +00002789 free_string_array(argvlist, argc);
2790 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002791 return posix_error();
2792}
2793
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002794
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002795PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002796"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002797Execute a path with arguments and environment, replacing current process.\n\
2798\n\
2799 path: path of executable file\n\
2800 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002801 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002802
Barry Warsaw53699e91996-12-10 23:23:01 +00002803static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002804posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002805{
2806 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002807 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002808 char **argvlist;
2809 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002810 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002811 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002812 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00002813 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002814
2815 /* execve has three arguments: (path, argv, env), where
2816 argv is a list or tuple of strings and env is a dictionary
2817 like posix.environ. */
2818
Martin v. Löwis114619e2002-10-07 06:44:21 +00002819 if (!PyArg_ParseTuple(args, "etOO:execve",
2820 Py_FileSystemDefaultEncoding,
2821 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002822 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002823 if (PyList_Check(argv)) {
2824 argc = PyList_Size(argv);
2825 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002826 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002827 else if (PyTuple_Check(argv)) {
2828 argc = PyTuple_Size(argv);
2829 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002830 }
2831 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002832 PyErr_SetString(PyExc_TypeError,
2833 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002834 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002835 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002836 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002837 PyErr_SetString(PyExc_TypeError,
2838 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002839 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002840 }
2841
Barry Warsaw53699e91996-12-10 23:23:01 +00002842 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002843 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002844 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002845 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002846 }
2847 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002848 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002849 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002850 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002851 &argvlist[i]))
2852 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002853 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002854 goto fail_1;
2855 }
2856 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002857 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002858 argvlist[argc] = NULL;
2859
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002860 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002861 if (i < 0)
2862 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002863 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002864 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002865 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002866 goto fail_1;
2867 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002868 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002869 keys = PyMapping_Keys(env);
2870 vals = PyMapping_Values(env);
2871 if (!keys || !vals)
2872 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002873 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2874 PyErr_SetString(PyExc_TypeError,
2875 "execve(): env.keys() or env.values() is not a list");
2876 goto fail_2;
2877 }
Tim Peters5aa91602002-01-30 05:46:57 +00002878
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002879 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002880 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002881 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002882
2883 key = PyList_GetItem(keys, pos);
2884 val = PyList_GetItem(vals, pos);
2885 if (!key || !val)
2886 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002887
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002888 if (!PyArg_Parse(
2889 key,
2890 "s;execve() arg 3 contains a non-string key",
2891 &k) ||
2892 !PyArg_Parse(
2893 val,
2894 "s;execve() arg 3 contains a non-string value",
2895 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002896 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002897 goto fail_2;
2898 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002899
2900#if defined(PYOS_OS2)
2901 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2902 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2903#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002904 len = PyString_Size(key) + PyString_Size(val) + 2;
2905 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002906 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002907 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002908 goto fail_2;
2909 }
Tim Petersc8996f52001-12-03 20:41:00 +00002910 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002911 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002912#if defined(PYOS_OS2)
2913 }
2914#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002915 }
2916 envlist[envc] = 0;
2917
2918 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002919
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002920 /* If we get here it's definitely an error */
2921
2922 (void) posix_error();
2923
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002924 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002925 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002926 PyMem_DEL(envlist[envc]);
2927 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002928 fail_1:
2929 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002930 Py_XDECREF(vals);
2931 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002932 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002933 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002934 return NULL;
2935}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002936#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002937
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002938
Guido van Rossuma1065681999-01-25 23:20:23 +00002939#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002940PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002941"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002942Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002943\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002944 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002945 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002946 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002947
2948static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002949posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002950{
2951 char *path;
2952 PyObject *argv;
2953 char **argvlist;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00002954 int mode, i;
2955 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002956 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002957 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00002958
2959 /* spawnv has three arguments: (mode, path, argv), where
2960 argv is a list or tuple of strings. */
2961
Martin v. Löwis114619e2002-10-07 06:44:21 +00002962 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2963 Py_FileSystemDefaultEncoding,
2964 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002965 return NULL;
2966 if (PyList_Check(argv)) {
2967 argc = PyList_Size(argv);
2968 getitem = PyList_GetItem;
2969 }
2970 else if (PyTuple_Check(argv)) {
2971 argc = PyTuple_Size(argv);
2972 getitem = PyTuple_GetItem;
2973 }
2974 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002975 PyErr_SetString(PyExc_TypeError,
2976 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002977 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002978 return NULL;
2979 }
2980
2981 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002982 if (argvlist == NULL) {
2983 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002984 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002985 }
Guido van Rossuma1065681999-01-25 23:20:23 +00002986 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002987 if (!PyArg_Parse((*getitem)(argv, i), "et",
2988 Py_FileSystemDefaultEncoding,
2989 &argvlist[i])) {
2990 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002991 PyErr_SetString(
2992 PyExc_TypeError,
2993 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002994 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00002995 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00002996 }
2997 }
2998 argvlist[argc] = NULL;
2999
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003000#if defined(PYOS_OS2) && defined(PYCC_GCC)
3001 Py_BEGIN_ALLOW_THREADS
3002 spawnval = spawnv(mode, path, argvlist);
3003 Py_END_ALLOW_THREADS
3004#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003005 if (mode == _OLD_P_OVERLAY)
3006 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003007
Tim Peters25059d32001-12-07 20:35:43 +00003008 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003009 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003010 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003011#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003012
Martin v. Löwis114619e2002-10-07 06:44:21 +00003013 free_string_array(argvlist, argc);
3014 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003015
Fred Drake699f3522000-06-29 21:12:41 +00003016 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003017 return posix_error();
3018 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003019#if SIZEOF_LONG == SIZEOF_VOID_P
3020 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003021#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003022 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003023#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003024}
3025
3026
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003027PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003028"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003029Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003030\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003031 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003032 path: path of executable file\n\
3033 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003034 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003035
3036static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003037posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003038{
3039 char *path;
3040 PyObject *argv, *env;
3041 char **argvlist;
3042 char **envlist;
3043 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003044 int mode, pos, envc;
3045 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003046 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003047 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003048 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003049
3050 /* spawnve has four arguments: (mode, path, argv, env), where
3051 argv is a list or tuple of strings and env is a dictionary
3052 like posix.environ. */
3053
Martin v. Löwis114619e2002-10-07 06:44:21 +00003054 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3055 Py_FileSystemDefaultEncoding,
3056 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003057 return NULL;
3058 if (PyList_Check(argv)) {
3059 argc = PyList_Size(argv);
3060 getitem = PyList_GetItem;
3061 }
3062 else if (PyTuple_Check(argv)) {
3063 argc = PyTuple_Size(argv);
3064 getitem = PyTuple_GetItem;
3065 }
3066 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003067 PyErr_SetString(PyExc_TypeError,
3068 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003069 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003070 }
3071 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003072 PyErr_SetString(PyExc_TypeError,
3073 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003074 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003075 }
3076
3077 argvlist = PyMem_NEW(char *, argc+1);
3078 if (argvlist == NULL) {
3079 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003080 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003081 }
3082 for (i = 0; i < argc; i++) {
3083 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003084 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003085 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003086 &argvlist[i]))
3087 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003088 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003089 goto fail_1;
3090 }
3091 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003092 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003093 argvlist[argc] = NULL;
3094
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003095 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003096 if (i < 0)
3097 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003098 envlist = PyMem_NEW(char *, i + 1);
3099 if (envlist == NULL) {
3100 PyErr_NoMemory();
3101 goto fail_1;
3102 }
3103 envc = 0;
3104 keys = PyMapping_Keys(env);
3105 vals = PyMapping_Values(env);
3106 if (!keys || !vals)
3107 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003108 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3109 PyErr_SetString(PyExc_TypeError,
3110 "spawnve(): env.keys() or env.values() is not a list");
3111 goto fail_2;
3112 }
Tim Peters5aa91602002-01-30 05:46:57 +00003113
Guido van Rossuma1065681999-01-25 23:20:23 +00003114 for (pos = 0; pos < i; pos++) {
3115 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003116 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003117
3118 key = PyList_GetItem(keys, pos);
3119 val = PyList_GetItem(vals, pos);
3120 if (!key || !val)
3121 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003122
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003123 if (!PyArg_Parse(
3124 key,
3125 "s;spawnve() arg 3 contains a non-string key",
3126 &k) ||
3127 !PyArg_Parse(
3128 val,
3129 "s;spawnve() arg 3 contains a non-string value",
3130 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003131 {
3132 goto fail_2;
3133 }
Tim Petersc8996f52001-12-03 20:41:00 +00003134 len = PyString_Size(key) + PyString_Size(val) + 2;
3135 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003136 if (p == NULL) {
3137 PyErr_NoMemory();
3138 goto fail_2;
3139 }
Tim Petersc8996f52001-12-03 20:41:00 +00003140 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003141 envlist[envc++] = p;
3142 }
3143 envlist[envc] = 0;
3144
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003145#if defined(PYOS_OS2) && defined(PYCC_GCC)
3146 Py_BEGIN_ALLOW_THREADS
3147 spawnval = spawnve(mode, path, argvlist, envlist);
3148 Py_END_ALLOW_THREADS
3149#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003150 if (mode == _OLD_P_OVERLAY)
3151 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003152
3153 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003154 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003155 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003156#endif
Tim Peters25059d32001-12-07 20:35:43 +00003157
Fred Drake699f3522000-06-29 21:12:41 +00003158 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003159 (void) posix_error();
3160 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003161#if SIZEOF_LONG == SIZEOF_VOID_P
3162 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003163#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003164 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003165#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003166
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003167 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003168 while (--envc >= 0)
3169 PyMem_DEL(envlist[envc]);
3170 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003171 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003172 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003173 Py_XDECREF(vals);
3174 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003175 fail_0:
3176 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003177 return res;
3178}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003179
3180/* OS/2 supports spawnvp & spawnvpe natively */
3181#if defined(PYOS_OS2)
3182PyDoc_STRVAR(posix_spawnvp__doc__,
3183"spawnvp(mode, file, args)\n\n\
3184Execute the program 'file' in a new process, using the environment\n\
3185search path to find the file.\n\
3186\n\
3187 mode: mode of process creation\n\
3188 file: executable file name\n\
3189 args: tuple or list of strings");
3190
3191static PyObject *
3192posix_spawnvp(PyObject *self, PyObject *args)
3193{
3194 char *path;
3195 PyObject *argv;
3196 char **argvlist;
3197 int mode, i, argc;
3198 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003199 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003200
3201 /* spawnvp has three arguments: (mode, path, argv), where
3202 argv is a list or tuple of strings. */
3203
3204 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3205 Py_FileSystemDefaultEncoding,
3206 &path, &argv))
3207 return NULL;
3208 if (PyList_Check(argv)) {
3209 argc = PyList_Size(argv);
3210 getitem = PyList_GetItem;
3211 }
3212 else if (PyTuple_Check(argv)) {
3213 argc = PyTuple_Size(argv);
3214 getitem = PyTuple_GetItem;
3215 }
3216 else {
3217 PyErr_SetString(PyExc_TypeError,
3218 "spawnvp() arg 2 must be a tuple or list");
3219 PyMem_Free(path);
3220 return NULL;
3221 }
3222
3223 argvlist = PyMem_NEW(char *, argc+1);
3224 if (argvlist == NULL) {
3225 PyMem_Free(path);
3226 return PyErr_NoMemory();
3227 }
3228 for (i = 0; i < argc; i++) {
3229 if (!PyArg_Parse((*getitem)(argv, i), "et",
3230 Py_FileSystemDefaultEncoding,
3231 &argvlist[i])) {
3232 free_string_array(argvlist, i);
3233 PyErr_SetString(
3234 PyExc_TypeError,
3235 "spawnvp() arg 2 must contain only strings");
3236 PyMem_Free(path);
3237 return NULL;
3238 }
3239 }
3240 argvlist[argc] = NULL;
3241
3242 Py_BEGIN_ALLOW_THREADS
3243#if defined(PYCC_GCC)
3244 spawnval = spawnvp(mode, path, argvlist);
3245#else
3246 spawnval = _spawnvp(mode, path, argvlist);
3247#endif
3248 Py_END_ALLOW_THREADS
3249
3250 free_string_array(argvlist, argc);
3251 PyMem_Free(path);
3252
3253 if (spawnval == -1)
3254 return posix_error();
3255 else
3256 return Py_BuildValue("l", (long) spawnval);
3257}
3258
3259
3260PyDoc_STRVAR(posix_spawnvpe__doc__,
3261"spawnvpe(mode, file, args, env)\n\n\
3262Execute the program 'file' in a new process, using the environment\n\
3263search path to find the file.\n\
3264\n\
3265 mode: mode of process creation\n\
3266 file: executable file name\n\
3267 args: tuple or list of arguments\n\
3268 env: dictionary of strings mapping to strings");
3269
3270static PyObject *
3271posix_spawnvpe(PyObject *self, PyObject *args)
3272{
3273 char *path;
3274 PyObject *argv, *env;
3275 char **argvlist;
3276 char **envlist;
3277 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3278 int mode, i, pos, argc, envc;
3279 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003280 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003281 int lastarg = 0;
3282
3283 /* spawnvpe has four arguments: (mode, path, argv, env), where
3284 argv is a list or tuple of strings and env is a dictionary
3285 like posix.environ. */
3286
3287 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3288 Py_FileSystemDefaultEncoding,
3289 &path, &argv, &env))
3290 return NULL;
3291 if (PyList_Check(argv)) {
3292 argc = PyList_Size(argv);
3293 getitem = PyList_GetItem;
3294 }
3295 else if (PyTuple_Check(argv)) {
3296 argc = PyTuple_Size(argv);
3297 getitem = PyTuple_GetItem;
3298 }
3299 else {
3300 PyErr_SetString(PyExc_TypeError,
3301 "spawnvpe() arg 2 must be a tuple or list");
3302 goto fail_0;
3303 }
3304 if (!PyMapping_Check(env)) {
3305 PyErr_SetString(PyExc_TypeError,
3306 "spawnvpe() arg 3 must be a mapping object");
3307 goto fail_0;
3308 }
3309
3310 argvlist = PyMem_NEW(char *, argc+1);
3311 if (argvlist == NULL) {
3312 PyErr_NoMemory();
3313 goto fail_0;
3314 }
3315 for (i = 0; i < argc; i++) {
3316 if (!PyArg_Parse((*getitem)(argv, i),
3317 "et;spawnvpe() arg 2 must contain only strings",
3318 Py_FileSystemDefaultEncoding,
3319 &argvlist[i]))
3320 {
3321 lastarg = i;
3322 goto fail_1;
3323 }
3324 }
3325 lastarg = argc;
3326 argvlist[argc] = NULL;
3327
3328 i = PyMapping_Size(env);
3329 if (i < 0)
3330 goto fail_1;
3331 envlist = PyMem_NEW(char *, i + 1);
3332 if (envlist == NULL) {
3333 PyErr_NoMemory();
3334 goto fail_1;
3335 }
3336 envc = 0;
3337 keys = PyMapping_Keys(env);
3338 vals = PyMapping_Values(env);
3339 if (!keys || !vals)
3340 goto fail_2;
3341 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3342 PyErr_SetString(PyExc_TypeError,
3343 "spawnvpe(): env.keys() or env.values() is not a list");
3344 goto fail_2;
3345 }
3346
3347 for (pos = 0; pos < i; pos++) {
3348 char *p, *k, *v;
3349 size_t len;
3350
3351 key = PyList_GetItem(keys, pos);
3352 val = PyList_GetItem(vals, pos);
3353 if (!key || !val)
3354 goto fail_2;
3355
3356 if (!PyArg_Parse(
3357 key,
3358 "s;spawnvpe() arg 3 contains a non-string key",
3359 &k) ||
3360 !PyArg_Parse(
3361 val,
3362 "s;spawnvpe() arg 3 contains a non-string value",
3363 &v))
3364 {
3365 goto fail_2;
3366 }
3367 len = PyString_Size(key) + PyString_Size(val) + 2;
3368 p = PyMem_NEW(char, len);
3369 if (p == NULL) {
3370 PyErr_NoMemory();
3371 goto fail_2;
3372 }
3373 PyOS_snprintf(p, len, "%s=%s", k, v);
3374 envlist[envc++] = p;
3375 }
3376 envlist[envc] = 0;
3377
3378 Py_BEGIN_ALLOW_THREADS
3379#if defined(PYCC_GCC)
3380 spawnval = spawnve(mode, path, argvlist, envlist);
3381#else
3382 spawnval = _spawnve(mode, path, argvlist, envlist);
3383#endif
3384 Py_END_ALLOW_THREADS
3385
3386 if (spawnval == -1)
3387 (void) posix_error();
3388 else
3389 res = Py_BuildValue("l", (long) spawnval);
3390
3391 fail_2:
3392 while (--envc >= 0)
3393 PyMem_DEL(envlist[envc]);
3394 PyMem_DEL(envlist);
3395 fail_1:
3396 free_string_array(argvlist, lastarg);
3397 Py_XDECREF(vals);
3398 Py_XDECREF(keys);
3399 fail_0:
3400 PyMem_Free(path);
3401 return res;
3402}
3403#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003404#endif /* HAVE_SPAWNV */
3405
3406
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003407#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003408PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003409"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003410Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3411\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003412Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003413
3414static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003415posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003416{
Neal Norwitze241ce82003-02-17 18:17:05 +00003417 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003418 if (pid == -1)
3419 return posix_error();
3420 PyOS_AfterFork();
3421 return PyInt_FromLong((long)pid);
3422}
3423#endif
3424
3425
Guido van Rossumad0ee831995-03-01 10:34:45 +00003426#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003427PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003428"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003429Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003430Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003431
Barry Warsaw53699e91996-12-10 23:23:01 +00003432static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003433posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003434{
Neal Norwitze241ce82003-02-17 18:17:05 +00003435 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003436 if (pid == -1)
3437 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003438 if (pid == 0)
3439 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003440 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003441}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003442#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003443
Neal Norwitzb59798b2003-03-21 01:43:31 +00003444/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003445/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3446#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003447#define DEV_PTY_FILE "/dev/ptc"
3448#define HAVE_DEV_PTMX
3449#else
3450#define DEV_PTY_FILE "/dev/ptmx"
3451#endif
3452
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003453#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003454#ifdef HAVE_PTY_H
3455#include <pty.h>
3456#else
3457#ifdef HAVE_LIBUTIL_H
3458#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003459#endif /* HAVE_LIBUTIL_H */
3460#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003461#ifdef HAVE_STROPTS_H
3462#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003463#endif
3464#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003465
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003466#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003467PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003468"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003469Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003470
3471static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003472posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003473{
3474 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003475#ifndef HAVE_OPENPTY
3476 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003477#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003478#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003479 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003480#ifdef sun
Neal Norwitz84a98e02006-04-10 07:44:23 +00003481 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003482#endif
3483#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003484
Thomas Wouters70c21a12000-07-14 14:28:33 +00003485#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003486 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3487 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003488#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003489 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3490 if (slave_name == NULL)
3491 return posix_error();
3492
3493 slave_fd = open(slave_name, O_RDWR);
3494 if (slave_fd < 0)
3495 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003496#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003497 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003498 if (master_fd < 0)
3499 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003500 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003501 /* change permission of slave */
3502 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003503 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003504 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003505 }
3506 /* unlock slave */
3507 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003508 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003509 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003510 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003511 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003512 slave_name = ptsname(master_fd); /* get name of slave */
3513 if (slave_name == NULL)
3514 return posix_error();
3515 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3516 if (slave_fd < 0)
3517 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003518#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003519 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3520 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003521#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003522 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003523#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003524#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003525#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003526
Fred Drake8cef4cf2000-06-28 16:40:38 +00003527 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003528
Fred Drake8cef4cf2000-06-28 16:40:38 +00003529}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003530#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003531
3532#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003533PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003534"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003535Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3536Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003537To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003538
3539static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003540posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003541{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003542 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003543
Fred Drake8cef4cf2000-06-28 16:40:38 +00003544 pid = forkpty(&master_fd, NULL, NULL, NULL);
3545 if (pid == -1)
3546 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003547 if (pid == 0)
3548 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003549 return Py_BuildValue("(ii)", pid, master_fd);
3550}
3551#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003552
Guido van Rossumad0ee831995-03-01 10:34:45 +00003553#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003554PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003555"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003556Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003557
Barry Warsaw53699e91996-12-10 23:23:01 +00003558static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003559posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003560{
Barry Warsaw53699e91996-12-10 23:23:01 +00003561 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003562}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003563#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003564
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003565
Guido van Rossumad0ee831995-03-01 10:34:45 +00003566#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003567PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003568"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003569Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003570
Barry Warsaw53699e91996-12-10 23:23:01 +00003571static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003572posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003573{
Barry Warsaw53699e91996-12-10 23:23:01 +00003574 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003575}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003576#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003577
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003578
Guido van Rossumad0ee831995-03-01 10:34:45 +00003579#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003580PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003581"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003582Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003583
Barry Warsaw53699e91996-12-10 23:23:01 +00003584static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003585posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003586{
Barry Warsaw53699e91996-12-10 23:23:01 +00003587 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003588}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003589#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003590
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003591
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003592PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003593"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003594Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003595
Barry Warsaw53699e91996-12-10 23:23:01 +00003596static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003597posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003598{
Barry Warsaw53699e91996-12-10 23:23:01 +00003599 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003600}
3601
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003602
Fred Drakec9680921999-12-13 16:37:25 +00003603#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003604PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003605"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003606Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003607
3608static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003609posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003610{
3611 PyObject *result = NULL;
3612
Fred Drakec9680921999-12-13 16:37:25 +00003613#ifdef NGROUPS_MAX
3614#define MAX_GROUPS NGROUPS_MAX
3615#else
3616 /* defined to be 16 on Solaris7, so this should be a small number */
3617#define MAX_GROUPS 64
3618#endif
3619 gid_t grouplist[MAX_GROUPS];
3620 int n;
3621
3622 n = getgroups(MAX_GROUPS, grouplist);
3623 if (n < 0)
3624 posix_error();
3625 else {
3626 result = PyList_New(n);
3627 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003628 int i;
3629 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003630 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003631 if (o == NULL) {
3632 Py_DECREF(result);
3633 result = NULL;
3634 break;
3635 }
3636 PyList_SET_ITEM(result, i, o);
3637 }
3638 }
3639 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003640
Fred Drakec9680921999-12-13 16:37:25 +00003641 return result;
3642}
3643#endif
3644
Martin v. Löwis606edc12002-06-13 21:09:11 +00003645#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003646PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003647"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003648Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003649
3650static PyObject *
3651posix_getpgid(PyObject *self, PyObject *args)
3652{
3653 int pid, pgid;
3654 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3655 return NULL;
3656 pgid = getpgid(pid);
3657 if (pgid < 0)
3658 return posix_error();
3659 return PyInt_FromLong((long)pgid);
3660}
3661#endif /* HAVE_GETPGID */
3662
3663
Guido van Rossumb6775db1994-08-01 11:34:53 +00003664#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003665PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003666"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003667Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003668
Barry Warsaw53699e91996-12-10 23:23:01 +00003669static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003670posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003671{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003672#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003673 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003674#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003675 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003676#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003677}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003678#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003679
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003680
Guido van Rossumb6775db1994-08-01 11:34:53 +00003681#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003682PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003683"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003684Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003685
Barry Warsaw53699e91996-12-10 23:23:01 +00003686static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003687posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003688{
Guido van Rossum64933891994-10-20 21:56:42 +00003689#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003690 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003691#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003692 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003693#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003694 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003695 Py_INCREF(Py_None);
3696 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003697}
3698
Guido van Rossumb6775db1994-08-01 11:34:53 +00003699#endif /* HAVE_SETPGRP */
3700
Guido van Rossumad0ee831995-03-01 10:34:45 +00003701#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003702PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003703"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003704Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003705
Barry Warsaw53699e91996-12-10 23:23:01 +00003706static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003707posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003708{
Barry Warsaw53699e91996-12-10 23:23:01 +00003709 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003710}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003711#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003712
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003713
Fred Drake12c6e2d1999-12-14 21:25:03 +00003714#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003715PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003716"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003717Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003718
3719static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003720posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003721{
Neal Norwitze241ce82003-02-17 18:17:05 +00003722 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003723 char *name;
3724 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003725
Fred Drakea30680b2000-12-06 21:24:28 +00003726 errno = 0;
3727 name = getlogin();
3728 if (name == NULL) {
3729 if (errno)
3730 posix_error();
3731 else
3732 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003733 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003734 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003735 else
3736 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003737 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003738
Fred Drake12c6e2d1999-12-14 21:25:03 +00003739 return result;
3740}
3741#endif
3742
Guido van Rossumad0ee831995-03-01 10:34:45 +00003743#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003744PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003745"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003746Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003747
Barry Warsaw53699e91996-12-10 23:23:01 +00003748static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003749posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003750{
Barry Warsaw53699e91996-12-10 23:23:01 +00003751 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003752}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003753#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003754
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003755
Guido van Rossumad0ee831995-03-01 10:34:45 +00003756#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003757PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003758"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003759Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003760
Barry Warsaw53699e91996-12-10 23:23:01 +00003761static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003762posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003763{
3764 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003765 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003766 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003767#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003768 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3769 APIRET rc;
3770 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003771 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003772
3773 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3774 APIRET rc;
3775 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003776 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003777
3778 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003779 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003780#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003781 if (kill(pid, sig) == -1)
3782 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003783#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003784 Py_INCREF(Py_None);
3785 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003786}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003787#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003788
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003789#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003790PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003791"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003792Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003793
3794static PyObject *
3795posix_killpg(PyObject *self, PyObject *args)
3796{
3797 int pgid, sig;
3798 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3799 return NULL;
3800 if (killpg(pgid, sig) == -1)
3801 return posix_error();
3802 Py_INCREF(Py_None);
3803 return Py_None;
3804}
3805#endif
3806
Guido van Rossumc0125471996-06-28 18:55:32 +00003807#ifdef HAVE_PLOCK
3808
3809#ifdef HAVE_SYS_LOCK_H
3810#include <sys/lock.h>
3811#endif
3812
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003813PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003814"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003815Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003816
Barry Warsaw53699e91996-12-10 23:23:01 +00003817static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003818posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003819{
3820 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003821 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003822 return NULL;
3823 if (plock(op) == -1)
3824 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003825 Py_INCREF(Py_None);
3826 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003827}
3828#endif
3829
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003830
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003831#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003832PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003833"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003834Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003835
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003836#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003837#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003838static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003839async_system(const char *command)
3840{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003841 char errormsg[256], args[1024];
3842 RESULTCODES rcodes;
3843 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003844
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003845 char *shell = getenv("COMSPEC");
3846 if (!shell)
3847 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003848
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003849 /* avoid overflowing the argument buffer */
3850 if (strlen(shell) + 3 + strlen(command) >= 1024)
3851 return ERROR_NOT_ENOUGH_MEMORY
3852
3853 args[0] = '\0';
3854 strcat(args, shell);
3855 strcat(args, "/c ");
3856 strcat(args, command);
3857
3858 /* execute asynchronously, inheriting the environment */
3859 rc = DosExecPgm(errormsg,
3860 sizeof(errormsg),
3861 EXEC_ASYNC,
3862 args,
3863 NULL,
3864 &rcodes,
3865 shell);
3866 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003867}
3868
Guido van Rossumd48f2521997-12-05 22:19:34 +00003869static FILE *
3870popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003871{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003872 int oldfd, tgtfd;
3873 HFILE pipeh[2];
3874 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003875
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003876 /* mode determines which of stdin or stdout is reconnected to
3877 * the pipe to the child
3878 */
3879 if (strchr(mode, 'r') != NULL) {
3880 tgt_fd = 1; /* stdout */
3881 } else if (strchr(mode, 'w')) {
3882 tgt_fd = 0; /* stdin */
3883 } else {
3884 *err = ERROR_INVALID_ACCESS;
3885 return NULL;
3886 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003887
Andrew MacIntyrea3be2582004-12-18 09:51:05 +00003888 /* setup the pipe */
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003889 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
3890 *err = rc;
3891 return NULL;
3892 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003893
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003894 /* prevent other threads accessing stdio */
3895 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003896
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003897 /* reconnect stdio and execute child */
3898 oldfd = dup(tgtfd);
3899 close(tgtfd);
3900 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
3901 DosClose(pipeh[tgtfd]);
3902 rc = async_system(command);
3903 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003904
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003905 /* restore stdio */
3906 dup2(oldfd, tgtfd);
3907 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003908
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003909 /* allow other threads access to stdio */
3910 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003911
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003912 /* if execution of child was successful return file stream */
3913 if (rc == NO_ERROR)
3914 return fdopen(pipeh[1 - tgtfd], mode);
3915 else {
3916 DosClose(pipeh[1 - tgtfd]);
3917 *err = rc;
3918 return NULL;
3919 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003920}
3921
3922static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003923posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003924{
3925 char *name;
3926 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003927 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003928 FILE *fp;
3929 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003930 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003931 return NULL;
3932 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003933 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003934 Py_END_ALLOW_THREADS
3935 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003936 return os2_error(err);
3937
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003938 f = PyFile_FromFile(fp, name, mode, fclose);
3939 if (f != NULL)
3940 PyFile_SetBufSize(f, bufsize);
3941 return f;
3942}
3943
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003944#elif defined(PYCC_GCC)
3945
3946/* standard posix version of popen() support */
3947static PyObject *
3948posix_popen(PyObject *self, PyObject *args)
3949{
3950 char *name;
3951 char *mode = "r";
3952 int bufsize = -1;
3953 FILE *fp;
3954 PyObject *f;
3955 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3956 return NULL;
3957 Py_BEGIN_ALLOW_THREADS
3958 fp = popen(name, mode);
3959 Py_END_ALLOW_THREADS
3960 if (fp == NULL)
3961 return posix_error();
3962 f = PyFile_FromFile(fp, name, mode, pclose);
3963 if (f != NULL)
3964 PyFile_SetBufSize(f, bufsize);
3965 return f;
3966}
3967
3968/* fork() under OS/2 has lots'o'warts
3969 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
3970 * most of this code is a ripoff of the win32 code, but using the
3971 * capabilities of EMX's C library routines
3972 */
3973
3974/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
3975#define POPEN_1 1
3976#define POPEN_2 2
3977#define POPEN_3 3
3978#define POPEN_4 4
3979
3980static PyObject *_PyPopen(char *, int, int, int);
3981static int _PyPclose(FILE *file);
3982
3983/*
3984 * Internal dictionary mapping popen* file pointers to process handles,
3985 * for use when retrieving the process exit code. See _PyPclose() below
3986 * for more information on this dictionary's use.
3987 */
3988static PyObject *_PyPopenProcs = NULL;
3989
3990/* os2emx version of popen2()
3991 *
3992 * The result of this function is a pipe (file) connected to the
3993 * process's stdin, and a pipe connected to the process's stdout.
3994 */
3995
3996static PyObject *
3997os2emx_popen2(PyObject *self, PyObject *args)
3998{
3999 PyObject *f;
4000 int tm=0;
4001
4002 char *cmdstring;
4003 char *mode = "t";
4004 int bufsize = -1;
4005 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4006 return NULL;
4007
4008 if (*mode == 't')
4009 tm = O_TEXT;
4010 else if (*mode != 'b') {
4011 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4012 return NULL;
4013 } else
4014 tm = O_BINARY;
4015
4016 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
4017
4018 return f;
4019}
4020
4021/*
4022 * Variation on os2emx.popen2
4023 *
4024 * The result of this function is 3 pipes - the process's stdin,
4025 * stdout and stderr
4026 */
4027
4028static PyObject *
4029os2emx_popen3(PyObject *self, PyObject *args)
4030{
4031 PyObject *f;
4032 int tm = 0;
4033
4034 char *cmdstring;
4035 char *mode = "t";
4036 int bufsize = -1;
4037 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4038 return NULL;
4039
4040 if (*mode == 't')
4041 tm = O_TEXT;
4042 else if (*mode != 'b') {
4043 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4044 return NULL;
4045 } else
4046 tm = O_BINARY;
4047
4048 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
4049
4050 return f;
4051}
4052
4053/*
4054 * Variation on os2emx.popen2
4055 *
Tim Peters11b23062003-04-23 02:39:17 +00004056 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004057 * and stdout+stderr combined as a single pipe.
4058 */
4059
4060static PyObject *
4061os2emx_popen4(PyObject *self, PyObject *args)
4062{
4063 PyObject *f;
4064 int tm = 0;
4065
4066 char *cmdstring;
4067 char *mode = "t";
4068 int bufsize = -1;
4069 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4070 return NULL;
4071
4072 if (*mode == 't')
4073 tm = O_TEXT;
4074 else if (*mode != 'b') {
4075 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4076 return NULL;
4077 } else
4078 tm = O_BINARY;
4079
4080 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
4081
4082 return f;
4083}
4084
4085/* a couple of structures for convenient handling of multiple
4086 * file handles and pipes
4087 */
4088struct file_ref
4089{
4090 int handle;
4091 int flags;
4092};
4093
4094struct pipe_ref
4095{
4096 int rd;
4097 int wr;
4098};
4099
4100/* The following code is derived from the win32 code */
4101
4102static PyObject *
4103_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4104{
4105 struct file_ref stdio[3];
4106 struct pipe_ref p_fd[3];
4107 FILE *p_s[3];
4108 int file_count, i, pipe_err, pipe_pid;
4109 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4110 PyObject *f, *p_f[3];
4111
4112 /* file modes for subsequent fdopen's on pipe handles */
4113 if (mode == O_TEXT)
4114 {
4115 rd_mode = "rt";
4116 wr_mode = "wt";
4117 }
4118 else
4119 {
4120 rd_mode = "rb";
4121 wr_mode = "wb";
4122 }
4123
4124 /* prepare shell references */
4125 if ((shell = getenv("EMXSHELL")) == NULL)
4126 if ((shell = getenv("COMSPEC")) == NULL)
4127 {
4128 errno = ENOENT;
4129 return posix_error();
4130 }
4131
4132 sh_name = _getname(shell);
4133 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4134 opt = "/c";
4135 else
4136 opt = "-c";
4137
4138 /* save current stdio fds + their flags, and set not inheritable */
4139 i = pipe_err = 0;
4140 while (pipe_err >= 0 && i < 3)
4141 {
4142 pipe_err = stdio[i].handle = dup(i);
4143 stdio[i].flags = fcntl(i, F_GETFD, 0);
4144 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4145 i++;
4146 }
4147 if (pipe_err < 0)
4148 {
4149 /* didn't get them all saved - clean up and bail out */
4150 int saved_err = errno;
4151 while (i-- > 0)
4152 {
4153 close(stdio[i].handle);
4154 }
4155 errno = saved_err;
4156 return posix_error();
4157 }
4158
4159 /* create pipe ends */
4160 file_count = 2;
4161 if (n == POPEN_3)
4162 file_count = 3;
4163 i = pipe_err = 0;
4164 while ((pipe_err == 0) && (i < file_count))
4165 pipe_err = pipe((int *)&p_fd[i++]);
4166 if (pipe_err < 0)
4167 {
4168 /* didn't get them all made - clean up and bail out */
4169 while (i-- > 0)
4170 {
4171 close(p_fd[i].wr);
4172 close(p_fd[i].rd);
4173 }
4174 errno = EPIPE;
4175 return posix_error();
4176 }
4177
4178 /* change the actual standard IO streams over temporarily,
4179 * making the retained pipe ends non-inheritable
4180 */
4181 pipe_err = 0;
4182
4183 /* - stdin */
4184 if (dup2(p_fd[0].rd, 0) == 0)
4185 {
4186 close(p_fd[0].rd);
4187 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4188 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4189 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4190 {
4191 close(p_fd[0].wr);
4192 pipe_err = -1;
4193 }
4194 }
4195 else
4196 {
4197 pipe_err = -1;
4198 }
4199
4200 /* - stdout */
4201 if (pipe_err == 0)
4202 {
4203 if (dup2(p_fd[1].wr, 1) == 1)
4204 {
4205 close(p_fd[1].wr);
4206 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4207 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4208 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4209 {
4210 close(p_fd[1].rd);
4211 pipe_err = -1;
4212 }
4213 }
4214 else
4215 {
4216 pipe_err = -1;
4217 }
4218 }
4219
4220 /* - stderr, as required */
4221 if (pipe_err == 0)
4222 switch (n)
4223 {
4224 case POPEN_3:
4225 {
4226 if (dup2(p_fd[2].wr, 2) == 2)
4227 {
4228 close(p_fd[2].wr);
4229 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4230 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4231 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4232 {
4233 close(p_fd[2].rd);
4234 pipe_err = -1;
4235 }
4236 }
4237 else
4238 {
4239 pipe_err = -1;
4240 }
4241 break;
4242 }
4243
4244 case POPEN_4:
4245 {
4246 if (dup2(1, 2) != 2)
4247 {
4248 pipe_err = -1;
4249 }
4250 break;
4251 }
4252 }
4253
4254 /* spawn the child process */
4255 if (pipe_err == 0)
4256 {
4257 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4258 if (pipe_pid == -1)
4259 {
4260 pipe_err = -1;
4261 }
4262 else
4263 {
4264 /* save the PID into the FILE structure
4265 * NOTE: this implementation doesn't actually
4266 * take advantage of this, but do it for
4267 * completeness - AIM Apr01
4268 */
4269 for (i = 0; i < file_count; i++)
4270 p_s[i]->_pid = pipe_pid;
4271 }
4272 }
4273
4274 /* reset standard IO to normal */
4275 for (i = 0; i < 3; i++)
4276 {
4277 dup2(stdio[i].handle, i);
4278 fcntl(i, F_SETFD, stdio[i].flags);
4279 close(stdio[i].handle);
4280 }
4281
4282 /* if any remnant problems, clean up and bail out */
4283 if (pipe_err < 0)
4284 {
4285 for (i = 0; i < 3; i++)
4286 {
4287 close(p_fd[i].rd);
4288 close(p_fd[i].wr);
4289 }
4290 errno = EPIPE;
4291 return posix_error_with_filename(cmdstring);
4292 }
4293
4294 /* build tuple of file objects to return */
4295 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4296 PyFile_SetBufSize(p_f[0], bufsize);
4297 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4298 PyFile_SetBufSize(p_f[1], bufsize);
4299 if (n == POPEN_3)
4300 {
4301 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4302 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004303 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004304 }
4305 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004306 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004307
4308 /*
4309 * Insert the files we've created into the process dictionary
4310 * all referencing the list with the process handle and the
4311 * initial number of files (see description below in _PyPclose).
4312 * Since if _PyPclose later tried to wait on a process when all
4313 * handles weren't closed, it could create a deadlock with the
4314 * child, we spend some energy here to try to ensure that we
4315 * either insert all file handles into the dictionary or none
4316 * at all. It's a little clumsy with the various popen modes
4317 * and variable number of files involved.
4318 */
4319 if (!_PyPopenProcs)
4320 {
4321 _PyPopenProcs = PyDict_New();
4322 }
4323
4324 if (_PyPopenProcs)
4325 {
4326 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4327 int ins_rc[3];
4328
4329 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4330 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4331
4332 procObj = PyList_New(2);
4333 pidObj = PyInt_FromLong((long) pipe_pid);
4334 intObj = PyInt_FromLong((long) file_count);
4335
4336 if (procObj && pidObj && intObj)
4337 {
4338 PyList_SetItem(procObj, 0, pidObj);
4339 PyList_SetItem(procObj, 1, intObj);
4340
4341 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4342 if (fileObj[0])
4343 {
4344 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4345 fileObj[0],
4346 procObj);
4347 }
4348 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4349 if (fileObj[1])
4350 {
4351 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4352 fileObj[1],
4353 procObj);
4354 }
4355 if (file_count >= 3)
4356 {
4357 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4358 if (fileObj[2])
4359 {
4360 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4361 fileObj[2],
4362 procObj);
4363 }
4364 }
4365
4366 if (ins_rc[0] < 0 || !fileObj[0] ||
4367 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4368 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4369 {
4370 /* Something failed - remove any dictionary
4371 * entries that did make it.
4372 */
4373 if (!ins_rc[0] && fileObj[0])
4374 {
4375 PyDict_DelItem(_PyPopenProcs,
4376 fileObj[0]);
4377 }
4378 if (!ins_rc[1] && fileObj[1])
4379 {
4380 PyDict_DelItem(_PyPopenProcs,
4381 fileObj[1]);
4382 }
4383 if (!ins_rc[2] && fileObj[2])
4384 {
4385 PyDict_DelItem(_PyPopenProcs,
4386 fileObj[2]);
4387 }
4388 }
4389 }
Tim Peters11b23062003-04-23 02:39:17 +00004390
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004391 /*
4392 * Clean up our localized references for the dictionary keys
4393 * and value since PyDict_SetItem will Py_INCREF any copies
4394 * that got placed in the dictionary.
4395 */
4396 Py_XDECREF(procObj);
4397 Py_XDECREF(fileObj[0]);
4398 Py_XDECREF(fileObj[1]);
4399 Py_XDECREF(fileObj[2]);
4400 }
4401
4402 /* Child is launched. */
4403 return f;
4404}
4405
4406/*
4407 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4408 * exit code for the child process and return as a result of the close.
4409 *
4410 * This function uses the _PyPopenProcs dictionary in order to map the
4411 * input file pointer to information about the process that was
4412 * originally created by the popen* call that created the file pointer.
4413 * The dictionary uses the file pointer as a key (with one entry
4414 * inserted for each file returned by the original popen* call) and a
4415 * single list object as the value for all files from a single call.
4416 * The list object contains the Win32 process handle at [0], and a file
4417 * count at [1], which is initialized to the total number of file
4418 * handles using that list.
4419 *
4420 * This function closes whichever handle it is passed, and decrements
4421 * the file count in the dictionary for the process handle pointed to
4422 * by this file. On the last close (when the file count reaches zero),
4423 * this function will wait for the child process and then return its
4424 * exit code as the result of the close() operation. This permits the
4425 * files to be closed in any order - it is always the close() of the
4426 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004427 *
4428 * NOTE: This function is currently called with the GIL released.
4429 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004430 */
4431
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004432static int _PyPclose(FILE *file)
4433{
4434 int result;
4435 int exit_code;
4436 int pipe_pid;
4437 PyObject *procObj, *pidObj, *intObj, *fileObj;
4438 int file_count;
4439#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004440 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004441#endif
4442
4443 /* Close the file handle first, to ensure it can't block the
4444 * child from exiting if it's the last handle.
4445 */
4446 result = fclose(file);
4447
4448#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004449 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004450#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004451 if (_PyPopenProcs)
4452 {
4453 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4454 (procObj = PyDict_GetItem(_PyPopenProcs,
4455 fileObj)) != NULL &&
4456 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4457 (intObj = PyList_GetItem(procObj,1)) != NULL)
4458 {
4459 pipe_pid = (int) PyInt_AsLong(pidObj);
4460 file_count = (int) PyInt_AsLong(intObj);
4461
4462 if (file_count > 1)
4463 {
4464 /* Still other files referencing process */
4465 file_count--;
4466 PyList_SetItem(procObj,1,
4467 PyInt_FromLong((long) file_count));
4468 }
4469 else
4470 {
4471 /* Last file for this process */
4472 if (result != EOF &&
4473 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4474 {
4475 /* extract exit status */
4476 if (WIFEXITED(exit_code))
4477 {
4478 result = WEXITSTATUS(exit_code);
4479 }
4480 else
4481 {
4482 errno = EPIPE;
4483 result = -1;
4484 }
4485 }
4486 else
4487 {
4488 /* Indicate failure - this will cause the file object
4489 * to raise an I/O error and translate the last
4490 * error code from errno. We do have a problem with
4491 * last errors that overlap the normal errno table,
4492 * but that's a consistent problem with the file object.
4493 */
4494 result = -1;
4495 }
4496 }
4497
4498 /* Remove this file pointer from dictionary */
4499 PyDict_DelItem(_PyPopenProcs, fileObj);
4500
4501 if (PyDict_Size(_PyPopenProcs) == 0)
4502 {
4503 Py_DECREF(_PyPopenProcs);
4504 _PyPopenProcs = NULL;
4505 }
4506
4507 } /* if object retrieval ok */
4508
4509 Py_XDECREF(fileObj);
4510 } /* if _PyPopenProcs */
4511
4512#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004513 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004514#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004515 return result;
4516}
4517
4518#endif /* PYCC_??? */
4519
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004520#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004521
4522/*
4523 * Portable 'popen' replacement for Win32.
4524 *
4525 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4526 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004527 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004528 */
4529
4530#include <malloc.h>
4531#include <io.h>
4532#include <fcntl.h>
4533
4534/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4535#define POPEN_1 1
4536#define POPEN_2 2
4537#define POPEN_3 3
4538#define POPEN_4 4
4539
4540static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004541static int _PyPclose(FILE *file);
4542
4543/*
4544 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004545 * for use when retrieving the process exit code. See _PyPclose() below
4546 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004547 */
4548static PyObject *_PyPopenProcs = NULL;
4549
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004550
4551/* popen that works from a GUI.
4552 *
4553 * The result of this function is a pipe (file) connected to the
4554 * processes stdin or stdout, depending on the requested mode.
4555 */
4556
4557static PyObject *
4558posix_popen(PyObject *self, PyObject *args)
4559{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00004560 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004561 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004562
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004563 char *cmdstring;
4564 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004565 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004566 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004567 return NULL;
4568
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004569 if (*mode == 'r')
4570 tm = _O_RDONLY;
4571 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00004572 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004573 return NULL;
4574 } else
4575 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004576
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004577 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004578 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004579 return NULL;
4580 }
4581
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004582 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004583 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004584 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004585 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004586 else
4587 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4588
4589 return f;
4590}
4591
4592/* Variation on win32pipe.popen
4593 *
4594 * The result of this function is a pipe (file) connected to the
4595 * process's stdin, and a pipe connected to the process's stdout.
4596 */
4597
4598static PyObject *
4599win32_popen2(PyObject *self, PyObject *args)
4600{
4601 PyObject *f;
4602 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004603
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004604 char *cmdstring;
4605 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004606 int bufsize = -1;
4607 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004608 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004609
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004610 if (*mode == 't')
4611 tm = _O_TEXT;
4612 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004613 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004614 return NULL;
4615 } else
4616 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004617
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004618 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004619 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004620 return NULL;
4621 }
4622
4623 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004624
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004625 return f;
4626}
4627
4628/*
4629 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004630 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004631 * The result of this function is 3 pipes - the process's stdin,
4632 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004633 */
4634
4635static PyObject *
4636win32_popen3(PyObject *self, PyObject *args)
4637{
4638 PyObject *f;
4639 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004640
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004641 char *cmdstring;
4642 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004643 int bufsize = -1;
4644 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004645 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004646
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004647 if (*mode == 't')
4648 tm = _O_TEXT;
4649 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004650 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004651 return NULL;
4652 } else
4653 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004654
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004655 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004656 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004657 return NULL;
4658 }
4659
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004660 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004661
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004662 return f;
4663}
4664
4665/*
4666 * Variation on win32pipe.popen
4667 *
Tim Peters5aa91602002-01-30 05:46:57 +00004668 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004669 * and stdout+stderr combined as a single pipe.
4670 */
4671
4672static PyObject *
4673win32_popen4(PyObject *self, PyObject *args)
4674{
4675 PyObject *f;
4676 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004677
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004678 char *cmdstring;
4679 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004680 int bufsize = -1;
4681 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004682 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004683
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004684 if (*mode == 't')
4685 tm = _O_TEXT;
4686 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004687 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004688 return NULL;
4689 } else
4690 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004691
4692 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004693 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004694 return NULL;
4695 }
4696
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004697 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004698
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004699 return f;
4700}
4701
Mark Hammond08501372001-01-31 07:30:29 +00004702static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004703_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004704 HANDLE hStdin,
4705 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004706 HANDLE hStderr,
4707 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004708{
4709 PROCESS_INFORMATION piProcInfo;
4710 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004711 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004712 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004713 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004714 int i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004715 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004716
4717 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004718 char *comshell;
4719
Tim Peters92e4dd82002-10-05 01:47:34 +00004720 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004721 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
Martin v. Löwis18e16552006-02-15 17:27:45 +00004722 /* x < i, so x fits into an integer */
4723 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00004724
4725 /* Explicitly check if we are using COMMAND.COM. If we are
4726 * then use the w9xpopen hack.
4727 */
4728 comshell = s1 + x;
4729 while (comshell >= s1 && *comshell != '\\')
4730 --comshell;
4731 ++comshell;
4732
4733 if (GetVersion() < 0x80000000 &&
4734 _stricmp(comshell, "command.com") != 0) {
4735 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004736 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004737 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004738 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004739 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004740 }
4741 else {
4742 /*
Tim Peters402d5982001-08-27 06:37:48 +00004743 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4744 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004745 */
Mark Hammond08501372001-01-31 07:30:29 +00004746 char modulepath[_MAX_PATH];
4747 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004748 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
Martin v. Löwis26fd9602006-04-22 11:15:41 +00004749 for (x = i = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004750 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004751 x = i+1;
4752 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004753 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004754 strncat(modulepath,
4755 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004756 (sizeof(modulepath)/sizeof(modulepath[0]))
4757 -strlen(modulepath));
4758 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004759 /* Eeek - file-not-found - possibly an embedding
4760 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004761 */
Tim Peters5aa91602002-01-30 05:46:57 +00004762 strncpy(modulepath,
4763 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00004764 sizeof(modulepath)/sizeof(modulepath[0]));
4765 if (modulepath[strlen(modulepath)-1] != '\\')
4766 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004767 strncat(modulepath,
4768 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004769 (sizeof(modulepath)/sizeof(modulepath[0]))
4770 -strlen(modulepath));
4771 /* No where else to look - raise an easily identifiable
4772 error, rather than leaving Windows to report
4773 "file not found" - as the user is probably blissfully
4774 unaware this shim EXE is used, and it will confuse them.
4775 (well, it confused me for a while ;-)
4776 */
4777 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004778 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004779 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004780 "for popen to work with your shell "
4781 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004782 szConsoleSpawn);
4783 return FALSE;
4784 }
4785 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004786 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004787 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004788 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004789
Tim Peters92e4dd82002-10-05 01:47:34 +00004790 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004791 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004792 /* To maintain correct argument passing semantics,
4793 we pass the command-line as it stands, and allow
4794 quoting to be applied. w9xpopen.exe will then
4795 use its argv vector, and re-quote the necessary
4796 args for the ultimate child process.
4797 */
Tim Peters75cdad52001-11-28 22:07:30 +00004798 PyOS_snprintf(
4799 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004800 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004801 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004802 s1,
4803 s3,
4804 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004805 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004806 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004807 dialog:
4808 "Your program accessed mem currently in use at xxx"
4809 and a hopeful warning about the stability of your
4810 system.
4811 Cost is Ctrl+C wont kill children, but anyone
4812 who cares can have a go!
4813 */
4814 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004815 }
4816 }
4817
4818 /* Could be an else here to try cmd.exe / command.com in the path
4819 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004820 else {
Tim Peters402d5982001-08-27 06:37:48 +00004821 PyErr_SetString(PyExc_RuntimeError,
4822 "Cannot locate a COMSPEC environment variable to "
4823 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004824 return FALSE;
4825 }
Tim Peters5aa91602002-01-30 05:46:57 +00004826
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004827 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4828 siStartInfo.cb = sizeof(STARTUPINFO);
4829 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4830 siStartInfo.hStdInput = hStdin;
4831 siStartInfo.hStdOutput = hStdout;
4832 siStartInfo.hStdError = hStderr;
4833 siStartInfo.wShowWindow = SW_HIDE;
4834
4835 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004836 s2,
4837 NULL,
4838 NULL,
4839 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004840 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004841 NULL,
4842 NULL,
4843 &siStartInfo,
4844 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004845 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004846 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004847
Mark Hammondb37a3732000-08-14 04:47:33 +00004848 /* Return process handle */
4849 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004850 return TRUE;
4851 }
Tim Peters402d5982001-08-27 06:37:48 +00004852 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004853 return FALSE;
4854}
4855
4856/* The following code is based off of KB: Q190351 */
4857
4858static PyObject *
4859_PyPopen(char *cmdstring, int mode, int n)
4860{
4861 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4862 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004863 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004864
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004865 SECURITY_ATTRIBUTES saAttr;
4866 BOOL fSuccess;
4867 int fd1, fd2, fd3;
4868 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004869 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004870 PyObject *f;
4871
4872 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4873 saAttr.bInheritHandle = TRUE;
4874 saAttr.lpSecurityDescriptor = NULL;
4875
4876 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4877 return win32_error("CreatePipe", NULL);
4878
4879 /* Create new output read handle and the input write handle. Set
4880 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004881 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004882 * being created. */
4883 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004884 GetCurrentProcess(), &hChildStdinWrDup, 0,
4885 FALSE,
4886 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004887 if (!fSuccess)
4888 return win32_error("DuplicateHandle", NULL);
4889
4890 /* Close the inheritable version of ChildStdin
4891 that we're using. */
4892 CloseHandle(hChildStdinWr);
4893
4894 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4895 return win32_error("CreatePipe", NULL);
4896
4897 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004898 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4899 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004900 if (!fSuccess)
4901 return win32_error("DuplicateHandle", NULL);
4902
4903 /* Close the inheritable version of ChildStdout
4904 that we're using. */
4905 CloseHandle(hChildStdoutRd);
4906
4907 if (n != POPEN_4) {
4908 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4909 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004910 fSuccess = DuplicateHandle(GetCurrentProcess(),
4911 hChildStderrRd,
4912 GetCurrentProcess(),
4913 &hChildStderrRdDup, 0,
4914 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004915 if (!fSuccess)
4916 return win32_error("DuplicateHandle", NULL);
4917 /* Close the inheritable version of ChildStdErr that we're using. */
4918 CloseHandle(hChildStderrRd);
4919 }
Tim Peters5aa91602002-01-30 05:46:57 +00004920
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004921 switch (n) {
4922 case POPEN_1:
4923 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4924 case _O_WRONLY | _O_TEXT:
4925 /* Case for writing to child Stdin in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004926 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004927 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004928 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004929 PyFile_SetBufSize(f, 0);
4930 /* We don't care about these pipes anymore, so close them. */
4931 CloseHandle(hChildStdoutRdDup);
4932 CloseHandle(hChildStderrRdDup);
4933 break;
4934
4935 case _O_RDONLY | _O_TEXT:
4936 /* Case for reading from child Stdout in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004937 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004938 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004939 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004940 PyFile_SetBufSize(f, 0);
4941 /* We don't care about these pipes anymore, so close them. */
4942 CloseHandle(hChildStdinWrDup);
4943 CloseHandle(hChildStderrRdDup);
4944 break;
4945
4946 case _O_RDONLY | _O_BINARY:
4947 /* Case for readinig from child Stdout in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004948 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004949 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004950 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004951 PyFile_SetBufSize(f, 0);
4952 /* We don't care about these pipes anymore, so close them. */
4953 CloseHandle(hChildStdinWrDup);
4954 CloseHandle(hChildStderrRdDup);
4955 break;
4956
4957 case _O_WRONLY | _O_BINARY:
4958 /* Case for writing to child Stdin in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004959 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004960 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004961 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004962 PyFile_SetBufSize(f, 0);
4963 /* We don't care about these pipes anymore, so close them. */
4964 CloseHandle(hChildStdoutRdDup);
4965 CloseHandle(hChildStderrRdDup);
4966 break;
4967 }
Mark Hammondb37a3732000-08-14 04:47:33 +00004968 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004969 break;
Tim Peters5aa91602002-01-30 05:46:57 +00004970
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004971 case POPEN_2:
4972 case POPEN_4:
4973 {
4974 char *m1, *m2;
4975 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00004976
Tim Peters7dca21e2002-08-19 00:42:29 +00004977 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004978 m1 = "r";
4979 m2 = "w";
4980 } else {
4981 m1 = "rb";
4982 m2 = "wb";
4983 }
4984
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004985 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004986 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004987 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004988 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004989 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004990 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00004991 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004992 PyFile_SetBufSize(p2, 0);
4993
4994 if (n != 4)
4995 CloseHandle(hChildStderrRdDup);
4996
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004997 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00004998 Py_XDECREF(p1);
4999 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00005000 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005001 break;
5002 }
Tim Peters5aa91602002-01-30 05:46:57 +00005003
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005004 case POPEN_3:
5005 {
5006 char *m1, *m2;
5007 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005008
Tim Peters7dca21e2002-08-19 00:42:29 +00005009 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005010 m1 = "r";
5011 m2 = "w";
5012 } else {
5013 m1 = "rb";
5014 m2 = "wb";
5015 }
5016
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005017 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005018 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005019 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005020 f2 = _fdopen(fd2, m1);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005021 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005022 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005023 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00005024 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5025 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005026 PyFile_SetBufSize(p1, 0);
5027 PyFile_SetBufSize(p2, 0);
5028 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005029 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00005030 Py_XDECREF(p1);
5031 Py_XDECREF(p2);
5032 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00005033 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005034 break;
5035 }
5036 }
5037
5038 if (n == POPEN_4) {
5039 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005040 hChildStdinRd,
5041 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005042 hChildStdoutWr,
5043 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005044 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005045 }
5046 else {
5047 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005048 hChildStdinRd,
5049 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005050 hChildStderrWr,
5051 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005052 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005053 }
5054
Mark Hammondb37a3732000-08-14 04:47:33 +00005055 /*
5056 * Insert the files we've created into the process dictionary
5057 * all referencing the list with the process handle and the
5058 * initial number of files (see description below in _PyPclose).
5059 * Since if _PyPclose later tried to wait on a process when all
5060 * handles weren't closed, it could create a deadlock with the
5061 * child, we spend some energy here to try to ensure that we
5062 * either insert all file handles into the dictionary or none
5063 * at all. It's a little clumsy with the various popen modes
5064 * and variable number of files involved.
5065 */
5066 if (!_PyPopenProcs) {
5067 _PyPopenProcs = PyDict_New();
5068 }
5069
5070 if (_PyPopenProcs) {
5071 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5072 int ins_rc[3];
5073
5074 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5075 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
5076
5077 procObj = PyList_New(2);
5078 hProcessObj = PyLong_FromVoidPtr(hProcess);
5079 intObj = PyInt_FromLong(file_count);
5080
5081 if (procObj && hProcessObj && intObj) {
5082 PyList_SetItem(procObj,0,hProcessObj);
5083 PyList_SetItem(procObj,1,intObj);
5084
5085 fileObj[0] = PyLong_FromVoidPtr(f1);
5086 if (fileObj[0]) {
5087 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5088 fileObj[0],
5089 procObj);
5090 }
5091 if (file_count >= 2) {
5092 fileObj[1] = PyLong_FromVoidPtr(f2);
5093 if (fileObj[1]) {
5094 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5095 fileObj[1],
5096 procObj);
5097 }
5098 }
5099 if (file_count >= 3) {
5100 fileObj[2] = PyLong_FromVoidPtr(f3);
5101 if (fileObj[2]) {
5102 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5103 fileObj[2],
5104 procObj);
5105 }
5106 }
5107
5108 if (ins_rc[0] < 0 || !fileObj[0] ||
5109 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5110 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5111 /* Something failed - remove any dictionary
5112 * entries that did make it.
5113 */
5114 if (!ins_rc[0] && fileObj[0]) {
5115 PyDict_DelItem(_PyPopenProcs,
5116 fileObj[0]);
5117 }
5118 if (!ins_rc[1] && fileObj[1]) {
5119 PyDict_DelItem(_PyPopenProcs,
5120 fileObj[1]);
5121 }
5122 if (!ins_rc[2] && fileObj[2]) {
5123 PyDict_DelItem(_PyPopenProcs,
5124 fileObj[2]);
5125 }
5126 }
5127 }
Tim Peters5aa91602002-01-30 05:46:57 +00005128
Mark Hammondb37a3732000-08-14 04:47:33 +00005129 /*
5130 * Clean up our localized references for the dictionary keys
5131 * and value since PyDict_SetItem will Py_INCREF any copies
5132 * that got placed in the dictionary.
5133 */
5134 Py_XDECREF(procObj);
5135 Py_XDECREF(fileObj[0]);
5136 Py_XDECREF(fileObj[1]);
5137 Py_XDECREF(fileObj[2]);
5138 }
5139
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005140 /* Child is launched. Close the parents copy of those pipe
5141 * handles that only the child should have open. You need to
5142 * make sure that no handles to the write end of the output pipe
5143 * are maintained in this process or else the pipe will not close
5144 * when the child process exits and the ReadFile will hang. */
5145
5146 if (!CloseHandle(hChildStdinRd))
5147 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005148
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005149 if (!CloseHandle(hChildStdoutWr))
5150 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005151
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005152 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5153 return win32_error("CloseHandle", NULL);
5154
5155 return f;
5156}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005157
5158/*
5159 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5160 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005161 *
5162 * This function uses the _PyPopenProcs dictionary in order to map the
5163 * input file pointer to information about the process that was
5164 * originally created by the popen* call that created the file pointer.
5165 * The dictionary uses the file pointer as a key (with one entry
5166 * inserted for each file returned by the original popen* call) and a
5167 * single list object as the value for all files from a single call.
5168 * The list object contains the Win32 process handle at [0], and a file
5169 * count at [1], which is initialized to the total number of file
5170 * handles using that list.
5171 *
5172 * This function closes whichever handle it is passed, and decrements
5173 * the file count in the dictionary for the process handle pointed to
5174 * by this file. On the last close (when the file count reaches zero),
5175 * this function will wait for the child process and then return its
5176 * exit code as the result of the close() operation. This permits the
5177 * files to be closed in any order - it is always the close() of the
5178 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005179 *
5180 * NOTE: This function is currently called with the GIL released.
5181 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005182 */
Tim Peters736aa322000-09-01 06:51:24 +00005183
Fredrik Lundh56055a42000-07-23 19:47:12 +00005184static int _PyPclose(FILE *file)
5185{
Fredrik Lundh20318932000-07-26 17:29:12 +00005186 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005187 DWORD exit_code;
5188 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00005189 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5190 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005191#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005192 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005193#endif
5194
Fredrik Lundh20318932000-07-26 17:29:12 +00005195 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00005196 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00005197 */
5198 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005199#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005200 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005201#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005202 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005203 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5204 (procObj = PyDict_GetItem(_PyPopenProcs,
5205 fileObj)) != NULL &&
5206 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5207 (intObj = PyList_GetItem(procObj,1)) != NULL) {
5208
5209 hProcess = PyLong_AsVoidPtr(hProcessObj);
5210 file_count = PyInt_AsLong(intObj);
5211
5212 if (file_count > 1) {
5213 /* Still other files referencing process */
5214 file_count--;
5215 PyList_SetItem(procObj,1,
5216 PyInt_FromLong(file_count));
5217 } else {
5218 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00005219 if (result != EOF &&
5220 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5221 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00005222 /* Possible truncation here in 16-bit environments, but
5223 * real exit codes are just the lower byte in any event.
5224 */
5225 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005226 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00005227 /* Indicate failure - this will cause the file object
5228 * to raise an I/O error and translate the last Win32
5229 * error code from errno. We do have a problem with
5230 * last errors that overlap the normal errno table,
5231 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005232 */
Fredrik Lundh20318932000-07-26 17:29:12 +00005233 if (result != EOF) {
5234 /* If the error wasn't from the fclose(), then
5235 * set errno for the file object error handling.
5236 */
5237 errno = GetLastError();
5238 }
5239 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005240 }
5241
5242 /* Free up the native handle at this point */
5243 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00005244 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005245
Mark Hammondb37a3732000-08-14 04:47:33 +00005246 /* Remove this file pointer from dictionary */
5247 PyDict_DelItem(_PyPopenProcs, fileObj);
5248
5249 if (PyDict_Size(_PyPopenProcs) == 0) {
5250 Py_DECREF(_PyPopenProcs);
5251 _PyPopenProcs = NULL;
5252 }
5253
5254 } /* if object retrieval ok */
5255
5256 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005257 } /* if _PyPopenProcs */
5258
Tim Peters736aa322000-09-01 06:51:24 +00005259#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005260 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005261#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005262 return result;
5263}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005264
5265#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005266static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005267posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005268{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005269 char *name;
5270 char *mode = "r";
5271 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00005272 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005273 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005274 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00005275 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00005276 /* Strip mode of binary or text modifiers */
5277 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5278 mode = "r";
5279 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5280 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00005281 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005282 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005283 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00005284 if (fp == NULL)
5285 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005286 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005287 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005288 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005289 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005290}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005291
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005292#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005293#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005294
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005295
Guido van Rossumb6775db1994-08-01 11:34:53 +00005296#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005297PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005298"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005299Set the current process's user id.");
5300
Barry Warsaw53699e91996-12-10 23:23:01 +00005301static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005302posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005303{
5304 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005305 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005306 return NULL;
5307 if (setuid(uid) < 0)
5308 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005309 Py_INCREF(Py_None);
5310 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005311}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005312#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005313
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005314
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005315#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005316PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005317"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005318Set the current process's effective user id.");
5319
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005320static PyObject *
5321posix_seteuid (PyObject *self, PyObject *args)
5322{
5323 int euid;
5324 if (!PyArg_ParseTuple(args, "i", &euid)) {
5325 return NULL;
5326 } else if (seteuid(euid) < 0) {
5327 return posix_error();
5328 } else {
5329 Py_INCREF(Py_None);
5330 return Py_None;
5331 }
5332}
5333#endif /* HAVE_SETEUID */
5334
5335#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005336PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005337"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005338Set the current process's effective group id.");
5339
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005340static PyObject *
5341posix_setegid (PyObject *self, PyObject *args)
5342{
5343 int egid;
5344 if (!PyArg_ParseTuple(args, "i", &egid)) {
5345 return NULL;
5346 } else if (setegid(egid) < 0) {
5347 return posix_error();
5348 } else {
5349 Py_INCREF(Py_None);
5350 return Py_None;
5351 }
5352}
5353#endif /* HAVE_SETEGID */
5354
5355#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005356PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005357"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005358Set the current process's real and effective user ids.");
5359
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005360static PyObject *
5361posix_setreuid (PyObject *self, PyObject *args)
5362{
5363 int ruid, euid;
5364 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
5365 return NULL;
5366 } else if (setreuid(ruid, euid) < 0) {
5367 return posix_error();
5368 } else {
5369 Py_INCREF(Py_None);
5370 return Py_None;
5371 }
5372}
5373#endif /* HAVE_SETREUID */
5374
5375#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005376PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005377"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005378Set the current process's real and effective group ids.");
5379
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005380static PyObject *
5381posix_setregid (PyObject *self, PyObject *args)
5382{
5383 int rgid, egid;
5384 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
5385 return NULL;
5386 } else if (setregid(rgid, egid) < 0) {
5387 return posix_error();
5388 } else {
5389 Py_INCREF(Py_None);
5390 return Py_None;
5391 }
5392}
5393#endif /* HAVE_SETREGID */
5394
Guido van Rossumb6775db1994-08-01 11:34:53 +00005395#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005396PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005397"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005398Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005399
Barry Warsaw53699e91996-12-10 23:23:01 +00005400static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005401posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005402{
5403 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005404 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005405 return NULL;
5406 if (setgid(gid) < 0)
5407 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005408 Py_INCREF(Py_None);
5409 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005410}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005411#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005412
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005413#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005414PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005415"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005416Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005417
5418static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005419posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005420{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005421 int i, len;
5422 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005423
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005424 if (!PySequence_Check(groups)) {
5425 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5426 return NULL;
5427 }
5428 len = PySequence_Size(groups);
5429 if (len > MAX_GROUPS) {
5430 PyErr_SetString(PyExc_ValueError, "too many groups");
5431 return NULL;
5432 }
5433 for(i = 0; i < len; i++) {
5434 PyObject *elem;
5435 elem = PySequence_GetItem(groups, i);
5436 if (!elem)
5437 return NULL;
5438 if (!PyInt_Check(elem)) {
Georg Brandla13c2442005-11-22 19:30:31 +00005439 if (!PyLong_Check(elem)) {
5440 PyErr_SetString(PyExc_TypeError,
5441 "groups must be integers");
5442 Py_DECREF(elem);
5443 return NULL;
5444 } else {
5445 unsigned long x = PyLong_AsUnsignedLong(elem);
5446 if (PyErr_Occurred()) {
5447 PyErr_SetString(PyExc_TypeError,
5448 "group id too big");
5449 Py_DECREF(elem);
5450 return NULL;
5451 }
5452 grouplist[i] = x;
5453 /* read back the value to see if it fitted in gid_t */
5454 if (grouplist[i] != x) {
5455 PyErr_SetString(PyExc_TypeError,
5456 "group id too big");
5457 Py_DECREF(elem);
5458 return NULL;
5459 }
5460 }
5461 } else {
5462 long x = PyInt_AsLong(elem);
5463 grouplist[i] = x;
5464 if (grouplist[i] != x) {
5465 PyErr_SetString(PyExc_TypeError,
5466 "group id too big");
5467 Py_DECREF(elem);
5468 return NULL;
5469 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005470 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005471 Py_DECREF(elem);
5472 }
5473
5474 if (setgroups(len, grouplist) < 0)
5475 return posix_error();
5476 Py_INCREF(Py_None);
5477 return Py_None;
5478}
5479#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005480
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005481#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00005482static PyObject *
5483wait_helper(int pid, int status, struct rusage *ru)
5484{
5485 PyObject *result;
5486 static PyObject *struct_rusage;
5487
5488 if (pid == -1)
5489 return posix_error();
5490
5491 if (struct_rusage == NULL) {
5492 PyObject *m = PyImport_ImportModule("resource");
5493 if (m == NULL)
5494 return NULL;
5495 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5496 Py_DECREF(m);
5497 if (struct_rusage == NULL)
5498 return NULL;
5499 }
5500
5501 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5502 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5503 if (!result)
5504 return NULL;
5505
5506#ifndef doubletime
5507#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5508#endif
5509
5510 PyStructSequence_SET_ITEM(result, 0,
5511 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5512 PyStructSequence_SET_ITEM(result, 1,
5513 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5514#define SET_INT(result, index, value)\
5515 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5516 SET_INT(result, 2, ru->ru_maxrss);
5517 SET_INT(result, 3, ru->ru_ixrss);
5518 SET_INT(result, 4, ru->ru_idrss);
5519 SET_INT(result, 5, ru->ru_isrss);
5520 SET_INT(result, 6, ru->ru_minflt);
5521 SET_INT(result, 7, ru->ru_majflt);
5522 SET_INT(result, 8, ru->ru_nswap);
5523 SET_INT(result, 9, ru->ru_inblock);
5524 SET_INT(result, 10, ru->ru_oublock);
5525 SET_INT(result, 11, ru->ru_msgsnd);
5526 SET_INT(result, 12, ru->ru_msgrcv);
5527 SET_INT(result, 13, ru->ru_nsignals);
5528 SET_INT(result, 14, ru->ru_nvcsw);
5529 SET_INT(result, 15, ru->ru_nivcsw);
5530#undef SET_INT
5531
5532 if (PyErr_Occurred()) {
5533 Py_DECREF(result);
5534 return NULL;
5535 }
5536
Neal Norwitz9b00a562006-03-20 08:47:12 +00005537 return Py_BuildValue("iiN", pid, status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00005538}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005539#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00005540
5541#ifdef HAVE_WAIT3
5542PyDoc_STRVAR(posix_wait3__doc__,
5543"wait3(options) -> (pid, status, rusage)\n\n\
5544Wait for completion of a child process.");
5545
5546static PyObject *
5547posix_wait3(PyObject *self, PyObject *args)
5548{
5549 int pid, options;
5550 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005551 WAIT_TYPE status;
5552 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005553
5554 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5555 return NULL;
5556
5557 Py_BEGIN_ALLOW_THREADS
5558 pid = wait3(&status, options, &ru);
5559 Py_END_ALLOW_THREADS
5560
Neal Norwitzd5a37542006-03-20 06:48:34 +00005561 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005562}
5563#endif /* HAVE_WAIT3 */
5564
5565#ifdef HAVE_WAIT4
5566PyDoc_STRVAR(posix_wait4__doc__,
5567"wait4(pid, options) -> (pid, status, rusage)\n\n\
5568Wait for completion of a given child process.");
5569
5570static PyObject *
5571posix_wait4(PyObject *self, PyObject *args)
5572{
5573 int pid, options;
5574 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005575 WAIT_TYPE status;
5576 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005577
5578 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
5579 return NULL;
5580
5581 Py_BEGIN_ALLOW_THREADS
5582 pid = wait4(pid, &status, options, &ru);
5583 Py_END_ALLOW_THREADS
5584
Neal Norwitzd5a37542006-03-20 06:48:34 +00005585 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005586}
5587#endif /* HAVE_WAIT4 */
5588
Guido van Rossumb6775db1994-08-01 11:34:53 +00005589#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005590PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005591"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005592Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005593
Barry Warsaw53699e91996-12-10 23:23:01 +00005594static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005595posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005596{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005597 int pid, options;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005598 WAIT_TYPE status;
5599 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005600
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005601 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00005602 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005603 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00005604 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00005605 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00005606 if (pid == -1)
5607 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005608
5609 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005610}
5611
Tim Petersab034fa2002-02-01 11:27:43 +00005612#elif defined(HAVE_CWAIT)
5613
5614/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005615PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005616"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005617"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005618
5619static PyObject *
5620posix_waitpid(PyObject *self, PyObject *args)
5621{
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005622 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005623 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005624
5625 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5626 return NULL;
5627 Py_BEGIN_ALLOW_THREADS
5628 pid = _cwait(&status, pid, options);
5629 Py_END_ALLOW_THREADS
5630 if (pid == -1)
5631 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005632
5633 /* shift the status left a byte so this is more like the POSIX waitpid */
5634 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005635}
5636#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005637
Guido van Rossumad0ee831995-03-01 10:34:45 +00005638#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005639PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005640"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005641Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005642
Barry Warsaw53699e91996-12-10 23:23:01 +00005643static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005644posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005645{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005646 int pid;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005647 WAIT_TYPE status;
5648 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005649
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005650 Py_BEGIN_ALLOW_THREADS
5651 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00005652 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00005653 if (pid == -1)
5654 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005655
5656 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005657}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005658#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005659
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005661PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005662"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005663Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005664
Barry Warsaw53699e91996-12-10 23:23:01 +00005665static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005666posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005667{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005668#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005669 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005670#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005671#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00005672 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005673#else
5674 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5675#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005676#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005677}
5678
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005679
Guido van Rossumb6775db1994-08-01 11:34:53 +00005680#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005681PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005682"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005683Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005684
Barry Warsaw53699e91996-12-10 23:23:01 +00005685static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005686posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005687{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005688 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005689 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005690 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005691 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005692 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005693 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00005694 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00005695 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005696 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00005697 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00005698 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005699}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005700#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005701
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005702
Guido van Rossumb6775db1994-08-01 11:34:53 +00005703#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005704PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005705"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005706Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005707
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005708static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005709posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005710{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00005711 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005712}
5713#endif /* HAVE_SYMLINK */
5714
5715
5716#ifdef HAVE_TIMES
5717#ifndef HZ
5718#define HZ 60 /* Universal constant :-) */
5719#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005720
Guido van Rossumd48f2521997-12-05 22:19:34 +00005721#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5722static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005723system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005724{
5725 ULONG value = 0;
5726
5727 Py_BEGIN_ALLOW_THREADS
5728 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5729 Py_END_ALLOW_THREADS
5730
5731 return value;
5732}
5733
5734static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005735posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005736{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005737 /* Currently Only Uptime is Provided -- Others Later */
5738 return Py_BuildValue("ddddd",
5739 (double)0 /* t.tms_utime / HZ */,
5740 (double)0 /* t.tms_stime / HZ */,
5741 (double)0 /* t.tms_cutime / HZ */,
5742 (double)0 /* t.tms_cstime / HZ */,
5743 (double)system_uptime() / 1000);
5744}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005745#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005746static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005747posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005748{
5749 struct tms t;
5750 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005751 errno = 0;
5752 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005753 if (c == (clock_t) -1)
5754 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005755 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005756 (double)t.tms_utime / HZ,
5757 (double)t.tms_stime / HZ,
5758 (double)t.tms_cutime / HZ,
5759 (double)t.tms_cstime / HZ,
5760 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005761}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005762#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005763#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005764
5765
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005766#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005767#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005768static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005769posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005770{
5771 FILETIME create, exit, kernel, user;
5772 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005773 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005774 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5775 /* The fields of a FILETIME structure are the hi and lo part
5776 of a 64-bit value expressed in 100 nanosecond units.
5777 1e7 is one second in such units; 1e-7 the inverse.
5778 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5779 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005780 return Py_BuildValue(
5781 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005782 (double)(kernel.dwHighDateTime*429.4967296 +
5783 kernel.dwLowDateTime*1e-7),
5784 (double)(user.dwHighDateTime*429.4967296 +
5785 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005786 (double)0,
5787 (double)0,
5788 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005789}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005790#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005791
5792#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005793PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005794"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005795Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005796#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005797
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005798
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005799#ifdef HAVE_GETSID
5800PyDoc_STRVAR(posix_getsid__doc__,
5801"getsid(pid) -> sid\n\n\
5802Call the system call getsid().");
5803
5804static PyObject *
5805posix_getsid(PyObject *self, PyObject *args)
5806{
5807 int pid, sid;
5808 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5809 return NULL;
5810 sid = getsid(pid);
5811 if (sid < 0)
5812 return posix_error();
5813 return PyInt_FromLong((long)sid);
5814}
5815#endif /* HAVE_GETSID */
5816
5817
Guido van Rossumb6775db1994-08-01 11:34:53 +00005818#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005819PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005820"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005821Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005822
Barry Warsaw53699e91996-12-10 23:23:01 +00005823static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005824posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005825{
Guido van Rossum687dd131993-05-17 08:34:16 +00005826 if (setsid() < 0)
5827 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005828 Py_INCREF(Py_None);
5829 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005830}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005831#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005832
Guido van Rossumb6775db1994-08-01 11:34:53 +00005833#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005834PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005835"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005836Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005837
Barry Warsaw53699e91996-12-10 23:23:01 +00005838static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005839posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005840{
5841 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005842 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005843 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005844 if (setpgid(pid, pgrp) < 0)
5845 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005846 Py_INCREF(Py_None);
5847 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005848}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005849#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005850
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005851
Guido van Rossumb6775db1994-08-01 11:34:53 +00005852#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005853PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005854"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005855Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005856
Barry Warsaw53699e91996-12-10 23:23:01 +00005857static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005858posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005859{
5860 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005861 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005862 return NULL;
5863 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005864 if (pgid < 0)
5865 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005866 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005867}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005868#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005869
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005870
Guido van Rossumb6775db1994-08-01 11:34:53 +00005871#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005872PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005873"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005874Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005875
Barry Warsaw53699e91996-12-10 23:23:01 +00005876static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005877posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005878{
5879 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005880 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005881 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005882 if (tcsetpgrp(fd, pgid) < 0)
5883 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005884 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005885 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005886}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005887#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005888
Guido van Rossum687dd131993-05-17 08:34:16 +00005889/* Functions acting on file descriptors */
5890
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005891PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005892"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005893Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005894
Barry Warsaw53699e91996-12-10 23:23:01 +00005895static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005896posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005897{
Mark Hammondef8b6542001-05-13 08:04:26 +00005898 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005899 int flag;
5900 int mode = 0777;
5901 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005902
5903#ifdef MS_WINDOWS
5904 if (unicode_file_names()) {
5905 PyUnicodeObject *po;
5906 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5907 Py_BEGIN_ALLOW_THREADS
5908 /* PyUnicode_AS_UNICODE OK without thread
5909 lock as it is a simple dereference. */
5910 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5911 Py_END_ALLOW_THREADS
5912 if (fd < 0)
5913 return posix_error();
5914 return PyInt_FromLong((long)fd);
5915 }
5916 /* Drop the argument parsing error as narrow strings
5917 are also valid. */
5918 PyErr_Clear();
5919 }
5920#endif
5921
Tim Peters5aa91602002-01-30 05:46:57 +00005922 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00005923 Py_FileSystemDefaultEncoding, &file,
5924 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00005925 return NULL;
5926
Barry Warsaw53699e91996-12-10 23:23:01 +00005927 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005928 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005929 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005930 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00005931 return posix_error_with_allocated_filename(file);
5932 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00005933 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005934}
5935
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005936
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005937PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005938"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005939Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005940
Barry Warsaw53699e91996-12-10 23:23:01 +00005941static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005942posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005943{
5944 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005945 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005946 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005947 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005948 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005949 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005950 if (res < 0)
5951 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005952 Py_INCREF(Py_None);
5953 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005954}
5955
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005956
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005957PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005958"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005959Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005960
Barry Warsaw53699e91996-12-10 23:23:01 +00005961static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005962posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005963{
5964 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005965 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005966 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005967 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005968 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005969 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005970 if (fd < 0)
5971 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005972 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005973}
5974
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005975
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005976PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005977"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005978Duplicate file descriptor.");
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_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005982{
5983 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005984 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005985 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005986 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005987 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005988 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005989 if (res < 0)
5990 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005991 Py_INCREF(Py_None);
5992 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005993}
5994
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005995
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005996PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005997"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005998Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005999
Barry Warsaw53699e91996-12-10 23:23:01 +00006000static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006001posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006002{
6003 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006004#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006005 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006006#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00006007 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006008#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006009 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006010 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00006011 return NULL;
6012#ifdef SEEK_SET
6013 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6014 switch (how) {
6015 case 0: how = SEEK_SET; break;
6016 case 1: how = SEEK_CUR; break;
6017 case 2: how = SEEK_END; break;
6018 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006019#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006020
6021#if !defined(HAVE_LARGEFILE_SUPPORT)
6022 pos = PyInt_AsLong(posobj);
6023#else
6024 pos = PyLong_Check(posobj) ?
6025 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
6026#endif
6027 if (PyErr_Occurred())
6028 return NULL;
6029
Barry Warsaw53699e91996-12-10 23:23:01 +00006030 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006031#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00006032 res = _lseeki64(fd, pos, how);
6033#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006034 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006035#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006036 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006037 if (res < 0)
6038 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006039
6040#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00006041 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006042#else
6043 return PyLong_FromLongLong(res);
6044#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006045}
6046
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006047
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006048PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006049"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006050Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006051
Barry Warsaw53699e91996-12-10 23:23:01 +00006052static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006053posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006054{
Guido van Rossum8bac5461996-06-11 18:38:48 +00006055 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00006056 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006057 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006058 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00006059 if (size < 0) {
6060 errno = EINVAL;
6061 return posix_error();
6062 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006063 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006064 if (buffer == NULL)
6065 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006066 Py_BEGIN_ALLOW_THREADS
6067 n = read(fd, PyString_AsString(buffer), size);
6068 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00006069 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006070 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00006071 return posix_error();
6072 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00006073 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00006074 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00006075 return buffer;
6076}
6077
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006078
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006079PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006080"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006081Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006082
Barry Warsaw53699e91996-12-10 23:23:01 +00006083static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006084posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006085{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006086 int fd;
6087 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00006088 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006089
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006090 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006091 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006092 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006093 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00006094 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006095 if (size < 0)
6096 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006097 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006098}
6099
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006101PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006102"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006103Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006104
Barry Warsaw53699e91996-12-10 23:23:01 +00006105static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006106posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006107{
6108 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00006109 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00006110 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006111 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006112 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006113#ifdef __VMS
6114 /* on OpenVMS we must ensure that all bytes are written to the file */
6115 fsync(fd);
6116#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006117 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00006118 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00006119 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00006120 if (res != 0) {
6121#ifdef MS_WINDOWS
6122 return win32_error("fstat", NULL);
6123#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006124 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006125#endif
6126 }
Tim Peters5aa91602002-01-30 05:46:57 +00006127
Martin v. Löwis14694662006-02-03 12:54:16 +00006128 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006129}
6130
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006131
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006132PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006133"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006134Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006135
Barry Warsaw53699e91996-12-10 23:23:01 +00006136static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006137posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006138{
Guido van Rossum687dd131993-05-17 08:34:16 +00006139 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006140 char *mode = "r";
6141 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00006142 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00006143 PyObject *f;
6144 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00006145 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006146
Thomas Heller1f043e22002-11-07 16:00:59 +00006147 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
6148 PyErr_Format(PyExc_ValueError,
6149 "invalid file mode '%s'", mode);
6150 return NULL;
6151 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006152 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006153#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Georg Brandl54a188a2006-03-31 20:00:11 +00006154 if (mode[0] == 'a') {
6155 /* try to make sure the O_APPEND flag is set */
6156 int flags;
6157 flags = fcntl(fd, F_GETFL);
6158 if (flags != -1)
6159 fcntl(fd, F_SETFL, flags | O_APPEND);
6160 fp = fdopen(fd, mode);
Thomas Wouters2a9a6b02006-03-31 22:38:19 +00006161 if (fp == NULL && flags != -1)
Georg Brandl54a188a2006-03-31 20:00:11 +00006162 /* restore old mode if fdopen failed */
6163 fcntl(fd, F_SETFL, flags);
6164 } else {
6165 fp = fdopen(fd, mode);
6166 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006167#else
6168 fp = fdopen(fd, mode);
6169#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006170 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006171 if (fp == NULL)
6172 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00006173 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006174 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00006175 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006176 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006177}
6178
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006179PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006180"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006181Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006182connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006183
6184static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006185posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006186{
6187 int fd;
6188 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6189 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006190 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006191}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006192
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006193#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006194PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006195"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006196Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006197
Barry Warsaw53699e91996-12-10 23:23:01 +00006198static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006199posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006200{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006201#if defined(PYOS_OS2)
6202 HFILE read, write;
6203 APIRET rc;
6204
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006205 Py_BEGIN_ALLOW_THREADS
6206 rc = DosCreatePipe( &read, &write, 4096);
6207 Py_END_ALLOW_THREADS
6208 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006209 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006210
6211 return Py_BuildValue("(ii)", read, write);
6212#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006213#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00006214 int fds[2];
6215 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00006216 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006217 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00006218 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006219 if (res != 0)
6220 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006221 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006222#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00006223 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006224 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00006225 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00006226 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006227 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00006228 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00006229 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006230 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00006231 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6232 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006233 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006234#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006235#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006236}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006237#endif /* HAVE_PIPE */
6238
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006239
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006240#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006241PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006242"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006243Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006244
Barry Warsaw53699e91996-12-10 23:23:01 +00006245static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006246posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006247{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006248 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006249 int mode = 0666;
6250 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006251 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006252 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006253 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006254 res = mkfifo(filename, mode);
6255 Py_END_ALLOW_THREADS
6256 if (res < 0)
6257 return posix_error();
6258 Py_INCREF(Py_None);
6259 return Py_None;
6260}
6261#endif
6262
6263
Neal Norwitz11690112002-07-30 01:08:28 +00006264#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006265PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006266"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006267Create a filesystem node (file, device special file or named pipe)\n\
6268named filename. mode specifies both the permissions to use and the\n\
6269type of node to be created, being combined (bitwise OR) with one of\n\
6270S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006271device defines the newly created device special file (probably using\n\
6272os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006273
6274
6275static PyObject *
6276posix_mknod(PyObject *self, PyObject *args)
6277{
6278 char *filename;
6279 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006280 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006281 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00006282 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006283 return NULL;
6284 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006285 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00006286 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +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 Rossuma4916fa1996-05-23 22:58:55 +00006291}
6292#endif
6293
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006294#ifdef HAVE_DEVICE_MACROS
6295PyDoc_STRVAR(posix_major__doc__,
6296"major(device) -> major number\n\
6297Extracts a device major number from a raw device number.");
6298
6299static PyObject *
6300posix_major(PyObject *self, PyObject *args)
6301{
6302 int device;
6303 if (!PyArg_ParseTuple(args, "i:major", &device))
6304 return NULL;
6305 return PyInt_FromLong((long)major(device));
6306}
6307
6308PyDoc_STRVAR(posix_minor__doc__,
6309"minor(device) -> minor number\n\
6310Extracts a device minor number from a raw device number.");
6311
6312static PyObject *
6313posix_minor(PyObject *self, PyObject *args)
6314{
6315 int device;
6316 if (!PyArg_ParseTuple(args, "i:minor", &device))
6317 return NULL;
6318 return PyInt_FromLong((long)minor(device));
6319}
6320
6321PyDoc_STRVAR(posix_makedev__doc__,
6322"makedev(major, minor) -> device number\n\
6323Composes a raw device number from the major and minor device numbers.");
6324
6325static PyObject *
6326posix_makedev(PyObject *self, PyObject *args)
6327{
6328 int major, minor;
6329 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6330 return NULL;
6331 return PyInt_FromLong((long)makedev(major, minor));
6332}
6333#endif /* device macros */
6334
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006335
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006336#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006337PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006338"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006339Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006340
Barry Warsaw53699e91996-12-10 23:23:01 +00006341static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006342posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006343{
6344 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006345 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006346 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006347 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006348
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006349 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006350 return NULL;
6351
6352#if !defined(HAVE_LARGEFILE_SUPPORT)
6353 length = PyInt_AsLong(lenobj);
6354#else
6355 length = PyLong_Check(lenobj) ?
6356 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6357#endif
6358 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006359 return NULL;
6360
Barry Warsaw53699e91996-12-10 23:23:01 +00006361 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006362 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00006363 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006364 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006365 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006366 return NULL;
6367 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006368 Py_INCREF(Py_None);
6369 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006370}
6371#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006372
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006373#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006374PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006375"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006376Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006377
Fred Drake762e2061999-08-26 17:23:54 +00006378/* Save putenv() parameters as values here, so we can collect them when they
6379 * get re-set with another call for the same key. */
6380static PyObject *posix_putenv_garbage;
6381
Tim Peters5aa91602002-01-30 05:46:57 +00006382static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006383posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006384{
6385 char *s1, *s2;
Anthony Baxter64182fe2006-04-11 12:14:09 +00006386 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00006387 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00006388 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006389
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006390 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006391 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006392
6393#if defined(PYOS_OS2)
6394 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6395 APIRET rc;
6396
Guido van Rossumd48f2521997-12-05 22:19:34 +00006397 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6398 if (rc != NO_ERROR)
6399 return os2_error(rc);
6400
6401 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6402 APIRET rc;
6403
Guido van Rossumd48f2521997-12-05 22:19:34 +00006404 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6405 if (rc != NO_ERROR)
6406 return os2_error(rc);
6407 } else {
6408#endif
6409
Fred Drake762e2061999-08-26 17:23:54 +00006410 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00006411 len = strlen(s1) + strlen(s2) + 2;
6412 /* len includes space for a trailing \0; the size arg to
6413 PyString_FromStringAndSize does not count that */
6414 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00006415 if (newstr == NULL)
6416 return PyErr_NoMemory();
Anthony Baxter64182fe2006-04-11 12:14:09 +00006417 newenv = PyString_AS_STRING(newstr);
6418 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6419 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00006420 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006421 posix_error();
6422 return NULL;
6423 }
Fred Drake762e2061999-08-26 17:23:54 +00006424 /* Install the first arg and newstr in posix_putenv_garbage;
6425 * this will cause previous value to be collected. This has to
6426 * happen after the real putenv() call because the old value
6427 * was still accessible until then. */
6428 if (PyDict_SetItem(posix_putenv_garbage,
6429 PyTuple_GET_ITEM(args, 0), newstr)) {
6430 /* really not much we can do; just leak */
6431 PyErr_Clear();
6432 }
6433 else {
6434 Py_DECREF(newstr);
6435 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006436
6437#if defined(PYOS_OS2)
6438 }
6439#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006440 Py_INCREF(Py_None);
6441 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006442}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006443#endif /* putenv */
6444
Guido van Rossumc524d952001-10-19 01:31:59 +00006445#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006446PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006447"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006448Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006449
6450static PyObject *
6451posix_unsetenv(PyObject *self, PyObject *args)
6452{
6453 char *s1;
6454
6455 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6456 return NULL;
6457
6458 unsetenv(s1);
6459
6460 /* Remove the key from posix_putenv_garbage;
6461 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00006462 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00006463 * old value was still accessible until then.
6464 */
6465 if (PyDict_DelItem(posix_putenv_garbage,
6466 PyTuple_GET_ITEM(args, 0))) {
6467 /* really not much we can do; just leak */
6468 PyErr_Clear();
6469 }
6470
6471 Py_INCREF(Py_None);
6472 return Py_None;
6473}
6474#endif /* unsetenv */
6475
Guido van Rossumb6a47161997-09-15 22:54:34 +00006476#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006477PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006478"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006479Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006480
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006481static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006482posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006483{
6484 int code;
6485 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006486 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00006487 return NULL;
6488 message = strerror(code);
6489 if (message == NULL) {
6490 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00006491 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006492 return NULL;
6493 }
6494 return PyString_FromString(message);
6495}
6496#endif /* strerror */
6497
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006498
Guido van Rossumc9641791998-08-04 15:26:23 +00006499#ifdef HAVE_SYS_WAIT_H
6500
Fred Drake106c1a02002-04-23 15:58:02 +00006501#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006502PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006503"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006504Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006505
6506static PyObject *
6507posix_WCOREDUMP(PyObject *self, PyObject *args)
6508{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006509 WAIT_TYPE status;
6510 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006511
Neal Norwitzd5a37542006-03-20 06:48:34 +00006512 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006513 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006514
6515 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006516}
6517#endif /* WCOREDUMP */
6518
6519#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006520PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006521"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006522Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006523job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006524
6525static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006526posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006527{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006528 WAIT_TYPE status;
6529 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006530
Neal Norwitzd5a37542006-03-20 06:48:34 +00006531 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006532 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006533
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006534 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006535}
6536#endif /* WIFCONTINUED */
6537
Guido van Rossumc9641791998-08-04 15:26:23 +00006538#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006539PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006540"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006541Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006542
6543static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006544posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006545{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006546 WAIT_TYPE status;
6547 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006548
Neal Norwitzd5a37542006-03-20 06:48:34 +00006549 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006550 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006551
Fred Drake106c1a02002-04-23 15:58:02 +00006552 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006553}
6554#endif /* WIFSTOPPED */
6555
6556#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006557PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006558"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006559Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006560
6561static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006562posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006563{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006564 WAIT_TYPE status;
6565 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006566
Neal Norwitzd5a37542006-03-20 06:48:34 +00006567 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006568 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006569
Fred Drake106c1a02002-04-23 15:58:02 +00006570 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006571}
6572#endif /* WIFSIGNALED */
6573
6574#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006575PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006576"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006577Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006578system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006579
6580static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006581posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006582{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006583 WAIT_TYPE status;
6584 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006585
Neal Norwitzd5a37542006-03-20 06:48:34 +00006586 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006587 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006588
Fred Drake106c1a02002-04-23 15:58:02 +00006589 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006590}
6591#endif /* WIFEXITED */
6592
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006593#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006594PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006595"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006596Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006597
6598static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006599posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006600{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006601 WAIT_TYPE status;
6602 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006603
Neal Norwitzd5a37542006-03-20 06:48:34 +00006604 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006605 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006606
Guido van Rossumc9641791998-08-04 15:26:23 +00006607 return Py_BuildValue("i", WEXITSTATUS(status));
6608}
6609#endif /* WEXITSTATUS */
6610
6611#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006612PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006613"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006614Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006615value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006616
6617static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006618posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006619{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006620 WAIT_TYPE status;
6621 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006622
Neal Norwitzd5a37542006-03-20 06:48:34 +00006623 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006624 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006625
Guido van Rossumc9641791998-08-04 15:26:23 +00006626 return Py_BuildValue("i", WTERMSIG(status));
6627}
6628#endif /* WTERMSIG */
6629
6630#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006631PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006632"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006633Return the signal that stopped the process that provided\n\
6634the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006635
6636static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006637posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006638{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006639 WAIT_TYPE status;
6640 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006641
Neal Norwitzd5a37542006-03-20 06:48:34 +00006642 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006643 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006644
Guido van Rossumc9641791998-08-04 15:26:23 +00006645 return Py_BuildValue("i", WSTOPSIG(status));
6646}
6647#endif /* WSTOPSIG */
6648
6649#endif /* HAVE_SYS_WAIT_H */
6650
6651
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006652#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006653#ifdef _SCO_DS
6654/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6655 needed definitions in sys/statvfs.h */
6656#define _SVID3
6657#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006658#include <sys/statvfs.h>
6659
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006660static PyObject*
6661_pystatvfs_fromstructstatvfs(struct statvfs st) {
6662 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6663 if (v == NULL)
6664 return NULL;
6665
6666#if !defined(HAVE_LARGEFILE_SUPPORT)
6667 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6668 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6669 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6670 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6671 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6672 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6673 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6674 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6675 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6676 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6677#else
6678 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6679 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006680 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006681 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006682 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006683 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006684 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006685 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006686 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006687 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006688 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006689 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006690 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006691 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006692 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6693 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6694#endif
6695
6696 return v;
6697}
6698
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006699PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006700"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006701Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006702
6703static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006704posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006705{
6706 int fd, res;
6707 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006708
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006709 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006710 return NULL;
6711 Py_BEGIN_ALLOW_THREADS
6712 res = fstatvfs(fd, &st);
6713 Py_END_ALLOW_THREADS
6714 if (res != 0)
6715 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006716
6717 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006718}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006719#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006720
6721
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006722#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006723#include <sys/statvfs.h>
6724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006725PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006726"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006727Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006728
6729static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006730posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006731{
6732 char *path;
6733 int res;
6734 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006735 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006736 return NULL;
6737 Py_BEGIN_ALLOW_THREADS
6738 res = statvfs(path, &st);
6739 Py_END_ALLOW_THREADS
6740 if (res != 0)
6741 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006742
6743 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006744}
6745#endif /* HAVE_STATVFS */
6746
6747
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006748#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006749PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006750"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006751Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006752The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006753or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006754
6755static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006756posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006757{
6758 PyObject *result = NULL;
6759 char *dir = NULL;
6760 char *pfx = NULL;
6761 char *name;
6762
6763 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6764 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006765
6766 if (PyErr_Warn(PyExc_RuntimeWarning,
6767 "tempnam is a potential security risk to your program") < 0)
6768 return NULL;
6769
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006770#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006771 name = _tempnam(dir, pfx);
6772#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006773 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006774#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006775 if (name == NULL)
6776 return PyErr_NoMemory();
6777 result = PyString_FromString(name);
6778 free(name);
6779 return result;
6780}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006781#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006782
6783
6784#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006785PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006786"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006787Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006788
6789static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006790posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006791{
6792 FILE *fp;
6793
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006794 fp = tmpfile();
6795 if (fp == NULL)
6796 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006797 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006798}
6799#endif
6800
6801
6802#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006803PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006804"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006805Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006806
6807static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006808posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006809{
6810 char buffer[L_tmpnam];
6811 char *name;
6812
Skip Montanaro95618b52001-08-18 18:52:10 +00006813 if (PyErr_Warn(PyExc_RuntimeWarning,
6814 "tmpnam is a potential security risk to your program") < 0)
6815 return NULL;
6816
Greg Wardb48bc172000-03-01 21:51:56 +00006817#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006818 name = tmpnam_r(buffer);
6819#else
6820 name = tmpnam(buffer);
6821#endif
6822 if (name == NULL) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006823 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006824#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006825 "unexpected NULL from tmpnam_r"
6826#else
6827 "unexpected NULL from tmpnam"
6828#endif
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006829 );
6830 PyErr_SetObject(PyExc_OSError, err);
6831 Py_XDECREF(err);
6832 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006833 }
6834 return PyString_FromString(buffer);
6835}
6836#endif
6837
6838
Fred Drakec9680921999-12-13 16:37:25 +00006839/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6840 * It maps strings representing configuration variable names to
6841 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006842 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006843 * rarely-used constants. There are three separate tables that use
6844 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006845 *
6846 * This code is always included, even if none of the interfaces that
6847 * need it are included. The #if hackery needed to avoid it would be
6848 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006849 */
6850struct constdef {
6851 char *name;
6852 long value;
6853};
6854
Fred Drake12c6e2d1999-12-14 21:25:03 +00006855static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006856conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6857 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006858{
6859 if (PyInt_Check(arg)) {
6860 *valuep = PyInt_AS_LONG(arg);
6861 return 1;
6862 }
6863 if (PyString_Check(arg)) {
6864 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006865 size_t lo = 0;
6866 size_t mid;
6867 size_t hi = tablesize;
6868 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006869 char *confname = PyString_AS_STRING(arg);
6870 while (lo < hi) {
6871 mid = (lo + hi) / 2;
6872 cmp = strcmp(confname, table[mid].name);
6873 if (cmp < 0)
6874 hi = mid;
6875 else if (cmp > 0)
6876 lo = mid + 1;
6877 else {
6878 *valuep = table[mid].value;
6879 return 1;
6880 }
6881 }
6882 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6883 }
6884 else
6885 PyErr_SetString(PyExc_TypeError,
6886 "configuration names must be strings or integers");
6887 return 0;
6888}
6889
6890
6891#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6892static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006893#ifdef _PC_ABI_AIO_XFER_MAX
6894 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6895#endif
6896#ifdef _PC_ABI_ASYNC_IO
6897 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6898#endif
Fred Drakec9680921999-12-13 16:37:25 +00006899#ifdef _PC_ASYNC_IO
6900 {"PC_ASYNC_IO", _PC_ASYNC_IO},
6901#endif
6902#ifdef _PC_CHOWN_RESTRICTED
6903 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
6904#endif
6905#ifdef _PC_FILESIZEBITS
6906 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
6907#endif
6908#ifdef _PC_LAST
6909 {"PC_LAST", _PC_LAST},
6910#endif
6911#ifdef _PC_LINK_MAX
6912 {"PC_LINK_MAX", _PC_LINK_MAX},
6913#endif
6914#ifdef _PC_MAX_CANON
6915 {"PC_MAX_CANON", _PC_MAX_CANON},
6916#endif
6917#ifdef _PC_MAX_INPUT
6918 {"PC_MAX_INPUT", _PC_MAX_INPUT},
6919#endif
6920#ifdef _PC_NAME_MAX
6921 {"PC_NAME_MAX", _PC_NAME_MAX},
6922#endif
6923#ifdef _PC_NO_TRUNC
6924 {"PC_NO_TRUNC", _PC_NO_TRUNC},
6925#endif
6926#ifdef _PC_PATH_MAX
6927 {"PC_PATH_MAX", _PC_PATH_MAX},
6928#endif
6929#ifdef _PC_PIPE_BUF
6930 {"PC_PIPE_BUF", _PC_PIPE_BUF},
6931#endif
6932#ifdef _PC_PRIO_IO
6933 {"PC_PRIO_IO", _PC_PRIO_IO},
6934#endif
6935#ifdef _PC_SOCK_MAXBUF
6936 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
6937#endif
6938#ifdef _PC_SYNC_IO
6939 {"PC_SYNC_IO", _PC_SYNC_IO},
6940#endif
6941#ifdef _PC_VDISABLE
6942 {"PC_VDISABLE", _PC_VDISABLE},
6943#endif
6944};
6945
Fred Drakec9680921999-12-13 16:37:25 +00006946static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006947conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006948{
6949 return conv_confname(arg, valuep, posix_constants_pathconf,
6950 sizeof(posix_constants_pathconf)
6951 / sizeof(struct constdef));
6952}
6953#endif
6954
6955#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006956PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006957"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006958Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006959If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006960
6961static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006962posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006963{
6964 PyObject *result = NULL;
6965 int name, fd;
6966
Fred Drake12c6e2d1999-12-14 21:25:03 +00006967 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6968 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00006969 long limit;
6970
6971 errno = 0;
6972 limit = fpathconf(fd, name);
6973 if (limit == -1 && errno != 0)
6974 posix_error();
6975 else
6976 result = PyInt_FromLong(limit);
6977 }
6978 return result;
6979}
6980#endif
6981
6982
6983#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006984PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006985"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006986Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006987If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006988
6989static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006990posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006991{
6992 PyObject *result = NULL;
6993 int name;
6994 char *path;
6995
6996 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6997 conv_path_confname, &name)) {
6998 long limit;
6999
7000 errno = 0;
7001 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007002 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00007003 if (errno == EINVAL)
7004 /* could be a path or name problem */
7005 posix_error();
7006 else
7007 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007008 }
Fred Drakec9680921999-12-13 16:37:25 +00007009 else
7010 result = PyInt_FromLong(limit);
7011 }
7012 return result;
7013}
7014#endif
7015
7016#ifdef HAVE_CONFSTR
7017static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007018#ifdef _CS_ARCHITECTURE
7019 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
7020#endif
7021#ifdef _CS_HOSTNAME
7022 {"CS_HOSTNAME", _CS_HOSTNAME},
7023#endif
7024#ifdef _CS_HW_PROVIDER
7025 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
7026#endif
7027#ifdef _CS_HW_SERIAL
7028 {"CS_HW_SERIAL", _CS_HW_SERIAL},
7029#endif
7030#ifdef _CS_INITTAB_NAME
7031 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
7032#endif
Fred Drakec9680921999-12-13 16:37:25 +00007033#ifdef _CS_LFS64_CFLAGS
7034 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
7035#endif
7036#ifdef _CS_LFS64_LDFLAGS
7037 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
7038#endif
7039#ifdef _CS_LFS64_LIBS
7040 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
7041#endif
7042#ifdef _CS_LFS64_LINTFLAGS
7043 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
7044#endif
7045#ifdef _CS_LFS_CFLAGS
7046 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
7047#endif
7048#ifdef _CS_LFS_LDFLAGS
7049 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
7050#endif
7051#ifdef _CS_LFS_LIBS
7052 {"CS_LFS_LIBS", _CS_LFS_LIBS},
7053#endif
7054#ifdef _CS_LFS_LINTFLAGS
7055 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
7056#endif
Fred Draked86ed291999-12-15 15:34:33 +00007057#ifdef _CS_MACHINE
7058 {"CS_MACHINE", _CS_MACHINE},
7059#endif
Fred Drakec9680921999-12-13 16:37:25 +00007060#ifdef _CS_PATH
7061 {"CS_PATH", _CS_PATH},
7062#endif
Fred Draked86ed291999-12-15 15:34:33 +00007063#ifdef _CS_RELEASE
7064 {"CS_RELEASE", _CS_RELEASE},
7065#endif
7066#ifdef _CS_SRPC_DOMAIN
7067 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
7068#endif
7069#ifdef _CS_SYSNAME
7070 {"CS_SYSNAME", _CS_SYSNAME},
7071#endif
7072#ifdef _CS_VERSION
7073 {"CS_VERSION", _CS_VERSION},
7074#endif
Fred Drakec9680921999-12-13 16:37:25 +00007075#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7076 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
7077#endif
7078#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7079 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
7080#endif
7081#ifdef _CS_XBS5_ILP32_OFF32_LIBS
7082 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
7083#endif
7084#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7085 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
7086#endif
7087#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7088 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
7089#endif
7090#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7091 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
7092#endif
7093#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7094 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
7095#endif
7096#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7097 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
7098#endif
7099#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7100 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
7101#endif
7102#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7103 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
7104#endif
7105#ifdef _CS_XBS5_LP64_OFF64_LIBS
7106 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
7107#endif
7108#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7109 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
7110#endif
7111#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7112 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
7113#endif
7114#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7115 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
7116#endif
7117#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7118 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
7119#endif
7120#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7121 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
7122#endif
Fred Draked86ed291999-12-15 15:34:33 +00007123#ifdef _MIPS_CS_AVAIL_PROCESSORS
7124 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
7125#endif
7126#ifdef _MIPS_CS_BASE
7127 {"MIPS_CS_BASE", _MIPS_CS_BASE},
7128#endif
7129#ifdef _MIPS_CS_HOSTID
7130 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
7131#endif
7132#ifdef _MIPS_CS_HW_NAME
7133 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7134#endif
7135#ifdef _MIPS_CS_NUM_PROCESSORS
7136 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7137#endif
7138#ifdef _MIPS_CS_OSREL_MAJ
7139 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7140#endif
7141#ifdef _MIPS_CS_OSREL_MIN
7142 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7143#endif
7144#ifdef _MIPS_CS_OSREL_PATCH
7145 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7146#endif
7147#ifdef _MIPS_CS_OS_NAME
7148 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7149#endif
7150#ifdef _MIPS_CS_OS_PROVIDER
7151 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7152#endif
7153#ifdef _MIPS_CS_PROCESSORS
7154 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7155#endif
7156#ifdef _MIPS_CS_SERIAL
7157 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7158#endif
7159#ifdef _MIPS_CS_VENDOR
7160 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7161#endif
Fred Drakec9680921999-12-13 16:37:25 +00007162};
7163
7164static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007165conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007166{
7167 return conv_confname(arg, valuep, posix_constants_confstr,
7168 sizeof(posix_constants_confstr)
7169 / sizeof(struct constdef));
7170}
7171
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007172PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007173"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007174Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007175
7176static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007177posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007178{
7179 PyObject *result = NULL;
7180 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007181 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007182
7183 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Skip Montanarodd527fc2006-04-18 00:49:49 +00007184 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007185
Fred Drakec9680921999-12-13 16:37:25 +00007186 errno = 0;
Skip Montanarodd527fc2006-04-18 00:49:49 +00007187 len = confstr(name, buffer, sizeof(buffer));
Skip Montanaro94785ef2006-04-20 01:29:48 +00007188 if (len == 0) {
7189 if (errno) {
7190 posix_error();
7191 }
7192 else {
7193 result = Py_None;
7194 Py_INCREF(Py_None);
7195 }
Fred Drakec9680921999-12-13 16:37:25 +00007196 }
7197 else {
Neal Norwitz0d21b1e2006-04-20 06:44:42 +00007198 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz449b24e2006-04-20 06:56:05 +00007199 result = PyString_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007200 if (result != NULL)
Neal Norwitz449b24e2006-04-20 06:56:05 +00007201 confstr(name, PyString_AS_STRING(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00007202 }
7203 else
Neal Norwitz449b24e2006-04-20 06:56:05 +00007204 result = PyString_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007205 }
7206 }
7207 return result;
7208}
7209#endif
7210
7211
7212#ifdef HAVE_SYSCONF
7213static struct constdef posix_constants_sysconf[] = {
7214#ifdef _SC_2_CHAR_TERM
7215 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7216#endif
7217#ifdef _SC_2_C_BIND
7218 {"SC_2_C_BIND", _SC_2_C_BIND},
7219#endif
7220#ifdef _SC_2_C_DEV
7221 {"SC_2_C_DEV", _SC_2_C_DEV},
7222#endif
7223#ifdef _SC_2_C_VERSION
7224 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7225#endif
7226#ifdef _SC_2_FORT_DEV
7227 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7228#endif
7229#ifdef _SC_2_FORT_RUN
7230 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7231#endif
7232#ifdef _SC_2_LOCALEDEF
7233 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7234#endif
7235#ifdef _SC_2_SW_DEV
7236 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7237#endif
7238#ifdef _SC_2_UPE
7239 {"SC_2_UPE", _SC_2_UPE},
7240#endif
7241#ifdef _SC_2_VERSION
7242 {"SC_2_VERSION", _SC_2_VERSION},
7243#endif
Fred Draked86ed291999-12-15 15:34:33 +00007244#ifdef _SC_ABI_ASYNCHRONOUS_IO
7245 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7246#endif
7247#ifdef _SC_ACL
7248 {"SC_ACL", _SC_ACL},
7249#endif
Fred Drakec9680921999-12-13 16:37:25 +00007250#ifdef _SC_AIO_LISTIO_MAX
7251 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7252#endif
Fred Drakec9680921999-12-13 16:37:25 +00007253#ifdef _SC_AIO_MAX
7254 {"SC_AIO_MAX", _SC_AIO_MAX},
7255#endif
7256#ifdef _SC_AIO_PRIO_DELTA_MAX
7257 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7258#endif
7259#ifdef _SC_ARG_MAX
7260 {"SC_ARG_MAX", _SC_ARG_MAX},
7261#endif
7262#ifdef _SC_ASYNCHRONOUS_IO
7263 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7264#endif
7265#ifdef _SC_ATEXIT_MAX
7266 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7267#endif
Fred Draked86ed291999-12-15 15:34:33 +00007268#ifdef _SC_AUDIT
7269 {"SC_AUDIT", _SC_AUDIT},
7270#endif
Fred Drakec9680921999-12-13 16:37:25 +00007271#ifdef _SC_AVPHYS_PAGES
7272 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7273#endif
7274#ifdef _SC_BC_BASE_MAX
7275 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7276#endif
7277#ifdef _SC_BC_DIM_MAX
7278 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7279#endif
7280#ifdef _SC_BC_SCALE_MAX
7281 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7282#endif
7283#ifdef _SC_BC_STRING_MAX
7284 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7285#endif
Fred Draked86ed291999-12-15 15:34:33 +00007286#ifdef _SC_CAP
7287 {"SC_CAP", _SC_CAP},
7288#endif
Fred Drakec9680921999-12-13 16:37:25 +00007289#ifdef _SC_CHARCLASS_NAME_MAX
7290 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7291#endif
7292#ifdef _SC_CHAR_BIT
7293 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7294#endif
7295#ifdef _SC_CHAR_MAX
7296 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7297#endif
7298#ifdef _SC_CHAR_MIN
7299 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7300#endif
7301#ifdef _SC_CHILD_MAX
7302 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7303#endif
7304#ifdef _SC_CLK_TCK
7305 {"SC_CLK_TCK", _SC_CLK_TCK},
7306#endif
7307#ifdef _SC_COHER_BLKSZ
7308 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7309#endif
7310#ifdef _SC_COLL_WEIGHTS_MAX
7311 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7312#endif
7313#ifdef _SC_DCACHE_ASSOC
7314 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7315#endif
7316#ifdef _SC_DCACHE_BLKSZ
7317 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7318#endif
7319#ifdef _SC_DCACHE_LINESZ
7320 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7321#endif
7322#ifdef _SC_DCACHE_SZ
7323 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7324#endif
7325#ifdef _SC_DCACHE_TBLKSZ
7326 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7327#endif
7328#ifdef _SC_DELAYTIMER_MAX
7329 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7330#endif
7331#ifdef _SC_EQUIV_CLASS_MAX
7332 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7333#endif
7334#ifdef _SC_EXPR_NEST_MAX
7335 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7336#endif
7337#ifdef _SC_FSYNC
7338 {"SC_FSYNC", _SC_FSYNC},
7339#endif
7340#ifdef _SC_GETGR_R_SIZE_MAX
7341 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7342#endif
7343#ifdef _SC_GETPW_R_SIZE_MAX
7344 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7345#endif
7346#ifdef _SC_ICACHE_ASSOC
7347 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7348#endif
7349#ifdef _SC_ICACHE_BLKSZ
7350 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7351#endif
7352#ifdef _SC_ICACHE_LINESZ
7353 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7354#endif
7355#ifdef _SC_ICACHE_SZ
7356 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7357#endif
Fred Draked86ed291999-12-15 15:34:33 +00007358#ifdef _SC_INF
7359 {"SC_INF", _SC_INF},
7360#endif
Fred Drakec9680921999-12-13 16:37:25 +00007361#ifdef _SC_INT_MAX
7362 {"SC_INT_MAX", _SC_INT_MAX},
7363#endif
7364#ifdef _SC_INT_MIN
7365 {"SC_INT_MIN", _SC_INT_MIN},
7366#endif
7367#ifdef _SC_IOV_MAX
7368 {"SC_IOV_MAX", _SC_IOV_MAX},
7369#endif
Fred Draked86ed291999-12-15 15:34:33 +00007370#ifdef _SC_IP_SECOPTS
7371 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7372#endif
Fred Drakec9680921999-12-13 16:37:25 +00007373#ifdef _SC_JOB_CONTROL
7374 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7375#endif
Fred Draked86ed291999-12-15 15:34:33 +00007376#ifdef _SC_KERN_POINTERS
7377 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7378#endif
7379#ifdef _SC_KERN_SIM
7380 {"SC_KERN_SIM", _SC_KERN_SIM},
7381#endif
Fred Drakec9680921999-12-13 16:37:25 +00007382#ifdef _SC_LINE_MAX
7383 {"SC_LINE_MAX", _SC_LINE_MAX},
7384#endif
7385#ifdef _SC_LOGIN_NAME_MAX
7386 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7387#endif
7388#ifdef _SC_LOGNAME_MAX
7389 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7390#endif
7391#ifdef _SC_LONG_BIT
7392 {"SC_LONG_BIT", _SC_LONG_BIT},
7393#endif
Fred Draked86ed291999-12-15 15:34:33 +00007394#ifdef _SC_MAC
7395 {"SC_MAC", _SC_MAC},
7396#endif
Fred Drakec9680921999-12-13 16:37:25 +00007397#ifdef _SC_MAPPED_FILES
7398 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7399#endif
7400#ifdef _SC_MAXPID
7401 {"SC_MAXPID", _SC_MAXPID},
7402#endif
7403#ifdef _SC_MB_LEN_MAX
7404 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7405#endif
7406#ifdef _SC_MEMLOCK
7407 {"SC_MEMLOCK", _SC_MEMLOCK},
7408#endif
7409#ifdef _SC_MEMLOCK_RANGE
7410 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7411#endif
7412#ifdef _SC_MEMORY_PROTECTION
7413 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7414#endif
7415#ifdef _SC_MESSAGE_PASSING
7416 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7417#endif
Fred Draked86ed291999-12-15 15:34:33 +00007418#ifdef _SC_MMAP_FIXED_ALIGNMENT
7419 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7420#endif
Fred Drakec9680921999-12-13 16:37:25 +00007421#ifdef _SC_MQ_OPEN_MAX
7422 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7423#endif
7424#ifdef _SC_MQ_PRIO_MAX
7425 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7426#endif
Fred Draked86ed291999-12-15 15:34:33 +00007427#ifdef _SC_NACLS_MAX
7428 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7429#endif
Fred Drakec9680921999-12-13 16:37:25 +00007430#ifdef _SC_NGROUPS_MAX
7431 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7432#endif
7433#ifdef _SC_NL_ARGMAX
7434 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7435#endif
7436#ifdef _SC_NL_LANGMAX
7437 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7438#endif
7439#ifdef _SC_NL_MSGMAX
7440 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7441#endif
7442#ifdef _SC_NL_NMAX
7443 {"SC_NL_NMAX", _SC_NL_NMAX},
7444#endif
7445#ifdef _SC_NL_SETMAX
7446 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7447#endif
7448#ifdef _SC_NL_TEXTMAX
7449 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7450#endif
7451#ifdef _SC_NPROCESSORS_CONF
7452 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7453#endif
7454#ifdef _SC_NPROCESSORS_ONLN
7455 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7456#endif
Fred Draked86ed291999-12-15 15:34:33 +00007457#ifdef _SC_NPROC_CONF
7458 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7459#endif
7460#ifdef _SC_NPROC_ONLN
7461 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7462#endif
Fred Drakec9680921999-12-13 16:37:25 +00007463#ifdef _SC_NZERO
7464 {"SC_NZERO", _SC_NZERO},
7465#endif
7466#ifdef _SC_OPEN_MAX
7467 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7468#endif
7469#ifdef _SC_PAGESIZE
7470 {"SC_PAGESIZE", _SC_PAGESIZE},
7471#endif
7472#ifdef _SC_PAGE_SIZE
7473 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7474#endif
7475#ifdef _SC_PASS_MAX
7476 {"SC_PASS_MAX", _SC_PASS_MAX},
7477#endif
7478#ifdef _SC_PHYS_PAGES
7479 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7480#endif
7481#ifdef _SC_PII
7482 {"SC_PII", _SC_PII},
7483#endif
7484#ifdef _SC_PII_INTERNET
7485 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7486#endif
7487#ifdef _SC_PII_INTERNET_DGRAM
7488 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7489#endif
7490#ifdef _SC_PII_INTERNET_STREAM
7491 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7492#endif
7493#ifdef _SC_PII_OSI
7494 {"SC_PII_OSI", _SC_PII_OSI},
7495#endif
7496#ifdef _SC_PII_OSI_CLTS
7497 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7498#endif
7499#ifdef _SC_PII_OSI_COTS
7500 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7501#endif
7502#ifdef _SC_PII_OSI_M
7503 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7504#endif
7505#ifdef _SC_PII_SOCKET
7506 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7507#endif
7508#ifdef _SC_PII_XTI
7509 {"SC_PII_XTI", _SC_PII_XTI},
7510#endif
7511#ifdef _SC_POLL
7512 {"SC_POLL", _SC_POLL},
7513#endif
7514#ifdef _SC_PRIORITIZED_IO
7515 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7516#endif
7517#ifdef _SC_PRIORITY_SCHEDULING
7518 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7519#endif
7520#ifdef _SC_REALTIME_SIGNALS
7521 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7522#endif
7523#ifdef _SC_RE_DUP_MAX
7524 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7525#endif
7526#ifdef _SC_RTSIG_MAX
7527 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7528#endif
7529#ifdef _SC_SAVED_IDS
7530 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7531#endif
7532#ifdef _SC_SCHAR_MAX
7533 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7534#endif
7535#ifdef _SC_SCHAR_MIN
7536 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7537#endif
7538#ifdef _SC_SELECT
7539 {"SC_SELECT", _SC_SELECT},
7540#endif
7541#ifdef _SC_SEMAPHORES
7542 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7543#endif
7544#ifdef _SC_SEM_NSEMS_MAX
7545 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7546#endif
7547#ifdef _SC_SEM_VALUE_MAX
7548 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7549#endif
7550#ifdef _SC_SHARED_MEMORY_OBJECTS
7551 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7552#endif
7553#ifdef _SC_SHRT_MAX
7554 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7555#endif
7556#ifdef _SC_SHRT_MIN
7557 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7558#endif
7559#ifdef _SC_SIGQUEUE_MAX
7560 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7561#endif
7562#ifdef _SC_SIGRT_MAX
7563 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7564#endif
7565#ifdef _SC_SIGRT_MIN
7566 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7567#endif
Fred Draked86ed291999-12-15 15:34:33 +00007568#ifdef _SC_SOFTPOWER
7569 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7570#endif
Fred Drakec9680921999-12-13 16:37:25 +00007571#ifdef _SC_SPLIT_CACHE
7572 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7573#endif
7574#ifdef _SC_SSIZE_MAX
7575 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7576#endif
7577#ifdef _SC_STACK_PROT
7578 {"SC_STACK_PROT", _SC_STACK_PROT},
7579#endif
7580#ifdef _SC_STREAM_MAX
7581 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7582#endif
7583#ifdef _SC_SYNCHRONIZED_IO
7584 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7585#endif
7586#ifdef _SC_THREADS
7587 {"SC_THREADS", _SC_THREADS},
7588#endif
7589#ifdef _SC_THREAD_ATTR_STACKADDR
7590 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7591#endif
7592#ifdef _SC_THREAD_ATTR_STACKSIZE
7593 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7594#endif
7595#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7596 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7597#endif
7598#ifdef _SC_THREAD_KEYS_MAX
7599 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7600#endif
7601#ifdef _SC_THREAD_PRIORITY_SCHEDULING
7602 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7603#endif
7604#ifdef _SC_THREAD_PRIO_INHERIT
7605 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7606#endif
7607#ifdef _SC_THREAD_PRIO_PROTECT
7608 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7609#endif
7610#ifdef _SC_THREAD_PROCESS_SHARED
7611 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7612#endif
7613#ifdef _SC_THREAD_SAFE_FUNCTIONS
7614 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7615#endif
7616#ifdef _SC_THREAD_STACK_MIN
7617 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7618#endif
7619#ifdef _SC_THREAD_THREADS_MAX
7620 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7621#endif
7622#ifdef _SC_TIMERS
7623 {"SC_TIMERS", _SC_TIMERS},
7624#endif
7625#ifdef _SC_TIMER_MAX
7626 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7627#endif
7628#ifdef _SC_TTY_NAME_MAX
7629 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7630#endif
7631#ifdef _SC_TZNAME_MAX
7632 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7633#endif
7634#ifdef _SC_T_IOV_MAX
7635 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7636#endif
7637#ifdef _SC_UCHAR_MAX
7638 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7639#endif
7640#ifdef _SC_UINT_MAX
7641 {"SC_UINT_MAX", _SC_UINT_MAX},
7642#endif
7643#ifdef _SC_UIO_MAXIOV
7644 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7645#endif
7646#ifdef _SC_ULONG_MAX
7647 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7648#endif
7649#ifdef _SC_USHRT_MAX
7650 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7651#endif
7652#ifdef _SC_VERSION
7653 {"SC_VERSION", _SC_VERSION},
7654#endif
7655#ifdef _SC_WORD_BIT
7656 {"SC_WORD_BIT", _SC_WORD_BIT},
7657#endif
7658#ifdef _SC_XBS5_ILP32_OFF32
7659 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7660#endif
7661#ifdef _SC_XBS5_ILP32_OFFBIG
7662 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7663#endif
7664#ifdef _SC_XBS5_LP64_OFF64
7665 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7666#endif
7667#ifdef _SC_XBS5_LPBIG_OFFBIG
7668 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7669#endif
7670#ifdef _SC_XOPEN_CRYPT
7671 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7672#endif
7673#ifdef _SC_XOPEN_ENH_I18N
7674 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7675#endif
7676#ifdef _SC_XOPEN_LEGACY
7677 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7678#endif
7679#ifdef _SC_XOPEN_REALTIME
7680 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7681#endif
7682#ifdef _SC_XOPEN_REALTIME_THREADS
7683 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7684#endif
7685#ifdef _SC_XOPEN_SHM
7686 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7687#endif
7688#ifdef _SC_XOPEN_UNIX
7689 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7690#endif
7691#ifdef _SC_XOPEN_VERSION
7692 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7693#endif
7694#ifdef _SC_XOPEN_XCU_VERSION
7695 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7696#endif
7697#ifdef _SC_XOPEN_XPG2
7698 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7699#endif
7700#ifdef _SC_XOPEN_XPG3
7701 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7702#endif
7703#ifdef _SC_XOPEN_XPG4
7704 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7705#endif
7706};
7707
7708static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007709conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007710{
7711 return conv_confname(arg, valuep, posix_constants_sysconf,
7712 sizeof(posix_constants_sysconf)
7713 / sizeof(struct constdef));
7714}
7715
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007716PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007717"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007718Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007719
7720static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007721posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007722{
7723 PyObject *result = NULL;
7724 int name;
7725
7726 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7727 int value;
7728
7729 errno = 0;
7730 value = sysconf(name);
7731 if (value == -1 && errno != 0)
7732 posix_error();
7733 else
7734 result = PyInt_FromLong(value);
7735 }
7736 return result;
7737}
7738#endif
7739
7740
Fred Drakebec628d1999-12-15 18:31:10 +00007741/* This code is used to ensure that the tables of configuration value names
7742 * are in sorted order as required by conv_confname(), and also to build the
7743 * the exported dictionaries that are used to publish information about the
7744 * names available on the host platform.
7745 *
7746 * Sorting the table at runtime ensures that the table is properly ordered
7747 * when used, even for platforms we're not able to test on. It also makes
7748 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007749 */
Fred Drakebec628d1999-12-15 18:31:10 +00007750
7751static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007752cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007753{
7754 const struct constdef *c1 =
7755 (const struct constdef *) v1;
7756 const struct constdef *c2 =
7757 (const struct constdef *) v2;
7758
7759 return strcmp(c1->name, c2->name);
7760}
7761
7762static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007763setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007764 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007765{
Fred Drakebec628d1999-12-15 18:31:10 +00007766 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007767 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007768
7769 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7770 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007771 if (d == NULL)
7772 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007773
Barry Warsaw3155db32000-04-13 15:20:40 +00007774 for (i=0; i < tablesize; ++i) {
7775 PyObject *o = PyInt_FromLong(table[i].value);
7776 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7777 Py_XDECREF(o);
7778 Py_DECREF(d);
7779 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007780 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007781 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007782 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007783 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007784}
7785
Fred Drakebec628d1999-12-15 18:31:10 +00007786/* Return -1 on failure, 0 on success. */
7787static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007788setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007789{
7790#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007791 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007792 sizeof(posix_constants_pathconf)
7793 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007794 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007795 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007796#endif
7797#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007798 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007799 sizeof(posix_constants_confstr)
7800 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007801 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007802 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007803#endif
7804#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007805 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007806 sizeof(posix_constants_sysconf)
7807 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007808 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007809 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007810#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007811 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007812}
Fred Draked86ed291999-12-15 15:34:33 +00007813
7814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007815PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007816"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007817Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007818in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007819
7820static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007821posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007822{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007823 abort();
7824 /*NOTREACHED*/
7825 Py_FatalError("abort() called from Python code didn't abort!");
7826 return NULL;
7827}
Fred Drakebec628d1999-12-15 18:31:10 +00007828
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007829#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007830PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007831"startfile(filepath [, operation]) - Start a file with its associated\n\
7832application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007833\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007834When \"operation\" is not specified or \"open\", this acts like\n\
7835double-clicking the file in Explorer, or giving the file name as an\n\
7836argument to the DOS \"start\" command: the file is opened with whatever\n\
7837application (if any) its extension is associated.\n\
7838When another \"operation\" is given, it specifies what should be done with\n\
7839the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007840\n\
7841startfile returns as soon as the associated application is launched.\n\
7842There is no option to wait for the application to close, and no way\n\
7843to retrieve the application's exit status.\n\
7844\n\
7845The filepath is relative to the current directory. If you want to use\n\
7846an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007847the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007848
7849static PyObject *
7850win32_startfile(PyObject *self, PyObject *args)
7851{
7852 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00007853 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007854 HINSTANCE rc;
Georg Brandlad89dc82006-04-03 12:26:26 +00007855#ifdef Py_WIN_WIDE_FILENAMES
7856 if (unicode_file_names()) {
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007857 PyObject *unipath, *woperation = NULL;
Georg Brandlad89dc82006-04-03 12:26:26 +00007858 if (!PyArg_ParseTuple(args, "U|s:startfile",
7859 &unipath, &operation)) {
7860 PyErr_Clear();
7861 goto normal;
7862 }
7863
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007864
7865 if (operation) {
7866 woperation = PyUnicode_DecodeASCII(operation,
7867 strlen(operation), NULL);
7868 if (!woperation) {
7869 PyErr_Clear();
7870 operation = NULL;
7871 goto normal;
7872 }
Georg Brandlad89dc82006-04-03 12:26:26 +00007873 }
7874
7875 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007876 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
Georg Brandlad89dc82006-04-03 12:26:26 +00007877 PyUnicode_AS_UNICODE(unipath),
Georg Brandlad89dc82006-04-03 12:26:26 +00007878 NULL, NULL, SW_SHOWNORMAL);
7879 Py_END_ALLOW_THREADS
7880
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007881 Py_XDECREF(woperation);
Georg Brandlad89dc82006-04-03 12:26:26 +00007882 if (rc <= (HINSTANCE)32) {
7883 PyObject *errval = win32_error_unicode("startfile",
7884 PyUnicode_AS_UNICODE(unipath));
7885 return errval;
7886 }
7887 Py_INCREF(Py_None);
7888 return Py_None;
7889 }
7890#endif
7891
7892normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00007893 if (!PyArg_ParseTuple(args, "et|s:startfile",
7894 Py_FileSystemDefaultEncoding, &filepath,
7895 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00007896 return NULL;
7897 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00007898 rc = ShellExecute((HWND)0, operation, filepath,
7899 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007900 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00007901 if (rc <= (HINSTANCE)32) {
7902 PyObject *errval = win32_error("startfile", filepath);
7903 PyMem_Free(filepath);
7904 return errval;
7905 }
7906 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007907 Py_INCREF(Py_None);
7908 return Py_None;
7909}
7910#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007911
Martin v. Löwis438b5342002-12-27 10:16:42 +00007912#ifdef HAVE_GETLOADAVG
7913PyDoc_STRVAR(posix_getloadavg__doc__,
7914"getloadavg() -> (float, float, float)\n\n\
7915Return the number of processes in the system run queue averaged over\n\
7916the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7917was unobtainable");
7918
7919static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007920posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007921{
7922 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007923 if (getloadavg(loadavg, 3)!=3) {
7924 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7925 return NULL;
7926 } else
7927 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
7928}
7929#endif
7930
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007931#ifdef MS_WINDOWS
7932
7933PyDoc_STRVAR(win32_urandom__doc__,
7934"urandom(n) -> str\n\n\
7935Return a string of n random bytes suitable for cryptographic use.");
7936
7937typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7938 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7939 DWORD dwFlags );
7940typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7941 BYTE *pbBuffer );
7942
7943static CRYPTGENRANDOM pCryptGenRandom = NULL;
7944static HCRYPTPROV hCryptProv = 0;
7945
Tim Peters4ad82172004-08-30 17:02:04 +00007946static PyObject*
7947win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007948{
Tim Petersd3115382004-08-30 17:36:46 +00007949 int howMany;
7950 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007951
Tim Peters4ad82172004-08-30 17:02:04 +00007952 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00007953 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00007954 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00007955 if (howMany < 0)
7956 return PyErr_Format(PyExc_ValueError,
7957 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007958
Tim Peters4ad82172004-08-30 17:02:04 +00007959 if (hCryptProv == 0) {
7960 HINSTANCE hAdvAPI32 = NULL;
7961 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007962
Tim Peters4ad82172004-08-30 17:02:04 +00007963 /* Obtain handle to the DLL containing CryptoAPI
7964 This should not fail */
7965 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7966 if(hAdvAPI32 == NULL)
7967 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007968
Tim Peters4ad82172004-08-30 17:02:04 +00007969 /* Obtain pointers to the CryptoAPI functions
7970 This will fail on some early versions of Win95 */
7971 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7972 hAdvAPI32,
7973 "CryptAcquireContextA");
7974 if (pCryptAcquireContext == NULL)
7975 return PyErr_Format(PyExc_NotImplementedError,
7976 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007977
Tim Peters4ad82172004-08-30 17:02:04 +00007978 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7979 hAdvAPI32, "CryptGenRandom");
Georg Brandlb20cb332006-09-06 06:04:06 +00007980 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00007981 return PyErr_Format(PyExc_NotImplementedError,
7982 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007983
Tim Peters4ad82172004-08-30 17:02:04 +00007984 /* Acquire context */
7985 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7986 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7987 return win32_error("CryptAcquireContext", NULL);
7988 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007989
Tim Peters4ad82172004-08-30 17:02:04 +00007990 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00007991 result = PyString_FromStringAndSize(NULL, howMany);
7992 if (result != NULL) {
7993 /* Get random data */
7994 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7995 PyString_AS_STRING(result))) {
7996 Py_DECREF(result);
7997 return win32_error("CryptGenRandom", NULL);
7998 }
Tim Peters4ad82172004-08-30 17:02:04 +00007999 }
Tim Petersd3115382004-08-30 17:36:46 +00008000 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008001}
8002#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008003
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008004#ifdef __VMS
8005/* Use openssl random routine */
8006#include <openssl/rand.h>
8007PyDoc_STRVAR(vms_urandom__doc__,
8008"urandom(n) -> str\n\n\
8009Return a string of n random bytes suitable for cryptographic use.");
8010
8011static PyObject*
8012vms_urandom(PyObject *self, PyObject *args)
8013{
8014 int howMany;
8015 PyObject* result;
8016
8017 /* Read arguments */
8018 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8019 return NULL;
8020 if (howMany < 0)
8021 return PyErr_Format(PyExc_ValueError,
8022 "negative argument not allowed");
8023
8024 /* Allocate bytes */
8025 result = PyString_FromStringAndSize(NULL, howMany);
8026 if (result != NULL) {
8027 /* Get random data */
8028 if (RAND_pseudo_bytes((unsigned char*)
8029 PyString_AS_STRING(result),
8030 howMany) < 0) {
8031 Py_DECREF(result);
8032 return PyErr_Format(PyExc_ValueError,
8033 "RAND_pseudo_bytes");
8034 }
8035 }
8036 return result;
8037}
8038#endif
8039
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008040static PyMethodDef posix_methods[] = {
8041 {"access", posix_access, METH_VARARGS, posix_access__doc__},
8042#ifdef HAVE_TTYNAME
8043 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
8044#endif
8045 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
8046 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008047#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008048 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008049#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008050#ifdef HAVE_LCHOWN
8051 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
8052#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008053#ifdef HAVE_CHROOT
8054 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
8055#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008056#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00008057 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008058#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008059#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00008060 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008061#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00008062 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008063#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008064#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008065#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008066 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008067#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008068 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8069 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8070 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008071#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008072 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008073#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008074#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008075 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008076#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008077 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8078 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8079 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00008080 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008081#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008082 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008083#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008084#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008085 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008086#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008087 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008088#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00008089 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008090#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008091 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8092 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8093 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008094#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00008095 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008096#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008097 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008098#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008099 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8100 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008101#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008102#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008103 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8104 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008105#if defined(PYOS_OS2)
8106 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8107 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
8108#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008109#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008110#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00008111 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008112#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008113#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00008114 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008115#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008116#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00008117 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008118#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008119#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00008120 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008121#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008122#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008123 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008124#endif /* HAVE_GETEGID */
8125#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008126 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008127#endif /* HAVE_GETEUID */
8128#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008129 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008130#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008131#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00008132 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008133#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008134 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008135#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008136 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008137#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008138#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00008139 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008140#endif /* HAVE_GETPPID */
8141#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008142 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008143#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008144#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00008145 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008146#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008147#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008148 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008149#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008150#ifdef HAVE_KILLPG
8151 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
8152#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008153#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008154 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008155#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008156#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008157 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008158#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008159 {"popen2", win32_popen2, METH_VARARGS},
8160 {"popen3", win32_popen3, METH_VARARGS},
8161 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00008162 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008163#else
8164#if defined(PYOS_OS2) && defined(PYCC_GCC)
8165 {"popen2", os2emx_popen2, METH_VARARGS},
8166 {"popen3", os2emx_popen3, METH_VARARGS},
8167 {"popen4", os2emx_popen4, METH_VARARGS},
8168#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008169#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008170#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008171#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008172 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008173#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008174#ifdef HAVE_SETEUID
8175 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8176#endif /* HAVE_SETEUID */
8177#ifdef HAVE_SETEGID
8178 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8179#endif /* HAVE_SETEGID */
8180#ifdef HAVE_SETREUID
8181 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8182#endif /* HAVE_SETREUID */
8183#ifdef HAVE_SETREGID
8184 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8185#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008186#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008187 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008188#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008189#ifdef HAVE_SETGROUPS
Georg Brandl96a8c392006-05-29 21:04:52 +00008190 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008191#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008192#ifdef HAVE_GETPGID
8193 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8194#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008195#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008196 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008197#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008198#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00008199 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008200#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008201#ifdef HAVE_WAIT3
8202 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8203#endif /* HAVE_WAIT3 */
8204#ifdef HAVE_WAIT4
8205 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8206#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008207#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008208 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008209#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008210#ifdef HAVE_GETSID
8211 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8212#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008213#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00008214 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008215#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008216#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008217 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008218#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008219#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008220 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008221#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008222#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008223 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008224#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008225 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8226 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8227 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8228 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8229 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8230 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8231 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8232 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8233 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00008234 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008235#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00008236 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008237#endif
8238#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008239 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008240#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008241#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008242 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8243#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008244#ifdef HAVE_DEVICE_MACROS
8245 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8246 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8247 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8248#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008249#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008250 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008251#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008252#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008253 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008254#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008255#ifdef HAVE_UNSETENV
8256 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8257#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00008258#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008259 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00008260#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00008261#ifdef HAVE_FCHDIR
8262 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8263#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008264#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008265 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008266#endif
8267#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008268 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008269#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008270#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008271#ifdef WCOREDUMP
8272 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8273#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008274#ifdef WIFCONTINUED
8275 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8276#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008277#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008278 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008279#endif /* WIFSTOPPED */
8280#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008281 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008282#endif /* WIFSIGNALED */
8283#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008284 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008285#endif /* WIFEXITED */
8286#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008287 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008288#endif /* WEXITSTATUS */
8289#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008290 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008291#endif /* WTERMSIG */
8292#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008293 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008294#endif /* WSTOPSIG */
8295#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008296#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008297 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008298#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008299#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008300 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008301#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008302#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00008303 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008304#endif
8305#ifdef HAVE_TEMPNAM
8306 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8307#endif
8308#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00008309 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008310#endif
Fred Drakec9680921999-12-13 16:37:25 +00008311#ifdef HAVE_CONFSTR
8312 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8313#endif
8314#ifdef HAVE_SYSCONF
8315 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8316#endif
8317#ifdef HAVE_FPATHCONF
8318 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8319#endif
8320#ifdef HAVE_PATHCONF
8321 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8322#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008323 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008324#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00008325 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8326#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008327#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00008328 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008329#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008330 #ifdef MS_WINDOWS
8331 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8332 #endif
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008333 #ifdef __VMS
8334 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
8335 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008336 {NULL, NULL} /* Sentinel */
8337};
8338
8339
Barry Warsaw4a342091996-12-19 23:50:02 +00008340static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008341ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008342{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008343 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008344}
8345
Guido van Rossumd48f2521997-12-05 22:19:34 +00008346#if defined(PYOS_OS2)
8347/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008348static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008349{
8350 APIRET rc;
8351 ULONG values[QSV_MAX+1];
8352 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008353 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008354
8355 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008356 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008357 Py_END_ALLOW_THREADS
8358
8359 if (rc != NO_ERROR) {
8360 os2_error(rc);
8361 return -1;
8362 }
8363
Fred Drake4d1e64b2002-04-15 19:40:07 +00008364 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8365 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8366 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8367 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8368 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8369 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8370 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008371
8372 switch (values[QSV_VERSION_MINOR]) {
8373 case 0: ver = "2.00"; break;
8374 case 10: ver = "2.10"; break;
8375 case 11: ver = "2.11"; break;
8376 case 30: ver = "3.00"; break;
8377 case 40: ver = "4.00"; break;
8378 case 50: ver = "5.00"; break;
8379 default:
Tim Peters885d4572001-11-28 20:27:42 +00008380 PyOS_snprintf(tmp, sizeof(tmp),
8381 "%d-%d", values[QSV_VERSION_MAJOR],
8382 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008383 ver = &tmp[0];
8384 }
8385
8386 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008387 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008388 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008389
8390 /* Add Indicator of Which Drive was Used to Boot the System */
8391 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8392 tmp[1] = ':';
8393 tmp[2] = '\0';
8394
Fred Drake4d1e64b2002-04-15 19:40:07 +00008395 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008396}
8397#endif
8398
Barry Warsaw4a342091996-12-19 23:50:02 +00008399static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008400all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008401{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008402#ifdef F_OK
8403 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008404#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008405#ifdef R_OK
8406 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008407#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008408#ifdef W_OK
8409 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008410#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008411#ifdef X_OK
8412 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008413#endif
Fred Drakec9680921999-12-13 16:37:25 +00008414#ifdef NGROUPS_MAX
8415 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8416#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008417#ifdef TMP_MAX
8418 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8419#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008420#ifdef WCONTINUED
8421 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8422#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008423#ifdef WNOHANG
8424 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008425#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008426#ifdef WUNTRACED
8427 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8428#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008429#ifdef O_RDONLY
8430 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8431#endif
8432#ifdef O_WRONLY
8433 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8434#endif
8435#ifdef O_RDWR
8436 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8437#endif
8438#ifdef O_NDELAY
8439 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8440#endif
8441#ifdef O_NONBLOCK
8442 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8443#endif
8444#ifdef O_APPEND
8445 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8446#endif
8447#ifdef O_DSYNC
8448 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8449#endif
8450#ifdef O_RSYNC
8451 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8452#endif
8453#ifdef O_SYNC
8454 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8455#endif
8456#ifdef O_NOCTTY
8457 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8458#endif
8459#ifdef O_CREAT
8460 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8461#endif
8462#ifdef O_EXCL
8463 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8464#endif
8465#ifdef O_TRUNC
8466 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8467#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008468#ifdef O_BINARY
8469 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8470#endif
8471#ifdef O_TEXT
8472 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8473#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008474#ifdef O_LARGEFILE
8475 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8476#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008477#ifdef O_SHLOCK
8478 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8479#endif
8480#ifdef O_EXLOCK
8481 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8482#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008483
Tim Peters5aa91602002-01-30 05:46:57 +00008484/* MS Windows */
8485#ifdef O_NOINHERIT
8486 /* Don't inherit in child processes. */
8487 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8488#endif
8489#ifdef _O_SHORT_LIVED
8490 /* Optimize for short life (keep in memory). */
8491 /* MS forgot to define this one with a non-underscore form too. */
8492 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8493#endif
8494#ifdef O_TEMPORARY
8495 /* Automatically delete when last handle is closed. */
8496 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8497#endif
8498#ifdef O_RANDOM
8499 /* Optimize for random access. */
8500 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8501#endif
8502#ifdef O_SEQUENTIAL
8503 /* Optimize for sequential access. */
8504 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8505#endif
8506
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008507/* GNU extensions. */
8508#ifdef O_DIRECT
8509 /* Direct disk access. */
8510 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8511#endif
8512#ifdef O_DIRECTORY
8513 /* Must be a directory. */
8514 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8515#endif
8516#ifdef O_NOFOLLOW
8517 /* Do not follow links. */
8518 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8519#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008520
Barry Warsaw5676bd12003-01-07 20:57:09 +00008521 /* These come from sysexits.h */
8522#ifdef EX_OK
8523 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008524#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008525#ifdef EX_USAGE
8526 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008527#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008528#ifdef EX_DATAERR
8529 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008530#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008531#ifdef EX_NOINPUT
8532 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008533#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008534#ifdef EX_NOUSER
8535 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008536#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008537#ifdef EX_NOHOST
8538 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008539#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008540#ifdef EX_UNAVAILABLE
8541 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008542#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008543#ifdef EX_SOFTWARE
8544 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008545#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008546#ifdef EX_OSERR
8547 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008548#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008549#ifdef EX_OSFILE
8550 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008551#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008552#ifdef EX_CANTCREAT
8553 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008554#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008555#ifdef EX_IOERR
8556 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008557#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008558#ifdef EX_TEMPFAIL
8559 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008560#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008561#ifdef EX_PROTOCOL
8562 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008563#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008564#ifdef EX_NOPERM
8565 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008566#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008567#ifdef EX_CONFIG
8568 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008569#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008570#ifdef EX_NOTFOUND
8571 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008572#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008573
Guido van Rossum246bc171999-02-01 23:54:31 +00008574#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008575#if defined(PYOS_OS2) && defined(PYCC_GCC)
8576 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8577 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8578 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8579 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8580 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8581 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8582 if (ins(d, "P_PM", (long)P_PM)) return -1;
8583 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8584 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8585 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8586 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8587 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8588 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8589 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8590 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8591 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8592 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8593 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8594 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8595 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8596#else
Guido van Rossum7d385291999-02-16 19:38:04 +00008597 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8598 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8599 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8600 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8601 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008602#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008603#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008604
Guido van Rossumd48f2521997-12-05 22:19:34 +00008605#if defined(PYOS_OS2)
8606 if (insertvalues(d)) return -1;
8607#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008608 return 0;
8609}
8610
8611
Tim Peters5aa91602002-01-30 05:46:57 +00008612#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008613#define INITFUNC initnt
8614#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008615
8616#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008617#define INITFUNC initos2
8618#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008619
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008620#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008621#define INITFUNC initposix
8622#define MODNAME "posix"
8623#endif
8624
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008625PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008626INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008627{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008628 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008629
Fred Drake4d1e64b2002-04-15 19:40:07 +00008630 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008631 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008632 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00008633 if (m == NULL)
8634 return;
Tim Peters5aa91602002-01-30 05:46:57 +00008635
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008636 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008637 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00008638 Py_XINCREF(v);
8639 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008640 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00008641 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008642
Fred Drake4d1e64b2002-04-15 19:40:07 +00008643 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00008644 return;
8645
Fred Drake4d1e64b2002-04-15 19:40:07 +00008646 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00008647 return;
8648
Fred Drake4d1e64b2002-04-15 19:40:07 +00008649 Py_INCREF(PyExc_OSError);
8650 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008651
Guido van Rossumb3d39562000-01-31 18:41:26 +00008652#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00008653 if (posix_putenv_garbage == NULL)
8654 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008655#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008656
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008657 if (!initialized) {
8658 stat_result_desc.name = MODNAME ".stat_result";
8659 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8660 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8661 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8662 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8663 structseq_new = StatResultType.tp_new;
8664 StatResultType.tp_new = statresult_new;
8665
8666 statvfs_result_desc.name = MODNAME ".statvfs_result";
8667 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
8668 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008669 Py_INCREF((PyObject*) &StatResultType);
8670 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00008671 Py_INCREF((PyObject*) &StatVFSResultType);
8672 PyModule_AddObject(m, "statvfs_result",
8673 (PyObject*) &StatVFSResultType);
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008674 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00008675
8676#ifdef __APPLE__
8677 /*
8678 * Step 2 of weak-linking support on Mac OS X.
8679 *
8680 * The code below removes functions that are not available on the
8681 * currently active platform.
8682 *
8683 * This block allow one to use a python binary that was build on
8684 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8685 * OSX 10.4.
8686 */
8687#ifdef HAVE_FSTATVFS
8688 if (fstatvfs == NULL) {
8689 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8690 return;
8691 }
8692 }
8693#endif /* HAVE_FSTATVFS */
8694
8695#ifdef HAVE_STATVFS
8696 if (statvfs == NULL) {
8697 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8698 return;
8699 }
8700 }
8701#endif /* HAVE_STATVFS */
8702
8703# ifdef HAVE_LCHOWN
8704 if (lchown == NULL) {
8705 if (PyObject_DelAttrString(m, "lchown") == -1) {
8706 return;
8707 }
8708 }
8709#endif /* HAVE_LCHOWN */
8710
8711
8712#endif /* __APPLE__ */
8713
Guido van Rossumb6775db1994-08-01 11:34:53 +00008714}
Anthony Baxterac6bd462006-04-13 02:06:09 +00008715
8716#ifdef __cplusplus
8717}
8718#endif
8719
Martin v. Löwis2c7aa632006-10-15 09:44:02 +00008720