blob: aaaa838c275670cfa11817c0f5c67697c4461fc9 [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
Martin v. Löwis88635442007-04-04 18:30:56 +0000847static BOOL
848attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
849{
850 HANDLE hFindFile;
851 WIN32_FIND_DATAA FileData;
852 hFindFile = FindFirstFileA(pszFile, &FileData);
853 if (hFindFile == INVALID_HANDLE_VALUE)
854 return FALSE;
855 FindClose(hFindFile);
856 pfad->dwFileAttributes = FileData.dwFileAttributes;
857 pfad->ftCreationTime = FileData.ftCreationTime;
858 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
859 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
860 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
861 pfad->nFileSizeLow = FileData.nFileSizeLow;
862 return TRUE;
863}
864
865static BOOL
866attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
867{
868 HANDLE hFindFile;
869 WIN32_FIND_DATAW FileData;
870 hFindFile = FindFirstFileW(pszFile, &FileData);
871 if (hFindFile == INVALID_HANDLE_VALUE)
872 return FALSE;
873 FindClose(hFindFile);
874 pfad->dwFileAttributes = FileData.dwFileAttributes;
875 pfad->ftCreationTime = FileData.ftCreationTime;
876 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
877 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
878 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
879 pfad->nFileSizeLow = FileData.nFileSizeLow;
880 return TRUE;
881}
882
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000883static BOOL WINAPI
884Py_GetFileAttributesExA(LPCSTR pszFile,
885 GET_FILEEX_INFO_LEVELS level,
886 LPVOID pv)
887{
888 BOOL result;
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000889 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
890 /* First try to use the system's implementation, if that is
891 available and either succeeds to gives an error other than
892 that it isn't implemented. */
893 check_gfax();
894 if (gfaxa) {
895 result = gfaxa(pszFile, level, pv);
896 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
897 return result;
898 }
899 /* It's either not present, or not implemented.
900 Emulate using FindFirstFile. */
901 if (level != GetFileExInfoStandard) {
902 SetLastError(ERROR_INVALID_PARAMETER);
903 return FALSE;
904 }
905 /* Use GetFileAttributes to validate that the file name
906 does not contain wildcards (which FindFirstFile would
907 accept). */
908 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
909 return FALSE;
Martin v. Löwis88635442007-04-04 18:30:56 +0000910 return attributes_from_dir(pszFile, pfad);
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000911}
912
913static BOOL WINAPI
914Py_GetFileAttributesExW(LPCWSTR pszFile,
915 GET_FILEEX_INFO_LEVELS level,
916 LPVOID pv)
917{
918 BOOL result;
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000919 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
920 /* First try to use the system's implementation, if that is
921 available and either succeeds to gives an error other than
922 that it isn't implemented. */
923 check_gfax();
924 if (gfaxa) {
925 result = gfaxw(pszFile, level, pv);
926 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
927 return result;
928 }
929 /* It's either not present, or not implemented.
930 Emulate using FindFirstFile. */
931 if (level != GetFileExInfoStandard) {
932 SetLastError(ERROR_INVALID_PARAMETER);
933 return FALSE;
934 }
935 /* Use GetFileAttributes to validate that the file name
936 does not contain wildcards (which FindFirstFile would
937 accept). */
938 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
939 return FALSE;
Martin v. Löwis88635442007-04-04 18:30:56 +0000940 return attributes_from_dir_w(pszFile, pfad);
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000941}
942
Martin v. Löwis14694662006-02-03 12:54:16 +0000943static int
944win32_stat(const char* path, struct win32_stat *result)
945{
946 WIN32_FILE_ATTRIBUTE_DATA info;
947 int code;
948 char *dot;
949 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000950 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis88635442007-04-04 18:30:56 +0000951 if (GetLastError() != ERROR_SHARING_VIOLATION) {
952 /* Protocol violation: we explicitly clear errno, instead of
953 setting it to a POSIX error. Callers should use GetLastError. */
954 errno = 0;
955 return -1;
956 } else {
957 /* Could not get attributes on open file. Fall back to
958 reading the directory. */
959 if (!attributes_from_dir(path, &info)) {
960 /* Very strange. This should not fail now */
961 errno = 0;
962 return -1;
963 }
964 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000965 }
966 code = attribute_data_to_stat(&info, result);
967 if (code != 0)
968 return code;
969 /* Set S_IFEXEC if it is an .exe, .bat, ... */
970 dot = strrchr(path, '.');
971 if (dot) {
972 if (stricmp(dot, ".bat") == 0 ||
973 stricmp(dot, ".cmd") == 0 ||
974 stricmp(dot, ".exe") == 0 ||
975 stricmp(dot, ".com") == 0)
976 result->st_mode |= 0111;
977 }
978 return code;
979}
980
981static int
982win32_wstat(const wchar_t* path, struct win32_stat *result)
983{
984 int code;
985 const wchar_t *dot;
986 WIN32_FILE_ATTRIBUTE_DATA info;
987 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000988 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis88635442007-04-04 18:30:56 +0000989 if (GetLastError() != ERROR_SHARING_VIOLATION) {
990 /* Protocol violation: we explicitly clear errno, instead of
991 setting it to a POSIX error. Callers should use GetLastError. */
992 errno = 0;
993 return -1;
994 } else {
995 /* Could not get attributes on open file. Fall back to
996 reading the directory. */
997 if (!attributes_from_dir_w(path, &info)) {
998 /* Very strange. This should not fail now */
999 errno = 0;
1000 return -1;
1001 }
1002 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001003 }
1004 code = attribute_data_to_stat(&info, result);
1005 if (code < 0)
1006 return code;
1007 /* Set IFEXEC if it is an .exe, .bat, ... */
1008 dot = wcsrchr(path, '.');
1009 if (dot) {
1010 if (_wcsicmp(dot, L".bat") == 0 ||
1011 _wcsicmp(dot, L".cmd") == 0 ||
1012 _wcsicmp(dot, L".exe") == 0 ||
1013 _wcsicmp(dot, L".com") == 0)
1014 result->st_mode |= 0111;
1015 }
1016 return code;
1017}
1018
1019static int
1020win32_fstat(int file_number, struct win32_stat *result)
1021{
1022 BY_HANDLE_FILE_INFORMATION info;
1023 HANDLE h;
1024 int type;
1025
1026 h = (HANDLE)_get_osfhandle(file_number);
1027
1028 /* Protocol violation: we explicitly clear errno, instead of
1029 setting it to a POSIX error. Callers should use GetLastError. */
1030 errno = 0;
1031
1032 if (h == INVALID_HANDLE_VALUE) {
1033 /* This is really a C library error (invalid file handle).
1034 We set the Win32 error to the closes one matching. */
1035 SetLastError(ERROR_INVALID_HANDLE);
1036 return -1;
1037 }
1038 memset(result, 0, sizeof(*result));
1039
1040 type = GetFileType(h);
1041 if (type == FILE_TYPE_UNKNOWN) {
1042 DWORD error = GetLastError();
1043 if (error != 0) {
1044 return -1;
1045 }
1046 /* else: valid but unknown file */
1047 }
1048
1049 if (type != FILE_TYPE_DISK) {
1050 if (type == FILE_TYPE_CHAR)
1051 result->st_mode = _S_IFCHR;
1052 else if (type == FILE_TYPE_PIPE)
1053 result->st_mode = _S_IFIFO;
1054 return 0;
1055 }
1056
1057 if (!GetFileInformationByHandle(h, &info)) {
1058 return -1;
1059 }
1060
1061 /* similar to stat() */
1062 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1063 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1064 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1065 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1066 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1067 /* specific to fstat() */
1068 result->st_nlink = info.nNumberOfLinks;
1069 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1070 return 0;
1071}
1072
1073#endif /* MS_WINDOWS */
1074
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001075PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001076"stat_result: Result from stat or lstat.\n\n\
1077This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001078 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001079or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1080\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001081Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1082or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001083\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001084See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001085
1086static PyStructSequence_Field stat_result_fields[] = {
1087 {"st_mode", "protection bits"},
1088 {"st_ino", "inode"},
1089 {"st_dev", "device"},
1090 {"st_nlink", "number of hard links"},
1091 {"st_uid", "user ID of owner"},
1092 {"st_gid", "group ID of owner"},
1093 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001094 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1095 {NULL, "integer time of last access"},
1096 {NULL, "integer time of last modification"},
1097 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001098 {"st_atime", "time of last access"},
1099 {"st_mtime", "time of last modification"},
1100 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001101#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001102 {"st_blksize", "blocksize for filesystem I/O"},
1103#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001104#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001105 {"st_blocks", "number of blocks allocated"},
1106#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001107#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001108 {"st_rdev", "device type (if inode device)"},
1109#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001110#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1111 {"st_flags", "user defined flags for file"},
1112#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001113#ifdef HAVE_STRUCT_STAT_ST_GEN
1114 {"st_gen", "generation number"},
1115#endif
1116#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1117 {"st_birthtime", "time of creation"},
1118#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001119 {0}
1120};
1121
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001122#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001123#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001124#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001125#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001126#endif
1127
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001128#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001129#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1130#else
1131#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1132#endif
1133
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001134#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001135#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1136#else
1137#define ST_RDEV_IDX ST_BLOCKS_IDX
1138#endif
1139
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001140#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1141#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1142#else
1143#define ST_FLAGS_IDX ST_RDEV_IDX
1144#endif
1145
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001146#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001147#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001148#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001149#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001150#endif
1151
1152#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1153#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1154#else
1155#define ST_BIRTHTIME_IDX ST_GEN_IDX
1156#endif
1157
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001158static PyStructSequence_Desc stat_result_desc = {
1159 "stat_result", /* name */
1160 stat_result__doc__, /* doc */
1161 stat_result_fields,
1162 10
1163};
1164
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001165PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001166"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1167This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001168 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001169or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001170\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001171See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001172
1173static PyStructSequence_Field statvfs_result_fields[] = {
1174 {"f_bsize", },
1175 {"f_frsize", },
1176 {"f_blocks", },
1177 {"f_bfree", },
1178 {"f_bavail", },
1179 {"f_files", },
1180 {"f_ffree", },
1181 {"f_favail", },
1182 {"f_flag", },
1183 {"f_namemax",},
1184 {0}
1185};
1186
1187static PyStructSequence_Desc statvfs_result_desc = {
1188 "statvfs_result", /* name */
1189 statvfs_result__doc__, /* doc */
1190 statvfs_result_fields,
1191 10
1192};
1193
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00001194static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001195static PyTypeObject StatResultType;
1196static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001197static newfunc structseq_new;
1198
1199static PyObject *
1200statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1201{
1202 PyStructSequence *result;
1203 int i;
1204
1205 result = (PyStructSequence*)structseq_new(type, args, kwds);
1206 if (!result)
1207 return NULL;
1208 /* If we have been initialized from a tuple,
1209 st_?time might be set to None. Initialize it
1210 from the int slots. */
1211 for (i = 7; i <= 9; i++) {
1212 if (result->ob_item[i+3] == Py_None) {
1213 Py_DECREF(Py_None);
1214 Py_INCREF(result->ob_item[i]);
1215 result->ob_item[i+3] = result->ob_item[i];
1216 }
1217 }
1218 return (PyObject*)result;
1219}
1220
1221
1222
1223/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001224static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001225
1226PyDoc_STRVAR(stat_float_times__doc__,
1227"stat_float_times([newval]) -> oldval\n\n\
1228Determine whether os.[lf]stat represents time stamps as float objects.\n\
1229If newval is True, future calls to stat() return floats, if it is False,\n\
1230future calls return ints. \n\
1231If newval is omitted, return the current setting.\n");
1232
1233static PyObject*
1234stat_float_times(PyObject* self, PyObject *args)
1235{
1236 int newval = -1;
1237 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1238 return NULL;
1239 if (newval == -1)
1240 /* Return old value */
1241 return PyBool_FromLong(_stat_float_times);
1242 _stat_float_times = newval;
1243 Py_INCREF(Py_None);
1244 return Py_None;
1245}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001246
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001247static void
1248fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1249{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001250 PyObject *fval,*ival;
1251#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001252 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001253#else
1254 ival = PyInt_FromLong((long)sec);
1255#endif
Neal Norwitze0a81af2006-08-12 01:50:38 +00001256 if (!ival)
1257 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001258 if (_stat_float_times) {
1259 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1260 } else {
1261 fval = ival;
1262 Py_INCREF(fval);
1263 }
1264 PyStructSequence_SET_ITEM(v, index, ival);
1265 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001266}
1267
Tim Peters5aa91602002-01-30 05:46:57 +00001268/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001269 (used by posix_stat() and posix_fstat()) */
1270static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001271_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001272{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001273 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001274 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001275 if (v == NULL)
1276 return NULL;
1277
Martin v. Löwis14694662006-02-03 12:54:16 +00001278 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001279#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001280 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001281 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001282#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001283 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001284#endif
1285#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001286 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001287 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001288#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001289 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001290#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001291 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1292 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1293 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001294#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001295 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001296 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001297#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001298 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001299#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001300
Martin v. Löwis14694662006-02-03 12:54:16 +00001301#if defined(HAVE_STAT_TV_NSEC)
1302 ansec = st->st_atim.tv_nsec;
1303 mnsec = st->st_mtim.tv_nsec;
1304 cnsec = st->st_ctim.tv_nsec;
1305#elif defined(HAVE_STAT_TV_NSEC2)
1306 ansec = st->st_atimespec.tv_nsec;
1307 mnsec = st->st_mtimespec.tv_nsec;
1308 cnsec = st->st_ctimespec.tv_nsec;
1309#elif defined(HAVE_STAT_NSEC)
1310 ansec = st->st_atime_nsec;
1311 mnsec = st->st_mtime_nsec;
1312 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001313#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001314 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001315#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001316 fill_time(v, 7, st->st_atime, ansec);
1317 fill_time(v, 8, st->st_mtime, mnsec);
1318 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001319
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001320#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001321 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001322 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001323#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001324#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001325 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001326 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001327#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001328#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001329 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001330 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001331#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001332#ifdef HAVE_STRUCT_STAT_ST_GEN
1333 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001334 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001335#endif
1336#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1337 {
1338 PyObject *val;
1339 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001340 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001341#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001342 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001343#else
1344 bnsec = 0;
1345#endif
1346 if (_stat_float_times) {
1347 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1348 } else {
1349 val = PyInt_FromLong((long)bsec);
1350 }
1351 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1352 val);
1353 }
1354#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001355#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1356 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001357 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001358#endif
Fred Drake699f3522000-06-29 21:12:41 +00001359
1360 if (PyErr_Occurred()) {
1361 Py_DECREF(v);
1362 return NULL;
1363 }
1364
1365 return v;
1366}
1367
Martin v. Löwisd8948722004-06-02 09:57:56 +00001368#ifdef MS_WINDOWS
1369
1370/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1371 where / can be used in place of \ and the trailing slash is optional.
1372 Both SERVER and SHARE must have at least one character.
1373*/
1374
1375#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1376#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001377#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001378#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001379#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001380
Tim Peters4ad82172004-08-30 17:02:04 +00001381static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001382IsUNCRootA(char *path, int pathlen)
1383{
1384 #define ISSLASH ISSLASHA
1385
1386 int i, share;
1387
1388 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1389 /* minimum UNCRoot is \\x\y */
1390 return FALSE;
1391 for (i = 2; i < pathlen ; i++)
1392 if (ISSLASH(path[i])) break;
1393 if (i == 2 || i == pathlen)
1394 /* do not allow \\\SHARE or \\SERVER */
1395 return FALSE;
1396 share = i+1;
1397 for (i = share; i < pathlen; i++)
1398 if (ISSLASH(path[i])) break;
1399 return (i != share && (i == pathlen || i == pathlen-1));
1400
1401 #undef ISSLASH
1402}
1403
1404#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001405static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001406IsUNCRootW(Py_UNICODE *path, int pathlen)
1407{
1408 #define ISSLASH ISSLASHW
1409
1410 int i, share;
1411
1412 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1413 /* minimum UNCRoot is \\x\y */
1414 return FALSE;
1415 for (i = 2; i < pathlen ; i++)
1416 if (ISSLASH(path[i])) break;
1417 if (i == 2 || i == pathlen)
1418 /* do not allow \\\SHARE or \\SERVER */
1419 return FALSE;
1420 share = i+1;
1421 for (i = share; i < pathlen; i++)
1422 if (ISSLASH(path[i])) break;
1423 return (i != share && (i == pathlen || i == pathlen-1));
1424
1425 #undef ISSLASH
1426}
1427#endif /* Py_WIN_WIDE_FILENAMES */
1428#endif /* MS_WINDOWS */
1429
Barry Warsaw53699e91996-12-10 23:23:01 +00001430static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001431posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001432 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001433#ifdef __VMS
1434 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1435#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001436 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001437#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001438 char *wformat,
1439 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001440{
Fred Drake699f3522000-06-29 21:12:41 +00001441 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001442 char *path = NULL; /* pass this to stat; do not free() it */
1443 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001444 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001445 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001446
1447#ifdef Py_WIN_WIDE_FILENAMES
1448 /* If on wide-character-capable OS see if argument
1449 is Unicode and if so use wide API. */
1450 if (unicode_file_names()) {
1451 PyUnicodeObject *po;
1452 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001453 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1454
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001455 Py_BEGIN_ALLOW_THREADS
1456 /* PyUnicode_AS_UNICODE result OK without
1457 thread lock as it is a simple dereference. */
1458 res = wstatfunc(wpath, &st);
1459 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001460
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001461 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001462 return win32_error_unicode("stat", wpath);
1463 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001464 }
1465 /* Drop the argument parsing error as narrow strings
1466 are also valid. */
1467 PyErr_Clear();
1468 }
1469#endif
1470
Tim Peters5aa91602002-01-30 05:46:57 +00001471 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001472 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001473 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001474 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001475
Barry Warsaw53699e91996-12-10 23:23:01 +00001476 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001477 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001478 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001479
1480 if (res != 0) {
1481#ifdef MS_WINDOWS
1482 result = win32_error("stat", pathfree);
1483#else
1484 result = posix_error_with_filename(pathfree);
1485#endif
1486 }
1487 else
1488 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001489
Tim Peters500bd032001-12-19 19:05:01 +00001490 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001491 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001492}
1493
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001494/* POSIX methods */
1495
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001496PyDoc_STRVAR(posix_access__doc__,
Georg Brandl3d26ef12007-01-27 19:38:56 +00001497"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001498Use the real uid/gid to test for access to a path. Note that most\n\
1499operations will use the effective uid/gid, therefore this routine can\n\
1500be used in a suid/sgid environment to test if the invoking user has the\n\
1501specified access to the path. The mode argument can be F_OK to test\n\
1502existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001503
1504static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001505posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001506{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001507 char *path;
1508 int mode;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001509
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001510#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001511 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001512 if (unicode_file_names()) {
1513 PyUnicodeObject *po;
1514 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1515 Py_BEGIN_ALLOW_THREADS
1516 /* PyUnicode_AS_UNICODE OK without thread lock as
1517 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001518 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001519 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001520 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001521 }
1522 /* Drop the argument parsing error as narrow strings
1523 are also valid. */
1524 PyErr_Clear();
1525 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001526 if (!PyArg_ParseTuple(args, "eti:access",
1527 Py_FileSystemDefaultEncoding, &path, &mode))
1528 return 0;
1529 Py_BEGIN_ALLOW_THREADS
1530 attr = GetFileAttributesA(path);
1531 Py_END_ALLOW_THREADS
1532 PyMem_Free(path);
1533finish:
1534 if (attr == 0xFFFFFFFF)
1535 /* File does not exist, or cannot read attributes */
1536 return PyBool_FromLong(0);
1537 /* Access is possible if either write access wasn't requested, or
Martin v. Löwisc8dbc922007-12-03 22:39:10 +00001538 the file isn't read-only, or if it's a directory, as there are
1539 no read-only directories on Windows. */
1540 return PyBool_FromLong(!(mode & 2)
1541 || !(attr & FILE_ATTRIBUTE_READONLY)
1542 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001543#else
1544 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001545 if (!PyArg_ParseTuple(args, "eti:access",
1546 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001547 return NULL;
1548 Py_BEGIN_ALLOW_THREADS
1549 res = access(path, mode);
1550 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001551 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001552 return PyBool_FromLong(res == 0);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001553#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001554}
1555
Guido van Rossumd371ff11999-01-25 16:12:23 +00001556#ifndef F_OK
1557#define F_OK 0
1558#endif
1559#ifndef R_OK
1560#define R_OK 4
1561#endif
1562#ifndef W_OK
1563#define W_OK 2
1564#endif
1565#ifndef X_OK
1566#define X_OK 1
1567#endif
1568
1569#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001570PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001571"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001572Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001573
1574static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001575posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001576{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001577 int id;
1578 char *ret;
1579
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001580 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001581 return NULL;
1582
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001583#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001584 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001585 if (id == 0) {
1586 ret = ttyname();
1587 }
1588 else {
1589 ret = NULL;
1590 }
1591#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001592 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001593#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001594 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001595 return posix_error();
1596 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001597}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001598#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001599
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001600#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001601PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001602"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001603Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001604
1605static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001606posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001607{
1608 char *ret;
1609 char buffer[L_ctermid];
1610
Greg Wardb48bc172000-03-01 21:51:56 +00001611#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001612 ret = ctermid_r(buffer);
1613#else
1614 ret = ctermid(buffer);
1615#endif
1616 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001617 return posix_error();
1618 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001619}
1620#endif
1621
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001622PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001623"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001624Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001625
Barry Warsaw53699e91996-12-10 23:23:01 +00001626static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001627posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001628{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001629#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001630 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001631#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001632 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001633#elif defined(__VMS)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001634 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001635#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001636 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001637#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001638}
1639
Fred Drake4d1e64b2002-04-15 19:40:07 +00001640#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001641PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001642"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001643Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001644opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001645
1646static PyObject *
1647posix_fchdir(PyObject *self, PyObject *fdobj)
1648{
1649 return posix_fildes(fdobj, fchdir);
1650}
1651#endif /* HAVE_FCHDIR */
1652
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001653
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001654PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001655"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001656Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001657
Barry Warsaw53699e91996-12-10 23:23:01 +00001658static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001659posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001660{
Mark Hammondef8b6542001-05-13 08:04:26 +00001661 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001662 int i;
1663 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001664#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001665 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001666 if (unicode_file_names()) {
1667 PyUnicodeObject *po;
1668 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1669 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001670 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1671 if (attr != 0xFFFFFFFF) {
1672 if (i & _S_IWRITE)
1673 attr &= ~FILE_ATTRIBUTE_READONLY;
1674 else
1675 attr |= FILE_ATTRIBUTE_READONLY;
1676 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1677 }
1678 else
1679 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001680 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001681 if (!res)
1682 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001683 PyUnicode_AS_UNICODE(po));
1684 Py_INCREF(Py_None);
1685 return Py_None;
1686 }
1687 /* Drop the argument parsing error as narrow strings
1688 are also valid. */
1689 PyErr_Clear();
1690 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001691 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1692 &path, &i))
1693 return NULL;
1694 Py_BEGIN_ALLOW_THREADS
1695 attr = GetFileAttributesA(path);
1696 if (attr != 0xFFFFFFFF) {
1697 if (i & _S_IWRITE)
1698 attr &= ~FILE_ATTRIBUTE_READONLY;
1699 else
1700 attr |= FILE_ATTRIBUTE_READONLY;
1701 res = SetFileAttributesA(path, attr);
1702 }
1703 else
1704 res = 0;
1705 Py_END_ALLOW_THREADS
1706 if (!res) {
1707 win32_error("chmod", path);
1708 PyMem_Free(path);
1709 return NULL;
1710 }
Martin v. Löwis9f485bc2006-05-08 05:25:56 +00001711 PyMem_Free(path);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001712 Py_INCREF(Py_None);
1713 return Py_None;
1714#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001715 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001716 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001717 return NULL;
1718 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001719 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001720 Py_END_ALLOW_THREADS
1721 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001722 return posix_error_with_allocated_filename(path);
1723 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001724 Py_INCREF(Py_None);
1725 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001726#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001727}
1728
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001729
Martin v. Löwis244edc82001-10-04 22:44:26 +00001730#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001731PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001732"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001733Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001734
1735static PyObject *
1736posix_chroot(PyObject *self, PyObject *args)
1737{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001738 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001739}
1740#endif
1741
Guido van Rossum21142a01999-01-08 21:05:37 +00001742#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001743PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001744"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001745force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001746
1747static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001748posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001749{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001750 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001751}
1752#endif /* HAVE_FSYNC */
1753
1754#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001755
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001756#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001757extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1758#endif
1759
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001760PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001761"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001762force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001763 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001764
1765static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001766posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001767{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001768 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001769}
1770#endif /* HAVE_FDATASYNC */
1771
1772
Fredrik Lundh10723342000-07-10 16:38:09 +00001773#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001774PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001775"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001776Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001777
Barry Warsaw53699e91996-12-10 23:23:01 +00001778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001779posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001780{
Mark Hammondef8b6542001-05-13 08:04:26 +00001781 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001782 int uid, gid;
1783 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001784 if (!PyArg_ParseTuple(args, "etii:chown",
1785 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001786 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001787 return NULL;
1788 Py_BEGIN_ALLOW_THREADS
1789 res = chown(path, (uid_t) uid, (gid_t) gid);
1790 Py_END_ALLOW_THREADS
1791 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001792 return posix_error_with_allocated_filename(path);
1793 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001794 Py_INCREF(Py_None);
1795 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001796}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001797#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001798
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001799#ifdef HAVE_LCHOWN
1800PyDoc_STRVAR(posix_lchown__doc__,
1801"lchown(path, uid, gid)\n\n\
1802Change the owner and group id of path to the numeric uid and gid.\n\
1803This function will not follow symbolic links.");
1804
1805static PyObject *
1806posix_lchown(PyObject *self, PyObject *args)
1807{
1808 char *path = NULL;
1809 int uid, gid;
1810 int res;
1811 if (!PyArg_ParseTuple(args, "etii:lchown",
1812 Py_FileSystemDefaultEncoding, &path,
1813 &uid, &gid))
1814 return NULL;
1815 Py_BEGIN_ALLOW_THREADS
1816 res = lchown(path, (uid_t) uid, (gid_t) gid);
1817 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001818 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001819 return posix_error_with_allocated_filename(path);
1820 PyMem_Free(path);
1821 Py_INCREF(Py_None);
1822 return Py_None;
1823}
1824#endif /* HAVE_LCHOWN */
1825
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001826
Guido van Rossum36bc6801995-06-14 22:54:23 +00001827#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001828PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001829"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001830Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001831
Barry Warsaw53699e91996-12-10 23:23:01 +00001832static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001833posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001834{
1835 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001836 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001837
Barry Warsaw53699e91996-12-10 23:23:01 +00001838 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001839#if defined(PYOS_OS2) && defined(PYCC_GCC)
1840 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001841#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001842 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001843#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001844 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001845 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001846 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001847 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001848}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001849
Walter Dörwald3b918c32002-11-21 20:18:46 +00001850#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001851PyDoc_STRVAR(posix_getcwdu__doc__,
1852"getcwdu() -> path\n\n\
1853Return a unicode string representing the current working directory.");
1854
1855static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001856posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001857{
1858 char buf[1026];
1859 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001860
1861#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001862 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001863 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001864 wchar_t wbuf[1026];
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001865 wchar_t *wbuf2 = wbuf;
1866 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001867 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001868 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1869 /* If the buffer is large enough, len does not include the
1870 terminating \0. If the buffer is too small, len includes
1871 the space needed for the terminator. */
1872 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1873 wbuf2 = malloc(len * sizeof(wchar_t));
1874 if (wbuf2)
1875 len = GetCurrentDirectoryW(len, wbuf2);
1876 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001877 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001878 if (!wbuf2) {
1879 PyErr_NoMemory();
1880 return NULL;
1881 }
1882 if (!len) {
1883 if (wbuf2 != wbuf) free(wbuf2);
1884 return win32_error("getcwdu", NULL);
1885 }
1886 resobj = PyUnicode_FromWideChar(wbuf2, len);
1887 if (wbuf2 != wbuf) free(wbuf2);
1888 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001889 }
1890#endif
1891
1892 Py_BEGIN_ALLOW_THREADS
1893#if defined(PYOS_OS2) && defined(PYCC_GCC)
1894 res = _getcwd2(buf, sizeof buf);
1895#else
1896 res = getcwd(buf, sizeof buf);
1897#endif
1898 Py_END_ALLOW_THREADS
1899 if (res == NULL)
1900 return posix_error();
1901 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1902}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001903#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001904#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001905
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001906
Guido van Rossumb6775db1994-08-01 11:34:53 +00001907#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001908PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001909"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001910Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001911
Barry Warsaw53699e91996-12-10 23:23:01 +00001912static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001913posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001914{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00001915 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001916}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001917#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001918
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001919
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001920PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001921"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001922Return a list containing the names of the entries in the directory.\n\
1923\n\
1924 path: path of directory to list\n\
1925\n\
1926The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001927entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001928
Barry Warsaw53699e91996-12-10 23:23:01 +00001929static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001930posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001931{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001932 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001933 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001934#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001935
Barry Warsaw53699e91996-12-10 23:23:01 +00001936 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001937 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001938 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001939 WIN32_FIND_DATA FileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001940 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00001941 char *bufptr = namebuf;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001942 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001943
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001944#ifdef Py_WIN_WIDE_FILENAMES
1945 /* If on wide-character-capable OS see if argument
1946 is Unicode and if so use wide API. */
1947 if (unicode_file_names()) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001948 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001949 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1950 WIN32_FIND_DATAW wFileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001951 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001952 Py_UNICODE wch;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001953 /* Overallocate for \\*.*\0 */
1954 len = PyUnicode_GET_SIZE(po);
1955 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
1956 if (!wnamebuf) {
1957 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001958 return NULL;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001959 }
1960 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
1961 wch = len > 0 ? wnamebuf[len-1] : '\0';
1962 if (wch != L'/' && wch != L'\\' && wch != L':')
1963 wnamebuf[len++] = L'\\';
1964 wcscpy(wnamebuf + len, L"*.*");
1965 if ((d = PyList_New(0)) == NULL) {
1966 free(wnamebuf);
1967 return NULL;
1968 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001969 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1970 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001971 int error = GetLastError();
1972 if (error == ERROR_FILE_NOT_FOUND) {
1973 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001974 return d;
1975 }
1976 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001977 win32_error_unicode("FindFirstFileW", wnamebuf);
1978 free(wnamebuf);
1979 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001980 }
1981 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001982 /* Skip over . and .. */
1983 if (wcscmp(wFileData.cFileName, L".") != 0 &&
1984 wcscmp(wFileData.cFileName, L"..") != 0) {
1985 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1986 if (v == NULL) {
1987 Py_DECREF(d);
1988 d = NULL;
1989 break;
1990 }
1991 if (PyList_Append(d, v) != 0) {
1992 Py_DECREF(v);
1993 Py_DECREF(d);
1994 d = NULL;
1995 break;
1996 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001997 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001998 }
Georg Brandl622927b2006-03-07 12:48:03 +00001999 Py_BEGIN_ALLOW_THREADS
2000 result = FindNextFileW(hFindFile, &wFileData);
2001 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00002002 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2003 it got to the end of the directory. */
2004 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2005 Py_DECREF(d);
2006 win32_error_unicode("FindNextFileW", wnamebuf);
2007 FindClose(hFindFile);
2008 free(wnamebuf);
2009 return NULL;
2010 }
Georg Brandl622927b2006-03-07 12:48:03 +00002011 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002012
2013 if (FindClose(hFindFile) == FALSE) {
2014 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002015 win32_error_unicode("FindClose", wnamebuf);
2016 free(wnamebuf);
2017 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002018 }
Martin v. Löwise3edaea2006-05-15 05:51:36 +00002019 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002020 return d;
2021 }
2022 /* Drop the argument parsing error as narrow strings
2023 are also valid. */
2024 PyErr_Clear();
2025 }
2026#endif
2027
Tim Peters5aa91602002-01-30 05:46:57 +00002028 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002029 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002030 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002031 if (len > 0) {
2032 char ch = namebuf[len-1];
2033 if (ch != SEP && ch != ALTSEP && ch != ':')
2034 namebuf[len++] = '/';
2035 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002036 strcpy(namebuf + len, "*.*");
2037
Barry Warsaw53699e91996-12-10 23:23:01 +00002038 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002039 return NULL;
2040
2041 hFindFile = FindFirstFile(namebuf, &FileData);
2042 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002043 int error = GetLastError();
2044 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002045 return d;
2046 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002047 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002048 }
2049 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002050 /* Skip over . and .. */
2051 if (strcmp(FileData.cFileName, ".") != 0 &&
2052 strcmp(FileData.cFileName, "..") != 0) {
2053 v = PyString_FromString(FileData.cFileName);
2054 if (v == NULL) {
2055 Py_DECREF(d);
2056 d = NULL;
2057 break;
2058 }
2059 if (PyList_Append(d, v) != 0) {
2060 Py_DECREF(v);
2061 Py_DECREF(d);
2062 d = NULL;
2063 break;
2064 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002065 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002066 }
Georg Brandl622927b2006-03-07 12:48:03 +00002067 Py_BEGIN_ALLOW_THREADS
2068 result = FindNextFile(hFindFile, &FileData);
2069 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00002070 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2071 it got to the end of the directory. */
2072 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2073 Py_DECREF(d);
2074 win32_error("FindNextFile", namebuf);
2075 FindClose(hFindFile);
2076 return NULL;
2077 }
Georg Brandl622927b2006-03-07 12:48:03 +00002078 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002079
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002080 if (FindClose(hFindFile) == FALSE) {
2081 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002082 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002083 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002084
2085 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002086
Tim Peters0bb44a42000-09-15 07:44:49 +00002087#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002088
2089#ifndef MAX_PATH
2090#define MAX_PATH CCHMAXPATH
2091#endif
2092 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002093 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002094 PyObject *d, *v;
2095 char namebuf[MAX_PATH+5];
2096 HDIR hdir = 1;
2097 ULONG srchcnt = 1;
2098 FILEFINDBUF3 ep;
2099 APIRET rc;
2100
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002101 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002102 return NULL;
2103 if (len >= MAX_PATH) {
2104 PyErr_SetString(PyExc_ValueError, "path too long");
2105 return NULL;
2106 }
2107 strcpy(namebuf, name);
2108 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002109 if (*pt == ALTSEP)
2110 *pt = SEP;
2111 if (namebuf[len-1] != SEP)
2112 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002113 strcpy(namebuf + len, "*.*");
2114
2115 if ((d = PyList_New(0)) == NULL)
2116 return NULL;
2117
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002118 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2119 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002120 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002121 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2122 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2123 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002124
2125 if (rc != NO_ERROR) {
2126 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002127 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002128 }
2129
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002130 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002131 do {
2132 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002133 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002134 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002135
2136 strcpy(namebuf, ep.achName);
2137
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002138 /* Leave Case of Name Alone -- In Native Form */
2139 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002140
2141 v = PyString_FromString(namebuf);
2142 if (v == NULL) {
2143 Py_DECREF(d);
2144 d = NULL;
2145 break;
2146 }
2147 if (PyList_Append(d, v) != 0) {
2148 Py_DECREF(v);
2149 Py_DECREF(d);
2150 d = NULL;
2151 break;
2152 }
2153 Py_DECREF(v);
2154 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2155 }
2156
2157 return d;
2158#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002159
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002160 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002161 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002162 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002163 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002164 int arg_is_unicode = 1;
2165
Georg Brandl05e89b82006-04-11 07:04:06 +00002166 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002167 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2168 arg_is_unicode = 0;
2169 PyErr_Clear();
2170 }
2171 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002172 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002173 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002174 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002175 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002176 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002177 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002178 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002179 return NULL;
2180 }
Georg Brandl622927b2006-03-07 12:48:03 +00002181 for (;;) {
2182 Py_BEGIN_ALLOW_THREADS
2183 ep = readdir(dirp);
2184 Py_END_ALLOW_THREADS
2185 if (ep == NULL)
2186 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002187 if (ep->d_name[0] == '.' &&
2188 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002189 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002190 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00002191 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002192 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002193 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002194 d = NULL;
2195 break;
2196 }
Just van Rossum46c97842003-02-25 21:42:15 +00002197#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00002198 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002199 PyObject *w;
2200
2201 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002202 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002203 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002204 if (w != NULL) {
2205 Py_DECREF(v);
2206 v = w;
2207 }
2208 else {
2209 /* fall back to the original byte string, as
2210 discussed in patch #683592 */
2211 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002212 }
Just van Rossum46c97842003-02-25 21:42:15 +00002213 }
2214#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002215 if (PyList_Append(d, v) != 0) {
2216 Py_DECREF(v);
2217 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002218 d = NULL;
2219 break;
2220 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002221 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002222 }
Georg Brandlbbfe4fa2006-04-11 06:47:43 +00002223 if (errno != 0 && d != NULL) {
2224 /* readdir() returned NULL and set errno */
2225 closedir(dirp);
2226 Py_DECREF(d);
2227 return posix_error_with_allocated_filename(name);
2228 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002229 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002230 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002231
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002232 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002233
Tim Peters0bb44a42000-09-15 07:44:49 +00002234#endif /* which OS */
2235} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002236
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002237#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002238/* A helper function for abspath on win32 */
2239static PyObject *
2240posix__getfullpathname(PyObject *self, PyObject *args)
2241{
2242 /* assume encoded strings wont more than double no of chars */
2243 char inbuf[MAX_PATH*2];
2244 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002245 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002246 char outbuf[MAX_PATH*2];
2247 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002248#ifdef Py_WIN_WIDE_FILENAMES
2249 if (unicode_file_names()) {
2250 PyUnicodeObject *po;
2251 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2252 Py_UNICODE woutbuf[MAX_PATH*2];
2253 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002254 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002255 sizeof(woutbuf)/sizeof(woutbuf[0]),
2256 woutbuf, &wtemp))
2257 return win32_error("GetFullPathName", "");
2258 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2259 }
2260 /* Drop the argument parsing error as narrow strings
2261 are also valid. */
2262 PyErr_Clear();
2263 }
2264#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002265 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2266 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002267 &insize))
2268 return NULL;
2269 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2270 outbuf, &temp))
2271 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002272 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2273 return PyUnicode_Decode(outbuf, strlen(outbuf),
2274 Py_FileSystemDefaultEncoding, NULL);
2275 }
Mark Hammondef8b6542001-05-13 08:04:26 +00002276 return PyString_FromString(outbuf);
2277} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002278#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002279
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002280PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002281"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002282Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002283
Barry Warsaw53699e91996-12-10 23:23:01 +00002284static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002285posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002286{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002287 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002288 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002289 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002290
2291#ifdef Py_WIN_WIDE_FILENAMES
2292 if (unicode_file_names()) {
2293 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002294 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002295 Py_BEGIN_ALLOW_THREADS
2296 /* PyUnicode_AS_UNICODE OK without thread lock as
2297 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002298 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002299 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002300 if (!res)
2301 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002302 Py_INCREF(Py_None);
2303 return Py_None;
2304 }
2305 /* Drop the argument parsing error as narrow strings
2306 are also valid. */
2307 PyErr_Clear();
2308 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002309 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2310 Py_FileSystemDefaultEncoding, &path, &mode))
2311 return NULL;
2312 Py_BEGIN_ALLOW_THREADS
2313 /* PyUnicode_AS_UNICODE OK without thread lock as
2314 it is a simple dereference. */
2315 res = CreateDirectoryA(path, NULL);
2316 Py_END_ALLOW_THREADS
2317 if (!res) {
2318 win32_error("mkdir", path);
2319 PyMem_Free(path);
2320 return NULL;
2321 }
2322 PyMem_Free(path);
2323 Py_INCREF(Py_None);
2324 return Py_None;
2325#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002326
Tim Peters5aa91602002-01-30 05:46:57 +00002327 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002328 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002329 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002330 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002331#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002332 res = mkdir(path);
2333#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002334 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002335#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002336 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002337 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002338 return posix_error_with_allocated_filename(path);
2339 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002340 Py_INCREF(Py_None);
2341 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002342#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002343}
2344
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002345
Neal Norwitz1818ed72006-03-26 00:29:48 +00002346/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2347#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002348#include <sys/resource.h>
2349#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002350
Neal Norwitz1818ed72006-03-26 00:29:48 +00002351
2352#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002353PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002354"nice(inc) -> new_priority\n\n\
2355Decrease the priority of process by inc and return the new priority.");
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_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002359{
2360 int increment, value;
2361
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002362 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002363 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002364
2365 /* There are two flavours of 'nice': one that returns the new
2366 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002367 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2368 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002369
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002370 If we are of the nice family that returns the new priority, we
2371 need to clear errno before the call, and check if errno is filled
2372 before calling posix_error() on a returnvalue of -1, because the
2373 -1 may be the actual new priority! */
2374
2375 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002376 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002377#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002378 if (value == 0)
2379 value = getpriority(PRIO_PROCESS, 0);
2380#endif
2381 if (value == -1 && errno != 0)
2382 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002383 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002384 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002385}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002386#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002387
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002388PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002389"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002390Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002391
Barry Warsaw53699e91996-12-10 23:23:01 +00002392static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002393posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002394{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002395#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002396 PyObject *o1, *o2;
2397 char *p1, *p2;
2398 BOOL result;
2399 if (unicode_file_names()) {
2400 if (!PyArg_ParseTuple(args, "O&O&:rename",
2401 convert_to_unicode, &o1,
2402 convert_to_unicode, &o2))
2403 PyErr_Clear();
2404 else {
2405 Py_BEGIN_ALLOW_THREADS
2406 result = MoveFileW(PyUnicode_AsUnicode(o1),
2407 PyUnicode_AsUnicode(o2));
2408 Py_END_ALLOW_THREADS
2409 Py_DECREF(o1);
2410 Py_DECREF(o2);
2411 if (!result)
2412 return win32_error("rename", NULL);
2413 Py_INCREF(Py_None);
2414 return Py_None;
2415 }
2416 }
2417 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2418 return NULL;
2419 Py_BEGIN_ALLOW_THREADS
2420 result = MoveFileA(p1, p2);
2421 Py_END_ALLOW_THREADS
2422 if (!result)
2423 return win32_error("rename", NULL);
2424 Py_INCREF(Py_None);
2425 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002426#else
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00002427 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002428#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002429}
2430
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002431
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002432PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002433"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002434Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002435
Barry Warsaw53699e91996-12-10 23:23:01 +00002436static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002437posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002438{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002439#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002440 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002441#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002442 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002443#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002444}
2445
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002446
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002447PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002448"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002449Perform a stat system call on the given path.");
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_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002453{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002454#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002455 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002456#else
2457 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2458#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002459}
2460
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002461
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002462#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002463PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002464"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002465Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002466
Barry Warsaw53699e91996-12-10 23:23:01 +00002467static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002468posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002469{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002470 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002471 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002472 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002473 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002474 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002475 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002476 Py_END_ALLOW_THREADS
2477 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002478}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002479#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002480
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002482PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002483"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002484Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002485
Barry Warsaw53699e91996-12-10 23:23:01 +00002486static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002487posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002488{
2489 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002490 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002491 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002492 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002493 if (i < 0)
2494 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002495 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002496}
2497
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002499PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002500"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002501Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002502
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002503PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002504"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002505Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002506
Barry Warsaw53699e91996-12-10 23:23:01 +00002507static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002508posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002509{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002510#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002511 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002512#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002513 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002514#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002515}
2516
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002517
Guido van Rossumb6775db1994-08-01 11:34:53 +00002518#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002519PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002520"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002521Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002522
Barry Warsaw53699e91996-12-10 23:23:01 +00002523static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002524posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002525{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002526 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002527 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002528
Barry Warsaw53699e91996-12-10 23:23:01 +00002529 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002530 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002531 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002532 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002533 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002534 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002535 u.sysname,
2536 u.nodename,
2537 u.release,
2538 u.version,
2539 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002540}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002541#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002542
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002543static int
2544extract_time(PyObject *t, long* sec, long* usec)
2545{
2546 long intval;
2547 if (PyFloat_Check(t)) {
2548 double tval = PyFloat_AsDouble(t);
2549 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
2550 if (!intobj)
2551 return -1;
2552 intval = PyInt_AsLong(intobj);
2553 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002554 if (intval == -1 && PyErr_Occurred())
2555 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002556 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002557 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002558 if (*usec < 0)
2559 /* If rounding gave us a negative number,
2560 truncate. */
2561 *usec = 0;
2562 return 0;
2563 }
2564 intval = PyInt_AsLong(t);
2565 if (intval == -1 && PyErr_Occurred())
2566 return -1;
2567 *sec = intval;
2568 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002569 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002570}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002571
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002572PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002573"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002574utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002575Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002576second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002577
Barry Warsaw53699e91996-12-10 23:23:01 +00002578static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002579posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002580{
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002581#ifdef Py_WIN_WIDE_FILENAMES
2582 PyObject *arg;
2583 PyUnicodeObject *obwpath;
2584 wchar_t *wpath = NULL;
2585 char *apath = NULL;
2586 HANDLE hFile;
2587 long atimesec, mtimesec, ausec, musec;
2588 FILETIME atime, mtime;
2589 PyObject *result = NULL;
2590
2591 if (unicode_file_names()) {
2592 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2593 wpath = PyUnicode_AS_UNICODE(obwpath);
2594 Py_BEGIN_ALLOW_THREADS
2595 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2596 NULL, OPEN_EXISTING, 0, NULL);
2597 Py_END_ALLOW_THREADS
2598 if (hFile == INVALID_HANDLE_VALUE)
2599 return win32_error_unicode("utime", wpath);
2600 } else
2601 /* Drop the argument parsing error as narrow strings
2602 are also valid. */
2603 PyErr_Clear();
2604 }
2605 if (!wpath) {
2606 if (!PyArg_ParseTuple(args, "etO:utime",
2607 Py_FileSystemDefaultEncoding, &apath, &arg))
2608 return NULL;
2609 Py_BEGIN_ALLOW_THREADS
2610 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2611 NULL, OPEN_EXISTING, 0, NULL);
2612 Py_END_ALLOW_THREADS
2613 if (hFile == INVALID_HANDLE_VALUE) {
2614 win32_error("utime", apath);
2615 PyMem_Free(apath);
2616 return NULL;
2617 }
2618 PyMem_Free(apath);
2619 }
2620
2621 if (arg == Py_None) {
2622 SYSTEMTIME now;
2623 GetSystemTime(&now);
2624 if (!SystemTimeToFileTime(&now, &mtime) ||
2625 !SystemTimeToFileTime(&now, &atime)) {
2626 win32_error("utime", NULL);
2627 goto done;
2628 }
2629 }
2630 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2631 PyErr_SetString(PyExc_TypeError,
2632 "utime() arg 2 must be a tuple (atime, mtime)");
2633 goto done;
2634 }
2635 else {
2636 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2637 &atimesec, &ausec) == -1)
2638 goto done;
Martin v. Löwis463a42b2006-10-09 20:44:50 +00002639 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002640 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2641 &mtimesec, &musec) == -1)
2642 goto done;
Martin v. Löwis463a42b2006-10-09 20:44:50 +00002643 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002644 }
2645 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2646 /* Avoid putting the file name into the error here,
2647 as that may confuse the user into believing that
2648 something is wrong with the file, when it also
2649 could be the time stamp that gives a problem. */
2650 win32_error("utime", NULL);
2651 }
2652 Py_INCREF(Py_None);
2653 result = Py_None;
2654done:
2655 CloseHandle(hFile);
2656 return result;
2657#else /* Py_WIN_WIDE_FILENAMES */
2658
Neal Norwitz2adf2102004-06-09 01:46:02 +00002659 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002660 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002661 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002662 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002663
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002664#if defined(HAVE_UTIMES)
2665 struct timeval buf[2];
2666#define ATIME buf[0].tv_sec
2667#define MTIME buf[1].tv_sec
2668#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002669/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002670 struct utimbuf buf;
2671#define ATIME buf.actime
2672#define MTIME buf.modtime
2673#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002674#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002675 time_t buf[2];
2676#define ATIME buf[0]
2677#define MTIME buf[1]
2678#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002679#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002680
Mark Hammond817c9292003-12-03 01:22:38 +00002681
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002682 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002683 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002684 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002685 if (arg == Py_None) {
2686 /* optional time values not given */
2687 Py_BEGIN_ALLOW_THREADS
2688 res = utime(path, NULL);
2689 Py_END_ALLOW_THREADS
2690 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002691 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002692 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002693 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002694 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002695 return NULL;
2696 }
2697 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002698 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002699 &atime, &ausec) == -1) {
2700 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002701 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002702 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002703 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002704 &mtime, &musec) == -1) {
2705 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002706 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002707 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002708 ATIME = atime;
2709 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002710#ifdef HAVE_UTIMES
2711 buf[0].tv_usec = ausec;
2712 buf[1].tv_usec = musec;
2713 Py_BEGIN_ALLOW_THREADS
2714 res = utimes(path, buf);
2715 Py_END_ALLOW_THREADS
2716#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002717 Py_BEGIN_ALLOW_THREADS
2718 res = utime(path, UTIME_ARG);
2719 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002720#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002721 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002722 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002723 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002724 }
Neal Norwitz96652712004-06-06 20:40:27 +00002725 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002726 Py_INCREF(Py_None);
2727 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002728#undef UTIME_ARG
2729#undef ATIME
2730#undef MTIME
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002731#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002732}
2733
Guido van Rossum85e3b011991-06-03 12:42:10 +00002734
Guido van Rossum3b066191991-06-04 19:40:25 +00002735/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002736
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002737PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002738"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002739Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002740
Barry Warsaw53699e91996-12-10 23:23:01 +00002741static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002742posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002743{
2744 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002745 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002746 return NULL;
2747 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002748 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002749}
2750
Martin v. Löwis114619e2002-10-07 06:44:21 +00002751#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2752static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002753free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002754{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002755 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002756 for (i = 0; i < count; i++)
2757 PyMem_Free(array[i]);
2758 PyMem_DEL(array);
2759}
2760#endif
2761
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002762
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002763#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002764PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002765"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002766Execute an executable path with arguments, replacing current process.\n\
2767\n\
2768 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002769 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002770
Barry Warsaw53699e91996-12-10 23:23:01 +00002771static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002772posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002773{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002774 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002775 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002776 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002777 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002778 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002779
Guido van Rossum89b33251993-10-22 14:26:06 +00002780 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002781 argv is a list or tuple of strings. */
2782
Martin v. Löwis114619e2002-10-07 06:44:21 +00002783 if (!PyArg_ParseTuple(args, "etO:execv",
2784 Py_FileSystemDefaultEncoding,
2785 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002786 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002787 if (PyList_Check(argv)) {
2788 argc = PyList_Size(argv);
2789 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002790 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002791 else if (PyTuple_Check(argv)) {
2792 argc = PyTuple_Size(argv);
2793 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002794 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002795 else {
Fred Drake661ea262000-10-24 19:57:45 +00002796 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002797 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002798 return NULL;
2799 }
2800
Barry Warsaw53699e91996-12-10 23:23:01 +00002801 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002802 if (argvlist == NULL) {
2803 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002804 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002805 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002806 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002807 if (!PyArg_Parse((*getitem)(argv, i), "et",
2808 Py_FileSystemDefaultEncoding,
2809 &argvlist[i])) {
2810 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002811 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002812 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002813 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002814 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002815
Guido van Rossum85e3b011991-06-03 12:42:10 +00002816 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002817 }
2818 argvlist[argc] = NULL;
2819
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002820 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002821
Guido van Rossum85e3b011991-06-03 12:42:10 +00002822 /* If we get here it's definitely an error */
2823
Martin v. Löwis114619e2002-10-07 06:44:21 +00002824 free_string_array(argvlist, argc);
2825 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002826 return posix_error();
2827}
2828
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002829
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002830PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002831"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002832Execute a path with arguments and environment, replacing current process.\n\
2833\n\
2834 path: path of executable file\n\
2835 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002836 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002837
Barry Warsaw53699e91996-12-10 23:23:01 +00002838static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002839posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002840{
2841 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002842 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002843 char **argvlist;
2844 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002845 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002846 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002847 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00002848 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002849
2850 /* execve has three arguments: (path, argv, env), where
2851 argv is a list or tuple of strings and env is a dictionary
2852 like posix.environ. */
2853
Martin v. Löwis114619e2002-10-07 06:44:21 +00002854 if (!PyArg_ParseTuple(args, "etOO:execve",
2855 Py_FileSystemDefaultEncoding,
2856 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002857 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002858 if (PyList_Check(argv)) {
2859 argc = PyList_Size(argv);
2860 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002861 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002862 else if (PyTuple_Check(argv)) {
2863 argc = PyTuple_Size(argv);
2864 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002865 }
2866 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002867 PyErr_SetString(PyExc_TypeError,
2868 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002869 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002870 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002871 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002872 PyErr_SetString(PyExc_TypeError,
2873 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002874 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002875 }
2876
Barry Warsaw53699e91996-12-10 23:23:01 +00002877 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002878 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002879 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002880 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002881 }
2882 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002883 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002884 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002885 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002886 &argvlist[i]))
2887 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002888 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002889 goto fail_1;
2890 }
2891 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002892 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002893 argvlist[argc] = NULL;
2894
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002895 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002896 if (i < 0)
2897 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002898 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002899 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002900 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002901 goto fail_1;
2902 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002903 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002904 keys = PyMapping_Keys(env);
2905 vals = PyMapping_Values(env);
2906 if (!keys || !vals)
2907 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002908 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2909 PyErr_SetString(PyExc_TypeError,
2910 "execve(): env.keys() or env.values() is not a list");
2911 goto fail_2;
2912 }
Tim Peters5aa91602002-01-30 05:46:57 +00002913
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002914 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002915 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002916 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002917
2918 key = PyList_GetItem(keys, pos);
2919 val = PyList_GetItem(vals, pos);
2920 if (!key || !val)
2921 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002922
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002923 if (!PyArg_Parse(
2924 key,
2925 "s;execve() arg 3 contains a non-string key",
2926 &k) ||
2927 !PyArg_Parse(
2928 val,
2929 "s;execve() arg 3 contains a non-string value",
2930 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002931 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002932 goto fail_2;
2933 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002934
2935#if defined(PYOS_OS2)
2936 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2937 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2938#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002939 len = PyString_Size(key) + PyString_Size(val) + 2;
2940 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002941 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002942 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002943 goto fail_2;
2944 }
Tim Petersc8996f52001-12-03 20:41:00 +00002945 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002946 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002947#if defined(PYOS_OS2)
2948 }
2949#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002950 }
2951 envlist[envc] = 0;
2952
2953 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002954
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002955 /* If we get here it's definitely an error */
2956
2957 (void) posix_error();
2958
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002959 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002960 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002961 PyMem_DEL(envlist[envc]);
2962 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002963 fail_1:
2964 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002965 Py_XDECREF(vals);
2966 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002967 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002968 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002969 return NULL;
2970}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002971#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002972
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002973
Guido van Rossuma1065681999-01-25 23:20:23 +00002974#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002975PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002976"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002977Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002978\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002979 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002980 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002981 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002982
2983static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002984posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002985{
2986 char *path;
2987 PyObject *argv;
2988 char **argvlist;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00002989 int mode, i;
2990 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002991 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002992 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00002993
2994 /* spawnv has three arguments: (mode, path, argv), where
2995 argv is a list or tuple of strings. */
2996
Martin v. Löwis114619e2002-10-07 06:44:21 +00002997 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2998 Py_FileSystemDefaultEncoding,
2999 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003000 return NULL;
3001 if (PyList_Check(argv)) {
3002 argc = PyList_Size(argv);
3003 getitem = PyList_GetItem;
3004 }
3005 else if (PyTuple_Check(argv)) {
3006 argc = PyTuple_Size(argv);
3007 getitem = PyTuple_GetItem;
3008 }
3009 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003010 PyErr_SetString(PyExc_TypeError,
3011 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003012 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003013 return NULL;
3014 }
3015
3016 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003017 if (argvlist == NULL) {
3018 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003019 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003020 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003021 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003022 if (!PyArg_Parse((*getitem)(argv, i), "et",
3023 Py_FileSystemDefaultEncoding,
3024 &argvlist[i])) {
3025 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003026 PyErr_SetString(
3027 PyExc_TypeError,
3028 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003029 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003030 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003031 }
3032 }
3033 argvlist[argc] = NULL;
3034
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003035#if defined(PYOS_OS2) && defined(PYCC_GCC)
3036 Py_BEGIN_ALLOW_THREADS
3037 spawnval = spawnv(mode, path, argvlist);
3038 Py_END_ALLOW_THREADS
3039#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003040 if (mode == _OLD_P_OVERLAY)
3041 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003042
Tim Peters25059d32001-12-07 20:35:43 +00003043 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003044 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003045 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003046#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003047
Martin v. Löwis114619e2002-10-07 06:44:21 +00003048 free_string_array(argvlist, argc);
3049 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003050
Fred Drake699f3522000-06-29 21:12:41 +00003051 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003052 return posix_error();
3053 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003054#if SIZEOF_LONG == SIZEOF_VOID_P
3055 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003056#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003057 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003058#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003059}
3060
3061
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003062PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003063"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003064Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003065\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003066 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003067 path: path of executable file\n\
3068 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003069 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003070
3071static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003072posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003073{
3074 char *path;
3075 PyObject *argv, *env;
3076 char **argvlist;
3077 char **envlist;
3078 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003079 int mode, pos, envc;
3080 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003081 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003082 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003083 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003084
3085 /* spawnve has four arguments: (mode, path, argv, env), where
3086 argv is a list or tuple of strings and env is a dictionary
3087 like posix.environ. */
3088
Martin v. Löwis114619e2002-10-07 06:44:21 +00003089 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3090 Py_FileSystemDefaultEncoding,
3091 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003092 return NULL;
3093 if (PyList_Check(argv)) {
3094 argc = PyList_Size(argv);
3095 getitem = PyList_GetItem;
3096 }
3097 else if (PyTuple_Check(argv)) {
3098 argc = PyTuple_Size(argv);
3099 getitem = PyTuple_GetItem;
3100 }
3101 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003102 PyErr_SetString(PyExc_TypeError,
3103 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003104 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003105 }
3106 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003107 PyErr_SetString(PyExc_TypeError,
3108 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003109 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003110 }
3111
3112 argvlist = PyMem_NEW(char *, argc+1);
3113 if (argvlist == NULL) {
3114 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003115 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003116 }
3117 for (i = 0; i < argc; i++) {
3118 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003119 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003120 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003121 &argvlist[i]))
3122 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003123 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003124 goto fail_1;
3125 }
3126 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003127 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003128 argvlist[argc] = NULL;
3129
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003130 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003131 if (i < 0)
3132 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003133 envlist = PyMem_NEW(char *, i + 1);
3134 if (envlist == NULL) {
3135 PyErr_NoMemory();
3136 goto fail_1;
3137 }
3138 envc = 0;
3139 keys = PyMapping_Keys(env);
3140 vals = PyMapping_Values(env);
3141 if (!keys || !vals)
3142 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003143 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3144 PyErr_SetString(PyExc_TypeError,
3145 "spawnve(): env.keys() or env.values() is not a list");
3146 goto fail_2;
3147 }
Tim Peters5aa91602002-01-30 05:46:57 +00003148
Guido van Rossuma1065681999-01-25 23:20:23 +00003149 for (pos = 0; pos < i; pos++) {
3150 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003151 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003152
3153 key = PyList_GetItem(keys, pos);
3154 val = PyList_GetItem(vals, pos);
3155 if (!key || !val)
3156 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003157
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003158 if (!PyArg_Parse(
3159 key,
3160 "s;spawnve() arg 3 contains a non-string key",
3161 &k) ||
3162 !PyArg_Parse(
3163 val,
3164 "s;spawnve() arg 3 contains a non-string value",
3165 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003166 {
3167 goto fail_2;
3168 }
Tim Petersc8996f52001-12-03 20:41:00 +00003169 len = PyString_Size(key) + PyString_Size(val) + 2;
3170 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003171 if (p == NULL) {
3172 PyErr_NoMemory();
3173 goto fail_2;
3174 }
Tim Petersc8996f52001-12-03 20:41:00 +00003175 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003176 envlist[envc++] = p;
3177 }
3178 envlist[envc] = 0;
3179
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003180#if defined(PYOS_OS2) && defined(PYCC_GCC)
3181 Py_BEGIN_ALLOW_THREADS
3182 spawnval = spawnve(mode, path, argvlist, envlist);
3183 Py_END_ALLOW_THREADS
3184#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003185 if (mode == _OLD_P_OVERLAY)
3186 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003187
3188 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003189 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003190 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003191#endif
Tim Peters25059d32001-12-07 20:35:43 +00003192
Fred Drake699f3522000-06-29 21:12:41 +00003193 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003194 (void) posix_error();
3195 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003196#if SIZEOF_LONG == SIZEOF_VOID_P
3197 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003198#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003199 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003200#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003201
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003202 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003203 while (--envc >= 0)
3204 PyMem_DEL(envlist[envc]);
3205 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003206 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003207 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003208 Py_XDECREF(vals);
3209 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003210 fail_0:
3211 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003212 return res;
3213}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003214
3215/* OS/2 supports spawnvp & spawnvpe natively */
3216#if defined(PYOS_OS2)
3217PyDoc_STRVAR(posix_spawnvp__doc__,
3218"spawnvp(mode, file, args)\n\n\
3219Execute the program 'file' in a new process, using the environment\n\
3220search path to find the file.\n\
3221\n\
3222 mode: mode of process creation\n\
3223 file: executable file name\n\
3224 args: tuple or list of strings");
3225
3226static PyObject *
3227posix_spawnvp(PyObject *self, PyObject *args)
3228{
3229 char *path;
3230 PyObject *argv;
3231 char **argvlist;
3232 int mode, i, argc;
3233 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003234 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003235
3236 /* spawnvp has three arguments: (mode, path, argv), where
3237 argv is a list or tuple of strings. */
3238
3239 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3240 Py_FileSystemDefaultEncoding,
3241 &path, &argv))
3242 return NULL;
3243 if (PyList_Check(argv)) {
3244 argc = PyList_Size(argv);
3245 getitem = PyList_GetItem;
3246 }
3247 else if (PyTuple_Check(argv)) {
3248 argc = PyTuple_Size(argv);
3249 getitem = PyTuple_GetItem;
3250 }
3251 else {
3252 PyErr_SetString(PyExc_TypeError,
3253 "spawnvp() arg 2 must be a tuple or list");
3254 PyMem_Free(path);
3255 return NULL;
3256 }
3257
3258 argvlist = PyMem_NEW(char *, argc+1);
3259 if (argvlist == NULL) {
3260 PyMem_Free(path);
3261 return PyErr_NoMemory();
3262 }
3263 for (i = 0; i < argc; i++) {
3264 if (!PyArg_Parse((*getitem)(argv, i), "et",
3265 Py_FileSystemDefaultEncoding,
3266 &argvlist[i])) {
3267 free_string_array(argvlist, i);
3268 PyErr_SetString(
3269 PyExc_TypeError,
3270 "spawnvp() arg 2 must contain only strings");
3271 PyMem_Free(path);
3272 return NULL;
3273 }
3274 }
3275 argvlist[argc] = NULL;
3276
3277 Py_BEGIN_ALLOW_THREADS
3278#if defined(PYCC_GCC)
3279 spawnval = spawnvp(mode, path, argvlist);
3280#else
3281 spawnval = _spawnvp(mode, path, argvlist);
3282#endif
3283 Py_END_ALLOW_THREADS
3284
3285 free_string_array(argvlist, argc);
3286 PyMem_Free(path);
3287
3288 if (spawnval == -1)
3289 return posix_error();
3290 else
3291 return Py_BuildValue("l", (long) spawnval);
3292}
3293
3294
3295PyDoc_STRVAR(posix_spawnvpe__doc__,
3296"spawnvpe(mode, file, args, env)\n\n\
3297Execute the program 'file' in a new process, using the environment\n\
3298search path to find the file.\n\
3299\n\
3300 mode: mode of process creation\n\
3301 file: executable file name\n\
3302 args: tuple or list of arguments\n\
3303 env: dictionary of strings mapping to strings");
3304
3305static PyObject *
3306posix_spawnvpe(PyObject *self, PyObject *args)
3307{
3308 char *path;
3309 PyObject *argv, *env;
3310 char **argvlist;
3311 char **envlist;
3312 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3313 int mode, i, pos, argc, envc;
3314 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003315 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003316 int lastarg = 0;
3317
3318 /* spawnvpe has four arguments: (mode, path, argv, env), where
3319 argv is a list or tuple of strings and env is a dictionary
3320 like posix.environ. */
3321
3322 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3323 Py_FileSystemDefaultEncoding,
3324 &path, &argv, &env))
3325 return NULL;
3326 if (PyList_Check(argv)) {
3327 argc = PyList_Size(argv);
3328 getitem = PyList_GetItem;
3329 }
3330 else if (PyTuple_Check(argv)) {
3331 argc = PyTuple_Size(argv);
3332 getitem = PyTuple_GetItem;
3333 }
3334 else {
3335 PyErr_SetString(PyExc_TypeError,
3336 "spawnvpe() arg 2 must be a tuple or list");
3337 goto fail_0;
3338 }
3339 if (!PyMapping_Check(env)) {
3340 PyErr_SetString(PyExc_TypeError,
3341 "spawnvpe() arg 3 must be a mapping object");
3342 goto fail_0;
3343 }
3344
3345 argvlist = PyMem_NEW(char *, argc+1);
3346 if (argvlist == NULL) {
3347 PyErr_NoMemory();
3348 goto fail_0;
3349 }
3350 for (i = 0; i < argc; i++) {
3351 if (!PyArg_Parse((*getitem)(argv, i),
3352 "et;spawnvpe() arg 2 must contain only strings",
3353 Py_FileSystemDefaultEncoding,
3354 &argvlist[i]))
3355 {
3356 lastarg = i;
3357 goto fail_1;
3358 }
3359 }
3360 lastarg = argc;
3361 argvlist[argc] = NULL;
3362
3363 i = PyMapping_Size(env);
3364 if (i < 0)
3365 goto fail_1;
3366 envlist = PyMem_NEW(char *, i + 1);
3367 if (envlist == NULL) {
3368 PyErr_NoMemory();
3369 goto fail_1;
3370 }
3371 envc = 0;
3372 keys = PyMapping_Keys(env);
3373 vals = PyMapping_Values(env);
3374 if (!keys || !vals)
3375 goto fail_2;
3376 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3377 PyErr_SetString(PyExc_TypeError,
3378 "spawnvpe(): env.keys() or env.values() is not a list");
3379 goto fail_2;
3380 }
3381
3382 for (pos = 0; pos < i; pos++) {
3383 char *p, *k, *v;
3384 size_t len;
3385
3386 key = PyList_GetItem(keys, pos);
3387 val = PyList_GetItem(vals, pos);
3388 if (!key || !val)
3389 goto fail_2;
3390
3391 if (!PyArg_Parse(
3392 key,
3393 "s;spawnvpe() arg 3 contains a non-string key",
3394 &k) ||
3395 !PyArg_Parse(
3396 val,
3397 "s;spawnvpe() arg 3 contains a non-string value",
3398 &v))
3399 {
3400 goto fail_2;
3401 }
3402 len = PyString_Size(key) + PyString_Size(val) + 2;
3403 p = PyMem_NEW(char, len);
3404 if (p == NULL) {
3405 PyErr_NoMemory();
3406 goto fail_2;
3407 }
3408 PyOS_snprintf(p, len, "%s=%s", k, v);
3409 envlist[envc++] = p;
3410 }
3411 envlist[envc] = 0;
3412
3413 Py_BEGIN_ALLOW_THREADS
3414#if defined(PYCC_GCC)
3415 spawnval = spawnve(mode, path, argvlist, envlist);
3416#else
3417 spawnval = _spawnve(mode, path, argvlist, envlist);
3418#endif
3419 Py_END_ALLOW_THREADS
3420
3421 if (spawnval == -1)
3422 (void) posix_error();
3423 else
3424 res = Py_BuildValue("l", (long) spawnval);
3425
3426 fail_2:
3427 while (--envc >= 0)
3428 PyMem_DEL(envlist[envc]);
3429 PyMem_DEL(envlist);
3430 fail_1:
3431 free_string_array(argvlist, lastarg);
3432 Py_XDECREF(vals);
3433 Py_XDECREF(keys);
3434 fail_0:
3435 PyMem_Free(path);
3436 return res;
3437}
3438#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003439#endif /* HAVE_SPAWNV */
3440
3441
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003442#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003443PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003444"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003445Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3446\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003447Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003448
3449static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003450posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003451{
Neal Norwitze241ce82003-02-17 18:17:05 +00003452 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003453 if (pid == -1)
3454 return posix_error();
3455 PyOS_AfterFork();
3456 return PyInt_FromLong((long)pid);
3457}
3458#endif
3459
3460
Guido van Rossumad0ee831995-03-01 10:34:45 +00003461#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003462PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003463"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003464Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003465Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003466
Barry Warsaw53699e91996-12-10 23:23:01 +00003467static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003468posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003469{
Neal Norwitze241ce82003-02-17 18:17:05 +00003470 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003471 if (pid == -1)
3472 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003473 if (pid == 0)
3474 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003475 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003476}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003477#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003478
Neal Norwitzb59798b2003-03-21 01:43:31 +00003479/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003480/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3481#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003482#define DEV_PTY_FILE "/dev/ptc"
3483#define HAVE_DEV_PTMX
3484#else
3485#define DEV_PTY_FILE "/dev/ptmx"
3486#endif
3487
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003488#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003489#ifdef HAVE_PTY_H
3490#include <pty.h>
3491#else
3492#ifdef HAVE_LIBUTIL_H
3493#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003494#endif /* HAVE_LIBUTIL_H */
3495#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003496#ifdef HAVE_STROPTS_H
3497#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003498#endif
3499#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003500
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003501#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003502PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003503"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003504Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003505
3506static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003507posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003508{
3509 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003510#ifndef HAVE_OPENPTY
3511 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003512#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003513#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003514 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003515#ifdef sun
Neal Norwitz84a98e02006-04-10 07:44:23 +00003516 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003517#endif
3518#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003519
Thomas Wouters70c21a12000-07-14 14:28:33 +00003520#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003521 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3522 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003523#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003524 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3525 if (slave_name == NULL)
3526 return posix_error();
3527
3528 slave_fd = open(slave_name, O_RDWR);
3529 if (slave_fd < 0)
3530 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003531#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003532 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003533 if (master_fd < 0)
3534 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003535 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003536 /* change permission of slave */
3537 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003538 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003539 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003540 }
3541 /* unlock slave */
3542 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003543 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003544 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003545 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003546 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003547 slave_name = ptsname(master_fd); /* get name of slave */
3548 if (slave_name == NULL)
3549 return posix_error();
3550 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3551 if (slave_fd < 0)
3552 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003553#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003554 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3555 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003556#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003557 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003558#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003559#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003560#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003561
Fred Drake8cef4cf2000-06-28 16:40:38 +00003562 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003563
Fred Drake8cef4cf2000-06-28 16:40:38 +00003564}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003565#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003566
3567#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003568PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003569"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003570Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3571Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003572To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003573
3574static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003575posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003576{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003577 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003578
Fred Drake8cef4cf2000-06-28 16:40:38 +00003579 pid = forkpty(&master_fd, NULL, NULL, NULL);
3580 if (pid == -1)
3581 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003582 if (pid == 0)
3583 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003584 return Py_BuildValue("(ii)", pid, master_fd);
3585}
3586#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003587
Guido van Rossumad0ee831995-03-01 10:34:45 +00003588#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003589PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003590"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003591Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003592
Barry Warsaw53699e91996-12-10 23:23:01 +00003593static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003594posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003595{
Barry Warsaw53699e91996-12-10 23:23:01 +00003596 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003597}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003598#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003599
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003600
Guido van Rossumad0ee831995-03-01 10:34:45 +00003601#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003602PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003603"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003604Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003605
Barry Warsaw53699e91996-12-10 23:23:01 +00003606static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003607posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003608{
Barry Warsaw53699e91996-12-10 23:23:01 +00003609 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003610}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003611#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003612
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003613
Guido van Rossumad0ee831995-03-01 10:34:45 +00003614#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003615PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003616"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003617Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003618
Barry Warsaw53699e91996-12-10 23:23:01 +00003619static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003620posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003621{
Barry Warsaw53699e91996-12-10 23:23:01 +00003622 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003623}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003624#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003625
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003626
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003627PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003628"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003629Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003630
Barry Warsaw53699e91996-12-10 23:23:01 +00003631static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003632posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003633{
Barry Warsaw53699e91996-12-10 23:23:01 +00003634 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003635}
3636
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003637
Fred Drakec9680921999-12-13 16:37:25 +00003638#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003639PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003640"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003641Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003642
3643static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003644posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003645{
3646 PyObject *result = NULL;
3647
Fred Drakec9680921999-12-13 16:37:25 +00003648#ifdef NGROUPS_MAX
3649#define MAX_GROUPS NGROUPS_MAX
3650#else
3651 /* defined to be 16 on Solaris7, so this should be a small number */
3652#define MAX_GROUPS 64
3653#endif
3654 gid_t grouplist[MAX_GROUPS];
3655 int n;
3656
3657 n = getgroups(MAX_GROUPS, grouplist);
3658 if (n < 0)
3659 posix_error();
3660 else {
3661 result = PyList_New(n);
3662 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003663 int i;
3664 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003665 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003666 if (o == NULL) {
3667 Py_DECREF(result);
3668 result = NULL;
3669 break;
3670 }
3671 PyList_SET_ITEM(result, i, o);
3672 }
3673 }
3674 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003675
Fred Drakec9680921999-12-13 16:37:25 +00003676 return result;
3677}
3678#endif
3679
Martin v. Löwis606edc12002-06-13 21:09:11 +00003680#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003681PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003682"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003683Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003684
3685static PyObject *
3686posix_getpgid(PyObject *self, PyObject *args)
3687{
3688 int pid, pgid;
3689 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3690 return NULL;
3691 pgid = getpgid(pid);
3692 if (pgid < 0)
3693 return posix_error();
3694 return PyInt_FromLong((long)pgid);
3695}
3696#endif /* HAVE_GETPGID */
3697
3698
Guido van Rossumb6775db1994-08-01 11:34:53 +00003699#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003700PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003701"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003702Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003703
Barry Warsaw53699e91996-12-10 23:23:01 +00003704static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003705posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003706{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003707#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003708 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003709#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003710 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003711#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003712}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003713#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003714
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003715
Guido van Rossumb6775db1994-08-01 11:34:53 +00003716#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003717PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003718"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003719Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003720
Barry Warsaw53699e91996-12-10 23:23:01 +00003721static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003722posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003723{
Guido van Rossum64933891994-10-20 21:56:42 +00003724#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003725 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003726#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003727 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003728#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003729 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003730 Py_INCREF(Py_None);
3731 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003732}
3733
Guido van Rossumb6775db1994-08-01 11:34:53 +00003734#endif /* HAVE_SETPGRP */
3735
Guido van Rossumad0ee831995-03-01 10:34:45 +00003736#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003737PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003738"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003739Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003740
Barry Warsaw53699e91996-12-10 23:23:01 +00003741static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003742posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003743{
Barry Warsaw53699e91996-12-10 23:23:01 +00003744 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003745}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003746#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003747
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003748
Fred Drake12c6e2d1999-12-14 21:25:03 +00003749#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003750PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003751"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003752Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003753
3754static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003755posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003756{
Neal Norwitze241ce82003-02-17 18:17:05 +00003757 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003758 char *name;
3759 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003760
Fred Drakea30680b2000-12-06 21:24:28 +00003761 errno = 0;
3762 name = getlogin();
3763 if (name == NULL) {
3764 if (errno)
3765 posix_error();
3766 else
3767 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003768 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003769 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003770 else
3771 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003772 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003773
Fred Drake12c6e2d1999-12-14 21:25:03 +00003774 return result;
3775}
3776#endif
3777
Guido van Rossumad0ee831995-03-01 10:34:45 +00003778#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003779PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003780"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003781Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003782
Barry Warsaw53699e91996-12-10 23:23:01 +00003783static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003784posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003785{
Barry Warsaw53699e91996-12-10 23:23:01 +00003786 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003787}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003788#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003789
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003790
Guido van Rossumad0ee831995-03-01 10:34:45 +00003791#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003792PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003793"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003794Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003795
Barry Warsaw53699e91996-12-10 23:23:01 +00003796static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003797posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003798{
3799 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003800 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003801 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003802#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003803 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3804 APIRET rc;
3805 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003806 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003807
3808 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3809 APIRET rc;
3810 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003811 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003812
3813 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003814 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003815#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003816 if (kill(pid, sig) == -1)
3817 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003818#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003819 Py_INCREF(Py_None);
3820 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003821}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003822#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003823
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003824#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003825PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003826"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003827Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003828
3829static PyObject *
3830posix_killpg(PyObject *self, PyObject *args)
3831{
3832 int pgid, sig;
3833 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3834 return NULL;
3835 if (killpg(pgid, sig) == -1)
3836 return posix_error();
3837 Py_INCREF(Py_None);
3838 return Py_None;
3839}
3840#endif
3841
Guido van Rossumc0125471996-06-28 18:55:32 +00003842#ifdef HAVE_PLOCK
3843
3844#ifdef HAVE_SYS_LOCK_H
3845#include <sys/lock.h>
3846#endif
3847
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003848PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003849"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003850Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003851
Barry Warsaw53699e91996-12-10 23:23:01 +00003852static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003853posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003854{
3855 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003856 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003857 return NULL;
3858 if (plock(op) == -1)
3859 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003860 Py_INCREF(Py_None);
3861 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003862}
3863#endif
3864
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003865
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003866#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003867PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003868"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003869Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003870
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003871#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003872#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003873static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003874async_system(const char *command)
3875{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003876 char errormsg[256], args[1024];
3877 RESULTCODES rcodes;
3878 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003879
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003880 char *shell = getenv("COMSPEC");
3881 if (!shell)
3882 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003883
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003884 /* avoid overflowing the argument buffer */
3885 if (strlen(shell) + 3 + strlen(command) >= 1024)
3886 return ERROR_NOT_ENOUGH_MEMORY
3887
3888 args[0] = '\0';
3889 strcat(args, shell);
3890 strcat(args, "/c ");
3891 strcat(args, command);
3892
3893 /* execute asynchronously, inheriting the environment */
3894 rc = DosExecPgm(errormsg,
3895 sizeof(errormsg),
3896 EXEC_ASYNC,
3897 args,
3898 NULL,
3899 &rcodes,
3900 shell);
3901 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003902}
3903
Guido van Rossumd48f2521997-12-05 22:19:34 +00003904static FILE *
3905popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003906{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003907 int oldfd, tgtfd;
3908 HFILE pipeh[2];
3909 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003910
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003911 /* mode determines which of stdin or stdout is reconnected to
3912 * the pipe to the child
3913 */
3914 if (strchr(mode, 'r') != NULL) {
3915 tgt_fd = 1; /* stdout */
3916 } else if (strchr(mode, 'w')) {
3917 tgt_fd = 0; /* stdin */
3918 } else {
3919 *err = ERROR_INVALID_ACCESS;
3920 return NULL;
3921 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003922
Andrew MacIntyrea3be2582004-12-18 09:51:05 +00003923 /* setup the pipe */
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003924 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
3925 *err = rc;
3926 return NULL;
3927 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003928
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003929 /* prevent other threads accessing stdio */
3930 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003931
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003932 /* reconnect stdio and execute child */
3933 oldfd = dup(tgtfd);
3934 close(tgtfd);
3935 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
3936 DosClose(pipeh[tgtfd]);
3937 rc = async_system(command);
3938 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003939
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003940 /* restore stdio */
3941 dup2(oldfd, tgtfd);
3942 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003943
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003944 /* allow other threads access to stdio */
3945 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003946
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003947 /* if execution of child was successful return file stream */
3948 if (rc == NO_ERROR)
3949 return fdopen(pipeh[1 - tgtfd], mode);
3950 else {
3951 DosClose(pipeh[1 - tgtfd]);
3952 *err = rc;
3953 return NULL;
3954 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003955}
3956
3957static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003958posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003959{
3960 char *name;
3961 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003962 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003963 FILE *fp;
3964 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003965 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003966 return NULL;
3967 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003968 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003969 Py_END_ALLOW_THREADS
3970 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003971 return os2_error(err);
3972
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003973 f = PyFile_FromFile(fp, name, mode, fclose);
3974 if (f != NULL)
3975 PyFile_SetBufSize(f, bufsize);
3976 return f;
3977}
3978
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003979#elif defined(PYCC_GCC)
3980
3981/* standard posix version of popen() support */
3982static PyObject *
3983posix_popen(PyObject *self, PyObject *args)
3984{
3985 char *name;
3986 char *mode = "r";
3987 int bufsize = -1;
3988 FILE *fp;
3989 PyObject *f;
3990 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3991 return NULL;
3992 Py_BEGIN_ALLOW_THREADS
3993 fp = popen(name, mode);
3994 Py_END_ALLOW_THREADS
3995 if (fp == NULL)
3996 return posix_error();
3997 f = PyFile_FromFile(fp, name, mode, pclose);
3998 if (f != NULL)
3999 PyFile_SetBufSize(f, bufsize);
4000 return f;
4001}
4002
4003/* fork() under OS/2 has lots'o'warts
4004 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4005 * most of this code is a ripoff of the win32 code, but using the
4006 * capabilities of EMX's C library routines
4007 */
4008
4009/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4010#define POPEN_1 1
4011#define POPEN_2 2
4012#define POPEN_3 3
4013#define POPEN_4 4
4014
4015static PyObject *_PyPopen(char *, int, int, int);
4016static int _PyPclose(FILE *file);
4017
4018/*
4019 * Internal dictionary mapping popen* file pointers to process handles,
4020 * for use when retrieving the process exit code. See _PyPclose() below
4021 * for more information on this dictionary's use.
4022 */
4023static PyObject *_PyPopenProcs = NULL;
4024
4025/* os2emx version of popen2()
4026 *
4027 * The result of this function is a pipe (file) connected to the
4028 * process's stdin, and a pipe connected to the process's stdout.
4029 */
4030
4031static PyObject *
4032os2emx_popen2(PyObject *self, PyObject *args)
4033{
4034 PyObject *f;
4035 int tm=0;
4036
4037 char *cmdstring;
4038 char *mode = "t";
4039 int bufsize = -1;
4040 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4041 return NULL;
4042
4043 if (*mode == 't')
4044 tm = O_TEXT;
4045 else if (*mode != 'b') {
4046 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4047 return NULL;
4048 } else
4049 tm = O_BINARY;
4050
4051 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
4052
4053 return f;
4054}
4055
4056/*
4057 * Variation on os2emx.popen2
4058 *
4059 * The result of this function is 3 pipes - the process's stdin,
4060 * stdout and stderr
4061 */
4062
4063static PyObject *
4064os2emx_popen3(PyObject *self, PyObject *args)
4065{
4066 PyObject *f;
4067 int tm = 0;
4068
4069 char *cmdstring;
4070 char *mode = "t";
4071 int bufsize = -1;
4072 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4073 return NULL;
4074
4075 if (*mode == 't')
4076 tm = O_TEXT;
4077 else if (*mode != 'b') {
4078 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4079 return NULL;
4080 } else
4081 tm = O_BINARY;
4082
4083 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
4084
4085 return f;
4086}
4087
4088/*
4089 * Variation on os2emx.popen2
4090 *
Tim Peters11b23062003-04-23 02:39:17 +00004091 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004092 * and stdout+stderr combined as a single pipe.
4093 */
4094
4095static PyObject *
4096os2emx_popen4(PyObject *self, PyObject *args)
4097{
4098 PyObject *f;
4099 int tm = 0;
4100
4101 char *cmdstring;
4102 char *mode = "t";
4103 int bufsize = -1;
4104 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4105 return NULL;
4106
4107 if (*mode == 't')
4108 tm = O_TEXT;
4109 else if (*mode != 'b') {
4110 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4111 return NULL;
4112 } else
4113 tm = O_BINARY;
4114
4115 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
4116
4117 return f;
4118}
4119
4120/* a couple of structures for convenient handling of multiple
4121 * file handles and pipes
4122 */
4123struct file_ref
4124{
4125 int handle;
4126 int flags;
4127};
4128
4129struct pipe_ref
4130{
4131 int rd;
4132 int wr;
4133};
4134
4135/* The following code is derived from the win32 code */
4136
4137static PyObject *
4138_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4139{
4140 struct file_ref stdio[3];
4141 struct pipe_ref p_fd[3];
4142 FILE *p_s[3];
4143 int file_count, i, pipe_err, pipe_pid;
4144 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4145 PyObject *f, *p_f[3];
4146
4147 /* file modes for subsequent fdopen's on pipe handles */
4148 if (mode == O_TEXT)
4149 {
4150 rd_mode = "rt";
4151 wr_mode = "wt";
4152 }
4153 else
4154 {
4155 rd_mode = "rb";
4156 wr_mode = "wb";
4157 }
4158
4159 /* prepare shell references */
4160 if ((shell = getenv("EMXSHELL")) == NULL)
4161 if ((shell = getenv("COMSPEC")) == NULL)
4162 {
4163 errno = ENOENT;
4164 return posix_error();
4165 }
4166
4167 sh_name = _getname(shell);
4168 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4169 opt = "/c";
4170 else
4171 opt = "-c";
4172
4173 /* save current stdio fds + their flags, and set not inheritable */
4174 i = pipe_err = 0;
4175 while (pipe_err >= 0 && i < 3)
4176 {
4177 pipe_err = stdio[i].handle = dup(i);
4178 stdio[i].flags = fcntl(i, F_GETFD, 0);
4179 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4180 i++;
4181 }
4182 if (pipe_err < 0)
4183 {
4184 /* didn't get them all saved - clean up and bail out */
4185 int saved_err = errno;
4186 while (i-- > 0)
4187 {
4188 close(stdio[i].handle);
4189 }
4190 errno = saved_err;
4191 return posix_error();
4192 }
4193
4194 /* create pipe ends */
4195 file_count = 2;
4196 if (n == POPEN_3)
4197 file_count = 3;
4198 i = pipe_err = 0;
4199 while ((pipe_err == 0) && (i < file_count))
4200 pipe_err = pipe((int *)&p_fd[i++]);
4201 if (pipe_err < 0)
4202 {
4203 /* didn't get them all made - clean up and bail out */
4204 while (i-- > 0)
4205 {
4206 close(p_fd[i].wr);
4207 close(p_fd[i].rd);
4208 }
4209 errno = EPIPE;
4210 return posix_error();
4211 }
4212
4213 /* change the actual standard IO streams over temporarily,
4214 * making the retained pipe ends non-inheritable
4215 */
4216 pipe_err = 0;
4217
4218 /* - stdin */
4219 if (dup2(p_fd[0].rd, 0) == 0)
4220 {
4221 close(p_fd[0].rd);
4222 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4223 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4224 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4225 {
4226 close(p_fd[0].wr);
4227 pipe_err = -1;
4228 }
4229 }
4230 else
4231 {
4232 pipe_err = -1;
4233 }
4234
4235 /* - stdout */
4236 if (pipe_err == 0)
4237 {
4238 if (dup2(p_fd[1].wr, 1) == 1)
4239 {
4240 close(p_fd[1].wr);
4241 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4242 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4243 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4244 {
4245 close(p_fd[1].rd);
4246 pipe_err = -1;
4247 }
4248 }
4249 else
4250 {
4251 pipe_err = -1;
4252 }
4253 }
4254
4255 /* - stderr, as required */
4256 if (pipe_err == 0)
4257 switch (n)
4258 {
4259 case POPEN_3:
4260 {
4261 if (dup2(p_fd[2].wr, 2) == 2)
4262 {
4263 close(p_fd[2].wr);
4264 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4265 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4266 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4267 {
4268 close(p_fd[2].rd);
4269 pipe_err = -1;
4270 }
4271 }
4272 else
4273 {
4274 pipe_err = -1;
4275 }
4276 break;
4277 }
4278
4279 case POPEN_4:
4280 {
4281 if (dup2(1, 2) != 2)
4282 {
4283 pipe_err = -1;
4284 }
4285 break;
4286 }
4287 }
4288
4289 /* spawn the child process */
4290 if (pipe_err == 0)
4291 {
4292 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4293 if (pipe_pid == -1)
4294 {
4295 pipe_err = -1;
4296 }
4297 else
4298 {
4299 /* save the PID into the FILE structure
4300 * NOTE: this implementation doesn't actually
4301 * take advantage of this, but do it for
4302 * completeness - AIM Apr01
4303 */
4304 for (i = 0; i < file_count; i++)
4305 p_s[i]->_pid = pipe_pid;
4306 }
4307 }
4308
4309 /* reset standard IO to normal */
4310 for (i = 0; i < 3; i++)
4311 {
4312 dup2(stdio[i].handle, i);
4313 fcntl(i, F_SETFD, stdio[i].flags);
4314 close(stdio[i].handle);
4315 }
4316
4317 /* if any remnant problems, clean up and bail out */
4318 if (pipe_err < 0)
4319 {
4320 for (i = 0; i < 3; i++)
4321 {
4322 close(p_fd[i].rd);
4323 close(p_fd[i].wr);
4324 }
4325 errno = EPIPE;
4326 return posix_error_with_filename(cmdstring);
4327 }
4328
4329 /* build tuple of file objects to return */
4330 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4331 PyFile_SetBufSize(p_f[0], bufsize);
4332 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4333 PyFile_SetBufSize(p_f[1], bufsize);
4334 if (n == POPEN_3)
4335 {
4336 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4337 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004338 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004339 }
4340 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004341 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004342
4343 /*
4344 * Insert the files we've created into the process dictionary
4345 * all referencing the list with the process handle and the
4346 * initial number of files (see description below in _PyPclose).
4347 * Since if _PyPclose later tried to wait on a process when all
4348 * handles weren't closed, it could create a deadlock with the
4349 * child, we spend some energy here to try to ensure that we
4350 * either insert all file handles into the dictionary or none
4351 * at all. It's a little clumsy with the various popen modes
4352 * and variable number of files involved.
4353 */
4354 if (!_PyPopenProcs)
4355 {
4356 _PyPopenProcs = PyDict_New();
4357 }
4358
4359 if (_PyPopenProcs)
4360 {
4361 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4362 int ins_rc[3];
4363
4364 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4365 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4366
4367 procObj = PyList_New(2);
4368 pidObj = PyInt_FromLong((long) pipe_pid);
4369 intObj = PyInt_FromLong((long) file_count);
4370
4371 if (procObj && pidObj && intObj)
4372 {
4373 PyList_SetItem(procObj, 0, pidObj);
4374 PyList_SetItem(procObj, 1, intObj);
4375
4376 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4377 if (fileObj[0])
4378 {
4379 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4380 fileObj[0],
4381 procObj);
4382 }
4383 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4384 if (fileObj[1])
4385 {
4386 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4387 fileObj[1],
4388 procObj);
4389 }
4390 if (file_count >= 3)
4391 {
4392 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4393 if (fileObj[2])
4394 {
4395 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4396 fileObj[2],
4397 procObj);
4398 }
4399 }
4400
4401 if (ins_rc[0] < 0 || !fileObj[0] ||
4402 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4403 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4404 {
4405 /* Something failed - remove any dictionary
4406 * entries that did make it.
4407 */
4408 if (!ins_rc[0] && fileObj[0])
4409 {
4410 PyDict_DelItem(_PyPopenProcs,
4411 fileObj[0]);
4412 }
4413 if (!ins_rc[1] && fileObj[1])
4414 {
4415 PyDict_DelItem(_PyPopenProcs,
4416 fileObj[1]);
4417 }
4418 if (!ins_rc[2] && fileObj[2])
4419 {
4420 PyDict_DelItem(_PyPopenProcs,
4421 fileObj[2]);
4422 }
4423 }
4424 }
Tim Peters11b23062003-04-23 02:39:17 +00004425
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004426 /*
4427 * Clean up our localized references for the dictionary keys
4428 * and value since PyDict_SetItem will Py_INCREF any copies
4429 * that got placed in the dictionary.
4430 */
4431 Py_XDECREF(procObj);
4432 Py_XDECREF(fileObj[0]);
4433 Py_XDECREF(fileObj[1]);
4434 Py_XDECREF(fileObj[2]);
4435 }
4436
4437 /* Child is launched. */
4438 return f;
4439}
4440
4441/*
4442 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4443 * exit code for the child process and return as a result of the close.
4444 *
4445 * This function uses the _PyPopenProcs dictionary in order to map the
4446 * input file pointer to information about the process that was
4447 * originally created by the popen* call that created the file pointer.
4448 * The dictionary uses the file pointer as a key (with one entry
4449 * inserted for each file returned by the original popen* call) and a
4450 * single list object as the value for all files from a single call.
4451 * The list object contains the Win32 process handle at [0], and a file
4452 * count at [1], which is initialized to the total number of file
4453 * handles using that list.
4454 *
4455 * This function closes whichever handle it is passed, and decrements
4456 * the file count in the dictionary for the process handle pointed to
4457 * by this file. On the last close (when the file count reaches zero),
4458 * this function will wait for the child process and then return its
4459 * exit code as the result of the close() operation. This permits the
4460 * files to be closed in any order - it is always the close() of the
4461 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004462 *
4463 * NOTE: This function is currently called with the GIL released.
4464 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004465 */
4466
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004467static int _PyPclose(FILE *file)
4468{
4469 int result;
4470 int exit_code;
4471 int pipe_pid;
4472 PyObject *procObj, *pidObj, *intObj, *fileObj;
4473 int file_count;
4474#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004475 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004476#endif
4477
4478 /* Close the file handle first, to ensure it can't block the
4479 * child from exiting if it's the last handle.
4480 */
4481 result = fclose(file);
4482
4483#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004484 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004485#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004486 if (_PyPopenProcs)
4487 {
4488 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4489 (procObj = PyDict_GetItem(_PyPopenProcs,
4490 fileObj)) != NULL &&
4491 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4492 (intObj = PyList_GetItem(procObj,1)) != NULL)
4493 {
4494 pipe_pid = (int) PyInt_AsLong(pidObj);
4495 file_count = (int) PyInt_AsLong(intObj);
4496
4497 if (file_count > 1)
4498 {
4499 /* Still other files referencing process */
4500 file_count--;
4501 PyList_SetItem(procObj,1,
4502 PyInt_FromLong((long) file_count));
4503 }
4504 else
4505 {
4506 /* Last file for this process */
4507 if (result != EOF &&
4508 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4509 {
4510 /* extract exit status */
4511 if (WIFEXITED(exit_code))
4512 {
4513 result = WEXITSTATUS(exit_code);
4514 }
4515 else
4516 {
4517 errno = EPIPE;
4518 result = -1;
4519 }
4520 }
4521 else
4522 {
4523 /* Indicate failure - this will cause the file object
4524 * to raise an I/O error and translate the last
4525 * error code from errno. We do have a problem with
4526 * last errors that overlap the normal errno table,
4527 * but that's a consistent problem with the file object.
4528 */
4529 result = -1;
4530 }
4531 }
4532
4533 /* Remove this file pointer from dictionary */
4534 PyDict_DelItem(_PyPopenProcs, fileObj);
4535
4536 if (PyDict_Size(_PyPopenProcs) == 0)
4537 {
4538 Py_DECREF(_PyPopenProcs);
4539 _PyPopenProcs = NULL;
4540 }
4541
4542 } /* if object retrieval ok */
4543
4544 Py_XDECREF(fileObj);
4545 } /* if _PyPopenProcs */
4546
4547#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004548 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004549#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004550 return result;
4551}
4552
4553#endif /* PYCC_??? */
4554
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004555#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004556
4557/*
4558 * Portable 'popen' replacement for Win32.
4559 *
4560 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4561 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004562 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004563 */
4564
4565#include <malloc.h>
4566#include <io.h>
4567#include <fcntl.h>
4568
4569/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4570#define POPEN_1 1
4571#define POPEN_2 2
4572#define POPEN_3 3
4573#define POPEN_4 4
4574
4575static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004576static int _PyPclose(FILE *file);
4577
4578/*
4579 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004580 * for use when retrieving the process exit code. See _PyPclose() below
4581 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004582 */
4583static PyObject *_PyPopenProcs = NULL;
4584
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004585
4586/* popen that works from a GUI.
4587 *
4588 * The result of this function is a pipe (file) connected to the
4589 * processes stdin or stdout, depending on the requested mode.
4590 */
4591
4592static PyObject *
4593posix_popen(PyObject *self, PyObject *args)
4594{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00004595 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004596 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004597
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004598 char *cmdstring;
4599 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004600 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004601 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004602 return NULL;
4603
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004604 if (*mode == 'r')
4605 tm = _O_RDONLY;
4606 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00004607 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004608 return NULL;
4609 } else
4610 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004611
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004612 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004613 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004614 return NULL;
4615 }
4616
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004617 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004618 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004619 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004620 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004621 else
4622 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4623
4624 return f;
4625}
4626
4627/* Variation on win32pipe.popen
4628 *
4629 * The result of this function is a pipe (file) connected to the
4630 * process's stdin, and a pipe connected to the process's stdout.
4631 */
4632
4633static PyObject *
4634win32_popen2(PyObject *self, PyObject *args)
4635{
4636 PyObject *f;
4637 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004638
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004639 char *cmdstring;
4640 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004641 int bufsize = -1;
4642 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004643 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004644
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004645 if (*mode == 't')
4646 tm = _O_TEXT;
4647 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004648 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004649 return NULL;
4650 } else
4651 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004652
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004653 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004654 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004655 return NULL;
4656 }
4657
4658 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004659
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004660 return f;
4661}
4662
4663/*
4664 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004665 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004666 * The result of this function is 3 pipes - the process's stdin,
4667 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004668 */
4669
4670static PyObject *
4671win32_popen3(PyObject *self, PyObject *args)
4672{
4673 PyObject *f;
4674 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004675
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004676 char *cmdstring;
4677 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004678 int bufsize = -1;
4679 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004680 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004681
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004682 if (*mode == 't')
4683 tm = _O_TEXT;
4684 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004685 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004686 return NULL;
4687 } else
4688 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004689
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004690 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004691 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004692 return NULL;
4693 }
4694
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004695 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004696
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004697 return f;
4698}
4699
4700/*
4701 * Variation on win32pipe.popen
4702 *
Tim Peters5aa91602002-01-30 05:46:57 +00004703 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004704 * and stdout+stderr combined as a single pipe.
4705 */
4706
4707static PyObject *
4708win32_popen4(PyObject *self, PyObject *args)
4709{
4710 PyObject *f;
4711 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004712
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004713 char *cmdstring;
4714 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004715 int bufsize = -1;
4716 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004717 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004718
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004719 if (*mode == 't')
4720 tm = _O_TEXT;
4721 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004722 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004723 return NULL;
4724 } else
4725 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004726
4727 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004728 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004729 return NULL;
4730 }
4731
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004732 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004733
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004734 return f;
4735}
4736
Mark Hammond08501372001-01-31 07:30:29 +00004737static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004738_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004739 HANDLE hStdin,
4740 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004741 HANDLE hStderr,
4742 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004743{
4744 PROCESS_INFORMATION piProcInfo;
4745 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004746 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004747 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004748 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004749 int i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004750 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004751
4752 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004753 char *comshell;
4754
Tim Peters92e4dd82002-10-05 01:47:34 +00004755 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004756 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
Martin v. Löwis18e16552006-02-15 17:27:45 +00004757 /* x < i, so x fits into an integer */
4758 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00004759
4760 /* Explicitly check if we are using COMMAND.COM. If we are
4761 * then use the w9xpopen hack.
4762 */
4763 comshell = s1 + x;
4764 while (comshell >= s1 && *comshell != '\\')
4765 --comshell;
4766 ++comshell;
4767
4768 if (GetVersion() < 0x80000000 &&
4769 _stricmp(comshell, "command.com") != 0) {
4770 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004771 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004772 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004773 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004774 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004775 }
4776 else {
4777 /*
Tim Peters402d5982001-08-27 06:37:48 +00004778 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4779 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004780 */
Mark Hammond08501372001-01-31 07:30:29 +00004781 char modulepath[_MAX_PATH];
4782 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004783 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
Martin v. Löwis26fd9602006-04-22 11:15:41 +00004784 for (x = i = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004785 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004786 x = i+1;
4787 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004788 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004789 strncat(modulepath,
4790 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004791 (sizeof(modulepath)/sizeof(modulepath[0]))
4792 -strlen(modulepath));
4793 if (stat(modulepath, &statinfo) != 0) {
Kristján Valur Jónsson5e4e31f2007-04-21 12:46:49 +00004794 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
Tim Peters5aa91602002-01-30 05:46:57 +00004795 /* Eeek - file-not-found - possibly an embedding
4796 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004797 */
Tim Peters5aa91602002-01-30 05:46:57 +00004798 strncpy(modulepath,
4799 Py_GetExecPrefix(),
Kristján Valur Jónsson5e4e31f2007-04-21 12:46:49 +00004800 mplen);
4801 modulepath[mplen-1] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004802 if (modulepath[strlen(modulepath)-1] != '\\')
4803 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004804 strncat(modulepath,
4805 szConsoleSpawn,
Kristján Valur Jónsson5e4e31f2007-04-21 12:46:49 +00004806 mplen-strlen(modulepath));
Mark Hammond08501372001-01-31 07:30:29 +00004807 /* No where else to look - raise an easily identifiable
4808 error, rather than leaving Windows to report
4809 "file not found" - as the user is probably blissfully
4810 unaware this shim EXE is used, and it will confuse them.
4811 (well, it confused me for a while ;-)
4812 */
4813 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004814 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004815 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004816 "for popen to work with your shell "
4817 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004818 szConsoleSpawn);
4819 return FALSE;
4820 }
4821 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004822 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004823 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004824 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004825
Tim Peters92e4dd82002-10-05 01:47:34 +00004826 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004827 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004828 /* To maintain correct argument passing semantics,
4829 we pass the command-line as it stands, and allow
4830 quoting to be applied. w9xpopen.exe will then
4831 use its argv vector, and re-quote the necessary
4832 args for the ultimate child process.
4833 */
Tim Peters75cdad52001-11-28 22:07:30 +00004834 PyOS_snprintf(
4835 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004836 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004837 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004838 s1,
4839 s3,
4840 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004841 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004842 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004843 dialog:
4844 "Your program accessed mem currently in use at xxx"
4845 and a hopeful warning about the stability of your
4846 system.
4847 Cost is Ctrl+C wont kill children, but anyone
4848 who cares can have a go!
4849 */
4850 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004851 }
4852 }
4853
4854 /* Could be an else here to try cmd.exe / command.com in the path
4855 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004856 else {
Tim Peters402d5982001-08-27 06:37:48 +00004857 PyErr_SetString(PyExc_RuntimeError,
4858 "Cannot locate a COMSPEC environment variable to "
4859 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004860 return FALSE;
4861 }
Tim Peters5aa91602002-01-30 05:46:57 +00004862
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004863 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4864 siStartInfo.cb = sizeof(STARTUPINFO);
4865 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4866 siStartInfo.hStdInput = hStdin;
4867 siStartInfo.hStdOutput = hStdout;
4868 siStartInfo.hStdError = hStderr;
4869 siStartInfo.wShowWindow = SW_HIDE;
4870
4871 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004872 s2,
4873 NULL,
4874 NULL,
4875 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004876 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004877 NULL,
4878 NULL,
4879 &siStartInfo,
4880 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004881 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004882 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004883
Mark Hammondb37a3732000-08-14 04:47:33 +00004884 /* Return process handle */
4885 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004886 return TRUE;
4887 }
Tim Peters402d5982001-08-27 06:37:48 +00004888 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004889 return FALSE;
4890}
4891
4892/* The following code is based off of KB: Q190351 */
4893
4894static PyObject *
4895_PyPopen(char *cmdstring, int mode, int n)
4896{
4897 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4898 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004899 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004900
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004901 SECURITY_ATTRIBUTES saAttr;
4902 BOOL fSuccess;
4903 int fd1, fd2, fd3;
4904 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004905 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004906 PyObject *f;
4907
4908 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4909 saAttr.bInheritHandle = TRUE;
4910 saAttr.lpSecurityDescriptor = NULL;
4911
4912 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4913 return win32_error("CreatePipe", NULL);
4914
4915 /* Create new output read handle and the input write handle. Set
4916 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004917 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004918 * being created. */
4919 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004920 GetCurrentProcess(), &hChildStdinWrDup, 0,
4921 FALSE,
4922 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004923 if (!fSuccess)
4924 return win32_error("DuplicateHandle", NULL);
4925
4926 /* Close the inheritable version of ChildStdin
4927 that we're using. */
4928 CloseHandle(hChildStdinWr);
4929
4930 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4931 return win32_error("CreatePipe", NULL);
4932
4933 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004934 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4935 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004936 if (!fSuccess)
4937 return win32_error("DuplicateHandle", NULL);
4938
4939 /* Close the inheritable version of ChildStdout
4940 that we're using. */
4941 CloseHandle(hChildStdoutRd);
4942
4943 if (n != POPEN_4) {
4944 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4945 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004946 fSuccess = DuplicateHandle(GetCurrentProcess(),
4947 hChildStderrRd,
4948 GetCurrentProcess(),
4949 &hChildStderrRdDup, 0,
4950 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004951 if (!fSuccess)
4952 return win32_error("DuplicateHandle", NULL);
4953 /* Close the inheritable version of ChildStdErr that we're using. */
4954 CloseHandle(hChildStderrRd);
4955 }
Tim Peters5aa91602002-01-30 05:46:57 +00004956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004957 switch (n) {
4958 case POPEN_1:
4959 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4960 case _O_WRONLY | _O_TEXT:
4961 /* Case for writing to child Stdin in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004962 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004963 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004964 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004965 PyFile_SetBufSize(f, 0);
4966 /* We don't care about these pipes anymore, so close them. */
4967 CloseHandle(hChildStdoutRdDup);
4968 CloseHandle(hChildStderrRdDup);
4969 break;
4970
4971 case _O_RDONLY | _O_TEXT:
4972 /* Case for reading from child Stdout in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004973 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004974 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004975 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004976 PyFile_SetBufSize(f, 0);
4977 /* We don't care about these pipes anymore, so close them. */
4978 CloseHandle(hChildStdinWrDup);
4979 CloseHandle(hChildStderrRdDup);
4980 break;
4981
4982 case _O_RDONLY | _O_BINARY:
4983 /* Case for readinig from child Stdout in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004984 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004985 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004986 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004987 PyFile_SetBufSize(f, 0);
4988 /* We don't care about these pipes anymore, so close them. */
4989 CloseHandle(hChildStdinWrDup);
4990 CloseHandle(hChildStderrRdDup);
4991 break;
4992
4993 case _O_WRONLY | _O_BINARY:
4994 /* Case for writing to child Stdin in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004995 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004996 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004997 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004998 PyFile_SetBufSize(f, 0);
4999 /* We don't care about these pipes anymore, so close them. */
5000 CloseHandle(hChildStdoutRdDup);
5001 CloseHandle(hChildStderrRdDup);
5002 break;
5003 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005004 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005005 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005006
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005007 case POPEN_2:
5008 case POPEN_4:
5009 {
5010 char *m1, *m2;
5011 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005012
Tim Peters7dca21e2002-08-19 00:42:29 +00005013 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005014 m1 = "r";
5015 m2 = "w";
5016 } else {
5017 m1 = "rb";
5018 m2 = "wb";
5019 }
5020
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005021 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005022 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005023 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005024 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005025 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005026 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00005027 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005028 PyFile_SetBufSize(p2, 0);
5029
5030 if (n != 4)
5031 CloseHandle(hChildStderrRdDup);
5032
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005033 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00005034 Py_XDECREF(p1);
5035 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00005036 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005037 break;
5038 }
Tim Peters5aa91602002-01-30 05:46:57 +00005039
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005040 case POPEN_3:
5041 {
5042 char *m1, *m2;
5043 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005044
Tim Peters7dca21e2002-08-19 00:42:29 +00005045 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005046 m1 = "r";
5047 m2 = "w";
5048 } else {
5049 m1 = "rb";
5050 m2 = "wb";
5051 }
5052
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005053 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005054 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005055 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005056 f2 = _fdopen(fd2, m1);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005057 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005058 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005059 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00005060 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5061 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005062 PyFile_SetBufSize(p1, 0);
5063 PyFile_SetBufSize(p2, 0);
5064 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005065 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00005066 Py_XDECREF(p1);
5067 Py_XDECREF(p2);
5068 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00005069 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005070 break;
5071 }
5072 }
5073
5074 if (n == POPEN_4) {
5075 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005076 hChildStdinRd,
5077 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005078 hChildStdoutWr,
5079 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005080 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005081 }
5082 else {
5083 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005084 hChildStdinRd,
5085 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005086 hChildStderrWr,
5087 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005088 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005089 }
5090
Mark Hammondb37a3732000-08-14 04:47:33 +00005091 /*
5092 * Insert the files we've created into the process dictionary
5093 * all referencing the list with the process handle and the
5094 * initial number of files (see description below in _PyPclose).
5095 * Since if _PyPclose later tried to wait on a process when all
5096 * handles weren't closed, it could create a deadlock with the
5097 * child, we spend some energy here to try to ensure that we
5098 * either insert all file handles into the dictionary or none
5099 * at all. It's a little clumsy with the various popen modes
5100 * and variable number of files involved.
5101 */
5102 if (!_PyPopenProcs) {
5103 _PyPopenProcs = PyDict_New();
5104 }
5105
5106 if (_PyPopenProcs) {
5107 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5108 int ins_rc[3];
5109
5110 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5111 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
5112
5113 procObj = PyList_New(2);
5114 hProcessObj = PyLong_FromVoidPtr(hProcess);
5115 intObj = PyInt_FromLong(file_count);
5116
5117 if (procObj && hProcessObj && intObj) {
5118 PyList_SetItem(procObj,0,hProcessObj);
5119 PyList_SetItem(procObj,1,intObj);
5120
5121 fileObj[0] = PyLong_FromVoidPtr(f1);
5122 if (fileObj[0]) {
5123 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5124 fileObj[0],
5125 procObj);
5126 }
5127 if (file_count >= 2) {
5128 fileObj[1] = PyLong_FromVoidPtr(f2);
5129 if (fileObj[1]) {
5130 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5131 fileObj[1],
5132 procObj);
5133 }
5134 }
5135 if (file_count >= 3) {
5136 fileObj[2] = PyLong_FromVoidPtr(f3);
5137 if (fileObj[2]) {
5138 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5139 fileObj[2],
5140 procObj);
5141 }
5142 }
5143
5144 if (ins_rc[0] < 0 || !fileObj[0] ||
5145 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5146 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5147 /* Something failed - remove any dictionary
5148 * entries that did make it.
5149 */
5150 if (!ins_rc[0] && fileObj[0]) {
5151 PyDict_DelItem(_PyPopenProcs,
5152 fileObj[0]);
5153 }
5154 if (!ins_rc[1] && fileObj[1]) {
5155 PyDict_DelItem(_PyPopenProcs,
5156 fileObj[1]);
5157 }
5158 if (!ins_rc[2] && fileObj[2]) {
5159 PyDict_DelItem(_PyPopenProcs,
5160 fileObj[2]);
5161 }
5162 }
5163 }
Tim Peters5aa91602002-01-30 05:46:57 +00005164
Mark Hammondb37a3732000-08-14 04:47:33 +00005165 /*
5166 * Clean up our localized references for the dictionary keys
5167 * and value since PyDict_SetItem will Py_INCREF any copies
5168 * that got placed in the dictionary.
5169 */
5170 Py_XDECREF(procObj);
5171 Py_XDECREF(fileObj[0]);
5172 Py_XDECREF(fileObj[1]);
5173 Py_XDECREF(fileObj[2]);
5174 }
5175
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005176 /* Child is launched. Close the parents copy of those pipe
5177 * handles that only the child should have open. You need to
5178 * make sure that no handles to the write end of the output pipe
5179 * are maintained in this process or else the pipe will not close
5180 * when the child process exits and the ReadFile will hang. */
5181
5182 if (!CloseHandle(hChildStdinRd))
5183 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005184
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005185 if (!CloseHandle(hChildStdoutWr))
5186 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005187
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005188 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5189 return win32_error("CloseHandle", NULL);
5190
5191 return f;
5192}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005193
5194/*
5195 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5196 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005197 *
5198 * This function uses the _PyPopenProcs dictionary in order to map the
5199 * input file pointer to information about the process that was
5200 * originally created by the popen* call that created the file pointer.
5201 * The dictionary uses the file pointer as a key (with one entry
5202 * inserted for each file returned by the original popen* call) and a
5203 * single list object as the value for all files from a single call.
5204 * The list object contains the Win32 process handle at [0], and a file
5205 * count at [1], which is initialized to the total number of file
5206 * handles using that list.
5207 *
5208 * This function closes whichever handle it is passed, and decrements
5209 * the file count in the dictionary for the process handle pointed to
5210 * by this file. On the last close (when the file count reaches zero),
5211 * this function will wait for the child process and then return its
5212 * exit code as the result of the close() operation. This permits the
5213 * files to be closed in any order - it is always the close() of the
5214 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005215 *
5216 * NOTE: This function is currently called with the GIL released.
5217 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005218 */
Tim Peters736aa322000-09-01 06:51:24 +00005219
Fredrik Lundh56055a42000-07-23 19:47:12 +00005220static int _PyPclose(FILE *file)
5221{
Fredrik Lundh20318932000-07-26 17:29:12 +00005222 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005223 DWORD exit_code;
5224 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00005225 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5226 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005227#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005228 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005229#endif
5230
Fredrik Lundh20318932000-07-26 17:29:12 +00005231 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00005232 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00005233 */
5234 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005235#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005236 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005237#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005238 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005239 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5240 (procObj = PyDict_GetItem(_PyPopenProcs,
5241 fileObj)) != NULL &&
5242 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5243 (intObj = PyList_GetItem(procObj,1)) != NULL) {
5244
5245 hProcess = PyLong_AsVoidPtr(hProcessObj);
5246 file_count = PyInt_AsLong(intObj);
5247
5248 if (file_count > 1) {
5249 /* Still other files referencing process */
5250 file_count--;
5251 PyList_SetItem(procObj,1,
5252 PyInt_FromLong(file_count));
5253 } else {
5254 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00005255 if (result != EOF &&
5256 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5257 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00005258 /* Possible truncation here in 16-bit environments, but
5259 * real exit codes are just the lower byte in any event.
5260 */
5261 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005262 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00005263 /* Indicate failure - this will cause the file object
5264 * to raise an I/O error and translate the last Win32
5265 * error code from errno. We do have a problem with
5266 * last errors that overlap the normal errno table,
5267 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005268 */
Fredrik Lundh20318932000-07-26 17:29:12 +00005269 if (result != EOF) {
5270 /* If the error wasn't from the fclose(), then
5271 * set errno for the file object error handling.
5272 */
5273 errno = GetLastError();
5274 }
5275 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005276 }
5277
5278 /* Free up the native handle at this point */
5279 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00005280 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005281
Mark Hammondb37a3732000-08-14 04:47:33 +00005282 /* Remove this file pointer from dictionary */
5283 PyDict_DelItem(_PyPopenProcs, fileObj);
5284
5285 if (PyDict_Size(_PyPopenProcs) == 0) {
5286 Py_DECREF(_PyPopenProcs);
5287 _PyPopenProcs = NULL;
5288 }
5289
5290 } /* if object retrieval ok */
5291
5292 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005293 } /* if _PyPopenProcs */
5294
Tim Peters736aa322000-09-01 06:51:24 +00005295#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005296 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005297#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005298 return result;
5299}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005300
5301#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005302static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005303posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005304{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005305 char *name;
5306 char *mode = "r";
5307 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00005308 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005309 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005310 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00005311 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00005312 /* Strip mode of binary or text modifiers */
5313 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5314 mode = "r";
5315 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5316 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00005317 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005318 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005319 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00005320 if (fp == NULL)
5321 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005322 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005323 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005324 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005325 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005326}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005327
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005328#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005329#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005330
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005331
Guido van Rossumb6775db1994-08-01 11:34:53 +00005332#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005333PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005334"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005335Set the current process's user id.");
5336
Barry Warsaw53699e91996-12-10 23:23:01 +00005337static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005338posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005339{
5340 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005341 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005342 return NULL;
5343 if (setuid(uid) < 0)
5344 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005345 Py_INCREF(Py_None);
5346 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005347}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005348#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005349
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005350
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005351#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005352PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005353"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005354Set the current process's effective user id.");
5355
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005356static PyObject *
5357posix_seteuid (PyObject *self, PyObject *args)
5358{
5359 int euid;
5360 if (!PyArg_ParseTuple(args, "i", &euid)) {
5361 return NULL;
5362 } else if (seteuid(euid) < 0) {
5363 return posix_error();
5364 } else {
5365 Py_INCREF(Py_None);
5366 return Py_None;
5367 }
5368}
5369#endif /* HAVE_SETEUID */
5370
5371#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005372PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005373"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005374Set the current process's effective group id.");
5375
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005376static PyObject *
5377posix_setegid (PyObject *self, PyObject *args)
5378{
5379 int egid;
5380 if (!PyArg_ParseTuple(args, "i", &egid)) {
5381 return NULL;
5382 } else if (setegid(egid) < 0) {
5383 return posix_error();
5384 } else {
5385 Py_INCREF(Py_None);
5386 return Py_None;
5387 }
5388}
5389#endif /* HAVE_SETEGID */
5390
5391#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005392PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005393"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005394Set the current process's real and effective user ids.");
5395
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005396static PyObject *
5397posix_setreuid (PyObject *self, PyObject *args)
5398{
5399 int ruid, euid;
5400 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
5401 return NULL;
5402 } else if (setreuid(ruid, euid) < 0) {
5403 return posix_error();
5404 } else {
5405 Py_INCREF(Py_None);
5406 return Py_None;
5407 }
5408}
5409#endif /* HAVE_SETREUID */
5410
5411#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005412PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005413"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005414Set the current process's real and effective group ids.");
5415
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005416static PyObject *
5417posix_setregid (PyObject *self, PyObject *args)
5418{
5419 int rgid, egid;
5420 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
5421 return NULL;
5422 } else if (setregid(rgid, egid) < 0) {
5423 return posix_error();
5424 } else {
5425 Py_INCREF(Py_None);
5426 return Py_None;
5427 }
5428}
5429#endif /* HAVE_SETREGID */
5430
Guido van Rossumb6775db1994-08-01 11:34:53 +00005431#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005432PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005433"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005434Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005435
Barry Warsaw53699e91996-12-10 23:23:01 +00005436static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005437posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005438{
5439 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005440 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005441 return NULL;
5442 if (setgid(gid) < 0)
5443 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005444 Py_INCREF(Py_None);
5445 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005446}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005447#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005448
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005449#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005450PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005451"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005452Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005453
5454static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005455posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005456{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005457 int i, len;
5458 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005459
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005460 if (!PySequence_Check(groups)) {
5461 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5462 return NULL;
5463 }
5464 len = PySequence_Size(groups);
5465 if (len > MAX_GROUPS) {
5466 PyErr_SetString(PyExc_ValueError, "too many groups");
5467 return NULL;
5468 }
5469 for(i = 0; i < len; i++) {
5470 PyObject *elem;
5471 elem = PySequence_GetItem(groups, i);
5472 if (!elem)
5473 return NULL;
5474 if (!PyInt_Check(elem)) {
Georg Brandla13c2442005-11-22 19:30:31 +00005475 if (!PyLong_Check(elem)) {
5476 PyErr_SetString(PyExc_TypeError,
5477 "groups must be integers");
5478 Py_DECREF(elem);
5479 return NULL;
5480 } else {
5481 unsigned long x = PyLong_AsUnsignedLong(elem);
5482 if (PyErr_Occurred()) {
5483 PyErr_SetString(PyExc_TypeError,
5484 "group id too big");
5485 Py_DECREF(elem);
5486 return NULL;
5487 }
5488 grouplist[i] = x;
5489 /* read back the value to see if it fitted in gid_t */
5490 if (grouplist[i] != x) {
5491 PyErr_SetString(PyExc_TypeError,
5492 "group id too big");
5493 Py_DECREF(elem);
5494 return NULL;
5495 }
5496 }
5497 } else {
5498 long x = PyInt_AsLong(elem);
5499 grouplist[i] = x;
5500 if (grouplist[i] != x) {
5501 PyErr_SetString(PyExc_TypeError,
5502 "group id too big");
5503 Py_DECREF(elem);
5504 return NULL;
5505 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005506 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005507 Py_DECREF(elem);
5508 }
5509
5510 if (setgroups(len, grouplist) < 0)
5511 return posix_error();
5512 Py_INCREF(Py_None);
5513 return Py_None;
5514}
5515#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005516
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005517#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00005518static PyObject *
5519wait_helper(int pid, int status, struct rusage *ru)
5520{
5521 PyObject *result;
5522 static PyObject *struct_rusage;
5523
5524 if (pid == -1)
5525 return posix_error();
5526
5527 if (struct_rusage == NULL) {
5528 PyObject *m = PyImport_ImportModule("resource");
5529 if (m == NULL)
5530 return NULL;
5531 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5532 Py_DECREF(m);
5533 if (struct_rusage == NULL)
5534 return NULL;
5535 }
5536
5537 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5538 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5539 if (!result)
5540 return NULL;
5541
5542#ifndef doubletime
5543#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5544#endif
5545
5546 PyStructSequence_SET_ITEM(result, 0,
5547 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5548 PyStructSequence_SET_ITEM(result, 1,
5549 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5550#define SET_INT(result, index, value)\
5551 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5552 SET_INT(result, 2, ru->ru_maxrss);
5553 SET_INT(result, 3, ru->ru_ixrss);
5554 SET_INT(result, 4, ru->ru_idrss);
5555 SET_INT(result, 5, ru->ru_isrss);
5556 SET_INT(result, 6, ru->ru_minflt);
5557 SET_INT(result, 7, ru->ru_majflt);
5558 SET_INT(result, 8, ru->ru_nswap);
5559 SET_INT(result, 9, ru->ru_inblock);
5560 SET_INT(result, 10, ru->ru_oublock);
5561 SET_INT(result, 11, ru->ru_msgsnd);
5562 SET_INT(result, 12, ru->ru_msgrcv);
5563 SET_INT(result, 13, ru->ru_nsignals);
5564 SET_INT(result, 14, ru->ru_nvcsw);
5565 SET_INT(result, 15, ru->ru_nivcsw);
5566#undef SET_INT
5567
5568 if (PyErr_Occurred()) {
5569 Py_DECREF(result);
5570 return NULL;
5571 }
5572
Neal Norwitz9b00a562006-03-20 08:47:12 +00005573 return Py_BuildValue("iiN", pid, status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00005574}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005575#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00005576
5577#ifdef HAVE_WAIT3
5578PyDoc_STRVAR(posix_wait3__doc__,
5579"wait3(options) -> (pid, status, rusage)\n\n\
5580Wait for completion of a child process.");
5581
5582static PyObject *
5583posix_wait3(PyObject *self, PyObject *args)
5584{
5585 int pid, options;
5586 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005587 WAIT_TYPE status;
5588 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005589
5590 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5591 return NULL;
5592
5593 Py_BEGIN_ALLOW_THREADS
5594 pid = wait3(&status, options, &ru);
5595 Py_END_ALLOW_THREADS
5596
Neal Norwitzd5a37542006-03-20 06:48:34 +00005597 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005598}
5599#endif /* HAVE_WAIT3 */
5600
5601#ifdef HAVE_WAIT4
5602PyDoc_STRVAR(posix_wait4__doc__,
5603"wait4(pid, options) -> (pid, status, rusage)\n\n\
5604Wait for completion of a given child process.");
5605
5606static PyObject *
5607posix_wait4(PyObject *self, PyObject *args)
5608{
5609 int pid, options;
5610 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005611 WAIT_TYPE status;
5612 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005613
5614 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
5615 return NULL;
5616
5617 Py_BEGIN_ALLOW_THREADS
5618 pid = wait4(pid, &status, options, &ru);
5619 Py_END_ALLOW_THREADS
5620
Neal Norwitzd5a37542006-03-20 06:48:34 +00005621 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005622}
5623#endif /* HAVE_WAIT4 */
5624
Guido van Rossumb6775db1994-08-01 11:34:53 +00005625#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005626PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005627"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005628Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005629
Barry Warsaw53699e91996-12-10 23:23:01 +00005630static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005631posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005632{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005633 int pid, options;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005634 WAIT_TYPE status;
5635 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005636
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005637 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00005638 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005639 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00005640 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00005641 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00005642 if (pid == -1)
5643 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005644
5645 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005646}
5647
Tim Petersab034fa2002-02-01 11:27:43 +00005648#elif defined(HAVE_CWAIT)
5649
5650/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005651PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005652"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005653"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005654
5655static PyObject *
5656posix_waitpid(PyObject *self, PyObject *args)
5657{
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005658 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005659 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005660
5661 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5662 return NULL;
5663 Py_BEGIN_ALLOW_THREADS
5664 pid = _cwait(&status, pid, options);
5665 Py_END_ALLOW_THREADS
5666 if (pid == -1)
5667 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005668
5669 /* shift the status left a byte so this is more like the POSIX waitpid */
5670 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005671}
5672#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005673
Guido van Rossumad0ee831995-03-01 10:34:45 +00005674#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005675PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005676"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005677Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005678
Barry Warsaw53699e91996-12-10 23:23:01 +00005679static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005680posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005681{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005682 int pid;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005683 WAIT_TYPE status;
5684 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005685
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005686 Py_BEGIN_ALLOW_THREADS
5687 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00005688 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00005689 if (pid == -1)
5690 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005691
5692 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005693}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005694#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005695
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005697PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005698"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005699Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005700
Barry Warsaw53699e91996-12-10 23:23:01 +00005701static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005702posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005703{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005704#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005705 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005706#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005707#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00005708 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005709#else
5710 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5711#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005712#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005713}
5714
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005715
Guido van Rossumb6775db1994-08-01 11:34:53 +00005716#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005717PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005718"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005719Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005720
Barry Warsaw53699e91996-12-10 23:23:01 +00005721static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005722posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005723{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005724 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005725 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005726 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005727 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005728 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005729 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00005730 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00005731 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005732 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00005733 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00005734 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005735}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005736#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005737
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005738
Guido van Rossumb6775db1994-08-01 11:34:53 +00005739#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005740PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005741"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005742Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005743
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005744static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005745posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005746{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00005747 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005748}
5749#endif /* HAVE_SYMLINK */
5750
5751
5752#ifdef HAVE_TIMES
5753#ifndef HZ
5754#define HZ 60 /* Universal constant :-) */
5755#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005756
Guido van Rossumd48f2521997-12-05 22:19:34 +00005757#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5758static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005759system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005760{
5761 ULONG value = 0;
5762
5763 Py_BEGIN_ALLOW_THREADS
5764 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5765 Py_END_ALLOW_THREADS
5766
5767 return value;
5768}
5769
5770static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005771posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005772{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005773 /* Currently Only Uptime is Provided -- Others Later */
5774 return Py_BuildValue("ddddd",
5775 (double)0 /* t.tms_utime / HZ */,
5776 (double)0 /* t.tms_stime / HZ */,
5777 (double)0 /* t.tms_cutime / HZ */,
5778 (double)0 /* t.tms_cstime / HZ */,
5779 (double)system_uptime() / 1000);
5780}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005781#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005782static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005783posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005784{
5785 struct tms t;
5786 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005787 errno = 0;
5788 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005789 if (c == (clock_t) -1)
5790 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005791 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005792 (double)t.tms_utime / HZ,
5793 (double)t.tms_stime / HZ,
5794 (double)t.tms_cutime / HZ,
5795 (double)t.tms_cstime / HZ,
5796 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005797}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005798#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005799#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005800
5801
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005802#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005803#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005804static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005805posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005806{
5807 FILETIME create, exit, kernel, user;
5808 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005809 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005810 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5811 /* The fields of a FILETIME structure are the hi and lo part
5812 of a 64-bit value expressed in 100 nanosecond units.
5813 1e7 is one second in such units; 1e-7 the inverse.
5814 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5815 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005816 return Py_BuildValue(
5817 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005818 (double)(kernel.dwHighDateTime*429.4967296 +
5819 kernel.dwLowDateTime*1e-7),
5820 (double)(user.dwHighDateTime*429.4967296 +
5821 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005822 (double)0,
5823 (double)0,
5824 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005825}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005826#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005827
5828#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005829PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005830"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005831Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005832#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005833
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005834
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005835#ifdef HAVE_GETSID
5836PyDoc_STRVAR(posix_getsid__doc__,
5837"getsid(pid) -> sid\n\n\
5838Call the system call getsid().");
5839
5840static PyObject *
5841posix_getsid(PyObject *self, PyObject *args)
5842{
5843 int pid, sid;
5844 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5845 return NULL;
5846 sid = getsid(pid);
5847 if (sid < 0)
5848 return posix_error();
5849 return PyInt_FromLong((long)sid);
5850}
5851#endif /* HAVE_GETSID */
5852
5853
Guido van Rossumb6775db1994-08-01 11:34:53 +00005854#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005855PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005856"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005857Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005858
Barry Warsaw53699e91996-12-10 23:23:01 +00005859static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005860posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005861{
Guido van Rossum687dd131993-05-17 08:34:16 +00005862 if (setsid() < 0)
5863 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005864 Py_INCREF(Py_None);
5865 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005866}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005867#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005868
Guido van Rossumb6775db1994-08-01 11:34:53 +00005869#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005870PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005871"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005872Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005873
Barry Warsaw53699e91996-12-10 23:23:01 +00005874static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005875posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005876{
5877 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005878 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005879 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005880 if (setpgid(pid, pgrp) < 0)
5881 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005882 Py_INCREF(Py_None);
5883 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005884}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005885#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005886
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005887
Guido van Rossumb6775db1994-08-01 11:34:53 +00005888#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005889PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005890"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005891Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005892
Barry Warsaw53699e91996-12-10 23:23:01 +00005893static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005894posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005895{
5896 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005897 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005898 return NULL;
5899 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005900 if (pgid < 0)
5901 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005902 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005903}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005904#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005905
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005906
Guido van Rossumb6775db1994-08-01 11:34:53 +00005907#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005908PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005909"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005910Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005911
Barry Warsaw53699e91996-12-10 23:23:01 +00005912static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005913posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005914{
5915 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005916 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005917 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005918 if (tcsetpgrp(fd, pgid) < 0)
5919 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005920 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005921 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005922}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005923#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005924
Guido van Rossum687dd131993-05-17 08:34:16 +00005925/* Functions acting on file descriptors */
5926
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005927PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005928"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005929Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005930
Barry Warsaw53699e91996-12-10 23:23:01 +00005931static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005932posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005933{
Mark Hammondef8b6542001-05-13 08:04:26 +00005934 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005935 int flag;
5936 int mode = 0777;
5937 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005938
5939#ifdef MS_WINDOWS
5940 if (unicode_file_names()) {
5941 PyUnicodeObject *po;
5942 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5943 Py_BEGIN_ALLOW_THREADS
5944 /* PyUnicode_AS_UNICODE OK without thread
5945 lock as it is a simple dereference. */
5946 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5947 Py_END_ALLOW_THREADS
5948 if (fd < 0)
5949 return posix_error();
5950 return PyInt_FromLong((long)fd);
5951 }
5952 /* Drop the argument parsing error as narrow strings
5953 are also valid. */
5954 PyErr_Clear();
5955 }
5956#endif
5957
Tim Peters5aa91602002-01-30 05:46:57 +00005958 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00005959 Py_FileSystemDefaultEncoding, &file,
5960 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00005961 return NULL;
5962
Barry Warsaw53699e91996-12-10 23:23:01 +00005963 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005964 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005965 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005966 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00005967 return posix_error_with_allocated_filename(file);
5968 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00005969 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005970}
5971
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005973PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005974"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005975Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005976
Barry Warsaw53699e91996-12-10 23:23:01 +00005977static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005978posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005979{
5980 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005981 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005982 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005983 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005984 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005985 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005986 if (res < 0)
5987 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005988 Py_INCREF(Py_None);
5989 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005990}
5991
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005992
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005993PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005994"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005995Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005996
Barry Warsaw53699e91996-12-10 23:23:01 +00005997static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005998posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005999{
6000 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006001 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006002 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006003 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006004 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006005 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006006 if (fd < 0)
6007 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006008 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006009}
6010
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006011
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006012PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006013"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006014Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006015
Barry Warsaw53699e91996-12-10 23:23:01 +00006016static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006017posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006018{
6019 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006020 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00006021 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006022 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006023 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00006024 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006025 if (res < 0)
6026 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006027 Py_INCREF(Py_None);
6028 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006029}
6030
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006031
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006032PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006033"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006034Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006035
Barry Warsaw53699e91996-12-10 23:23:01 +00006036static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006037posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006038{
6039 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006040#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006041 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006042#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00006043 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006044#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006045 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006046 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00006047 return NULL;
6048#ifdef SEEK_SET
6049 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6050 switch (how) {
6051 case 0: how = SEEK_SET; break;
6052 case 1: how = SEEK_CUR; break;
6053 case 2: how = SEEK_END; break;
6054 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006055#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006056
6057#if !defined(HAVE_LARGEFILE_SUPPORT)
6058 pos = PyInt_AsLong(posobj);
6059#else
6060 pos = PyLong_Check(posobj) ?
6061 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
6062#endif
6063 if (PyErr_Occurred())
6064 return NULL;
6065
Barry Warsaw53699e91996-12-10 23:23:01 +00006066 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006067#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00006068 res = _lseeki64(fd, pos, how);
6069#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006070 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006071#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006072 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006073 if (res < 0)
6074 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006075
6076#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00006077 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006078#else
6079 return PyLong_FromLongLong(res);
6080#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006081}
6082
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006083
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006084PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006085"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006086Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006087
Barry Warsaw53699e91996-12-10 23:23:01 +00006088static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006089posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006090{
Guido van Rossum8bac5461996-06-11 18:38:48 +00006091 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00006092 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006093 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006094 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00006095 if (size < 0) {
6096 errno = EINVAL;
6097 return posix_error();
6098 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006099 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006100 if (buffer == NULL)
6101 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006102 Py_BEGIN_ALLOW_THREADS
6103 n = read(fd, PyString_AsString(buffer), size);
6104 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00006105 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006106 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00006107 return posix_error();
6108 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00006109 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00006110 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00006111 return buffer;
6112}
6113
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006114
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006115PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006116"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006117Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006118
Barry Warsaw53699e91996-12-10 23:23:01 +00006119static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006120posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006121{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006122 int fd;
6123 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00006124 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006125
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006126 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006127 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006128 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006129 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00006130 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006131 if (size < 0)
6132 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006133 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006134}
6135
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006137PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006138"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006139Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006140
Barry Warsaw53699e91996-12-10 23:23:01 +00006141static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006142posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006143{
6144 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00006145 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00006146 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006147 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006148 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006149#ifdef __VMS
6150 /* on OpenVMS we must ensure that all bytes are written to the file */
6151 fsync(fd);
6152#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006153 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00006154 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00006155 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00006156 if (res != 0) {
6157#ifdef MS_WINDOWS
6158 return win32_error("fstat", NULL);
6159#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006160 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006161#endif
6162 }
Tim Peters5aa91602002-01-30 05:46:57 +00006163
Martin v. Löwis14694662006-02-03 12:54:16 +00006164 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006165}
6166
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006168PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006169"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006170Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006171
Barry Warsaw53699e91996-12-10 23:23:01 +00006172static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006173posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006174{
Guido van Rossum687dd131993-05-17 08:34:16 +00006175 int fd;
Kristján Valur Jónssona1392d52007-05-07 19:25:38 +00006176 char *orgmode = "r";
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006177 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00006178 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00006179 PyObject *f;
Kristján Valur Jónssona1392d52007-05-07 19:25:38 +00006180 char *mode;
6181 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00006182 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006183
Kristján Valur Jónssona1392d52007-05-07 19:25:38 +00006184 /* Sanitize mode. See fileobject.c */
6185 mode = PyMem_MALLOC(strlen(orgmode)+3);
6186 if (!mode) {
6187 PyErr_NoMemory();
6188 return NULL;
6189 }
6190 strcpy(mode, orgmode);
6191 if (_PyFile_SanitizeMode(mode)) {
6192 PyMem_FREE(mode);
Thomas Heller1f043e22002-11-07 16:00:59 +00006193 return NULL;
6194 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006195 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006196#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Georg Brandl54a188a2006-03-31 20:00:11 +00006197 if (mode[0] == 'a') {
6198 /* try to make sure the O_APPEND flag is set */
6199 int flags;
6200 flags = fcntl(fd, F_GETFL);
6201 if (flags != -1)
6202 fcntl(fd, F_SETFL, flags | O_APPEND);
6203 fp = fdopen(fd, mode);
Thomas Wouters2a9a6b02006-03-31 22:38:19 +00006204 if (fp == NULL && flags != -1)
Georg Brandl54a188a2006-03-31 20:00:11 +00006205 /* restore old mode if fdopen failed */
6206 fcntl(fd, F_SETFL, flags);
6207 } else {
6208 fp = fdopen(fd, mode);
6209 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006210#else
6211 fp = fdopen(fd, mode);
6212#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006213 Py_END_ALLOW_THREADS
Neal Norwitz6ca6f142007-05-09 06:45:53 +00006214 PyMem_FREE(mode);
Guido van Rossum687dd131993-05-17 08:34:16 +00006215 if (fp == NULL)
6216 return posix_error();
Kristján Valur Jónssona1392d52007-05-07 19:25:38 +00006217 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006218 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00006219 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006220 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006221}
6222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006223PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006224"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006225Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006226connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006227
6228static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006229posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006230{
6231 int fd;
6232 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6233 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006234 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006235}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006236
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006237#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006238PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006239"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006240Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006241
Barry Warsaw53699e91996-12-10 23:23:01 +00006242static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006243posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006244{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006245#if defined(PYOS_OS2)
6246 HFILE read, write;
6247 APIRET rc;
6248
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006249 Py_BEGIN_ALLOW_THREADS
6250 rc = DosCreatePipe( &read, &write, 4096);
6251 Py_END_ALLOW_THREADS
6252 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006253 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006254
6255 return Py_BuildValue("(ii)", read, write);
6256#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006257#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00006258 int fds[2];
6259 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00006260 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006261 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00006262 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006263 if (res != 0)
6264 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006265 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006266#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00006267 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006268 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00006269 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00006270 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006271 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00006272 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00006273 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006274 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00006275 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6276 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006277 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006278#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006279#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006280}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006281#endif /* HAVE_PIPE */
6282
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006283
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006284#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006285PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006286"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006287Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006288
Barry Warsaw53699e91996-12-10 23:23:01 +00006289static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006290posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006291{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006292 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006293 int mode = 0666;
6294 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006295 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006296 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006297 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006298 res = mkfifo(filename, mode);
6299 Py_END_ALLOW_THREADS
6300 if (res < 0)
6301 return posix_error();
6302 Py_INCREF(Py_None);
6303 return Py_None;
6304}
6305#endif
6306
6307
Neal Norwitz11690112002-07-30 01:08:28 +00006308#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006309PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006310"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006311Create a filesystem node (file, device special file or named pipe)\n\
6312named filename. mode specifies both the permissions to use and the\n\
6313type of node to be created, being combined (bitwise OR) with one of\n\
6314S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006315device defines the newly created device special file (probably using\n\
6316os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006317
6318
6319static PyObject *
6320posix_mknod(PyObject *self, PyObject *args)
6321{
6322 char *filename;
6323 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006324 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006325 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00006326 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006327 return NULL;
6328 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006329 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00006330 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006331 if (res < 0)
6332 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006333 Py_INCREF(Py_None);
6334 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006335}
6336#endif
6337
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006338#ifdef HAVE_DEVICE_MACROS
6339PyDoc_STRVAR(posix_major__doc__,
6340"major(device) -> major number\n\
6341Extracts a device major number from a raw device number.");
6342
6343static PyObject *
6344posix_major(PyObject *self, PyObject *args)
6345{
6346 int device;
6347 if (!PyArg_ParseTuple(args, "i:major", &device))
6348 return NULL;
6349 return PyInt_FromLong((long)major(device));
6350}
6351
6352PyDoc_STRVAR(posix_minor__doc__,
6353"minor(device) -> minor number\n\
6354Extracts a device minor number from a raw device number.");
6355
6356static PyObject *
6357posix_minor(PyObject *self, PyObject *args)
6358{
6359 int device;
6360 if (!PyArg_ParseTuple(args, "i:minor", &device))
6361 return NULL;
6362 return PyInt_FromLong((long)minor(device));
6363}
6364
6365PyDoc_STRVAR(posix_makedev__doc__,
6366"makedev(major, minor) -> device number\n\
6367Composes a raw device number from the major and minor device numbers.");
6368
6369static PyObject *
6370posix_makedev(PyObject *self, PyObject *args)
6371{
6372 int major, minor;
6373 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6374 return NULL;
6375 return PyInt_FromLong((long)makedev(major, minor));
6376}
6377#endif /* device macros */
6378
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006379
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006380#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006381PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006382"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006383Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006384
Barry Warsaw53699e91996-12-10 23:23:01 +00006385static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006386posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006387{
6388 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006389 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006390 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006391 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006392
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006393 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006394 return NULL;
6395
6396#if !defined(HAVE_LARGEFILE_SUPPORT)
6397 length = PyInt_AsLong(lenobj);
6398#else
6399 length = PyLong_Check(lenobj) ?
6400 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6401#endif
6402 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006403 return NULL;
6404
Barry Warsaw53699e91996-12-10 23:23:01 +00006405 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006406 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00006407 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006408 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006409 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006410 return NULL;
6411 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006412 Py_INCREF(Py_None);
6413 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006414}
6415#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006416
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006417#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006418PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006419"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006420Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006421
Fred Drake762e2061999-08-26 17:23:54 +00006422/* Save putenv() parameters as values here, so we can collect them when they
6423 * get re-set with another call for the same key. */
6424static PyObject *posix_putenv_garbage;
6425
Tim Peters5aa91602002-01-30 05:46:57 +00006426static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006427posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006428{
6429 char *s1, *s2;
Anthony Baxter64182fe2006-04-11 12:14:09 +00006430 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00006431 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00006432 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006433
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006434 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006435 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006436
6437#if defined(PYOS_OS2)
6438 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6439 APIRET rc;
6440
Guido van Rossumd48f2521997-12-05 22:19:34 +00006441 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6442 if (rc != NO_ERROR)
6443 return os2_error(rc);
6444
6445 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6446 APIRET rc;
6447
Guido van Rossumd48f2521997-12-05 22:19:34 +00006448 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6449 if (rc != NO_ERROR)
6450 return os2_error(rc);
6451 } else {
6452#endif
6453
Fred Drake762e2061999-08-26 17:23:54 +00006454 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00006455 len = strlen(s1) + strlen(s2) + 2;
6456 /* len includes space for a trailing \0; the size arg to
6457 PyString_FromStringAndSize does not count that */
6458 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00006459 if (newstr == NULL)
6460 return PyErr_NoMemory();
Anthony Baxter64182fe2006-04-11 12:14:09 +00006461 newenv = PyString_AS_STRING(newstr);
6462 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6463 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00006464 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006465 posix_error();
6466 return NULL;
6467 }
Fred Drake762e2061999-08-26 17:23:54 +00006468 /* Install the first arg and newstr in posix_putenv_garbage;
6469 * this will cause previous value to be collected. This has to
6470 * happen after the real putenv() call because the old value
6471 * was still accessible until then. */
6472 if (PyDict_SetItem(posix_putenv_garbage,
6473 PyTuple_GET_ITEM(args, 0), newstr)) {
6474 /* really not much we can do; just leak */
6475 PyErr_Clear();
6476 }
6477 else {
6478 Py_DECREF(newstr);
6479 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006480
6481#if defined(PYOS_OS2)
6482 }
6483#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006484 Py_INCREF(Py_None);
6485 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006486}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006487#endif /* putenv */
6488
Guido van Rossumc524d952001-10-19 01:31:59 +00006489#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006490PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006491"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006492Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006493
6494static PyObject *
6495posix_unsetenv(PyObject *self, PyObject *args)
6496{
6497 char *s1;
6498
6499 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6500 return NULL;
6501
6502 unsetenv(s1);
6503
6504 /* Remove the key from posix_putenv_garbage;
6505 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00006506 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00006507 * old value was still accessible until then.
6508 */
6509 if (PyDict_DelItem(posix_putenv_garbage,
6510 PyTuple_GET_ITEM(args, 0))) {
6511 /* really not much we can do; just leak */
6512 PyErr_Clear();
6513 }
6514
6515 Py_INCREF(Py_None);
6516 return Py_None;
6517}
6518#endif /* unsetenv */
6519
Guido van Rossumb6a47161997-09-15 22:54:34 +00006520#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006521PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006522"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006523Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006524
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006525static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006526posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006527{
6528 int code;
6529 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006530 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00006531 return NULL;
6532 message = strerror(code);
6533 if (message == NULL) {
6534 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00006535 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006536 return NULL;
6537 }
6538 return PyString_FromString(message);
6539}
6540#endif /* strerror */
6541
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006542
Guido van Rossumc9641791998-08-04 15:26:23 +00006543#ifdef HAVE_SYS_WAIT_H
6544
Fred Drake106c1a02002-04-23 15:58:02 +00006545#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006546PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006547"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006548Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006549
6550static PyObject *
6551posix_WCOREDUMP(PyObject *self, PyObject *args)
6552{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006553 WAIT_TYPE status;
6554 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006555
Neal Norwitzd5a37542006-03-20 06:48:34 +00006556 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006557 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006558
6559 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006560}
6561#endif /* WCOREDUMP */
6562
6563#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006564PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006565"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006566Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006567job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006568
6569static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006570posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006571{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006572 WAIT_TYPE status;
6573 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006574
Neal Norwitzd5a37542006-03-20 06:48:34 +00006575 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006576 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006577
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006578 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006579}
6580#endif /* WIFCONTINUED */
6581
Guido van Rossumc9641791998-08-04 15:26:23 +00006582#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006583PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006584"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006585Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006586
6587static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006588posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006589{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006590 WAIT_TYPE status;
6591 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006592
Neal Norwitzd5a37542006-03-20 06:48:34 +00006593 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006594 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006595
Fred Drake106c1a02002-04-23 15:58:02 +00006596 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006597}
6598#endif /* WIFSTOPPED */
6599
6600#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006601PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006602"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006603Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006604
6605static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006606posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006607{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006608 WAIT_TYPE status;
6609 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006610
Neal Norwitzd5a37542006-03-20 06:48:34 +00006611 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006612 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006613
Fred Drake106c1a02002-04-23 15:58:02 +00006614 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006615}
6616#endif /* WIFSIGNALED */
6617
6618#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006619PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006620"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006621Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006622system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006623
6624static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006625posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006626{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006627 WAIT_TYPE status;
6628 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006629
Neal Norwitzd5a37542006-03-20 06:48:34 +00006630 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006631 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006632
Fred Drake106c1a02002-04-23 15:58:02 +00006633 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006634}
6635#endif /* WIFEXITED */
6636
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006637#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006638PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006639"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006640Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006641
6642static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006643posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006644{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006645 WAIT_TYPE status;
6646 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006647
Neal Norwitzd5a37542006-03-20 06:48:34 +00006648 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006649 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006650
Guido van Rossumc9641791998-08-04 15:26:23 +00006651 return Py_BuildValue("i", WEXITSTATUS(status));
6652}
6653#endif /* WEXITSTATUS */
6654
6655#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006656PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006657"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006658Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006659value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006660
6661static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006662posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006663{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006664 WAIT_TYPE status;
6665 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006666
Neal Norwitzd5a37542006-03-20 06:48:34 +00006667 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006668 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006669
Guido van Rossumc9641791998-08-04 15:26:23 +00006670 return Py_BuildValue("i", WTERMSIG(status));
6671}
6672#endif /* WTERMSIG */
6673
6674#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006675PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006676"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006677Return the signal that stopped the process that provided\n\
6678the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006679
6680static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006681posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006682{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006683 WAIT_TYPE status;
6684 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006685
Neal Norwitzd5a37542006-03-20 06:48:34 +00006686 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006687 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006688
Guido van Rossumc9641791998-08-04 15:26:23 +00006689 return Py_BuildValue("i", WSTOPSIG(status));
6690}
6691#endif /* WSTOPSIG */
6692
6693#endif /* HAVE_SYS_WAIT_H */
6694
6695
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006696#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006697#ifdef _SCO_DS
6698/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6699 needed definitions in sys/statvfs.h */
6700#define _SVID3
6701#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006702#include <sys/statvfs.h>
6703
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006704static PyObject*
6705_pystatvfs_fromstructstatvfs(struct statvfs st) {
6706 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6707 if (v == NULL)
6708 return NULL;
6709
6710#if !defined(HAVE_LARGEFILE_SUPPORT)
6711 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6712 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6713 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6714 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6715 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6716 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6717 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6718 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6719 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6720 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6721#else
6722 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6723 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006724 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006725 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006726 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006727 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006728 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006729 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006730 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006731 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006732 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006733 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006734 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006735 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006736 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6737 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6738#endif
6739
6740 return v;
6741}
6742
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006743PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006744"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006745Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006746
6747static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006748posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006749{
6750 int fd, res;
6751 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006752
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006753 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006754 return NULL;
6755 Py_BEGIN_ALLOW_THREADS
6756 res = fstatvfs(fd, &st);
6757 Py_END_ALLOW_THREADS
6758 if (res != 0)
6759 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006760
6761 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006762}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006763#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006764
6765
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006766#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006767#include <sys/statvfs.h>
6768
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006769PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006770"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006771Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006772
6773static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006774posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006775{
6776 char *path;
6777 int res;
6778 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006779 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006780 return NULL;
6781 Py_BEGIN_ALLOW_THREADS
6782 res = statvfs(path, &st);
6783 Py_END_ALLOW_THREADS
6784 if (res != 0)
6785 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006786
6787 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006788}
6789#endif /* HAVE_STATVFS */
6790
6791
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006792#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006793PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006794"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006795Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006796The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006797or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006798
6799static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006800posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006801{
6802 PyObject *result = NULL;
6803 char *dir = NULL;
6804 char *pfx = NULL;
6805 char *name;
6806
6807 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6808 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006809
6810 if (PyErr_Warn(PyExc_RuntimeWarning,
6811 "tempnam is a potential security risk to your program") < 0)
6812 return NULL;
6813
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006814#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006815 name = _tempnam(dir, pfx);
6816#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006817 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006818#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006819 if (name == NULL)
6820 return PyErr_NoMemory();
6821 result = PyString_FromString(name);
6822 free(name);
6823 return result;
6824}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006825#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006826
6827
6828#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006829PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006830"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006831Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006832
6833static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006834posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006835{
6836 FILE *fp;
6837
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006838 fp = tmpfile();
6839 if (fp == NULL)
6840 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006841 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006842}
6843#endif
6844
6845
6846#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006847PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006848"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006849Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006850
6851static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006852posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006853{
6854 char buffer[L_tmpnam];
6855 char *name;
6856
Skip Montanaro95618b52001-08-18 18:52:10 +00006857 if (PyErr_Warn(PyExc_RuntimeWarning,
6858 "tmpnam is a potential security risk to your program") < 0)
6859 return NULL;
6860
Greg Wardb48bc172000-03-01 21:51:56 +00006861#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006862 name = tmpnam_r(buffer);
6863#else
6864 name = tmpnam(buffer);
6865#endif
6866 if (name == NULL) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006867 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006868#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006869 "unexpected NULL from tmpnam_r"
6870#else
6871 "unexpected NULL from tmpnam"
6872#endif
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006873 );
6874 PyErr_SetObject(PyExc_OSError, err);
6875 Py_XDECREF(err);
6876 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006877 }
6878 return PyString_FromString(buffer);
6879}
6880#endif
6881
6882
Fred Drakec9680921999-12-13 16:37:25 +00006883/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6884 * It maps strings representing configuration variable names to
6885 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006886 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006887 * rarely-used constants. There are three separate tables that use
6888 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006889 *
6890 * This code is always included, even if none of the interfaces that
6891 * need it are included. The #if hackery needed to avoid it would be
6892 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006893 */
6894struct constdef {
6895 char *name;
6896 long value;
6897};
6898
Fred Drake12c6e2d1999-12-14 21:25:03 +00006899static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006900conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6901 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006902{
6903 if (PyInt_Check(arg)) {
6904 *valuep = PyInt_AS_LONG(arg);
6905 return 1;
6906 }
6907 if (PyString_Check(arg)) {
6908 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006909 size_t lo = 0;
6910 size_t mid;
6911 size_t hi = tablesize;
6912 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006913 char *confname = PyString_AS_STRING(arg);
6914 while (lo < hi) {
6915 mid = (lo + hi) / 2;
6916 cmp = strcmp(confname, table[mid].name);
6917 if (cmp < 0)
6918 hi = mid;
6919 else if (cmp > 0)
6920 lo = mid + 1;
6921 else {
6922 *valuep = table[mid].value;
6923 return 1;
6924 }
6925 }
6926 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6927 }
6928 else
6929 PyErr_SetString(PyExc_TypeError,
6930 "configuration names must be strings or integers");
6931 return 0;
6932}
6933
6934
6935#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6936static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006937#ifdef _PC_ABI_AIO_XFER_MAX
6938 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6939#endif
6940#ifdef _PC_ABI_ASYNC_IO
6941 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6942#endif
Fred Drakec9680921999-12-13 16:37:25 +00006943#ifdef _PC_ASYNC_IO
6944 {"PC_ASYNC_IO", _PC_ASYNC_IO},
6945#endif
6946#ifdef _PC_CHOWN_RESTRICTED
6947 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
6948#endif
6949#ifdef _PC_FILESIZEBITS
6950 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
6951#endif
6952#ifdef _PC_LAST
6953 {"PC_LAST", _PC_LAST},
6954#endif
6955#ifdef _PC_LINK_MAX
6956 {"PC_LINK_MAX", _PC_LINK_MAX},
6957#endif
6958#ifdef _PC_MAX_CANON
6959 {"PC_MAX_CANON", _PC_MAX_CANON},
6960#endif
6961#ifdef _PC_MAX_INPUT
6962 {"PC_MAX_INPUT", _PC_MAX_INPUT},
6963#endif
6964#ifdef _PC_NAME_MAX
6965 {"PC_NAME_MAX", _PC_NAME_MAX},
6966#endif
6967#ifdef _PC_NO_TRUNC
6968 {"PC_NO_TRUNC", _PC_NO_TRUNC},
6969#endif
6970#ifdef _PC_PATH_MAX
6971 {"PC_PATH_MAX", _PC_PATH_MAX},
6972#endif
6973#ifdef _PC_PIPE_BUF
6974 {"PC_PIPE_BUF", _PC_PIPE_BUF},
6975#endif
6976#ifdef _PC_PRIO_IO
6977 {"PC_PRIO_IO", _PC_PRIO_IO},
6978#endif
6979#ifdef _PC_SOCK_MAXBUF
6980 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
6981#endif
6982#ifdef _PC_SYNC_IO
6983 {"PC_SYNC_IO", _PC_SYNC_IO},
6984#endif
6985#ifdef _PC_VDISABLE
6986 {"PC_VDISABLE", _PC_VDISABLE},
6987#endif
6988};
6989
Fred Drakec9680921999-12-13 16:37:25 +00006990static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006991conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006992{
6993 return conv_confname(arg, valuep, posix_constants_pathconf,
6994 sizeof(posix_constants_pathconf)
6995 / sizeof(struct constdef));
6996}
6997#endif
6998
6999#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007000PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007001"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007002Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007003If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007004
7005static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007006posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007007{
7008 PyObject *result = NULL;
7009 int name, fd;
7010
Fred Drake12c6e2d1999-12-14 21:25:03 +00007011 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7012 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00007013 long limit;
7014
7015 errno = 0;
7016 limit = fpathconf(fd, name);
7017 if (limit == -1 && errno != 0)
7018 posix_error();
7019 else
7020 result = PyInt_FromLong(limit);
7021 }
7022 return result;
7023}
7024#endif
7025
7026
7027#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007028PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007029"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007030Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007031If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007032
7033static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007034posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007035{
7036 PyObject *result = NULL;
7037 int name;
7038 char *path;
7039
7040 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7041 conv_path_confname, &name)) {
7042 long limit;
7043
7044 errno = 0;
7045 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007046 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00007047 if (errno == EINVAL)
7048 /* could be a path or name problem */
7049 posix_error();
7050 else
7051 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007052 }
Fred Drakec9680921999-12-13 16:37:25 +00007053 else
7054 result = PyInt_FromLong(limit);
7055 }
7056 return result;
7057}
7058#endif
7059
7060#ifdef HAVE_CONFSTR
7061static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007062#ifdef _CS_ARCHITECTURE
7063 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
7064#endif
7065#ifdef _CS_HOSTNAME
7066 {"CS_HOSTNAME", _CS_HOSTNAME},
7067#endif
7068#ifdef _CS_HW_PROVIDER
7069 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
7070#endif
7071#ifdef _CS_HW_SERIAL
7072 {"CS_HW_SERIAL", _CS_HW_SERIAL},
7073#endif
7074#ifdef _CS_INITTAB_NAME
7075 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
7076#endif
Fred Drakec9680921999-12-13 16:37:25 +00007077#ifdef _CS_LFS64_CFLAGS
7078 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
7079#endif
7080#ifdef _CS_LFS64_LDFLAGS
7081 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
7082#endif
7083#ifdef _CS_LFS64_LIBS
7084 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
7085#endif
7086#ifdef _CS_LFS64_LINTFLAGS
7087 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
7088#endif
7089#ifdef _CS_LFS_CFLAGS
7090 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
7091#endif
7092#ifdef _CS_LFS_LDFLAGS
7093 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
7094#endif
7095#ifdef _CS_LFS_LIBS
7096 {"CS_LFS_LIBS", _CS_LFS_LIBS},
7097#endif
7098#ifdef _CS_LFS_LINTFLAGS
7099 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
7100#endif
Fred Draked86ed291999-12-15 15:34:33 +00007101#ifdef _CS_MACHINE
7102 {"CS_MACHINE", _CS_MACHINE},
7103#endif
Fred Drakec9680921999-12-13 16:37:25 +00007104#ifdef _CS_PATH
7105 {"CS_PATH", _CS_PATH},
7106#endif
Fred Draked86ed291999-12-15 15:34:33 +00007107#ifdef _CS_RELEASE
7108 {"CS_RELEASE", _CS_RELEASE},
7109#endif
7110#ifdef _CS_SRPC_DOMAIN
7111 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
7112#endif
7113#ifdef _CS_SYSNAME
7114 {"CS_SYSNAME", _CS_SYSNAME},
7115#endif
7116#ifdef _CS_VERSION
7117 {"CS_VERSION", _CS_VERSION},
7118#endif
Fred Drakec9680921999-12-13 16:37:25 +00007119#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7120 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
7121#endif
7122#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7123 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
7124#endif
7125#ifdef _CS_XBS5_ILP32_OFF32_LIBS
7126 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
7127#endif
7128#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7129 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
7130#endif
7131#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7132 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
7133#endif
7134#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7135 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
7136#endif
7137#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7138 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
7139#endif
7140#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7141 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
7142#endif
7143#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7144 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
7145#endif
7146#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7147 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
7148#endif
7149#ifdef _CS_XBS5_LP64_OFF64_LIBS
7150 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
7151#endif
7152#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7153 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
7154#endif
7155#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7156 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
7157#endif
7158#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7159 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
7160#endif
7161#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7162 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
7163#endif
7164#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7165 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
7166#endif
Fred Draked86ed291999-12-15 15:34:33 +00007167#ifdef _MIPS_CS_AVAIL_PROCESSORS
7168 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
7169#endif
7170#ifdef _MIPS_CS_BASE
7171 {"MIPS_CS_BASE", _MIPS_CS_BASE},
7172#endif
7173#ifdef _MIPS_CS_HOSTID
7174 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
7175#endif
7176#ifdef _MIPS_CS_HW_NAME
7177 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7178#endif
7179#ifdef _MIPS_CS_NUM_PROCESSORS
7180 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7181#endif
7182#ifdef _MIPS_CS_OSREL_MAJ
7183 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7184#endif
7185#ifdef _MIPS_CS_OSREL_MIN
7186 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7187#endif
7188#ifdef _MIPS_CS_OSREL_PATCH
7189 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7190#endif
7191#ifdef _MIPS_CS_OS_NAME
7192 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7193#endif
7194#ifdef _MIPS_CS_OS_PROVIDER
7195 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7196#endif
7197#ifdef _MIPS_CS_PROCESSORS
7198 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7199#endif
7200#ifdef _MIPS_CS_SERIAL
7201 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7202#endif
7203#ifdef _MIPS_CS_VENDOR
7204 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7205#endif
Fred Drakec9680921999-12-13 16:37:25 +00007206};
7207
7208static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007209conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007210{
7211 return conv_confname(arg, valuep, posix_constants_confstr,
7212 sizeof(posix_constants_confstr)
7213 / sizeof(struct constdef));
7214}
7215
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007216PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007217"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007218Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007219
7220static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007221posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007222{
7223 PyObject *result = NULL;
7224 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007225 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007226
7227 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Skip Montanarodd527fc2006-04-18 00:49:49 +00007228 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007229
Fred Drakec9680921999-12-13 16:37:25 +00007230 errno = 0;
Skip Montanarodd527fc2006-04-18 00:49:49 +00007231 len = confstr(name, buffer, sizeof(buffer));
Skip Montanaro94785ef2006-04-20 01:29:48 +00007232 if (len == 0) {
7233 if (errno) {
7234 posix_error();
7235 }
7236 else {
7237 result = Py_None;
7238 Py_INCREF(Py_None);
7239 }
Fred Drakec9680921999-12-13 16:37:25 +00007240 }
7241 else {
Neal Norwitz0d21b1e2006-04-20 06:44:42 +00007242 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz449b24e2006-04-20 06:56:05 +00007243 result = PyString_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007244 if (result != NULL)
Neal Norwitz449b24e2006-04-20 06:56:05 +00007245 confstr(name, PyString_AS_STRING(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00007246 }
7247 else
Neal Norwitz449b24e2006-04-20 06:56:05 +00007248 result = PyString_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007249 }
7250 }
7251 return result;
7252}
7253#endif
7254
7255
7256#ifdef HAVE_SYSCONF
7257static struct constdef posix_constants_sysconf[] = {
7258#ifdef _SC_2_CHAR_TERM
7259 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7260#endif
7261#ifdef _SC_2_C_BIND
7262 {"SC_2_C_BIND", _SC_2_C_BIND},
7263#endif
7264#ifdef _SC_2_C_DEV
7265 {"SC_2_C_DEV", _SC_2_C_DEV},
7266#endif
7267#ifdef _SC_2_C_VERSION
7268 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7269#endif
7270#ifdef _SC_2_FORT_DEV
7271 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7272#endif
7273#ifdef _SC_2_FORT_RUN
7274 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7275#endif
7276#ifdef _SC_2_LOCALEDEF
7277 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7278#endif
7279#ifdef _SC_2_SW_DEV
7280 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7281#endif
7282#ifdef _SC_2_UPE
7283 {"SC_2_UPE", _SC_2_UPE},
7284#endif
7285#ifdef _SC_2_VERSION
7286 {"SC_2_VERSION", _SC_2_VERSION},
7287#endif
Fred Draked86ed291999-12-15 15:34:33 +00007288#ifdef _SC_ABI_ASYNCHRONOUS_IO
7289 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7290#endif
7291#ifdef _SC_ACL
7292 {"SC_ACL", _SC_ACL},
7293#endif
Fred Drakec9680921999-12-13 16:37:25 +00007294#ifdef _SC_AIO_LISTIO_MAX
7295 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7296#endif
Fred Drakec9680921999-12-13 16:37:25 +00007297#ifdef _SC_AIO_MAX
7298 {"SC_AIO_MAX", _SC_AIO_MAX},
7299#endif
7300#ifdef _SC_AIO_PRIO_DELTA_MAX
7301 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7302#endif
7303#ifdef _SC_ARG_MAX
7304 {"SC_ARG_MAX", _SC_ARG_MAX},
7305#endif
7306#ifdef _SC_ASYNCHRONOUS_IO
7307 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7308#endif
7309#ifdef _SC_ATEXIT_MAX
7310 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7311#endif
Fred Draked86ed291999-12-15 15:34:33 +00007312#ifdef _SC_AUDIT
7313 {"SC_AUDIT", _SC_AUDIT},
7314#endif
Fred Drakec9680921999-12-13 16:37:25 +00007315#ifdef _SC_AVPHYS_PAGES
7316 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7317#endif
7318#ifdef _SC_BC_BASE_MAX
7319 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7320#endif
7321#ifdef _SC_BC_DIM_MAX
7322 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7323#endif
7324#ifdef _SC_BC_SCALE_MAX
7325 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7326#endif
7327#ifdef _SC_BC_STRING_MAX
7328 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7329#endif
Fred Draked86ed291999-12-15 15:34:33 +00007330#ifdef _SC_CAP
7331 {"SC_CAP", _SC_CAP},
7332#endif
Fred Drakec9680921999-12-13 16:37:25 +00007333#ifdef _SC_CHARCLASS_NAME_MAX
7334 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7335#endif
7336#ifdef _SC_CHAR_BIT
7337 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7338#endif
7339#ifdef _SC_CHAR_MAX
7340 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7341#endif
7342#ifdef _SC_CHAR_MIN
7343 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7344#endif
7345#ifdef _SC_CHILD_MAX
7346 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7347#endif
7348#ifdef _SC_CLK_TCK
7349 {"SC_CLK_TCK", _SC_CLK_TCK},
7350#endif
7351#ifdef _SC_COHER_BLKSZ
7352 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7353#endif
7354#ifdef _SC_COLL_WEIGHTS_MAX
7355 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7356#endif
7357#ifdef _SC_DCACHE_ASSOC
7358 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7359#endif
7360#ifdef _SC_DCACHE_BLKSZ
7361 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7362#endif
7363#ifdef _SC_DCACHE_LINESZ
7364 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7365#endif
7366#ifdef _SC_DCACHE_SZ
7367 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7368#endif
7369#ifdef _SC_DCACHE_TBLKSZ
7370 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7371#endif
7372#ifdef _SC_DELAYTIMER_MAX
7373 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7374#endif
7375#ifdef _SC_EQUIV_CLASS_MAX
7376 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7377#endif
7378#ifdef _SC_EXPR_NEST_MAX
7379 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7380#endif
7381#ifdef _SC_FSYNC
7382 {"SC_FSYNC", _SC_FSYNC},
7383#endif
7384#ifdef _SC_GETGR_R_SIZE_MAX
7385 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7386#endif
7387#ifdef _SC_GETPW_R_SIZE_MAX
7388 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7389#endif
7390#ifdef _SC_ICACHE_ASSOC
7391 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7392#endif
7393#ifdef _SC_ICACHE_BLKSZ
7394 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7395#endif
7396#ifdef _SC_ICACHE_LINESZ
7397 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7398#endif
7399#ifdef _SC_ICACHE_SZ
7400 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7401#endif
Fred Draked86ed291999-12-15 15:34:33 +00007402#ifdef _SC_INF
7403 {"SC_INF", _SC_INF},
7404#endif
Fred Drakec9680921999-12-13 16:37:25 +00007405#ifdef _SC_INT_MAX
7406 {"SC_INT_MAX", _SC_INT_MAX},
7407#endif
7408#ifdef _SC_INT_MIN
7409 {"SC_INT_MIN", _SC_INT_MIN},
7410#endif
7411#ifdef _SC_IOV_MAX
7412 {"SC_IOV_MAX", _SC_IOV_MAX},
7413#endif
Fred Draked86ed291999-12-15 15:34:33 +00007414#ifdef _SC_IP_SECOPTS
7415 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7416#endif
Fred Drakec9680921999-12-13 16:37:25 +00007417#ifdef _SC_JOB_CONTROL
7418 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7419#endif
Fred Draked86ed291999-12-15 15:34:33 +00007420#ifdef _SC_KERN_POINTERS
7421 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7422#endif
7423#ifdef _SC_KERN_SIM
7424 {"SC_KERN_SIM", _SC_KERN_SIM},
7425#endif
Fred Drakec9680921999-12-13 16:37:25 +00007426#ifdef _SC_LINE_MAX
7427 {"SC_LINE_MAX", _SC_LINE_MAX},
7428#endif
7429#ifdef _SC_LOGIN_NAME_MAX
7430 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7431#endif
7432#ifdef _SC_LOGNAME_MAX
7433 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7434#endif
7435#ifdef _SC_LONG_BIT
7436 {"SC_LONG_BIT", _SC_LONG_BIT},
7437#endif
Fred Draked86ed291999-12-15 15:34:33 +00007438#ifdef _SC_MAC
7439 {"SC_MAC", _SC_MAC},
7440#endif
Fred Drakec9680921999-12-13 16:37:25 +00007441#ifdef _SC_MAPPED_FILES
7442 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7443#endif
7444#ifdef _SC_MAXPID
7445 {"SC_MAXPID", _SC_MAXPID},
7446#endif
7447#ifdef _SC_MB_LEN_MAX
7448 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7449#endif
7450#ifdef _SC_MEMLOCK
7451 {"SC_MEMLOCK", _SC_MEMLOCK},
7452#endif
7453#ifdef _SC_MEMLOCK_RANGE
7454 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7455#endif
7456#ifdef _SC_MEMORY_PROTECTION
7457 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7458#endif
7459#ifdef _SC_MESSAGE_PASSING
7460 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7461#endif
Fred Draked86ed291999-12-15 15:34:33 +00007462#ifdef _SC_MMAP_FIXED_ALIGNMENT
7463 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7464#endif
Fred Drakec9680921999-12-13 16:37:25 +00007465#ifdef _SC_MQ_OPEN_MAX
7466 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7467#endif
7468#ifdef _SC_MQ_PRIO_MAX
7469 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7470#endif
Fred Draked86ed291999-12-15 15:34:33 +00007471#ifdef _SC_NACLS_MAX
7472 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7473#endif
Fred Drakec9680921999-12-13 16:37:25 +00007474#ifdef _SC_NGROUPS_MAX
7475 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7476#endif
7477#ifdef _SC_NL_ARGMAX
7478 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7479#endif
7480#ifdef _SC_NL_LANGMAX
7481 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7482#endif
7483#ifdef _SC_NL_MSGMAX
7484 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7485#endif
7486#ifdef _SC_NL_NMAX
7487 {"SC_NL_NMAX", _SC_NL_NMAX},
7488#endif
7489#ifdef _SC_NL_SETMAX
7490 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7491#endif
7492#ifdef _SC_NL_TEXTMAX
7493 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7494#endif
7495#ifdef _SC_NPROCESSORS_CONF
7496 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7497#endif
7498#ifdef _SC_NPROCESSORS_ONLN
7499 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7500#endif
Fred Draked86ed291999-12-15 15:34:33 +00007501#ifdef _SC_NPROC_CONF
7502 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7503#endif
7504#ifdef _SC_NPROC_ONLN
7505 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7506#endif
Fred Drakec9680921999-12-13 16:37:25 +00007507#ifdef _SC_NZERO
7508 {"SC_NZERO", _SC_NZERO},
7509#endif
7510#ifdef _SC_OPEN_MAX
7511 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7512#endif
7513#ifdef _SC_PAGESIZE
7514 {"SC_PAGESIZE", _SC_PAGESIZE},
7515#endif
7516#ifdef _SC_PAGE_SIZE
7517 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7518#endif
7519#ifdef _SC_PASS_MAX
7520 {"SC_PASS_MAX", _SC_PASS_MAX},
7521#endif
7522#ifdef _SC_PHYS_PAGES
7523 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7524#endif
7525#ifdef _SC_PII
7526 {"SC_PII", _SC_PII},
7527#endif
7528#ifdef _SC_PII_INTERNET
7529 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7530#endif
7531#ifdef _SC_PII_INTERNET_DGRAM
7532 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7533#endif
7534#ifdef _SC_PII_INTERNET_STREAM
7535 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7536#endif
7537#ifdef _SC_PII_OSI
7538 {"SC_PII_OSI", _SC_PII_OSI},
7539#endif
7540#ifdef _SC_PII_OSI_CLTS
7541 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7542#endif
7543#ifdef _SC_PII_OSI_COTS
7544 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7545#endif
7546#ifdef _SC_PII_OSI_M
7547 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7548#endif
7549#ifdef _SC_PII_SOCKET
7550 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7551#endif
7552#ifdef _SC_PII_XTI
7553 {"SC_PII_XTI", _SC_PII_XTI},
7554#endif
7555#ifdef _SC_POLL
7556 {"SC_POLL", _SC_POLL},
7557#endif
7558#ifdef _SC_PRIORITIZED_IO
7559 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7560#endif
7561#ifdef _SC_PRIORITY_SCHEDULING
7562 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7563#endif
7564#ifdef _SC_REALTIME_SIGNALS
7565 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7566#endif
7567#ifdef _SC_RE_DUP_MAX
7568 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7569#endif
7570#ifdef _SC_RTSIG_MAX
7571 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7572#endif
7573#ifdef _SC_SAVED_IDS
7574 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7575#endif
7576#ifdef _SC_SCHAR_MAX
7577 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7578#endif
7579#ifdef _SC_SCHAR_MIN
7580 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7581#endif
7582#ifdef _SC_SELECT
7583 {"SC_SELECT", _SC_SELECT},
7584#endif
7585#ifdef _SC_SEMAPHORES
7586 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7587#endif
7588#ifdef _SC_SEM_NSEMS_MAX
7589 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7590#endif
7591#ifdef _SC_SEM_VALUE_MAX
7592 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7593#endif
7594#ifdef _SC_SHARED_MEMORY_OBJECTS
7595 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7596#endif
7597#ifdef _SC_SHRT_MAX
7598 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7599#endif
7600#ifdef _SC_SHRT_MIN
7601 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7602#endif
7603#ifdef _SC_SIGQUEUE_MAX
7604 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7605#endif
7606#ifdef _SC_SIGRT_MAX
7607 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7608#endif
7609#ifdef _SC_SIGRT_MIN
7610 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7611#endif
Fred Draked86ed291999-12-15 15:34:33 +00007612#ifdef _SC_SOFTPOWER
7613 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7614#endif
Fred Drakec9680921999-12-13 16:37:25 +00007615#ifdef _SC_SPLIT_CACHE
7616 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7617#endif
7618#ifdef _SC_SSIZE_MAX
7619 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7620#endif
7621#ifdef _SC_STACK_PROT
7622 {"SC_STACK_PROT", _SC_STACK_PROT},
7623#endif
7624#ifdef _SC_STREAM_MAX
7625 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7626#endif
7627#ifdef _SC_SYNCHRONIZED_IO
7628 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7629#endif
7630#ifdef _SC_THREADS
7631 {"SC_THREADS", _SC_THREADS},
7632#endif
7633#ifdef _SC_THREAD_ATTR_STACKADDR
7634 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7635#endif
7636#ifdef _SC_THREAD_ATTR_STACKSIZE
7637 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7638#endif
7639#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7640 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7641#endif
7642#ifdef _SC_THREAD_KEYS_MAX
7643 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7644#endif
7645#ifdef _SC_THREAD_PRIORITY_SCHEDULING
7646 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7647#endif
7648#ifdef _SC_THREAD_PRIO_INHERIT
7649 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7650#endif
7651#ifdef _SC_THREAD_PRIO_PROTECT
7652 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7653#endif
7654#ifdef _SC_THREAD_PROCESS_SHARED
7655 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7656#endif
7657#ifdef _SC_THREAD_SAFE_FUNCTIONS
7658 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7659#endif
7660#ifdef _SC_THREAD_STACK_MIN
7661 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7662#endif
7663#ifdef _SC_THREAD_THREADS_MAX
7664 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7665#endif
7666#ifdef _SC_TIMERS
7667 {"SC_TIMERS", _SC_TIMERS},
7668#endif
7669#ifdef _SC_TIMER_MAX
7670 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7671#endif
7672#ifdef _SC_TTY_NAME_MAX
7673 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7674#endif
7675#ifdef _SC_TZNAME_MAX
7676 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7677#endif
7678#ifdef _SC_T_IOV_MAX
7679 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7680#endif
7681#ifdef _SC_UCHAR_MAX
7682 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7683#endif
7684#ifdef _SC_UINT_MAX
7685 {"SC_UINT_MAX", _SC_UINT_MAX},
7686#endif
7687#ifdef _SC_UIO_MAXIOV
7688 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7689#endif
7690#ifdef _SC_ULONG_MAX
7691 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7692#endif
7693#ifdef _SC_USHRT_MAX
7694 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7695#endif
7696#ifdef _SC_VERSION
7697 {"SC_VERSION", _SC_VERSION},
7698#endif
7699#ifdef _SC_WORD_BIT
7700 {"SC_WORD_BIT", _SC_WORD_BIT},
7701#endif
7702#ifdef _SC_XBS5_ILP32_OFF32
7703 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7704#endif
7705#ifdef _SC_XBS5_ILP32_OFFBIG
7706 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7707#endif
7708#ifdef _SC_XBS5_LP64_OFF64
7709 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7710#endif
7711#ifdef _SC_XBS5_LPBIG_OFFBIG
7712 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7713#endif
7714#ifdef _SC_XOPEN_CRYPT
7715 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7716#endif
7717#ifdef _SC_XOPEN_ENH_I18N
7718 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7719#endif
7720#ifdef _SC_XOPEN_LEGACY
7721 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7722#endif
7723#ifdef _SC_XOPEN_REALTIME
7724 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7725#endif
7726#ifdef _SC_XOPEN_REALTIME_THREADS
7727 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7728#endif
7729#ifdef _SC_XOPEN_SHM
7730 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7731#endif
7732#ifdef _SC_XOPEN_UNIX
7733 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7734#endif
7735#ifdef _SC_XOPEN_VERSION
7736 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7737#endif
7738#ifdef _SC_XOPEN_XCU_VERSION
7739 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7740#endif
7741#ifdef _SC_XOPEN_XPG2
7742 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7743#endif
7744#ifdef _SC_XOPEN_XPG3
7745 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7746#endif
7747#ifdef _SC_XOPEN_XPG4
7748 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7749#endif
7750};
7751
7752static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007753conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007754{
7755 return conv_confname(arg, valuep, posix_constants_sysconf,
7756 sizeof(posix_constants_sysconf)
7757 / sizeof(struct constdef));
7758}
7759
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007760PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007761"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007762Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007763
7764static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007765posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007766{
7767 PyObject *result = NULL;
7768 int name;
7769
7770 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7771 int value;
7772
7773 errno = 0;
7774 value = sysconf(name);
7775 if (value == -1 && errno != 0)
7776 posix_error();
7777 else
7778 result = PyInt_FromLong(value);
7779 }
7780 return result;
7781}
7782#endif
7783
7784
Fred Drakebec628d1999-12-15 18:31:10 +00007785/* This code is used to ensure that the tables of configuration value names
7786 * are in sorted order as required by conv_confname(), and also to build the
7787 * the exported dictionaries that are used to publish information about the
7788 * names available on the host platform.
7789 *
7790 * Sorting the table at runtime ensures that the table is properly ordered
7791 * when used, even for platforms we're not able to test on. It also makes
7792 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007793 */
Fred Drakebec628d1999-12-15 18:31:10 +00007794
7795static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007796cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007797{
7798 const struct constdef *c1 =
7799 (const struct constdef *) v1;
7800 const struct constdef *c2 =
7801 (const struct constdef *) v2;
7802
7803 return strcmp(c1->name, c2->name);
7804}
7805
7806static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007807setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007808 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007809{
Fred Drakebec628d1999-12-15 18:31:10 +00007810 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007811 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007812
7813 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7814 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007815 if (d == NULL)
7816 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007817
Barry Warsaw3155db32000-04-13 15:20:40 +00007818 for (i=0; i < tablesize; ++i) {
7819 PyObject *o = PyInt_FromLong(table[i].value);
7820 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7821 Py_XDECREF(o);
7822 Py_DECREF(d);
7823 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007824 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007825 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007826 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007827 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007828}
7829
Fred Drakebec628d1999-12-15 18:31:10 +00007830/* Return -1 on failure, 0 on success. */
7831static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007832setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007833{
7834#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007835 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007836 sizeof(posix_constants_pathconf)
7837 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007838 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007839 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007840#endif
7841#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007842 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007843 sizeof(posix_constants_confstr)
7844 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007845 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007846 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007847#endif
7848#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007849 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007850 sizeof(posix_constants_sysconf)
7851 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007852 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007853 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007854#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007855 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007856}
Fred Draked86ed291999-12-15 15:34:33 +00007857
7858
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007859PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007860"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007861Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007862in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007863
7864static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007865posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007866{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007867 abort();
7868 /*NOTREACHED*/
7869 Py_FatalError("abort() called from Python code didn't abort!");
7870 return NULL;
7871}
Fred Drakebec628d1999-12-15 18:31:10 +00007872
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007873#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007874PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007875"startfile(filepath [, operation]) - Start a file with its associated\n\
7876application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007877\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007878When \"operation\" is not specified or \"open\", this acts like\n\
7879double-clicking the file in Explorer, or giving the file name as an\n\
7880argument to the DOS \"start\" command: the file is opened with whatever\n\
7881application (if any) its extension is associated.\n\
7882When another \"operation\" is given, it specifies what should be done with\n\
7883the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007884\n\
7885startfile returns as soon as the associated application is launched.\n\
7886There is no option to wait for the application to close, and no way\n\
7887to retrieve the application's exit status.\n\
7888\n\
7889The filepath is relative to the current directory. If you want to use\n\
7890an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007891the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007892
7893static PyObject *
7894win32_startfile(PyObject *self, PyObject *args)
7895{
7896 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00007897 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007898 HINSTANCE rc;
Georg Brandlad89dc82006-04-03 12:26:26 +00007899#ifdef Py_WIN_WIDE_FILENAMES
7900 if (unicode_file_names()) {
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007901 PyObject *unipath, *woperation = NULL;
Georg Brandlad89dc82006-04-03 12:26:26 +00007902 if (!PyArg_ParseTuple(args, "U|s:startfile",
7903 &unipath, &operation)) {
7904 PyErr_Clear();
7905 goto normal;
7906 }
7907
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007908
7909 if (operation) {
7910 woperation = PyUnicode_DecodeASCII(operation,
7911 strlen(operation), NULL);
7912 if (!woperation) {
7913 PyErr_Clear();
7914 operation = NULL;
7915 goto normal;
7916 }
Georg Brandlad89dc82006-04-03 12:26:26 +00007917 }
7918
7919 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007920 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
Georg Brandlad89dc82006-04-03 12:26:26 +00007921 PyUnicode_AS_UNICODE(unipath),
Georg Brandlad89dc82006-04-03 12:26:26 +00007922 NULL, NULL, SW_SHOWNORMAL);
7923 Py_END_ALLOW_THREADS
7924
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007925 Py_XDECREF(woperation);
Georg Brandlad89dc82006-04-03 12:26:26 +00007926 if (rc <= (HINSTANCE)32) {
7927 PyObject *errval = win32_error_unicode("startfile",
7928 PyUnicode_AS_UNICODE(unipath));
7929 return errval;
7930 }
7931 Py_INCREF(Py_None);
7932 return Py_None;
7933 }
7934#endif
7935
7936normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00007937 if (!PyArg_ParseTuple(args, "et|s:startfile",
7938 Py_FileSystemDefaultEncoding, &filepath,
7939 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00007940 return NULL;
7941 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00007942 rc = ShellExecute((HWND)0, operation, filepath,
7943 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007944 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00007945 if (rc <= (HINSTANCE)32) {
7946 PyObject *errval = win32_error("startfile", filepath);
7947 PyMem_Free(filepath);
7948 return errval;
7949 }
7950 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007951 Py_INCREF(Py_None);
7952 return Py_None;
7953}
7954#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007955
Martin v. Löwis438b5342002-12-27 10:16:42 +00007956#ifdef HAVE_GETLOADAVG
7957PyDoc_STRVAR(posix_getloadavg__doc__,
7958"getloadavg() -> (float, float, float)\n\n\
7959Return the number of processes in the system run queue averaged over\n\
7960the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7961was unobtainable");
7962
7963static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007964posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007965{
7966 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007967 if (getloadavg(loadavg, 3)!=3) {
7968 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7969 return NULL;
7970 } else
7971 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
7972}
7973#endif
7974
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007975#ifdef MS_WINDOWS
7976
7977PyDoc_STRVAR(win32_urandom__doc__,
7978"urandom(n) -> str\n\n\
7979Return a string of n random bytes suitable for cryptographic use.");
7980
7981typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7982 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7983 DWORD dwFlags );
7984typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7985 BYTE *pbBuffer );
7986
7987static CRYPTGENRANDOM pCryptGenRandom = NULL;
7988static HCRYPTPROV hCryptProv = 0;
7989
Tim Peters4ad82172004-08-30 17:02:04 +00007990static PyObject*
7991win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007992{
Tim Petersd3115382004-08-30 17:36:46 +00007993 int howMany;
7994 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007995
Tim Peters4ad82172004-08-30 17:02:04 +00007996 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00007997 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00007998 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00007999 if (howMany < 0)
8000 return PyErr_Format(PyExc_ValueError,
8001 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008002
Tim Peters4ad82172004-08-30 17:02:04 +00008003 if (hCryptProv == 0) {
8004 HINSTANCE hAdvAPI32 = NULL;
8005 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008006
Tim Peters4ad82172004-08-30 17:02:04 +00008007 /* Obtain handle to the DLL containing CryptoAPI
8008 This should not fail */
8009 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8010 if(hAdvAPI32 == NULL)
8011 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008012
Tim Peters4ad82172004-08-30 17:02:04 +00008013 /* Obtain pointers to the CryptoAPI functions
8014 This will fail on some early versions of Win95 */
8015 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8016 hAdvAPI32,
8017 "CryptAcquireContextA");
8018 if (pCryptAcquireContext == NULL)
8019 return PyErr_Format(PyExc_NotImplementedError,
8020 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008021
Tim Peters4ad82172004-08-30 17:02:04 +00008022 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8023 hAdvAPI32, "CryptGenRandom");
Georg Brandlb20cb332006-09-06 06:04:06 +00008024 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00008025 return PyErr_Format(PyExc_NotImplementedError,
8026 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008027
Tim Peters4ad82172004-08-30 17:02:04 +00008028 /* Acquire context */
8029 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8030 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8031 return win32_error("CryptAcquireContext", NULL);
8032 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008033
Tim Peters4ad82172004-08-30 17:02:04 +00008034 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00008035 result = PyString_FromStringAndSize(NULL, howMany);
8036 if (result != NULL) {
8037 /* Get random data */
8038 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8039 PyString_AS_STRING(result))) {
8040 Py_DECREF(result);
8041 return win32_error("CryptGenRandom", NULL);
8042 }
Tim Peters4ad82172004-08-30 17:02:04 +00008043 }
Tim Petersd3115382004-08-30 17:36:46 +00008044 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008045}
8046#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008047
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008048#ifdef __VMS
8049/* Use openssl random routine */
8050#include <openssl/rand.h>
8051PyDoc_STRVAR(vms_urandom__doc__,
8052"urandom(n) -> str\n\n\
8053Return a string of n random bytes suitable for cryptographic use.");
8054
8055static PyObject*
8056vms_urandom(PyObject *self, PyObject *args)
8057{
8058 int howMany;
8059 PyObject* result;
8060
8061 /* Read arguments */
8062 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8063 return NULL;
8064 if (howMany < 0)
8065 return PyErr_Format(PyExc_ValueError,
8066 "negative argument not allowed");
8067
8068 /* Allocate bytes */
8069 result = PyString_FromStringAndSize(NULL, howMany);
8070 if (result != NULL) {
8071 /* Get random data */
8072 if (RAND_pseudo_bytes((unsigned char*)
8073 PyString_AS_STRING(result),
8074 howMany) < 0) {
8075 Py_DECREF(result);
8076 return PyErr_Format(PyExc_ValueError,
8077 "RAND_pseudo_bytes");
8078 }
8079 }
8080 return result;
8081}
8082#endif
8083
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008084static PyMethodDef posix_methods[] = {
8085 {"access", posix_access, METH_VARARGS, posix_access__doc__},
8086#ifdef HAVE_TTYNAME
8087 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
8088#endif
8089 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
8090 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008091#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008092 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008093#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008094#ifdef HAVE_LCHOWN
8095 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
8096#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008097#ifdef HAVE_CHROOT
8098 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
8099#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008100#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00008101 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008102#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008103#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00008104 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008105#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00008106 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008107#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008108#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008109#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008110 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008111#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008112 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8113 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8114 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008115#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008116 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008117#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008118#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008119 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008120#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008121 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8122 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8123 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00008124 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008125#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008126 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008127#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008128#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008129 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008130#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008131 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008132#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00008133 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008134#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008135 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8136 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8137 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008138#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00008139 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008140#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008141 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008142#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008143 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8144 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008145#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008146#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008147 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8148 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008149#if defined(PYOS_OS2)
8150 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8151 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
8152#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008153#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008154#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00008155 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008156#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008157#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00008158 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008159#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008160#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00008161 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008162#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008163#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00008164 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008165#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008166#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008167 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008168#endif /* HAVE_GETEGID */
8169#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008170 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008171#endif /* HAVE_GETEUID */
8172#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008173 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008174#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008175#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00008176 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008177#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008178 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008179#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008180 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008181#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008182#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00008183 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008184#endif /* HAVE_GETPPID */
8185#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008186 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008187#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008188#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00008189 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008190#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008191#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008192 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008193#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008194#ifdef HAVE_KILLPG
8195 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
8196#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008197#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008198 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008199#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008200#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008201 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008202#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008203 {"popen2", win32_popen2, METH_VARARGS},
8204 {"popen3", win32_popen3, METH_VARARGS},
8205 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00008206 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008207#else
8208#if defined(PYOS_OS2) && defined(PYCC_GCC)
8209 {"popen2", os2emx_popen2, METH_VARARGS},
8210 {"popen3", os2emx_popen3, METH_VARARGS},
8211 {"popen4", os2emx_popen4, METH_VARARGS},
8212#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008213#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008214#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008215#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008216 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008217#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008218#ifdef HAVE_SETEUID
8219 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8220#endif /* HAVE_SETEUID */
8221#ifdef HAVE_SETEGID
8222 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8223#endif /* HAVE_SETEGID */
8224#ifdef HAVE_SETREUID
8225 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8226#endif /* HAVE_SETREUID */
8227#ifdef HAVE_SETREGID
8228 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8229#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008230#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008231 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008232#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008233#ifdef HAVE_SETGROUPS
Georg Brandl96a8c392006-05-29 21:04:52 +00008234 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008235#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008236#ifdef HAVE_GETPGID
8237 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8238#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008239#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008240 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008241#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008242#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00008243 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008244#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008245#ifdef HAVE_WAIT3
8246 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8247#endif /* HAVE_WAIT3 */
8248#ifdef HAVE_WAIT4
8249 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8250#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008251#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008252 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008253#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008254#ifdef HAVE_GETSID
8255 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8256#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008257#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00008258 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008259#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008260#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008261 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008262#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008263#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008264 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008265#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008266#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008267 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008268#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008269 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8270 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8271 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8272 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8273 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8274 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8275 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8276 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8277 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00008278 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008279#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00008280 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008281#endif
8282#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008283 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008284#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008285#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008286 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8287#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008288#ifdef HAVE_DEVICE_MACROS
8289 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8290 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8291 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8292#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008293#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008294 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008295#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008296#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008297 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008298#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008299#ifdef HAVE_UNSETENV
8300 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8301#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00008302#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008303 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00008304#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00008305#ifdef HAVE_FCHDIR
8306 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8307#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008308#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008309 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008310#endif
8311#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008312 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008313#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008314#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008315#ifdef WCOREDUMP
8316 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8317#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008318#ifdef WIFCONTINUED
8319 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8320#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008321#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008322 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008323#endif /* WIFSTOPPED */
8324#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008325 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008326#endif /* WIFSIGNALED */
8327#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008328 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008329#endif /* WIFEXITED */
8330#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008331 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008332#endif /* WEXITSTATUS */
8333#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008334 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008335#endif /* WTERMSIG */
8336#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008337 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008338#endif /* WSTOPSIG */
8339#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008340#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008341 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008342#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008343#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008344 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008345#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008346#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00008347 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008348#endif
8349#ifdef HAVE_TEMPNAM
8350 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8351#endif
8352#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00008353 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008354#endif
Fred Drakec9680921999-12-13 16:37:25 +00008355#ifdef HAVE_CONFSTR
8356 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8357#endif
8358#ifdef HAVE_SYSCONF
8359 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8360#endif
8361#ifdef HAVE_FPATHCONF
8362 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8363#endif
8364#ifdef HAVE_PATHCONF
8365 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8366#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008367 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008368#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00008369 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8370#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008371#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00008372 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008373#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008374 #ifdef MS_WINDOWS
8375 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8376 #endif
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008377 #ifdef __VMS
8378 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
8379 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008380 {NULL, NULL} /* Sentinel */
8381};
8382
8383
Barry Warsaw4a342091996-12-19 23:50:02 +00008384static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008385ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008386{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008387 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008388}
8389
Guido van Rossumd48f2521997-12-05 22:19:34 +00008390#if defined(PYOS_OS2)
8391/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008392static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008393{
8394 APIRET rc;
8395 ULONG values[QSV_MAX+1];
8396 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008397 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008398
8399 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008400 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008401 Py_END_ALLOW_THREADS
8402
8403 if (rc != NO_ERROR) {
8404 os2_error(rc);
8405 return -1;
8406 }
8407
Fred Drake4d1e64b2002-04-15 19:40:07 +00008408 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8409 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8410 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8411 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8412 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8413 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8414 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008415
8416 switch (values[QSV_VERSION_MINOR]) {
8417 case 0: ver = "2.00"; break;
8418 case 10: ver = "2.10"; break;
8419 case 11: ver = "2.11"; break;
8420 case 30: ver = "3.00"; break;
8421 case 40: ver = "4.00"; break;
8422 case 50: ver = "5.00"; break;
8423 default:
Tim Peters885d4572001-11-28 20:27:42 +00008424 PyOS_snprintf(tmp, sizeof(tmp),
8425 "%d-%d", values[QSV_VERSION_MAJOR],
8426 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008427 ver = &tmp[0];
8428 }
8429
8430 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008431 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008432 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008433
8434 /* Add Indicator of Which Drive was Used to Boot the System */
8435 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8436 tmp[1] = ':';
8437 tmp[2] = '\0';
8438
Fred Drake4d1e64b2002-04-15 19:40:07 +00008439 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008440}
8441#endif
8442
Barry Warsaw4a342091996-12-19 23:50:02 +00008443static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008444all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008445{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008446#ifdef F_OK
8447 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008448#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008449#ifdef R_OK
8450 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008451#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008452#ifdef W_OK
8453 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008454#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008455#ifdef X_OK
8456 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008457#endif
Fred Drakec9680921999-12-13 16:37:25 +00008458#ifdef NGROUPS_MAX
8459 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8460#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008461#ifdef TMP_MAX
8462 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8463#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008464#ifdef WCONTINUED
8465 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8466#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008467#ifdef WNOHANG
8468 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008469#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008470#ifdef WUNTRACED
8471 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8472#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008473#ifdef O_RDONLY
8474 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8475#endif
8476#ifdef O_WRONLY
8477 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8478#endif
8479#ifdef O_RDWR
8480 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8481#endif
8482#ifdef O_NDELAY
8483 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8484#endif
8485#ifdef O_NONBLOCK
8486 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8487#endif
8488#ifdef O_APPEND
8489 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8490#endif
8491#ifdef O_DSYNC
8492 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8493#endif
8494#ifdef O_RSYNC
8495 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8496#endif
8497#ifdef O_SYNC
8498 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8499#endif
8500#ifdef O_NOCTTY
8501 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8502#endif
8503#ifdef O_CREAT
8504 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8505#endif
8506#ifdef O_EXCL
8507 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8508#endif
8509#ifdef O_TRUNC
8510 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8511#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008512#ifdef O_BINARY
8513 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8514#endif
8515#ifdef O_TEXT
8516 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8517#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008518#ifdef O_LARGEFILE
8519 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8520#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008521#ifdef O_SHLOCK
8522 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8523#endif
8524#ifdef O_EXLOCK
8525 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8526#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008527
Tim Peters5aa91602002-01-30 05:46:57 +00008528/* MS Windows */
8529#ifdef O_NOINHERIT
8530 /* Don't inherit in child processes. */
8531 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8532#endif
8533#ifdef _O_SHORT_LIVED
8534 /* Optimize for short life (keep in memory). */
8535 /* MS forgot to define this one with a non-underscore form too. */
8536 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8537#endif
8538#ifdef O_TEMPORARY
8539 /* Automatically delete when last handle is closed. */
8540 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8541#endif
8542#ifdef O_RANDOM
8543 /* Optimize for random access. */
8544 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8545#endif
8546#ifdef O_SEQUENTIAL
8547 /* Optimize for sequential access. */
8548 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8549#endif
8550
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008551/* GNU extensions. */
8552#ifdef O_DIRECT
8553 /* Direct disk access. */
8554 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8555#endif
8556#ifdef O_DIRECTORY
8557 /* Must be a directory. */
8558 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8559#endif
8560#ifdef O_NOFOLLOW
8561 /* Do not follow links. */
8562 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8563#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008564
Barry Warsaw5676bd12003-01-07 20:57:09 +00008565 /* These come from sysexits.h */
8566#ifdef EX_OK
8567 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008568#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008569#ifdef EX_USAGE
8570 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008571#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008572#ifdef EX_DATAERR
8573 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008574#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008575#ifdef EX_NOINPUT
8576 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008577#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008578#ifdef EX_NOUSER
8579 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008580#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008581#ifdef EX_NOHOST
8582 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008583#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008584#ifdef EX_UNAVAILABLE
8585 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008586#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008587#ifdef EX_SOFTWARE
8588 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008589#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008590#ifdef EX_OSERR
8591 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008592#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008593#ifdef EX_OSFILE
8594 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008595#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008596#ifdef EX_CANTCREAT
8597 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008598#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008599#ifdef EX_IOERR
8600 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008601#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008602#ifdef EX_TEMPFAIL
8603 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008604#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008605#ifdef EX_PROTOCOL
8606 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008607#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008608#ifdef EX_NOPERM
8609 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008610#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008611#ifdef EX_CONFIG
8612 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008613#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008614#ifdef EX_NOTFOUND
8615 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008616#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008617
Guido van Rossum246bc171999-02-01 23:54:31 +00008618#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008619#if defined(PYOS_OS2) && defined(PYCC_GCC)
8620 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8621 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8622 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8623 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8624 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8625 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8626 if (ins(d, "P_PM", (long)P_PM)) return -1;
8627 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8628 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8629 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8630 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8631 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8632 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8633 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8634 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8635 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8636 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8637 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8638 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8639 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8640#else
Guido van Rossum7d385291999-02-16 19:38:04 +00008641 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8642 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8643 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8644 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8645 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008646#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008647#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008648
Guido van Rossumd48f2521997-12-05 22:19:34 +00008649#if defined(PYOS_OS2)
8650 if (insertvalues(d)) return -1;
8651#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008652 return 0;
8653}
8654
8655
Tim Peters5aa91602002-01-30 05:46:57 +00008656#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008657#define INITFUNC initnt
8658#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008659
8660#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008661#define INITFUNC initos2
8662#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008663
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008664#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008665#define INITFUNC initposix
8666#define MODNAME "posix"
8667#endif
8668
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008669PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008670INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008671{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008672 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008673
Fred Drake4d1e64b2002-04-15 19:40:07 +00008674 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008675 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008676 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00008677 if (m == NULL)
8678 return;
Tim Peters5aa91602002-01-30 05:46:57 +00008679
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008680 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008681 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00008682 Py_XINCREF(v);
8683 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008684 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00008685 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008686
Fred Drake4d1e64b2002-04-15 19:40:07 +00008687 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00008688 return;
8689
Fred Drake4d1e64b2002-04-15 19:40:07 +00008690 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00008691 return;
8692
Fred Drake4d1e64b2002-04-15 19:40:07 +00008693 Py_INCREF(PyExc_OSError);
8694 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008695
Guido van Rossumb3d39562000-01-31 18:41:26 +00008696#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00008697 if (posix_putenv_garbage == NULL)
8698 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008699#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008700
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008701 if (!initialized) {
8702 stat_result_desc.name = MODNAME ".stat_result";
8703 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8704 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8705 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8706 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8707 structseq_new = StatResultType.tp_new;
8708 StatResultType.tp_new = statresult_new;
8709
8710 statvfs_result_desc.name = MODNAME ".statvfs_result";
8711 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
8712 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008713 Py_INCREF((PyObject*) &StatResultType);
8714 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00008715 Py_INCREF((PyObject*) &StatVFSResultType);
8716 PyModule_AddObject(m, "statvfs_result",
8717 (PyObject*) &StatVFSResultType);
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008718 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00008719
8720#ifdef __APPLE__
8721 /*
8722 * Step 2 of weak-linking support on Mac OS X.
8723 *
8724 * The code below removes functions that are not available on the
8725 * currently active platform.
8726 *
8727 * This block allow one to use a python binary that was build on
8728 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8729 * OSX 10.4.
8730 */
8731#ifdef HAVE_FSTATVFS
8732 if (fstatvfs == NULL) {
8733 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8734 return;
8735 }
8736 }
8737#endif /* HAVE_FSTATVFS */
8738
8739#ifdef HAVE_STATVFS
8740 if (statvfs == NULL) {
8741 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8742 return;
8743 }
8744 }
8745#endif /* HAVE_STATVFS */
8746
8747# ifdef HAVE_LCHOWN
8748 if (lchown == NULL) {
8749 if (PyObject_DelAttrString(m, "lchown") == -1) {
8750 return;
8751 }
8752 }
8753#endif /* HAVE_LCHOWN */
8754
8755
8756#endif /* __APPLE__ */
8757
Guido van Rossumb6775db1994-08-01 11:34:53 +00008758}
Anthony Baxterac6bd462006-04-13 02:06:09 +00008759
8760#ifdef __cplusplus
8761}
8762#endif
8763
Martin v. Löwis2c7aa632006-10-15 09:44:02 +00008764