blob: 01fcfb23efa105504e59b1ead19b95ca60c6b761 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047#ifndef Py_USING_UNICODE
48/* This is used in signatures of functions. */
49#define Py_UNICODE void
50#endif
51
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000052#if defined(PYOS_OS2)
53#define INCL_DOS
54#define INCL_DOSERRORS
55#define INCL_DOSPROCESS
56#define INCL_NOPMAPI
57#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#if defined(PYCC_GCC)
59#include <ctype.h>
60#include <io.h>
61#include <stdio.h>
62#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000063#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000064#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000065#endif
66
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +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>
Thomas Wouters0e3f5912006-08-11 14:57:12 +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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000080#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +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
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258#endif
259#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000261#endif
262#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +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 Wouters477c8d52006-05-27 19:21:47 +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 Wouters477c8d52006-05-27 19:21:47 +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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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{
Thomas Wouters477c8d52006-05-27 19:21:47 +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. */
Thomas Wouters477c8d52006-05-27 19:21:47 +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 }
Thomas Wouters477c8d52006-05-27 19:21:47 +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 *
Thomas Wouters477c8d52006-05-27 19:21:47 +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,
Thomas Wouters477c8d52006-05-27 19:21:47 +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
Thomas Wouters477c8d52006-05-27 19:21:47 +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{
Thomas Wouters477c8d52006-05-27 19:21:47 +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
Thomas Wouters477c8d52006-05-27 19:21:47 +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;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000795 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000796 memcpy(out_ptr, &out, sizeof(out));
797}
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
Thomas Wouters89f507f2006-12-13 04:49:30 +0000831/* Emulate GetFileAttributesEx[AW] on Windows 95 */
832static int checked = 0;
833static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
834static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
835static void
836check_gfax()
837{
838 HINSTANCE hKernel32;
839 if (checked)
840 return;
841 checked = 1;
842 hKernel32 = GetModuleHandle("KERNEL32");
843 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
844 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
845}
846
847static BOOL WINAPI
848Py_GetFileAttributesExA(LPCSTR pszFile,
849 GET_FILEEX_INFO_LEVELS level,
850 LPVOID pv)
851{
852 BOOL result;
853 HANDLE hFindFile;
854 WIN32_FIND_DATAA FileData;
855 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
856 /* First try to use the system's implementation, if that is
857 available and either succeeds to gives an error other than
858 that it isn't implemented. */
859 check_gfax();
860 if (gfaxa) {
861 result = gfaxa(pszFile, level, pv);
862 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
863 return result;
864 }
865 /* It's either not present, or not implemented.
866 Emulate using FindFirstFile. */
867 if (level != GetFileExInfoStandard) {
868 SetLastError(ERROR_INVALID_PARAMETER);
869 return FALSE;
870 }
871 /* Use GetFileAttributes to validate that the file name
872 does not contain wildcards (which FindFirstFile would
873 accept). */
874 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
875 return FALSE;
876 hFindFile = FindFirstFileA(pszFile, &FileData);
877 if (hFindFile == INVALID_HANDLE_VALUE)
878 return FALSE;
879 FindClose(hFindFile);
880 pfad->dwFileAttributes = FileData.dwFileAttributes;
881 pfad->ftCreationTime = FileData.ftCreationTime;
882 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
883 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
884 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
885 pfad->nFileSizeLow = FileData.nFileSizeLow;
886 return TRUE;
887}
888
889static BOOL WINAPI
890Py_GetFileAttributesExW(LPCWSTR pszFile,
891 GET_FILEEX_INFO_LEVELS level,
892 LPVOID pv)
893{
894 BOOL result;
895 HANDLE hFindFile;
896 WIN32_FIND_DATAW FileData;
897 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
898 /* First try to use the system's implementation, if that is
899 available and either succeeds to gives an error other than
900 that it isn't implemented. */
901 check_gfax();
902 if (gfaxa) {
903 result = gfaxw(pszFile, level, pv);
904 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
905 return result;
906 }
907 /* It's either not present, or not implemented.
908 Emulate using FindFirstFile. */
909 if (level != GetFileExInfoStandard) {
910 SetLastError(ERROR_INVALID_PARAMETER);
911 return FALSE;
912 }
913 /* Use GetFileAttributes to validate that the file name
914 does not contain wildcards (which FindFirstFile would
915 accept). */
916 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
917 return FALSE;
918 hFindFile = FindFirstFileW(pszFile, &FileData);
919 if (hFindFile == INVALID_HANDLE_VALUE)
920 return FALSE;
921 FindClose(hFindFile);
922 pfad->dwFileAttributes = FileData.dwFileAttributes;
923 pfad->ftCreationTime = FileData.ftCreationTime;
924 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
925 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
926 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
927 pfad->nFileSizeLow = FileData.nFileSizeLow;
928 return TRUE;
929}
930
Martin v. Löwis14694662006-02-03 12:54:16 +0000931static int
932win32_stat(const char* path, struct win32_stat *result)
933{
934 WIN32_FILE_ATTRIBUTE_DATA info;
935 int code;
936 char *dot;
937 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000938 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis14694662006-02-03 12:54:16 +0000939 /* Protocol violation: we explicitly clear errno, instead of
940 setting it to a POSIX error. Callers should use GetLastError. */
941 errno = 0;
942 return -1;
943 }
944 code = attribute_data_to_stat(&info, result);
945 if (code != 0)
946 return code;
947 /* Set S_IFEXEC if it is an .exe, .bat, ... */
948 dot = strrchr(path, '.');
949 if (dot) {
950 if (stricmp(dot, ".bat") == 0 ||
951 stricmp(dot, ".cmd") == 0 ||
952 stricmp(dot, ".exe") == 0 ||
953 stricmp(dot, ".com") == 0)
954 result->st_mode |= 0111;
955 }
956 return code;
957}
958
959static int
960win32_wstat(const wchar_t* path, struct win32_stat *result)
961{
962 int code;
963 const wchar_t *dot;
964 WIN32_FILE_ATTRIBUTE_DATA info;
965 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000966 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis14694662006-02-03 12:54:16 +0000967 /* Protocol violation: we explicitly clear errno, instead of
968 setting it to a POSIX error. Callers should use GetLastError. */
969 errno = 0;
970 return -1;
971 }
972 code = attribute_data_to_stat(&info, result);
973 if (code < 0)
974 return code;
975 /* Set IFEXEC if it is an .exe, .bat, ... */
976 dot = wcsrchr(path, '.');
977 if (dot) {
978 if (_wcsicmp(dot, L".bat") == 0 ||
979 _wcsicmp(dot, L".cmd") == 0 ||
980 _wcsicmp(dot, L".exe") == 0 ||
981 _wcsicmp(dot, L".com") == 0)
982 result->st_mode |= 0111;
983 }
984 return code;
985}
986
987static int
988win32_fstat(int file_number, struct win32_stat *result)
989{
990 BY_HANDLE_FILE_INFORMATION info;
991 HANDLE h;
992 int type;
993
994 h = (HANDLE)_get_osfhandle(file_number);
995
996 /* Protocol violation: we explicitly clear errno, instead of
997 setting it to a POSIX error. Callers should use GetLastError. */
998 errno = 0;
999
1000 if (h == INVALID_HANDLE_VALUE) {
1001 /* This is really a C library error (invalid file handle).
1002 We set the Win32 error to the closes one matching. */
1003 SetLastError(ERROR_INVALID_HANDLE);
1004 return -1;
1005 }
1006 memset(result, 0, sizeof(*result));
1007
1008 type = GetFileType(h);
1009 if (type == FILE_TYPE_UNKNOWN) {
1010 DWORD error = GetLastError();
1011 if (error != 0) {
1012 return -1;
1013 }
1014 /* else: valid but unknown file */
1015 }
1016
1017 if (type != FILE_TYPE_DISK) {
1018 if (type == FILE_TYPE_CHAR)
1019 result->st_mode = _S_IFCHR;
1020 else if (type == FILE_TYPE_PIPE)
1021 result->st_mode = _S_IFIFO;
1022 return 0;
1023 }
1024
1025 if (!GetFileInformationByHandle(h, &info)) {
1026 return -1;
1027 }
1028
1029 /* similar to stat() */
1030 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1031 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1032 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1033 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1034 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1035 /* specific to fstat() */
1036 result->st_nlink = info.nNumberOfLinks;
1037 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1038 return 0;
1039}
1040
1041#endif /* MS_WINDOWS */
1042
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001043PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001044"stat_result: Result from stat or lstat.\n\n\
1045This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001046 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001047or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1048\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001049Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1050or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001051\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001052See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001053
1054static PyStructSequence_Field stat_result_fields[] = {
1055 {"st_mode", "protection bits"},
1056 {"st_ino", "inode"},
1057 {"st_dev", "device"},
1058 {"st_nlink", "number of hard links"},
1059 {"st_uid", "user ID of owner"},
1060 {"st_gid", "group ID of owner"},
1061 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001062 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1063 {NULL, "integer time of last access"},
1064 {NULL, "integer time of last modification"},
1065 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001066 {"st_atime", "time of last access"},
1067 {"st_mtime", "time of last modification"},
1068 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001069#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001070 {"st_blksize", "blocksize for filesystem I/O"},
1071#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001072#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001073 {"st_blocks", "number of blocks allocated"},
1074#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001075#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001076 {"st_rdev", "device type (if inode device)"},
1077#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001078#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1079 {"st_flags", "user defined flags for file"},
1080#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001081#ifdef HAVE_STRUCT_STAT_ST_GEN
1082 {"st_gen", "generation number"},
1083#endif
1084#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1085 {"st_birthtime", "time of creation"},
1086#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001087 {0}
1088};
1089
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001090#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001091#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001092#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001093#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001094#endif
1095
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001096#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001097#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1098#else
1099#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1100#endif
1101
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001102#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001103#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1104#else
1105#define ST_RDEV_IDX ST_BLOCKS_IDX
1106#endif
1107
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001108#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1109#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1110#else
1111#define ST_FLAGS_IDX ST_RDEV_IDX
1112#endif
1113
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001114#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001115#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001116#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001117#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001118#endif
1119
1120#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1121#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1122#else
1123#define ST_BIRTHTIME_IDX ST_GEN_IDX
1124#endif
1125
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001126static PyStructSequence_Desc stat_result_desc = {
1127 "stat_result", /* name */
1128 stat_result__doc__, /* doc */
1129 stat_result_fields,
1130 10
1131};
1132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001133PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001134"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1135This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001136 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001137or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001138\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001139See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001140
1141static PyStructSequence_Field statvfs_result_fields[] = {
1142 {"f_bsize", },
1143 {"f_frsize", },
1144 {"f_blocks", },
1145 {"f_bfree", },
1146 {"f_bavail", },
1147 {"f_files", },
1148 {"f_ffree", },
1149 {"f_favail", },
1150 {"f_flag", },
1151 {"f_namemax",},
1152 {0}
1153};
1154
1155static PyStructSequence_Desc statvfs_result_desc = {
1156 "statvfs_result", /* name */
1157 statvfs_result__doc__, /* doc */
1158 statvfs_result_fields,
1159 10
1160};
1161
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001162static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001163static PyTypeObject StatResultType;
1164static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001165static newfunc structseq_new;
1166
1167static PyObject *
1168statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1169{
1170 PyStructSequence *result;
1171 int i;
1172
1173 result = (PyStructSequence*)structseq_new(type, args, kwds);
1174 if (!result)
1175 return NULL;
1176 /* If we have been initialized from a tuple,
1177 st_?time might be set to None. Initialize it
1178 from the int slots. */
1179 for (i = 7; i <= 9; i++) {
1180 if (result->ob_item[i+3] == Py_None) {
1181 Py_DECREF(Py_None);
1182 Py_INCREF(result->ob_item[i]);
1183 result->ob_item[i+3] = result->ob_item[i];
1184 }
1185 }
1186 return (PyObject*)result;
1187}
1188
1189
1190
1191/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001192static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001193
1194PyDoc_STRVAR(stat_float_times__doc__,
1195"stat_float_times([newval]) -> oldval\n\n\
1196Determine whether os.[lf]stat represents time stamps as float objects.\n\
1197If newval is True, future calls to stat() return floats, if it is False,\n\
1198future calls return ints. \n\
1199If newval is omitted, return the current setting.\n");
1200
1201static PyObject*
1202stat_float_times(PyObject* self, PyObject *args)
1203{
1204 int newval = -1;
1205 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1206 return NULL;
1207 if (newval == -1)
1208 /* Return old value */
1209 return PyBool_FromLong(_stat_float_times);
1210 _stat_float_times = newval;
1211 Py_INCREF(Py_None);
1212 return Py_None;
1213}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001214
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001215static void
1216fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1217{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001218 PyObject *fval,*ival;
1219#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001220 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001221#else
1222 ival = PyInt_FromLong((long)sec);
1223#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001224 if (!ival)
1225 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001226 if (_stat_float_times) {
1227 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1228 } else {
1229 fval = ival;
1230 Py_INCREF(fval);
1231 }
1232 PyStructSequence_SET_ITEM(v, index, ival);
1233 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001234}
1235
Tim Peters5aa91602002-01-30 05:46:57 +00001236/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001237 (used by posix_stat() and posix_fstat()) */
1238static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001239_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001240{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001241 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001242 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001243 if (v == NULL)
1244 return NULL;
1245
Martin v. Löwis14694662006-02-03 12:54:16 +00001246 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001247#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001248 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001249 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001250#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001251 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001252#endif
1253#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001254 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001255 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001256#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001257 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001258#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001259 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1260 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1261 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001262#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001263 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001264 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001265#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001266 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001267#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001268
Martin v. Löwis14694662006-02-03 12:54:16 +00001269#if defined(HAVE_STAT_TV_NSEC)
1270 ansec = st->st_atim.tv_nsec;
1271 mnsec = st->st_mtim.tv_nsec;
1272 cnsec = st->st_ctim.tv_nsec;
1273#elif defined(HAVE_STAT_TV_NSEC2)
1274 ansec = st->st_atimespec.tv_nsec;
1275 mnsec = st->st_mtimespec.tv_nsec;
1276 cnsec = st->st_ctimespec.tv_nsec;
1277#elif defined(HAVE_STAT_NSEC)
1278 ansec = st->st_atime_nsec;
1279 mnsec = st->st_mtime_nsec;
1280 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001281#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001282 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001283#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001284 fill_time(v, 7, st->st_atime, ansec);
1285 fill_time(v, 8, st->st_mtime, mnsec);
1286 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001287
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001288#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001289 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001290 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001291#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001292#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001293 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001294 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001295#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001296#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001297 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001298 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001299#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001300#ifdef HAVE_STRUCT_STAT_ST_GEN
1301 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001302 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001303#endif
1304#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1305 {
1306 PyObject *val;
1307 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001308 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001309#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001310 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001311#else
1312 bnsec = 0;
1313#endif
1314 if (_stat_float_times) {
1315 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1316 } else {
1317 val = PyInt_FromLong((long)bsec);
1318 }
1319 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1320 val);
1321 }
1322#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001323#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1324 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001325 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001326#endif
Fred Drake699f3522000-06-29 21:12:41 +00001327
1328 if (PyErr_Occurred()) {
1329 Py_DECREF(v);
1330 return NULL;
1331 }
1332
1333 return v;
1334}
1335
Martin v. Löwisd8948722004-06-02 09:57:56 +00001336#ifdef MS_WINDOWS
1337
1338/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1339 where / can be used in place of \ and the trailing slash is optional.
1340 Both SERVER and SHARE must have at least one character.
1341*/
1342
1343#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1344#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001345#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001346#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001347#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001348
Tim Peters4ad82172004-08-30 17:02:04 +00001349static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001350IsUNCRootA(char *path, int pathlen)
1351{
1352 #define ISSLASH ISSLASHA
1353
1354 int i, share;
1355
1356 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1357 /* minimum UNCRoot is \\x\y */
1358 return FALSE;
1359 for (i = 2; i < pathlen ; i++)
1360 if (ISSLASH(path[i])) break;
1361 if (i == 2 || i == pathlen)
1362 /* do not allow \\\SHARE or \\SERVER */
1363 return FALSE;
1364 share = i+1;
1365 for (i = share; i < pathlen; i++)
1366 if (ISSLASH(path[i])) break;
1367 return (i != share && (i == pathlen || i == pathlen-1));
1368
1369 #undef ISSLASH
1370}
1371
1372#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001373static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001374IsUNCRootW(Py_UNICODE *path, int pathlen)
1375{
1376 #define ISSLASH ISSLASHW
1377
1378 int i, share;
1379
1380 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1381 /* minimum UNCRoot is \\x\y */
1382 return FALSE;
1383 for (i = 2; i < pathlen ; i++)
1384 if (ISSLASH(path[i])) break;
1385 if (i == 2 || i == pathlen)
1386 /* do not allow \\\SHARE or \\SERVER */
1387 return FALSE;
1388 share = i+1;
1389 for (i = share; i < pathlen; i++)
1390 if (ISSLASH(path[i])) break;
1391 return (i != share && (i == pathlen || i == pathlen-1));
1392
1393 #undef ISSLASH
1394}
1395#endif /* Py_WIN_WIDE_FILENAMES */
1396#endif /* MS_WINDOWS */
1397
Barry Warsaw53699e91996-12-10 23:23:01 +00001398static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001399posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001400 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001401#ifdef __VMS
1402 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1403#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001404 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001405#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001406 char *wformat,
1407 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001408{
Fred Drake699f3522000-06-29 21:12:41 +00001409 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001410 char *path = NULL; /* pass this to stat; do not free() it */
1411 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001412 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001413 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001414
1415#ifdef Py_WIN_WIDE_FILENAMES
1416 /* If on wide-character-capable OS see if argument
1417 is Unicode and if so use wide API. */
1418 if (unicode_file_names()) {
1419 PyUnicodeObject *po;
1420 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001421 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1422
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001423 Py_BEGIN_ALLOW_THREADS
1424 /* PyUnicode_AS_UNICODE result OK without
1425 thread lock as it is a simple dereference. */
1426 res = wstatfunc(wpath, &st);
1427 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001428
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001429 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001430 return win32_error_unicode("stat", wpath);
1431 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001432 }
1433 /* Drop the argument parsing error as narrow strings
1434 are also valid. */
1435 PyErr_Clear();
1436 }
1437#endif
1438
Tim Peters5aa91602002-01-30 05:46:57 +00001439 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001440 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001441 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001442 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001443
Barry Warsaw53699e91996-12-10 23:23:01 +00001444 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001445 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001446 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001447
1448 if (res != 0) {
1449#ifdef MS_WINDOWS
1450 result = win32_error("stat", pathfree);
1451#else
1452 result = posix_error_with_filename(pathfree);
1453#endif
1454 }
1455 else
1456 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001457
Tim Peters500bd032001-12-19 19:05:01 +00001458 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001459 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001460}
1461
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001462/* POSIX methods */
1463
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001464PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001465"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001466Use the real uid/gid to test for access to a path. Note that most\n\
1467operations will use the effective uid/gid, therefore this routine can\n\
1468be used in a suid/sgid environment to test if the invoking user has the\n\
1469specified access to the path. The mode argument can be F_OK to test\n\
1470existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001471
1472static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001473posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001474{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001475 char *path;
1476 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001477
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001478#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001479 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001480 if (unicode_file_names()) {
1481 PyUnicodeObject *po;
1482 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1483 Py_BEGIN_ALLOW_THREADS
1484 /* PyUnicode_AS_UNICODE OK without thread lock as
1485 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001486 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001487 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001488 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001489 }
1490 /* Drop the argument parsing error as narrow strings
1491 are also valid. */
1492 PyErr_Clear();
1493 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001494 if (!PyArg_ParseTuple(args, "eti:access",
1495 Py_FileSystemDefaultEncoding, &path, &mode))
1496 return 0;
1497 Py_BEGIN_ALLOW_THREADS
1498 attr = GetFileAttributesA(path);
1499 Py_END_ALLOW_THREADS
1500 PyMem_Free(path);
1501finish:
1502 if (attr == 0xFFFFFFFF)
1503 /* File does not exist, or cannot read attributes */
1504 return PyBool_FromLong(0);
1505 /* Access is possible if either write access wasn't requested, or
1506 the file isn't read-only. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001507 return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001508#else
1509 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001510 if (!PyArg_ParseTuple(args, "eti:access",
1511 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001512 return NULL;
1513 Py_BEGIN_ALLOW_THREADS
1514 res = access(path, mode);
1515 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001516 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001517 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001518#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001519}
1520
Guido van Rossumd371ff11999-01-25 16:12:23 +00001521#ifndef F_OK
1522#define F_OK 0
1523#endif
1524#ifndef R_OK
1525#define R_OK 4
1526#endif
1527#ifndef W_OK
1528#define W_OK 2
1529#endif
1530#ifndef X_OK
1531#define X_OK 1
1532#endif
1533
1534#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001535PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001536"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001537Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001538
1539static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001540posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001541{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001542 int id;
1543 char *ret;
1544
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001545 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001546 return NULL;
1547
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001548#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001549 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001550 if (id == 0) {
1551 ret = ttyname();
1552 }
1553 else {
1554 ret = NULL;
1555 }
1556#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001557 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001558#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001559 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001560 return posix_error();
1561 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001562}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001563#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001564
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001565#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001566PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001567"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001568Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001569
1570static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001571posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001572{
1573 char *ret;
1574 char buffer[L_ctermid];
1575
Greg Wardb48bc172000-03-01 21:51:56 +00001576#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001577 ret = ctermid_r(buffer);
1578#else
1579 ret = ctermid(buffer);
1580#endif
1581 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001582 return posix_error();
1583 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001584}
1585#endif
1586
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001587PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001588"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001589Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001590
Barry Warsaw53699e91996-12-10 23:23:01 +00001591static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001592posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001593{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001594#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001595 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001596#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001597 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001598#elif defined(__VMS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001599 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001600#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00001601 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001602#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001603}
1604
Fred Drake4d1e64b2002-04-15 19:40:07 +00001605#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001606PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001607"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001608Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001609opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001610
1611static PyObject *
1612posix_fchdir(PyObject *self, PyObject *fdobj)
1613{
1614 return posix_fildes(fdobj, fchdir);
1615}
1616#endif /* HAVE_FCHDIR */
1617
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001618
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001619PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001620"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001621Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001622
Barry Warsaw53699e91996-12-10 23:23:01 +00001623static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001624posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001625{
Mark Hammondef8b6542001-05-13 08:04:26 +00001626 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001627 int i;
1628 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001629#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001630 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001631 if (unicode_file_names()) {
1632 PyUnicodeObject *po;
1633 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1634 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001635 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1636 if (attr != 0xFFFFFFFF) {
1637 if (i & _S_IWRITE)
1638 attr &= ~FILE_ATTRIBUTE_READONLY;
1639 else
1640 attr |= FILE_ATTRIBUTE_READONLY;
1641 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1642 }
1643 else
1644 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001645 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001646 if (!res)
1647 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001648 PyUnicode_AS_UNICODE(po));
1649 Py_INCREF(Py_None);
1650 return Py_None;
1651 }
1652 /* Drop the argument parsing error as narrow strings
1653 are also valid. */
1654 PyErr_Clear();
1655 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001656 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1657 &path, &i))
1658 return NULL;
1659 Py_BEGIN_ALLOW_THREADS
1660 attr = GetFileAttributesA(path);
1661 if (attr != 0xFFFFFFFF) {
1662 if (i & _S_IWRITE)
1663 attr &= ~FILE_ATTRIBUTE_READONLY;
1664 else
1665 attr |= FILE_ATTRIBUTE_READONLY;
1666 res = SetFileAttributesA(path, attr);
1667 }
1668 else
1669 res = 0;
1670 Py_END_ALLOW_THREADS
1671 if (!res) {
1672 win32_error("chmod", path);
1673 PyMem_Free(path);
1674 return NULL;
1675 }
1676 PyMem_Free(path);
1677 Py_INCREF(Py_None);
1678 return Py_None;
1679#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001680 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001681 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001682 return NULL;
1683 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001684 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001685 Py_END_ALLOW_THREADS
1686 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001687 return posix_error_with_allocated_filename(path);
1688 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001689 Py_INCREF(Py_None);
1690 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001691#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001692}
1693
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001694
Thomas Wouterscf297e42007-02-23 15:07:44 +00001695#ifdef HAVE_CHFLAGS
1696PyDoc_STRVAR(posix_chflags__doc__,
1697"chflags(path, flags)\n\n\
1698Set file flags.");
1699
1700static PyObject *
1701posix_chflags(PyObject *self, PyObject *args)
1702{
1703 char *path;
1704 unsigned long flags;
1705 int res;
1706 if (!PyArg_ParseTuple(args, "etk:chflags",
1707 Py_FileSystemDefaultEncoding, &path, &flags))
1708 return NULL;
1709 Py_BEGIN_ALLOW_THREADS
1710 res = chflags(path, flags);
1711 Py_END_ALLOW_THREADS
1712 if (res < 0)
1713 return posix_error_with_allocated_filename(path);
1714 PyMem_Free(path);
1715 Py_INCREF(Py_None);
1716 return Py_None;
1717}
1718#endif /* HAVE_CHFLAGS */
1719
1720#ifdef HAVE_LCHFLAGS
1721PyDoc_STRVAR(posix_lchflags__doc__,
1722"lchflags(path, flags)\n\n\
1723Set file flags.\n\
1724This function will not follow symbolic links.");
1725
1726static PyObject *
1727posix_lchflags(PyObject *self, PyObject *args)
1728{
1729 char *path;
1730 unsigned long flags;
1731 int res;
1732 if (!PyArg_ParseTuple(args, "etk:lchflags",
1733 Py_FileSystemDefaultEncoding, &path, &flags))
1734 return NULL;
1735 Py_BEGIN_ALLOW_THREADS
1736 res = lchflags(path, flags);
1737 Py_END_ALLOW_THREADS
1738 if (res < 0)
1739 return posix_error_with_allocated_filename(path);
1740 PyMem_Free(path);
1741 Py_INCREF(Py_None);
1742 return Py_None;
1743}
1744#endif /* HAVE_LCHFLAGS */
1745
Martin v. Löwis244edc82001-10-04 22:44:26 +00001746#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001747PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001748"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001749Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001750
1751static PyObject *
1752posix_chroot(PyObject *self, PyObject *args)
1753{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001754 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001755}
1756#endif
1757
Guido van Rossum21142a01999-01-08 21:05:37 +00001758#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001759PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001760"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001761force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001762
1763static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001764posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001765{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001766 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001767}
1768#endif /* HAVE_FSYNC */
1769
1770#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001771
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001772#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001773extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1774#endif
1775
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001776PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001777"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001778force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001779 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001780
1781static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001782posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001783{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001784 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001785}
1786#endif /* HAVE_FDATASYNC */
1787
1788
Fredrik Lundh10723342000-07-10 16:38:09 +00001789#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001790PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001791"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001792Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001793
Barry Warsaw53699e91996-12-10 23:23:01 +00001794static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001795posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001796{
Mark Hammondef8b6542001-05-13 08:04:26 +00001797 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001798 int uid, gid;
1799 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001800 if (!PyArg_ParseTuple(args, "etii:chown",
1801 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001802 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001803 return NULL;
1804 Py_BEGIN_ALLOW_THREADS
1805 res = chown(path, (uid_t) uid, (gid_t) gid);
1806 Py_END_ALLOW_THREADS
1807 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001808 return posix_error_with_allocated_filename(path);
1809 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001810 Py_INCREF(Py_None);
1811 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001812}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001813#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001814
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001815#ifdef HAVE_LCHOWN
1816PyDoc_STRVAR(posix_lchown__doc__,
1817"lchown(path, uid, gid)\n\n\
1818Change the owner and group id of path to the numeric uid and gid.\n\
1819This function will not follow symbolic links.");
1820
1821static PyObject *
1822posix_lchown(PyObject *self, PyObject *args)
1823{
1824 char *path = NULL;
1825 int uid, gid;
1826 int res;
1827 if (!PyArg_ParseTuple(args, "etii:lchown",
1828 Py_FileSystemDefaultEncoding, &path,
1829 &uid, &gid))
1830 return NULL;
1831 Py_BEGIN_ALLOW_THREADS
1832 res = lchown(path, (uid_t) uid, (gid_t) gid);
1833 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001834 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001835 return posix_error_with_allocated_filename(path);
1836 PyMem_Free(path);
1837 Py_INCREF(Py_None);
1838 return Py_None;
1839}
1840#endif /* HAVE_LCHOWN */
1841
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001842
Guido van Rossum36bc6801995-06-14 22:54:23 +00001843#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001844PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001845"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001846Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001847
Barry Warsaw53699e91996-12-10 23:23:01 +00001848static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001849posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001850{
1851 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001852 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001853
Barry Warsaw53699e91996-12-10 23:23:01 +00001854 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001855#if defined(PYOS_OS2) && defined(PYCC_GCC)
1856 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001857#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001858 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001859#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001860 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001861 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001862 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001863 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001864}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001865
Walter Dörwald3b918c32002-11-21 20:18:46 +00001866#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001867PyDoc_STRVAR(posix_getcwdu__doc__,
1868"getcwdu() -> path\n\n\
1869Return a unicode string representing the current working directory.");
1870
1871static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001872posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001873{
1874 char buf[1026];
1875 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001876
1877#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001878 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001879 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001880 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001881 wchar_t *wbuf2 = wbuf;
1882 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001883 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001884 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1885 /* If the buffer is large enough, len does not include the
1886 terminating \0. If the buffer is too small, len includes
1887 the space needed for the terminator. */
1888 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1889 wbuf2 = malloc(len * sizeof(wchar_t));
1890 if (wbuf2)
1891 len = GetCurrentDirectoryW(len, wbuf2);
1892 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001893 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001894 if (!wbuf2) {
1895 PyErr_NoMemory();
1896 return NULL;
1897 }
1898 if (!len) {
1899 if (wbuf2 != wbuf) free(wbuf2);
1900 return win32_error("getcwdu", NULL);
1901 }
1902 resobj = PyUnicode_FromWideChar(wbuf2, len);
1903 if (wbuf2 != wbuf) free(wbuf2);
1904 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001905 }
1906#endif
1907
1908 Py_BEGIN_ALLOW_THREADS
1909#if defined(PYOS_OS2) && defined(PYCC_GCC)
1910 res = _getcwd2(buf, sizeof buf);
1911#else
1912 res = getcwd(buf, sizeof buf);
1913#endif
1914 Py_END_ALLOW_THREADS
1915 if (res == NULL)
1916 return posix_error();
1917 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1918}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001919#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001920#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001921
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001922
Guido van Rossumb6775db1994-08-01 11:34:53 +00001923#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001924PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001925"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001926Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001927
Barry Warsaw53699e91996-12-10 23:23:01 +00001928static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001929posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001930{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001931 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001932}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001933#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001934
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001935
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001936PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001937"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001938Return a list containing the names of the entries in the directory.\n\
1939\n\
1940 path: path of directory to list\n\
1941\n\
1942The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001943entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001944
Barry Warsaw53699e91996-12-10 23:23:01 +00001945static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001946posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001947{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001948 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001949 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001950#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001951
Barry Warsaw53699e91996-12-10 23:23:01 +00001952 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001953 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001954 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001955 WIN32_FIND_DATA FileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001956 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00001957 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001958 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001959
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001960#ifdef Py_WIN_WIDE_FILENAMES
1961 /* If on wide-character-capable OS see if argument
1962 is Unicode and if so use wide API. */
1963 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001964 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001965 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1966 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001967 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001968 Py_UNICODE wch;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001969 /* Overallocate for \\*.*\0 */
1970 len = PyUnicode_GET_SIZE(po);
1971 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
1972 if (!wnamebuf) {
1973 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001974 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001975 }
1976 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
1977 wch = len > 0 ? wnamebuf[len-1] : '\0';
1978 if (wch != L'/' && wch != L'\\' && wch != L':')
1979 wnamebuf[len++] = L'\\';
1980 wcscpy(wnamebuf + len, L"*.*");
1981 if ((d = PyList_New(0)) == NULL) {
1982 free(wnamebuf);
1983 return NULL;
1984 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001985 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1986 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001987 int error = GetLastError();
1988 if (error == ERROR_FILE_NOT_FOUND) {
1989 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001990 return d;
1991 }
1992 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001993 win32_error_unicode("FindFirstFileW", wnamebuf);
1994 free(wnamebuf);
1995 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001996 }
1997 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001998 /* Skip over . and .. */
1999 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2000 wcscmp(wFileData.cFileName, L"..") != 0) {
2001 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2002 if (v == NULL) {
2003 Py_DECREF(d);
2004 d = NULL;
2005 break;
2006 }
2007 if (PyList_Append(d, v) != 0) {
2008 Py_DECREF(v);
2009 Py_DECREF(d);
2010 d = NULL;
2011 break;
2012 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002013 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002014 }
Georg Brandl622927b2006-03-07 12:48:03 +00002015 Py_BEGIN_ALLOW_THREADS
2016 result = FindNextFileW(hFindFile, &wFileData);
2017 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002018 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2019 it got to the end of the directory. */
2020 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2021 Py_DECREF(d);
2022 win32_error_unicode("FindNextFileW", wnamebuf);
2023 FindClose(hFindFile);
2024 free(wnamebuf);
2025 return NULL;
2026 }
Georg Brandl622927b2006-03-07 12:48:03 +00002027 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002028
2029 if (FindClose(hFindFile) == FALSE) {
2030 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002031 win32_error_unicode("FindClose", wnamebuf);
2032 free(wnamebuf);
2033 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002034 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002035 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002036 return d;
2037 }
2038 /* Drop the argument parsing error as narrow strings
2039 are also valid. */
2040 PyErr_Clear();
2041 }
2042#endif
2043
Tim Peters5aa91602002-01-30 05:46:57 +00002044 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002045 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002046 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002047 if (len > 0) {
2048 char ch = namebuf[len-1];
2049 if (ch != SEP && ch != ALTSEP && ch != ':')
2050 namebuf[len++] = '/';
2051 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002052 strcpy(namebuf + len, "*.*");
2053
Barry Warsaw53699e91996-12-10 23:23:01 +00002054 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002055 return NULL;
2056
2057 hFindFile = FindFirstFile(namebuf, &FileData);
2058 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002059 int error = GetLastError();
2060 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002061 return d;
2062 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002063 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002064 }
2065 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002066 /* Skip over . and .. */
2067 if (strcmp(FileData.cFileName, ".") != 0 &&
2068 strcmp(FileData.cFileName, "..") != 0) {
2069 v = PyString_FromString(FileData.cFileName);
2070 if (v == NULL) {
2071 Py_DECREF(d);
2072 d = NULL;
2073 break;
2074 }
2075 if (PyList_Append(d, v) != 0) {
2076 Py_DECREF(v);
2077 Py_DECREF(d);
2078 d = NULL;
2079 break;
2080 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002081 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002082 }
Georg Brandl622927b2006-03-07 12:48:03 +00002083 Py_BEGIN_ALLOW_THREADS
2084 result = FindNextFile(hFindFile, &FileData);
2085 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002086 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2087 it got to the end of the directory. */
2088 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2089 Py_DECREF(d);
2090 win32_error("FindNextFile", namebuf);
2091 FindClose(hFindFile);
2092 return NULL;
2093 }
Georg Brandl622927b2006-03-07 12:48:03 +00002094 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002095
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002096 if (FindClose(hFindFile) == FALSE) {
2097 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002098 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002099 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002100
2101 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002102
Tim Peters0bb44a42000-09-15 07:44:49 +00002103#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002104
2105#ifndef MAX_PATH
2106#define MAX_PATH CCHMAXPATH
2107#endif
2108 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002109 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002110 PyObject *d, *v;
2111 char namebuf[MAX_PATH+5];
2112 HDIR hdir = 1;
2113 ULONG srchcnt = 1;
2114 FILEFINDBUF3 ep;
2115 APIRET rc;
2116
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002117 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002118 return NULL;
2119 if (len >= MAX_PATH) {
2120 PyErr_SetString(PyExc_ValueError, "path too long");
2121 return NULL;
2122 }
2123 strcpy(namebuf, name);
2124 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002125 if (*pt == ALTSEP)
2126 *pt = SEP;
2127 if (namebuf[len-1] != SEP)
2128 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002129 strcpy(namebuf + len, "*.*");
2130
2131 if ((d = PyList_New(0)) == NULL)
2132 return NULL;
2133
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002134 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2135 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002136 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002137 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2138 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2139 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002140
2141 if (rc != NO_ERROR) {
2142 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002143 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002144 }
2145
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002146 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002147 do {
2148 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002149 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002150 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002151
2152 strcpy(namebuf, ep.achName);
2153
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002154 /* Leave Case of Name Alone -- In Native Form */
2155 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002156
2157 v = PyString_FromString(namebuf);
2158 if (v == NULL) {
2159 Py_DECREF(d);
2160 d = NULL;
2161 break;
2162 }
2163 if (PyList_Append(d, v) != 0) {
2164 Py_DECREF(v);
2165 Py_DECREF(d);
2166 d = NULL;
2167 break;
2168 }
2169 Py_DECREF(v);
2170 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2171 }
2172
2173 return d;
2174#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002175
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002176 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002177 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002178 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002179 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002180 int arg_is_unicode = 1;
2181
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002182 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002183 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2184 arg_is_unicode = 0;
2185 PyErr_Clear();
2186 }
2187 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002188 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002189 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002190 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002191 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002192 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002193 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002194 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002195 return NULL;
2196 }
Georg Brandl622927b2006-03-07 12:48:03 +00002197 for (;;) {
2198 Py_BEGIN_ALLOW_THREADS
2199 ep = readdir(dirp);
2200 Py_END_ALLOW_THREADS
2201 if (ep == NULL)
2202 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002203 if (ep->d_name[0] == '.' &&
2204 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002205 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002206 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00002207 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002208 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002209 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002210 d = NULL;
2211 break;
2212 }
Just van Rossum46c97842003-02-25 21:42:15 +00002213#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00002214 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002215 PyObject *w;
2216
2217 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002218 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002219 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002220 if (w != NULL) {
2221 Py_DECREF(v);
2222 v = w;
2223 }
2224 else {
2225 /* fall back to the original byte string, as
2226 discussed in patch #683592 */
2227 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002228 }
Just van Rossum46c97842003-02-25 21:42:15 +00002229 }
2230#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002231 if (PyList_Append(d, v) != 0) {
2232 Py_DECREF(v);
2233 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002234 d = NULL;
2235 break;
2236 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002237 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002238 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002239 if (errno != 0 && d != NULL) {
2240 /* readdir() returned NULL and set errno */
2241 closedir(dirp);
2242 Py_DECREF(d);
2243 return posix_error_with_allocated_filename(name);
2244 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002245 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002246 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002247
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002248 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002249
Tim Peters0bb44a42000-09-15 07:44:49 +00002250#endif /* which OS */
2251} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002252
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002253#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002254/* A helper function for abspath on win32 */
2255static PyObject *
2256posix__getfullpathname(PyObject *self, PyObject *args)
2257{
2258 /* assume encoded strings wont more than double no of chars */
2259 char inbuf[MAX_PATH*2];
2260 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002261 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002262 char outbuf[MAX_PATH*2];
2263 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002264#ifdef Py_WIN_WIDE_FILENAMES
2265 if (unicode_file_names()) {
2266 PyUnicodeObject *po;
2267 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2268 Py_UNICODE woutbuf[MAX_PATH*2];
2269 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002270 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002271 sizeof(woutbuf)/sizeof(woutbuf[0]),
2272 woutbuf, &wtemp))
2273 return win32_error("GetFullPathName", "");
2274 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2275 }
2276 /* Drop the argument parsing error as narrow strings
2277 are also valid. */
2278 PyErr_Clear();
2279 }
2280#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002281 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2282 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002283 &insize))
2284 return NULL;
2285 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2286 outbuf, &temp))
2287 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002288 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2289 return PyUnicode_Decode(outbuf, strlen(outbuf),
2290 Py_FileSystemDefaultEncoding, NULL);
2291 }
Mark Hammondef8b6542001-05-13 08:04:26 +00002292 return PyString_FromString(outbuf);
2293} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002294#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002295
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002296PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002297"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002298Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002299
Barry Warsaw53699e91996-12-10 23:23:01 +00002300static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002301posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002302{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002303 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002304 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002305 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002306
2307#ifdef Py_WIN_WIDE_FILENAMES
2308 if (unicode_file_names()) {
2309 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002310 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002311 Py_BEGIN_ALLOW_THREADS
2312 /* PyUnicode_AS_UNICODE OK without thread lock as
2313 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002314 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002315 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002316 if (!res)
2317 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002318 Py_INCREF(Py_None);
2319 return Py_None;
2320 }
2321 /* Drop the argument parsing error as narrow strings
2322 are also valid. */
2323 PyErr_Clear();
2324 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002325 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2326 Py_FileSystemDefaultEncoding, &path, &mode))
2327 return NULL;
2328 Py_BEGIN_ALLOW_THREADS
2329 /* PyUnicode_AS_UNICODE OK without thread lock as
2330 it is a simple dereference. */
2331 res = CreateDirectoryA(path, NULL);
2332 Py_END_ALLOW_THREADS
2333 if (!res) {
2334 win32_error("mkdir", path);
2335 PyMem_Free(path);
2336 return NULL;
2337 }
2338 PyMem_Free(path);
2339 Py_INCREF(Py_None);
2340 return Py_None;
2341#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002342
Tim Peters5aa91602002-01-30 05:46:57 +00002343 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002344 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002345 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002346 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002347#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002348 res = mkdir(path);
2349#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002350 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002351#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002352 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002353 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002354 return posix_error_with_allocated_filename(path);
2355 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002356 Py_INCREF(Py_None);
2357 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002358#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002359}
2360
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002361
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002362/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2363#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002364#include <sys/resource.h>
2365#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002366
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002367
2368#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002369PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002370"nice(inc) -> new_priority\n\n\
2371Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002372
Barry Warsaw53699e91996-12-10 23:23:01 +00002373static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002374posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002375{
2376 int increment, value;
2377
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002378 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002379 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002380
2381 /* There are two flavours of 'nice': one that returns the new
2382 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002383 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2384 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002385
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002386 If we are of the nice family that returns the new priority, we
2387 need to clear errno before the call, and check if errno is filled
2388 before calling posix_error() on a returnvalue of -1, because the
2389 -1 may be the actual new priority! */
2390
2391 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002392 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002393#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002394 if (value == 0)
2395 value = getpriority(PRIO_PROCESS, 0);
2396#endif
2397 if (value == -1 && errno != 0)
2398 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002399 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002400 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002401}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002402#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002403
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002404PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002405"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002406Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002407
Barry Warsaw53699e91996-12-10 23:23:01 +00002408static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002409posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002410{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002411#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002412 PyObject *o1, *o2;
2413 char *p1, *p2;
2414 BOOL result;
2415 if (unicode_file_names()) {
2416 if (!PyArg_ParseTuple(args, "O&O&:rename",
2417 convert_to_unicode, &o1,
2418 convert_to_unicode, &o2))
2419 PyErr_Clear();
2420 else {
2421 Py_BEGIN_ALLOW_THREADS
2422 result = MoveFileW(PyUnicode_AsUnicode(o1),
2423 PyUnicode_AsUnicode(o2));
2424 Py_END_ALLOW_THREADS
2425 Py_DECREF(o1);
2426 Py_DECREF(o2);
2427 if (!result)
2428 return win32_error("rename", NULL);
2429 Py_INCREF(Py_None);
2430 return Py_None;
2431 }
2432 }
2433 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2434 return NULL;
2435 Py_BEGIN_ALLOW_THREADS
2436 result = MoveFileA(p1, p2);
2437 Py_END_ALLOW_THREADS
2438 if (!result)
2439 return win32_error("rename", NULL);
2440 Py_INCREF(Py_None);
2441 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002442#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002443 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002444#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002445}
2446
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002448PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002449"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002450Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002451
Barry Warsaw53699e91996-12-10 23:23:01 +00002452static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002453posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002454{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002455#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002456 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002457#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002458 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002459#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002460}
2461
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002463PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002464"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002465Perform a stat system call on the given path.");
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_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002469{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002470#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002471 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002472#else
2473 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2474#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002475}
2476
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002477
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002478#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002479PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002480"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002481Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002482
Barry Warsaw53699e91996-12-10 23:23:01 +00002483static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002484posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002485{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002486 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002487 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002488 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002489 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002490 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002491 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002492 Py_END_ALLOW_THREADS
2493 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002494}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002495#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002496
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002497
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002498PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002499"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002500Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002501
Barry Warsaw53699e91996-12-10 23:23:01 +00002502static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002503posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002504{
2505 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002506 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002507 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002508 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002509 if (i < 0)
2510 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002511 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002512}
2513
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002514
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002515PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002516"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002517Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002518
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002519PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002520"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002521Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002522
Barry Warsaw53699e91996-12-10 23:23:01 +00002523static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002524posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002525{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002526#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002527 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002528#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002529 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002530#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002531}
2532
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002533
Guido van Rossumb6775db1994-08-01 11:34:53 +00002534#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002535PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002536"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002537Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002538
Barry Warsaw53699e91996-12-10 23:23:01 +00002539static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002540posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002541{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002542 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002543 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002544
Barry Warsaw53699e91996-12-10 23:23:01 +00002545 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002546 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002547 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002548 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002549 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002550 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002551 u.sysname,
2552 u.nodename,
2553 u.release,
2554 u.version,
2555 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002556}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002557#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002558
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002559static int
2560extract_time(PyObject *t, long* sec, long* usec)
2561{
2562 long intval;
2563 if (PyFloat_Check(t)) {
2564 double tval = PyFloat_AsDouble(t);
2565 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
2566 if (!intobj)
2567 return -1;
2568 intval = PyInt_AsLong(intobj);
2569 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002570 if (intval == -1 && PyErr_Occurred())
2571 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002572 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002573 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002574 if (*usec < 0)
2575 /* If rounding gave us a negative number,
2576 truncate. */
2577 *usec = 0;
2578 return 0;
2579 }
2580 intval = PyInt_AsLong(t);
2581 if (intval == -1 && PyErr_Occurred())
2582 return -1;
2583 *sec = intval;
2584 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002585 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002586}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002587
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002588PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002589"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002590utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002591Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002592second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002593
Barry Warsaw53699e91996-12-10 23:23:01 +00002594static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002595posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002596{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002597#ifdef Py_WIN_WIDE_FILENAMES
2598 PyObject *arg;
2599 PyUnicodeObject *obwpath;
2600 wchar_t *wpath = NULL;
2601 char *apath = NULL;
2602 HANDLE hFile;
2603 long atimesec, mtimesec, ausec, musec;
2604 FILETIME atime, mtime;
2605 PyObject *result = NULL;
2606
2607 if (unicode_file_names()) {
2608 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2609 wpath = PyUnicode_AS_UNICODE(obwpath);
2610 Py_BEGIN_ALLOW_THREADS
2611 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002612 NULL, OPEN_EXISTING,
2613 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002614 Py_END_ALLOW_THREADS
2615 if (hFile == INVALID_HANDLE_VALUE)
2616 return win32_error_unicode("utime", wpath);
2617 } else
2618 /* Drop the argument parsing error as narrow strings
2619 are also valid. */
2620 PyErr_Clear();
2621 }
2622 if (!wpath) {
2623 if (!PyArg_ParseTuple(args, "etO:utime",
2624 Py_FileSystemDefaultEncoding, &apath, &arg))
2625 return NULL;
2626 Py_BEGIN_ALLOW_THREADS
2627 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002628 NULL, OPEN_EXISTING,
2629 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002630 Py_END_ALLOW_THREADS
2631 if (hFile == INVALID_HANDLE_VALUE) {
2632 win32_error("utime", apath);
2633 PyMem_Free(apath);
2634 return NULL;
2635 }
2636 PyMem_Free(apath);
2637 }
2638
2639 if (arg == Py_None) {
2640 SYSTEMTIME now;
2641 GetSystemTime(&now);
2642 if (!SystemTimeToFileTime(&now, &mtime) ||
2643 !SystemTimeToFileTime(&now, &atime)) {
2644 win32_error("utime", NULL);
2645 goto done;
2646 }
2647 }
2648 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2649 PyErr_SetString(PyExc_TypeError,
2650 "utime() arg 2 must be a tuple (atime, mtime)");
2651 goto done;
2652 }
2653 else {
2654 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2655 &atimesec, &ausec) == -1)
2656 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002657 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002658 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2659 &mtimesec, &musec) == -1)
2660 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002661 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002662 }
2663 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2664 /* Avoid putting the file name into the error here,
2665 as that may confuse the user into believing that
2666 something is wrong with the file, when it also
2667 could be the time stamp that gives a problem. */
2668 win32_error("utime", NULL);
2669 }
2670 Py_INCREF(Py_None);
2671 result = Py_None;
2672done:
2673 CloseHandle(hFile);
2674 return result;
2675#else /* Py_WIN_WIDE_FILENAMES */
2676
Neal Norwitz2adf2102004-06-09 01:46:02 +00002677 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002678 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002679 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002680 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002681
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002682#if defined(HAVE_UTIMES)
2683 struct timeval buf[2];
2684#define ATIME buf[0].tv_sec
2685#define MTIME buf[1].tv_sec
2686#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002687/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002688 struct utimbuf buf;
2689#define ATIME buf.actime
2690#define MTIME buf.modtime
2691#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002692#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002693 time_t buf[2];
2694#define ATIME buf[0]
2695#define MTIME buf[1]
2696#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002697#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002698
Mark Hammond817c9292003-12-03 01:22:38 +00002699
Thomas Wouters477c8d52006-05-27 19:21:47 +00002700 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002701 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002702 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002703 if (arg == Py_None) {
2704 /* optional time values not given */
2705 Py_BEGIN_ALLOW_THREADS
2706 res = utime(path, NULL);
2707 Py_END_ALLOW_THREADS
2708 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002709 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002710 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002711 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002712 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002713 return NULL;
2714 }
2715 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002716 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002717 &atime, &ausec) == -1) {
2718 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002719 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002720 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002721 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002722 &mtime, &musec) == -1) {
2723 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002724 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002725 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002726 ATIME = atime;
2727 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002728#ifdef HAVE_UTIMES
2729 buf[0].tv_usec = ausec;
2730 buf[1].tv_usec = musec;
2731 Py_BEGIN_ALLOW_THREADS
2732 res = utimes(path, buf);
2733 Py_END_ALLOW_THREADS
2734#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002735 Py_BEGIN_ALLOW_THREADS
2736 res = utime(path, UTIME_ARG);
2737 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002738#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002739 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002740 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002741 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002742 }
Neal Norwitz96652712004-06-06 20:40:27 +00002743 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002744 Py_INCREF(Py_None);
2745 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002746#undef UTIME_ARG
2747#undef ATIME
2748#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002749#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002750}
2751
Guido van Rossum85e3b011991-06-03 12:42:10 +00002752
Guido van Rossum3b066191991-06-04 19:40:25 +00002753/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002754
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002755PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002756"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002757Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002758
Barry Warsaw53699e91996-12-10 23:23:01 +00002759static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002760posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002761{
2762 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002763 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002764 return NULL;
2765 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002766 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002767}
2768
Martin v. Löwis114619e2002-10-07 06:44:21 +00002769#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2770static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002771free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002772{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002773 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002774 for (i = 0; i < count; i++)
2775 PyMem_Free(array[i]);
2776 PyMem_DEL(array);
2777}
2778#endif
2779
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002780
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002781#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002782PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002783"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002784Execute an executable path with arguments, replacing current process.\n\
2785\n\
2786 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002787 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002788
Barry Warsaw53699e91996-12-10 23:23:01 +00002789static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002790posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002791{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002792 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002793 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002794 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002795 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002796 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002797
Guido van Rossum89b33251993-10-22 14:26:06 +00002798 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002799 argv is a list or tuple of strings. */
2800
Martin v. Löwis114619e2002-10-07 06:44:21 +00002801 if (!PyArg_ParseTuple(args, "etO:execv",
2802 Py_FileSystemDefaultEncoding,
2803 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002804 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002805 if (PyList_Check(argv)) {
2806 argc = PyList_Size(argv);
2807 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002808 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002809 else if (PyTuple_Check(argv)) {
2810 argc = PyTuple_Size(argv);
2811 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002812 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002813 else {
Fred Drake661ea262000-10-24 19:57:45 +00002814 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002815 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002816 return NULL;
2817 }
2818
Barry Warsaw53699e91996-12-10 23:23:01 +00002819 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002820 if (argvlist == NULL) {
2821 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002822 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002823 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002824 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002825 if (!PyArg_Parse((*getitem)(argv, i), "et",
2826 Py_FileSystemDefaultEncoding,
2827 &argvlist[i])) {
2828 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002829 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002830 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002831 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002832 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002833
Guido van Rossum85e3b011991-06-03 12:42:10 +00002834 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002835 }
2836 argvlist[argc] = NULL;
2837
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002838 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002839
Guido van Rossum85e3b011991-06-03 12:42:10 +00002840 /* If we get here it's definitely an error */
2841
Martin v. Löwis114619e2002-10-07 06:44:21 +00002842 free_string_array(argvlist, argc);
2843 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002844 return posix_error();
2845}
2846
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002847
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002848PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002849"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002850Execute a path with arguments and environment, replacing current process.\n\
2851\n\
2852 path: path of executable file\n\
2853 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002854 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002855
Barry Warsaw53699e91996-12-10 23:23:01 +00002856static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002857posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002858{
2859 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002860 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002861 char **argvlist;
2862 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002863 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002864 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002865 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002866 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002867
2868 /* execve has three arguments: (path, argv, env), where
2869 argv is a list or tuple of strings and env is a dictionary
2870 like posix.environ. */
2871
Martin v. Löwis114619e2002-10-07 06:44:21 +00002872 if (!PyArg_ParseTuple(args, "etOO:execve",
2873 Py_FileSystemDefaultEncoding,
2874 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002875 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002876 if (PyList_Check(argv)) {
2877 argc = PyList_Size(argv);
2878 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002879 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002880 else if (PyTuple_Check(argv)) {
2881 argc = PyTuple_Size(argv);
2882 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002883 }
2884 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002885 PyErr_SetString(PyExc_TypeError,
2886 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002887 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002888 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002889 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002890 PyErr_SetString(PyExc_TypeError,
2891 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002892 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002893 }
2894
Barry Warsaw53699e91996-12-10 23:23:01 +00002895 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002896 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002897 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002898 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002899 }
2900 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002901 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002902 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002903 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002904 &argvlist[i]))
2905 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002906 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002907 goto fail_1;
2908 }
2909 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002910 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002911 argvlist[argc] = NULL;
2912
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002913 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002914 if (i < 0)
2915 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002916 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002917 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002918 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002919 goto fail_1;
2920 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002921 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002922 keys = PyMapping_Keys(env);
2923 vals = PyMapping_Values(env);
2924 if (!keys || !vals)
2925 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002926 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2927 PyErr_SetString(PyExc_TypeError,
2928 "execve(): env.keys() or env.values() is not a list");
2929 goto fail_2;
2930 }
Tim Peters5aa91602002-01-30 05:46:57 +00002931
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002932 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002933 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002934 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002935
2936 key = PyList_GetItem(keys, pos);
2937 val = PyList_GetItem(vals, pos);
2938 if (!key || !val)
2939 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002940
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002941 if (!PyArg_Parse(
2942 key,
2943 "s;execve() arg 3 contains a non-string key",
2944 &k) ||
2945 !PyArg_Parse(
2946 val,
2947 "s;execve() arg 3 contains a non-string value",
2948 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002949 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002950 goto fail_2;
2951 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002952
2953#if defined(PYOS_OS2)
2954 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2955 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2956#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002957 len = PyString_Size(key) + PyString_Size(val) + 2;
2958 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002959 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002960 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002961 goto fail_2;
2962 }
Tim Petersc8996f52001-12-03 20:41:00 +00002963 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002964 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002965#if defined(PYOS_OS2)
2966 }
2967#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002968 }
2969 envlist[envc] = 0;
2970
2971 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002972
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002973 /* If we get here it's definitely an error */
2974
2975 (void) posix_error();
2976
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002977 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002978 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002979 PyMem_DEL(envlist[envc]);
2980 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002981 fail_1:
2982 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002983 Py_XDECREF(vals);
2984 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002985 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002986 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002987 return NULL;
2988}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002989#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002990
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002991
Guido van Rossuma1065681999-01-25 23:20:23 +00002992#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002993PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002994"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002995Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002996\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002997 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002998 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002999 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003000
3001static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003002posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003003{
3004 char *path;
3005 PyObject *argv;
3006 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003007 int mode, i;
3008 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003009 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003010 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003011
3012 /* spawnv has three arguments: (mode, path, argv), where
3013 argv is a list or tuple of strings. */
3014
Martin v. Löwis114619e2002-10-07 06:44:21 +00003015 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3016 Py_FileSystemDefaultEncoding,
3017 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003018 return NULL;
3019 if (PyList_Check(argv)) {
3020 argc = PyList_Size(argv);
3021 getitem = PyList_GetItem;
3022 }
3023 else if (PyTuple_Check(argv)) {
3024 argc = PyTuple_Size(argv);
3025 getitem = PyTuple_GetItem;
3026 }
3027 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003028 PyErr_SetString(PyExc_TypeError,
3029 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003030 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003031 return NULL;
3032 }
3033
3034 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003035 if (argvlist == NULL) {
3036 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003037 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003038 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003039 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003040 if (!PyArg_Parse((*getitem)(argv, i), "et",
3041 Py_FileSystemDefaultEncoding,
3042 &argvlist[i])) {
3043 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003044 PyErr_SetString(
3045 PyExc_TypeError,
3046 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003047 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003048 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003049 }
3050 }
3051 argvlist[argc] = NULL;
3052
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003053#if defined(PYOS_OS2) && defined(PYCC_GCC)
3054 Py_BEGIN_ALLOW_THREADS
3055 spawnval = spawnv(mode, path, argvlist);
3056 Py_END_ALLOW_THREADS
3057#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003058 if (mode == _OLD_P_OVERLAY)
3059 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003060
Tim Peters25059d32001-12-07 20:35:43 +00003061 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003062 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003063 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003064#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003065
Martin v. Löwis114619e2002-10-07 06:44:21 +00003066 free_string_array(argvlist, argc);
3067 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003068
Fred Drake699f3522000-06-29 21:12:41 +00003069 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003070 return posix_error();
3071 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003072#if SIZEOF_LONG == SIZEOF_VOID_P
3073 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003074#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003075 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003076#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003077}
3078
3079
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003080PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003081"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003082Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003083\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003084 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003085 path: path of executable file\n\
3086 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003087 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003088
3089static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003090posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003091{
3092 char *path;
3093 PyObject *argv, *env;
3094 char **argvlist;
3095 char **envlist;
3096 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003097 int mode, pos, envc;
3098 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003099 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003100 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003101 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003102
3103 /* spawnve has four arguments: (mode, path, argv, env), where
3104 argv is a list or tuple of strings and env is a dictionary
3105 like posix.environ. */
3106
Martin v. Löwis114619e2002-10-07 06:44:21 +00003107 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3108 Py_FileSystemDefaultEncoding,
3109 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003110 return NULL;
3111 if (PyList_Check(argv)) {
3112 argc = PyList_Size(argv);
3113 getitem = PyList_GetItem;
3114 }
3115 else if (PyTuple_Check(argv)) {
3116 argc = PyTuple_Size(argv);
3117 getitem = PyTuple_GetItem;
3118 }
3119 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003120 PyErr_SetString(PyExc_TypeError,
3121 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003122 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003123 }
3124 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003125 PyErr_SetString(PyExc_TypeError,
3126 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003127 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003128 }
3129
3130 argvlist = PyMem_NEW(char *, argc+1);
3131 if (argvlist == NULL) {
3132 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003133 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003134 }
3135 for (i = 0; i < argc; i++) {
3136 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003137 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003138 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003139 &argvlist[i]))
3140 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003141 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003142 goto fail_1;
3143 }
3144 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003145 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003146 argvlist[argc] = NULL;
3147
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003148 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003149 if (i < 0)
3150 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003151 envlist = PyMem_NEW(char *, i + 1);
3152 if (envlist == NULL) {
3153 PyErr_NoMemory();
3154 goto fail_1;
3155 }
3156 envc = 0;
3157 keys = PyMapping_Keys(env);
3158 vals = PyMapping_Values(env);
3159 if (!keys || !vals)
3160 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003161 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3162 PyErr_SetString(PyExc_TypeError,
3163 "spawnve(): env.keys() or env.values() is not a list");
3164 goto fail_2;
3165 }
Tim Peters5aa91602002-01-30 05:46:57 +00003166
Guido van Rossuma1065681999-01-25 23:20:23 +00003167 for (pos = 0; pos < i; pos++) {
3168 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003169 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003170
3171 key = PyList_GetItem(keys, pos);
3172 val = PyList_GetItem(vals, pos);
3173 if (!key || !val)
3174 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003175
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003176 if (!PyArg_Parse(
3177 key,
3178 "s;spawnve() arg 3 contains a non-string key",
3179 &k) ||
3180 !PyArg_Parse(
3181 val,
3182 "s;spawnve() arg 3 contains a non-string value",
3183 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003184 {
3185 goto fail_2;
3186 }
Tim Petersc8996f52001-12-03 20:41:00 +00003187 len = PyString_Size(key) + PyString_Size(val) + 2;
3188 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003189 if (p == NULL) {
3190 PyErr_NoMemory();
3191 goto fail_2;
3192 }
Tim Petersc8996f52001-12-03 20:41:00 +00003193 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003194 envlist[envc++] = p;
3195 }
3196 envlist[envc] = 0;
3197
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003198#if defined(PYOS_OS2) && defined(PYCC_GCC)
3199 Py_BEGIN_ALLOW_THREADS
3200 spawnval = spawnve(mode, path, argvlist, envlist);
3201 Py_END_ALLOW_THREADS
3202#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003203 if (mode == _OLD_P_OVERLAY)
3204 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003205
3206 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003207 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003208 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003209#endif
Tim Peters25059d32001-12-07 20:35:43 +00003210
Fred Drake699f3522000-06-29 21:12:41 +00003211 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003212 (void) posix_error();
3213 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003214#if SIZEOF_LONG == SIZEOF_VOID_P
3215 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003216#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003217 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003218#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003219
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003220 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003221 while (--envc >= 0)
3222 PyMem_DEL(envlist[envc]);
3223 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003224 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003225 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003226 Py_XDECREF(vals);
3227 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003228 fail_0:
3229 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003230 return res;
3231}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003232
3233/* OS/2 supports spawnvp & spawnvpe natively */
3234#if defined(PYOS_OS2)
3235PyDoc_STRVAR(posix_spawnvp__doc__,
3236"spawnvp(mode, file, args)\n\n\
3237Execute the program 'file' in a new process, using the environment\n\
3238search path to find the file.\n\
3239\n\
3240 mode: mode of process creation\n\
3241 file: executable file name\n\
3242 args: tuple or list of strings");
3243
3244static PyObject *
3245posix_spawnvp(PyObject *self, PyObject *args)
3246{
3247 char *path;
3248 PyObject *argv;
3249 char **argvlist;
3250 int mode, i, argc;
3251 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003252 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003253
3254 /* spawnvp has three arguments: (mode, path, argv), where
3255 argv is a list or tuple of strings. */
3256
3257 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3258 Py_FileSystemDefaultEncoding,
3259 &path, &argv))
3260 return NULL;
3261 if (PyList_Check(argv)) {
3262 argc = PyList_Size(argv);
3263 getitem = PyList_GetItem;
3264 }
3265 else if (PyTuple_Check(argv)) {
3266 argc = PyTuple_Size(argv);
3267 getitem = PyTuple_GetItem;
3268 }
3269 else {
3270 PyErr_SetString(PyExc_TypeError,
3271 "spawnvp() arg 2 must be a tuple or list");
3272 PyMem_Free(path);
3273 return NULL;
3274 }
3275
3276 argvlist = PyMem_NEW(char *, argc+1);
3277 if (argvlist == NULL) {
3278 PyMem_Free(path);
3279 return PyErr_NoMemory();
3280 }
3281 for (i = 0; i < argc; i++) {
3282 if (!PyArg_Parse((*getitem)(argv, i), "et",
3283 Py_FileSystemDefaultEncoding,
3284 &argvlist[i])) {
3285 free_string_array(argvlist, i);
3286 PyErr_SetString(
3287 PyExc_TypeError,
3288 "spawnvp() arg 2 must contain only strings");
3289 PyMem_Free(path);
3290 return NULL;
3291 }
3292 }
3293 argvlist[argc] = NULL;
3294
3295 Py_BEGIN_ALLOW_THREADS
3296#if defined(PYCC_GCC)
3297 spawnval = spawnvp(mode, path, argvlist);
3298#else
3299 spawnval = _spawnvp(mode, path, argvlist);
3300#endif
3301 Py_END_ALLOW_THREADS
3302
3303 free_string_array(argvlist, argc);
3304 PyMem_Free(path);
3305
3306 if (spawnval == -1)
3307 return posix_error();
3308 else
3309 return Py_BuildValue("l", (long) spawnval);
3310}
3311
3312
3313PyDoc_STRVAR(posix_spawnvpe__doc__,
3314"spawnvpe(mode, file, args, env)\n\n\
3315Execute the program 'file' in a new process, using the environment\n\
3316search path to find the file.\n\
3317\n\
3318 mode: mode of process creation\n\
3319 file: executable file name\n\
3320 args: tuple or list of arguments\n\
3321 env: dictionary of strings mapping to strings");
3322
3323static PyObject *
3324posix_spawnvpe(PyObject *self, PyObject *args)
3325{
3326 char *path;
3327 PyObject *argv, *env;
3328 char **argvlist;
3329 char **envlist;
3330 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3331 int mode, i, pos, argc, envc;
3332 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003333 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003334 int lastarg = 0;
3335
3336 /* spawnvpe has four arguments: (mode, path, argv, env), where
3337 argv is a list or tuple of strings and env is a dictionary
3338 like posix.environ. */
3339
3340 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3341 Py_FileSystemDefaultEncoding,
3342 &path, &argv, &env))
3343 return NULL;
3344 if (PyList_Check(argv)) {
3345 argc = PyList_Size(argv);
3346 getitem = PyList_GetItem;
3347 }
3348 else if (PyTuple_Check(argv)) {
3349 argc = PyTuple_Size(argv);
3350 getitem = PyTuple_GetItem;
3351 }
3352 else {
3353 PyErr_SetString(PyExc_TypeError,
3354 "spawnvpe() arg 2 must be a tuple or list");
3355 goto fail_0;
3356 }
3357 if (!PyMapping_Check(env)) {
3358 PyErr_SetString(PyExc_TypeError,
3359 "spawnvpe() arg 3 must be a mapping object");
3360 goto fail_0;
3361 }
3362
3363 argvlist = PyMem_NEW(char *, argc+1);
3364 if (argvlist == NULL) {
3365 PyErr_NoMemory();
3366 goto fail_0;
3367 }
3368 for (i = 0; i < argc; i++) {
3369 if (!PyArg_Parse((*getitem)(argv, i),
3370 "et;spawnvpe() arg 2 must contain only strings",
3371 Py_FileSystemDefaultEncoding,
3372 &argvlist[i]))
3373 {
3374 lastarg = i;
3375 goto fail_1;
3376 }
3377 }
3378 lastarg = argc;
3379 argvlist[argc] = NULL;
3380
3381 i = PyMapping_Size(env);
3382 if (i < 0)
3383 goto fail_1;
3384 envlist = PyMem_NEW(char *, i + 1);
3385 if (envlist == NULL) {
3386 PyErr_NoMemory();
3387 goto fail_1;
3388 }
3389 envc = 0;
3390 keys = PyMapping_Keys(env);
3391 vals = PyMapping_Values(env);
3392 if (!keys || !vals)
3393 goto fail_2;
3394 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3395 PyErr_SetString(PyExc_TypeError,
3396 "spawnvpe(): env.keys() or env.values() is not a list");
3397 goto fail_2;
3398 }
3399
3400 for (pos = 0; pos < i; pos++) {
3401 char *p, *k, *v;
3402 size_t len;
3403
3404 key = PyList_GetItem(keys, pos);
3405 val = PyList_GetItem(vals, pos);
3406 if (!key || !val)
3407 goto fail_2;
3408
3409 if (!PyArg_Parse(
3410 key,
3411 "s;spawnvpe() arg 3 contains a non-string key",
3412 &k) ||
3413 !PyArg_Parse(
3414 val,
3415 "s;spawnvpe() arg 3 contains a non-string value",
3416 &v))
3417 {
3418 goto fail_2;
3419 }
3420 len = PyString_Size(key) + PyString_Size(val) + 2;
3421 p = PyMem_NEW(char, len);
3422 if (p == NULL) {
3423 PyErr_NoMemory();
3424 goto fail_2;
3425 }
3426 PyOS_snprintf(p, len, "%s=%s", k, v);
3427 envlist[envc++] = p;
3428 }
3429 envlist[envc] = 0;
3430
3431 Py_BEGIN_ALLOW_THREADS
3432#if defined(PYCC_GCC)
3433 spawnval = spawnve(mode, path, argvlist, envlist);
3434#else
3435 spawnval = _spawnve(mode, path, argvlist, envlist);
3436#endif
3437 Py_END_ALLOW_THREADS
3438
3439 if (spawnval == -1)
3440 (void) posix_error();
3441 else
3442 res = Py_BuildValue("l", (long) spawnval);
3443
3444 fail_2:
3445 while (--envc >= 0)
3446 PyMem_DEL(envlist[envc]);
3447 PyMem_DEL(envlist);
3448 fail_1:
3449 free_string_array(argvlist, lastarg);
3450 Py_XDECREF(vals);
3451 Py_XDECREF(keys);
3452 fail_0:
3453 PyMem_Free(path);
3454 return res;
3455}
3456#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003457#endif /* HAVE_SPAWNV */
3458
3459
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003460#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003461PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003462"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003463Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3464\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003465Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003466
3467static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003468posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003469{
Neal Norwitze241ce82003-02-17 18:17:05 +00003470 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003471 if (pid == -1)
3472 return posix_error();
3473 PyOS_AfterFork();
3474 return PyInt_FromLong((long)pid);
3475}
3476#endif
3477
3478
Guido van Rossumad0ee831995-03-01 10:34:45 +00003479#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003480PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003481"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003482Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003483Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003484
Barry Warsaw53699e91996-12-10 23:23:01 +00003485static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003486posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003487{
Neal Norwitze241ce82003-02-17 18:17:05 +00003488 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003489 if (pid == -1)
3490 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003491 if (pid == 0)
3492 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003493 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003494}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003495#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003496
Neal Norwitzb59798b2003-03-21 01:43:31 +00003497/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003498/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3499#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003500#define DEV_PTY_FILE "/dev/ptc"
3501#define HAVE_DEV_PTMX
3502#else
3503#define DEV_PTY_FILE "/dev/ptmx"
3504#endif
3505
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003506#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003507#ifdef HAVE_PTY_H
3508#include <pty.h>
3509#else
3510#ifdef HAVE_LIBUTIL_H
3511#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003512#endif /* HAVE_LIBUTIL_H */
3513#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003514#ifdef HAVE_STROPTS_H
3515#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003516#endif
3517#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003518
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003519#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003520PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003521"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003522Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003523
3524static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003525posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003526{
3527 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003528#ifndef HAVE_OPENPTY
3529 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003530#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003531#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003532 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003533#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003534 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003535#endif
3536#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003537
Thomas Wouters70c21a12000-07-14 14:28:33 +00003538#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003539 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3540 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003541#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003542 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3543 if (slave_name == NULL)
3544 return posix_error();
3545
3546 slave_fd = open(slave_name, O_RDWR);
3547 if (slave_fd < 0)
3548 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003549#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003550 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003551 if (master_fd < 0)
3552 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003553 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003554 /* change permission of slave */
3555 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003556 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003557 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003558 }
3559 /* unlock slave */
3560 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003561 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003562 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003563 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003564 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003565 slave_name = ptsname(master_fd); /* get name of slave */
3566 if (slave_name == NULL)
3567 return posix_error();
3568 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3569 if (slave_fd < 0)
3570 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003571#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003572 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3573 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003574#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003575 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003576#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003577#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003578#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003579
Fred Drake8cef4cf2000-06-28 16:40:38 +00003580 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003581
Fred Drake8cef4cf2000-06-28 16:40:38 +00003582}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003583#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003584
3585#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003586PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003587"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003588Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3589Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003590To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003591
3592static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003593posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003594{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003595 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003596
Fred Drake8cef4cf2000-06-28 16:40:38 +00003597 pid = forkpty(&master_fd, NULL, NULL, NULL);
3598 if (pid == -1)
3599 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003600 if (pid == 0)
3601 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003602 return Py_BuildValue("(ii)", pid, master_fd);
3603}
3604#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003605
Guido van Rossumad0ee831995-03-01 10:34:45 +00003606#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003607PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003608"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003609Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003610
Barry Warsaw53699e91996-12-10 23:23:01 +00003611static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003612posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003613{
Barry Warsaw53699e91996-12-10 23:23:01 +00003614 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003615}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003616#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003617
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003618
Guido van Rossumad0ee831995-03-01 10:34:45 +00003619#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003620PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003621"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003622Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003623
Barry Warsaw53699e91996-12-10 23:23:01 +00003624static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003625posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003626{
Barry Warsaw53699e91996-12-10 23:23:01 +00003627 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003628}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003629#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003630
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003631
Guido van Rossumad0ee831995-03-01 10:34:45 +00003632#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003633PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003634"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003635Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003636
Barry Warsaw53699e91996-12-10 23:23:01 +00003637static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003638posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003639{
Barry Warsaw53699e91996-12-10 23:23:01 +00003640 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003641}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003642#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003643
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003644
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003645PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003646"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003647Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003648
Barry Warsaw53699e91996-12-10 23:23:01 +00003649static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003650posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003651{
Barry Warsaw53699e91996-12-10 23:23:01 +00003652 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003653}
3654
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003655
Fred Drakec9680921999-12-13 16:37:25 +00003656#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003657PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003658"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003659Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003660
3661static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003662posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003663{
3664 PyObject *result = NULL;
3665
Fred Drakec9680921999-12-13 16:37:25 +00003666#ifdef NGROUPS_MAX
3667#define MAX_GROUPS NGROUPS_MAX
3668#else
3669 /* defined to be 16 on Solaris7, so this should be a small number */
3670#define MAX_GROUPS 64
3671#endif
3672 gid_t grouplist[MAX_GROUPS];
3673 int n;
3674
3675 n = getgroups(MAX_GROUPS, grouplist);
3676 if (n < 0)
3677 posix_error();
3678 else {
3679 result = PyList_New(n);
3680 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003681 int i;
3682 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003683 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003684 if (o == NULL) {
3685 Py_DECREF(result);
3686 result = NULL;
3687 break;
3688 }
3689 PyList_SET_ITEM(result, i, o);
3690 }
3691 }
3692 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003693
Fred Drakec9680921999-12-13 16:37:25 +00003694 return result;
3695}
3696#endif
3697
Martin v. Löwis606edc12002-06-13 21:09:11 +00003698#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003699PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003700"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003701Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003702
3703static PyObject *
3704posix_getpgid(PyObject *self, PyObject *args)
3705{
3706 int pid, pgid;
3707 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3708 return NULL;
3709 pgid = getpgid(pid);
3710 if (pgid < 0)
3711 return posix_error();
3712 return PyInt_FromLong((long)pgid);
3713}
3714#endif /* HAVE_GETPGID */
3715
3716
Guido van Rossumb6775db1994-08-01 11:34:53 +00003717#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003718PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003719"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003720Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003721
Barry Warsaw53699e91996-12-10 23:23:01 +00003722static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003723posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003724{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003725#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003726 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003727#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003728 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003729#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003730}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003731#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003732
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003733
Guido van Rossumb6775db1994-08-01 11:34:53 +00003734#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003735PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003736"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003737Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003738
Barry Warsaw53699e91996-12-10 23:23:01 +00003739static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003740posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003741{
Guido van Rossum64933891994-10-20 21:56:42 +00003742#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003743 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003744#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003745 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003746#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003747 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003748 Py_INCREF(Py_None);
3749 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003750}
3751
Guido van Rossumb6775db1994-08-01 11:34:53 +00003752#endif /* HAVE_SETPGRP */
3753
Guido van Rossumad0ee831995-03-01 10:34:45 +00003754#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003755PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003756"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003757Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003758
Barry Warsaw53699e91996-12-10 23:23:01 +00003759static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003760posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003761{
Barry Warsaw53699e91996-12-10 23:23:01 +00003762 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003763}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003764#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003765
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003766
Fred Drake12c6e2d1999-12-14 21:25:03 +00003767#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003768PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003769"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003770Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003771
3772static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003773posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003774{
Neal Norwitze241ce82003-02-17 18:17:05 +00003775 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003776 char *name;
3777 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003778
Fred Drakea30680b2000-12-06 21:24:28 +00003779 errno = 0;
3780 name = getlogin();
3781 if (name == NULL) {
3782 if (errno)
3783 posix_error();
3784 else
3785 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003786 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003787 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003788 else
3789 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003790 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003791
Fred Drake12c6e2d1999-12-14 21:25:03 +00003792 return result;
3793}
3794#endif
3795
Guido van Rossumad0ee831995-03-01 10:34:45 +00003796#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003797PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003798"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003799Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003800
Barry Warsaw53699e91996-12-10 23:23:01 +00003801static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003802posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003803{
Barry Warsaw53699e91996-12-10 23:23:01 +00003804 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003805}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003806#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003807
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003808
Guido van Rossumad0ee831995-03-01 10:34:45 +00003809#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003810PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003811"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003812Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003813
Barry Warsaw53699e91996-12-10 23:23:01 +00003814static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003815posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003816{
3817 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003818 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003819 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003820#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003821 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3822 APIRET rc;
3823 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003824 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003825
3826 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3827 APIRET rc;
3828 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003829 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003830
3831 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003832 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003833#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003834 if (kill(pid, sig) == -1)
3835 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003836#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003837 Py_INCREF(Py_None);
3838 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003839}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003840#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003841
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003842#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003843PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003844"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003845Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003846
3847static PyObject *
3848posix_killpg(PyObject *self, PyObject *args)
3849{
3850 int pgid, sig;
3851 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3852 return NULL;
3853 if (killpg(pgid, sig) == -1)
3854 return posix_error();
3855 Py_INCREF(Py_None);
3856 return Py_None;
3857}
3858#endif
3859
Guido van Rossumc0125471996-06-28 18:55:32 +00003860#ifdef HAVE_PLOCK
3861
3862#ifdef HAVE_SYS_LOCK_H
3863#include <sys/lock.h>
3864#endif
3865
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003866PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003867"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003868Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003869
Barry Warsaw53699e91996-12-10 23:23:01 +00003870static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003871posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003872{
3873 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003874 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003875 return NULL;
3876 if (plock(op) == -1)
3877 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003878 Py_INCREF(Py_None);
3879 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003880}
3881#endif
3882
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003883
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003884#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003885PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003886"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003887Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003888
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003889#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003890#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003891static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003892async_system(const char *command)
3893{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003894 char errormsg[256], args[1024];
3895 RESULTCODES rcodes;
3896 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003897
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003898 char *shell = getenv("COMSPEC");
3899 if (!shell)
3900 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003901
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003902 /* avoid overflowing the argument buffer */
3903 if (strlen(shell) + 3 + strlen(command) >= 1024)
3904 return ERROR_NOT_ENOUGH_MEMORY
3905
3906 args[0] = '\0';
3907 strcat(args, shell);
3908 strcat(args, "/c ");
3909 strcat(args, command);
3910
3911 /* execute asynchronously, inheriting the environment */
3912 rc = DosExecPgm(errormsg,
3913 sizeof(errormsg),
3914 EXEC_ASYNC,
3915 args,
3916 NULL,
3917 &rcodes,
3918 shell);
3919 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003920}
3921
Guido van Rossumd48f2521997-12-05 22:19:34 +00003922static FILE *
3923popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003924{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003925 int oldfd, tgtfd;
3926 HFILE pipeh[2];
3927 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003928
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003929 /* mode determines which of stdin or stdout is reconnected to
3930 * the pipe to the child
3931 */
3932 if (strchr(mode, 'r') != NULL) {
3933 tgt_fd = 1; /* stdout */
3934 } else if (strchr(mode, 'w')) {
3935 tgt_fd = 0; /* stdin */
3936 } else {
3937 *err = ERROR_INVALID_ACCESS;
3938 return NULL;
3939 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003940
Andrew MacIntyrea3be2582004-12-18 09:51:05 +00003941 /* setup the pipe */
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003942 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
3943 *err = rc;
3944 return NULL;
3945 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003946
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003947 /* prevent other threads accessing stdio */
3948 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003949
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003950 /* reconnect stdio and execute child */
3951 oldfd = dup(tgtfd);
3952 close(tgtfd);
3953 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
3954 DosClose(pipeh[tgtfd]);
3955 rc = async_system(command);
3956 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003957
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003958 /* restore stdio */
3959 dup2(oldfd, tgtfd);
3960 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003961
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003962 /* allow other threads access to stdio */
3963 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003964
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003965 /* if execution of child was successful return file stream */
3966 if (rc == NO_ERROR)
3967 return fdopen(pipeh[1 - tgtfd], mode);
3968 else {
3969 DosClose(pipeh[1 - tgtfd]);
3970 *err = rc;
3971 return NULL;
3972 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003973}
3974
3975static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003976posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003977{
3978 char *name;
3979 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003980 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003981 FILE *fp;
3982 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003983 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003984 return NULL;
3985 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003986 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003987 Py_END_ALLOW_THREADS
3988 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003989 return os2_error(err);
3990
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003991 f = PyFile_FromFile(fp, name, mode, fclose);
3992 if (f != NULL)
3993 PyFile_SetBufSize(f, bufsize);
3994 return f;
3995}
3996
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003997#elif defined(PYCC_GCC)
3998
3999/* standard posix version of popen() support */
4000static PyObject *
4001posix_popen(PyObject *self, PyObject *args)
4002{
4003 char *name;
4004 char *mode = "r";
4005 int bufsize = -1;
4006 FILE *fp;
4007 PyObject *f;
4008 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4009 return NULL;
4010 Py_BEGIN_ALLOW_THREADS
4011 fp = popen(name, mode);
4012 Py_END_ALLOW_THREADS
4013 if (fp == NULL)
4014 return posix_error();
4015 f = PyFile_FromFile(fp, name, mode, pclose);
4016 if (f != NULL)
4017 PyFile_SetBufSize(f, bufsize);
4018 return f;
4019}
4020
4021/* fork() under OS/2 has lots'o'warts
4022 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4023 * most of this code is a ripoff of the win32 code, but using the
4024 * capabilities of EMX's C library routines
4025 */
4026
4027/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4028#define POPEN_1 1
4029#define POPEN_2 2
4030#define POPEN_3 3
4031#define POPEN_4 4
4032
4033static PyObject *_PyPopen(char *, int, int, int);
4034static int _PyPclose(FILE *file);
4035
4036/*
4037 * Internal dictionary mapping popen* file pointers to process handles,
4038 * for use when retrieving the process exit code. See _PyPclose() below
4039 * for more information on this dictionary's use.
4040 */
4041static PyObject *_PyPopenProcs = NULL;
4042
4043/* os2emx version of popen2()
4044 *
4045 * The result of this function is a pipe (file) connected to the
4046 * process's stdin, and a pipe connected to the process's stdout.
4047 */
4048
4049static PyObject *
4050os2emx_popen2(PyObject *self, PyObject *args)
4051{
4052 PyObject *f;
4053 int tm=0;
4054
4055 char *cmdstring;
4056 char *mode = "t";
4057 int bufsize = -1;
4058 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4059 return NULL;
4060
4061 if (*mode == 't')
4062 tm = O_TEXT;
4063 else if (*mode != 'b') {
4064 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4065 return NULL;
4066 } else
4067 tm = O_BINARY;
4068
4069 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
4070
4071 return f;
4072}
4073
4074/*
4075 * Variation on os2emx.popen2
4076 *
4077 * The result of this function is 3 pipes - the process's stdin,
4078 * stdout and stderr
4079 */
4080
4081static PyObject *
4082os2emx_popen3(PyObject *self, PyObject *args)
4083{
4084 PyObject *f;
4085 int tm = 0;
4086
4087 char *cmdstring;
4088 char *mode = "t";
4089 int bufsize = -1;
4090 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4091 return NULL;
4092
4093 if (*mode == 't')
4094 tm = O_TEXT;
4095 else if (*mode != 'b') {
4096 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4097 return NULL;
4098 } else
4099 tm = O_BINARY;
4100
4101 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
4102
4103 return f;
4104}
4105
4106/*
4107 * Variation on os2emx.popen2
4108 *
Tim Peters11b23062003-04-23 02:39:17 +00004109 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004110 * and stdout+stderr combined as a single pipe.
4111 */
4112
4113static PyObject *
4114os2emx_popen4(PyObject *self, PyObject *args)
4115{
4116 PyObject *f;
4117 int tm = 0;
4118
4119 char *cmdstring;
4120 char *mode = "t";
4121 int bufsize = -1;
4122 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4123 return NULL;
4124
4125 if (*mode == 't')
4126 tm = O_TEXT;
4127 else if (*mode != 'b') {
4128 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4129 return NULL;
4130 } else
4131 tm = O_BINARY;
4132
4133 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
4134
4135 return f;
4136}
4137
4138/* a couple of structures for convenient handling of multiple
4139 * file handles and pipes
4140 */
4141struct file_ref
4142{
4143 int handle;
4144 int flags;
4145};
4146
4147struct pipe_ref
4148{
4149 int rd;
4150 int wr;
4151};
4152
4153/* The following code is derived from the win32 code */
4154
4155static PyObject *
4156_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4157{
4158 struct file_ref stdio[3];
4159 struct pipe_ref p_fd[3];
4160 FILE *p_s[3];
4161 int file_count, i, pipe_err, pipe_pid;
4162 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4163 PyObject *f, *p_f[3];
4164
4165 /* file modes for subsequent fdopen's on pipe handles */
4166 if (mode == O_TEXT)
4167 {
4168 rd_mode = "rt";
4169 wr_mode = "wt";
4170 }
4171 else
4172 {
4173 rd_mode = "rb";
4174 wr_mode = "wb";
4175 }
4176
4177 /* prepare shell references */
4178 if ((shell = getenv("EMXSHELL")) == NULL)
4179 if ((shell = getenv("COMSPEC")) == NULL)
4180 {
4181 errno = ENOENT;
4182 return posix_error();
4183 }
4184
4185 sh_name = _getname(shell);
4186 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4187 opt = "/c";
4188 else
4189 opt = "-c";
4190
4191 /* save current stdio fds + their flags, and set not inheritable */
4192 i = pipe_err = 0;
4193 while (pipe_err >= 0 && i < 3)
4194 {
4195 pipe_err = stdio[i].handle = dup(i);
4196 stdio[i].flags = fcntl(i, F_GETFD, 0);
4197 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4198 i++;
4199 }
4200 if (pipe_err < 0)
4201 {
4202 /* didn't get them all saved - clean up and bail out */
4203 int saved_err = errno;
4204 while (i-- > 0)
4205 {
4206 close(stdio[i].handle);
4207 }
4208 errno = saved_err;
4209 return posix_error();
4210 }
4211
4212 /* create pipe ends */
4213 file_count = 2;
4214 if (n == POPEN_3)
4215 file_count = 3;
4216 i = pipe_err = 0;
4217 while ((pipe_err == 0) && (i < file_count))
4218 pipe_err = pipe((int *)&p_fd[i++]);
4219 if (pipe_err < 0)
4220 {
4221 /* didn't get them all made - clean up and bail out */
4222 while (i-- > 0)
4223 {
4224 close(p_fd[i].wr);
4225 close(p_fd[i].rd);
4226 }
4227 errno = EPIPE;
4228 return posix_error();
4229 }
4230
4231 /* change the actual standard IO streams over temporarily,
4232 * making the retained pipe ends non-inheritable
4233 */
4234 pipe_err = 0;
4235
4236 /* - stdin */
4237 if (dup2(p_fd[0].rd, 0) == 0)
4238 {
4239 close(p_fd[0].rd);
4240 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4241 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4242 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4243 {
4244 close(p_fd[0].wr);
4245 pipe_err = -1;
4246 }
4247 }
4248 else
4249 {
4250 pipe_err = -1;
4251 }
4252
4253 /* - stdout */
4254 if (pipe_err == 0)
4255 {
4256 if (dup2(p_fd[1].wr, 1) == 1)
4257 {
4258 close(p_fd[1].wr);
4259 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4260 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4261 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4262 {
4263 close(p_fd[1].rd);
4264 pipe_err = -1;
4265 }
4266 }
4267 else
4268 {
4269 pipe_err = -1;
4270 }
4271 }
4272
4273 /* - stderr, as required */
4274 if (pipe_err == 0)
4275 switch (n)
4276 {
4277 case POPEN_3:
4278 {
4279 if (dup2(p_fd[2].wr, 2) == 2)
4280 {
4281 close(p_fd[2].wr);
4282 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4283 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4284 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4285 {
4286 close(p_fd[2].rd);
4287 pipe_err = -1;
4288 }
4289 }
4290 else
4291 {
4292 pipe_err = -1;
4293 }
4294 break;
4295 }
4296
4297 case POPEN_4:
4298 {
4299 if (dup2(1, 2) != 2)
4300 {
4301 pipe_err = -1;
4302 }
4303 break;
4304 }
4305 }
4306
4307 /* spawn the child process */
4308 if (pipe_err == 0)
4309 {
4310 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4311 if (pipe_pid == -1)
4312 {
4313 pipe_err = -1;
4314 }
4315 else
4316 {
4317 /* save the PID into the FILE structure
4318 * NOTE: this implementation doesn't actually
4319 * take advantage of this, but do it for
4320 * completeness - AIM Apr01
4321 */
4322 for (i = 0; i < file_count; i++)
4323 p_s[i]->_pid = pipe_pid;
4324 }
4325 }
4326
4327 /* reset standard IO to normal */
4328 for (i = 0; i < 3; i++)
4329 {
4330 dup2(stdio[i].handle, i);
4331 fcntl(i, F_SETFD, stdio[i].flags);
4332 close(stdio[i].handle);
4333 }
4334
4335 /* if any remnant problems, clean up and bail out */
4336 if (pipe_err < 0)
4337 {
4338 for (i = 0; i < 3; i++)
4339 {
4340 close(p_fd[i].rd);
4341 close(p_fd[i].wr);
4342 }
4343 errno = EPIPE;
4344 return posix_error_with_filename(cmdstring);
4345 }
4346
4347 /* build tuple of file objects to return */
4348 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4349 PyFile_SetBufSize(p_f[0], bufsize);
4350 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4351 PyFile_SetBufSize(p_f[1], bufsize);
4352 if (n == POPEN_3)
4353 {
4354 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4355 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004356 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004357 }
4358 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004359 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004360
4361 /*
4362 * Insert the files we've created into the process dictionary
4363 * all referencing the list with the process handle and the
4364 * initial number of files (see description below in _PyPclose).
4365 * Since if _PyPclose later tried to wait on a process when all
4366 * handles weren't closed, it could create a deadlock with the
4367 * child, we spend some energy here to try to ensure that we
4368 * either insert all file handles into the dictionary or none
4369 * at all. It's a little clumsy with the various popen modes
4370 * and variable number of files involved.
4371 */
4372 if (!_PyPopenProcs)
4373 {
4374 _PyPopenProcs = PyDict_New();
4375 }
4376
4377 if (_PyPopenProcs)
4378 {
4379 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4380 int ins_rc[3];
4381
4382 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4383 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4384
4385 procObj = PyList_New(2);
4386 pidObj = PyInt_FromLong((long) pipe_pid);
4387 intObj = PyInt_FromLong((long) file_count);
4388
4389 if (procObj && pidObj && intObj)
4390 {
4391 PyList_SetItem(procObj, 0, pidObj);
4392 PyList_SetItem(procObj, 1, intObj);
4393
4394 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4395 if (fileObj[0])
4396 {
4397 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4398 fileObj[0],
4399 procObj);
4400 }
4401 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4402 if (fileObj[1])
4403 {
4404 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4405 fileObj[1],
4406 procObj);
4407 }
4408 if (file_count >= 3)
4409 {
4410 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4411 if (fileObj[2])
4412 {
4413 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4414 fileObj[2],
4415 procObj);
4416 }
4417 }
4418
4419 if (ins_rc[0] < 0 || !fileObj[0] ||
4420 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4421 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4422 {
4423 /* Something failed - remove any dictionary
4424 * entries that did make it.
4425 */
4426 if (!ins_rc[0] && fileObj[0])
4427 {
4428 PyDict_DelItem(_PyPopenProcs,
4429 fileObj[0]);
4430 }
4431 if (!ins_rc[1] && fileObj[1])
4432 {
4433 PyDict_DelItem(_PyPopenProcs,
4434 fileObj[1]);
4435 }
4436 if (!ins_rc[2] && fileObj[2])
4437 {
4438 PyDict_DelItem(_PyPopenProcs,
4439 fileObj[2]);
4440 }
4441 }
4442 }
Tim Peters11b23062003-04-23 02:39:17 +00004443
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004444 /*
4445 * Clean up our localized references for the dictionary keys
4446 * and value since PyDict_SetItem will Py_INCREF any copies
4447 * that got placed in the dictionary.
4448 */
4449 Py_XDECREF(procObj);
4450 Py_XDECREF(fileObj[0]);
4451 Py_XDECREF(fileObj[1]);
4452 Py_XDECREF(fileObj[2]);
4453 }
4454
4455 /* Child is launched. */
4456 return f;
4457}
4458
4459/*
4460 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4461 * exit code for the child process and return as a result of the close.
4462 *
4463 * This function uses the _PyPopenProcs dictionary in order to map the
4464 * input file pointer to information about the process that was
4465 * originally created by the popen* call that created the file pointer.
4466 * The dictionary uses the file pointer as a key (with one entry
4467 * inserted for each file returned by the original popen* call) and a
4468 * single list object as the value for all files from a single call.
4469 * The list object contains the Win32 process handle at [0], and a file
4470 * count at [1], which is initialized to the total number of file
4471 * handles using that list.
4472 *
4473 * This function closes whichever handle it is passed, and decrements
4474 * the file count in the dictionary for the process handle pointed to
4475 * by this file. On the last close (when the file count reaches zero),
4476 * this function will wait for the child process and then return its
4477 * exit code as the result of the close() operation. This permits the
4478 * files to be closed in any order - it is always the close() of the
4479 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004480 *
4481 * NOTE: This function is currently called with the GIL released.
4482 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004483 */
4484
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004485static int _PyPclose(FILE *file)
4486{
4487 int result;
4488 int exit_code;
4489 int pipe_pid;
4490 PyObject *procObj, *pidObj, *intObj, *fileObj;
4491 int file_count;
4492#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004493 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004494#endif
4495
4496 /* Close the file handle first, to ensure it can't block the
4497 * child from exiting if it's the last handle.
4498 */
4499 result = fclose(file);
4500
4501#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004502 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004503#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004504 if (_PyPopenProcs)
4505 {
4506 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4507 (procObj = PyDict_GetItem(_PyPopenProcs,
4508 fileObj)) != NULL &&
4509 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4510 (intObj = PyList_GetItem(procObj,1)) != NULL)
4511 {
4512 pipe_pid = (int) PyInt_AsLong(pidObj);
4513 file_count = (int) PyInt_AsLong(intObj);
4514
4515 if (file_count > 1)
4516 {
4517 /* Still other files referencing process */
4518 file_count--;
4519 PyList_SetItem(procObj,1,
4520 PyInt_FromLong((long) file_count));
4521 }
4522 else
4523 {
4524 /* Last file for this process */
4525 if (result != EOF &&
4526 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4527 {
4528 /* extract exit status */
4529 if (WIFEXITED(exit_code))
4530 {
4531 result = WEXITSTATUS(exit_code);
4532 }
4533 else
4534 {
4535 errno = EPIPE;
4536 result = -1;
4537 }
4538 }
4539 else
4540 {
4541 /* Indicate failure - this will cause the file object
4542 * to raise an I/O error and translate the last
4543 * error code from errno. We do have a problem with
4544 * last errors that overlap the normal errno table,
4545 * but that's a consistent problem with the file object.
4546 */
4547 result = -1;
4548 }
4549 }
4550
4551 /* Remove this file pointer from dictionary */
4552 PyDict_DelItem(_PyPopenProcs, fileObj);
4553
4554 if (PyDict_Size(_PyPopenProcs) == 0)
4555 {
4556 Py_DECREF(_PyPopenProcs);
4557 _PyPopenProcs = NULL;
4558 }
4559
4560 } /* if object retrieval ok */
4561
4562 Py_XDECREF(fileObj);
4563 } /* if _PyPopenProcs */
4564
4565#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004566 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004567#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004568 return result;
4569}
4570
4571#endif /* PYCC_??? */
4572
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004573#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004574
4575/*
4576 * Portable 'popen' replacement for Win32.
4577 *
4578 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4579 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004580 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004581 */
4582
4583#include <malloc.h>
4584#include <io.h>
4585#include <fcntl.h>
4586
4587/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4588#define POPEN_1 1
4589#define POPEN_2 2
4590#define POPEN_3 3
4591#define POPEN_4 4
4592
4593static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004594static int _PyPclose(FILE *file);
4595
4596/*
4597 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004598 * for use when retrieving the process exit code. See _PyPclose() below
4599 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004600 */
4601static PyObject *_PyPopenProcs = NULL;
4602
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004603
4604/* popen that works from a GUI.
4605 *
4606 * The result of this function is a pipe (file) connected to the
4607 * processes stdin or stdout, depending on the requested mode.
4608 */
4609
4610static PyObject *
4611posix_popen(PyObject *self, PyObject *args)
4612{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00004613 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004614 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004615
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004616 char *cmdstring;
4617 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004618 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004619 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004620 return NULL;
4621
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004622 if (*mode == 'r')
4623 tm = _O_RDONLY;
4624 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00004625 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004626 return NULL;
4627 } else
4628 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004629
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004630 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004631 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004632 return NULL;
4633 }
4634
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004635 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004636 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004637 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004638 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004639 else
4640 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4641
4642 return f;
4643}
4644
4645/* Variation on win32pipe.popen
4646 *
4647 * The result of this function is a pipe (file) connected to the
4648 * process's stdin, and a pipe connected to the process's stdout.
4649 */
4650
4651static PyObject *
4652win32_popen2(PyObject *self, PyObject *args)
4653{
4654 PyObject *f;
4655 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004656
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004657 char *cmdstring;
4658 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004659 int bufsize = -1;
4660 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004661 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004662
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004663 if (*mode == 't')
4664 tm = _O_TEXT;
4665 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004666 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004667 return NULL;
4668 } else
4669 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004670
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004671 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004672 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004673 return NULL;
4674 }
4675
4676 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004677
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004678 return f;
4679}
4680
4681/*
4682 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004683 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004684 * The result of this function is 3 pipes - the process's stdin,
4685 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004686 */
4687
4688static PyObject *
4689win32_popen3(PyObject *self, PyObject *args)
4690{
4691 PyObject *f;
4692 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004693
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004694 char *cmdstring;
4695 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004696 int bufsize = -1;
4697 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004698 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004699
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004700 if (*mode == 't')
4701 tm = _O_TEXT;
4702 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004703 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004704 return NULL;
4705 } else
4706 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004707
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004708 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004709 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004710 return NULL;
4711 }
4712
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004713 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004714
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004715 return f;
4716}
4717
4718/*
4719 * Variation on win32pipe.popen
4720 *
Tim Peters5aa91602002-01-30 05:46:57 +00004721 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004722 * and stdout+stderr combined as a single pipe.
4723 */
4724
4725static PyObject *
4726win32_popen4(PyObject *self, PyObject *args)
4727{
4728 PyObject *f;
4729 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004730
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004731 char *cmdstring;
4732 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004733 int bufsize = -1;
4734 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004735 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004736
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004737 if (*mode == 't')
4738 tm = _O_TEXT;
4739 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004740 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004741 return NULL;
4742 } else
4743 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004744
4745 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004746 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004747 return NULL;
4748 }
4749
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004750 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004751
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004752 return f;
4753}
4754
Mark Hammond08501372001-01-31 07:30:29 +00004755static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004756_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004757 HANDLE hStdin,
4758 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004759 HANDLE hStderr,
4760 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004761{
4762 PROCESS_INFORMATION piProcInfo;
4763 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004764 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004765 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004766 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004767 int i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004768 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004769
4770 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004771 char *comshell;
4772
Tim Peters92e4dd82002-10-05 01:47:34 +00004773 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004774 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
Martin v. Löwis18e16552006-02-15 17:27:45 +00004775 /* x < i, so x fits into an integer */
4776 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00004777
4778 /* Explicitly check if we are using COMMAND.COM. If we are
4779 * then use the w9xpopen hack.
4780 */
4781 comshell = s1 + x;
4782 while (comshell >= s1 && *comshell != '\\')
4783 --comshell;
4784 ++comshell;
4785
4786 if (GetVersion() < 0x80000000 &&
4787 _stricmp(comshell, "command.com") != 0) {
4788 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004789 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004790 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004791 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004792 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004793 }
4794 else {
4795 /*
Tim Peters402d5982001-08-27 06:37:48 +00004796 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4797 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004798 */
Mark Hammond08501372001-01-31 07:30:29 +00004799 char modulepath[_MAX_PATH];
4800 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004801 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
Thomas Wouters477c8d52006-05-27 19:21:47 +00004802 for (x = i = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004803 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004804 x = i+1;
4805 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004806 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004807 strncat(modulepath,
4808 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004809 (sizeof(modulepath)/sizeof(modulepath[0]))
4810 -strlen(modulepath));
4811 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004812 /* Eeek - file-not-found - possibly an embedding
4813 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004814 */
Tim Peters5aa91602002-01-30 05:46:57 +00004815 strncpy(modulepath,
4816 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00004817 sizeof(modulepath)/sizeof(modulepath[0]));
4818 if (modulepath[strlen(modulepath)-1] != '\\')
4819 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004820 strncat(modulepath,
4821 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004822 (sizeof(modulepath)/sizeof(modulepath[0]))
4823 -strlen(modulepath));
4824 /* No where else to look - raise an easily identifiable
4825 error, rather than leaving Windows to report
4826 "file not found" - as the user is probably blissfully
4827 unaware this shim EXE is used, and it will confuse them.
4828 (well, it confused me for a while ;-)
4829 */
4830 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004831 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004832 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004833 "for popen to work with your shell "
4834 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004835 szConsoleSpawn);
4836 return FALSE;
4837 }
4838 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004839 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004840 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004841 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004842
Tim Peters92e4dd82002-10-05 01:47:34 +00004843 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004844 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004845 /* To maintain correct argument passing semantics,
4846 we pass the command-line as it stands, and allow
4847 quoting to be applied. w9xpopen.exe will then
4848 use its argv vector, and re-quote the necessary
4849 args for the ultimate child process.
4850 */
Tim Peters75cdad52001-11-28 22:07:30 +00004851 PyOS_snprintf(
4852 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004853 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004854 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004855 s1,
4856 s3,
4857 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004858 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004859 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004860 dialog:
4861 "Your program accessed mem currently in use at xxx"
4862 and a hopeful warning about the stability of your
4863 system.
4864 Cost is Ctrl+C wont kill children, but anyone
4865 who cares can have a go!
4866 */
4867 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004868 }
4869 }
4870
4871 /* Could be an else here to try cmd.exe / command.com in the path
4872 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004873 else {
Tim Peters402d5982001-08-27 06:37:48 +00004874 PyErr_SetString(PyExc_RuntimeError,
4875 "Cannot locate a COMSPEC environment variable to "
4876 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004877 return FALSE;
4878 }
Tim Peters5aa91602002-01-30 05:46:57 +00004879
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004880 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4881 siStartInfo.cb = sizeof(STARTUPINFO);
4882 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4883 siStartInfo.hStdInput = hStdin;
4884 siStartInfo.hStdOutput = hStdout;
4885 siStartInfo.hStdError = hStderr;
4886 siStartInfo.wShowWindow = SW_HIDE;
4887
4888 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004889 s2,
4890 NULL,
4891 NULL,
4892 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004893 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004894 NULL,
4895 NULL,
4896 &siStartInfo,
4897 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004898 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004899 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004900
Mark Hammondb37a3732000-08-14 04:47:33 +00004901 /* Return process handle */
4902 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004903 return TRUE;
4904 }
Tim Peters402d5982001-08-27 06:37:48 +00004905 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004906 return FALSE;
4907}
4908
4909/* The following code is based off of KB: Q190351 */
4910
4911static PyObject *
4912_PyPopen(char *cmdstring, int mode, int n)
4913{
4914 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4915 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004916 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004917
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004918 SECURITY_ATTRIBUTES saAttr;
4919 BOOL fSuccess;
4920 int fd1, fd2, fd3;
4921 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004922 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004923 PyObject *f;
4924
4925 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4926 saAttr.bInheritHandle = TRUE;
4927 saAttr.lpSecurityDescriptor = NULL;
4928
4929 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4930 return win32_error("CreatePipe", NULL);
4931
4932 /* Create new output read handle and the input write handle. Set
4933 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004934 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004935 * being created. */
4936 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004937 GetCurrentProcess(), &hChildStdinWrDup, 0,
4938 FALSE,
4939 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004940 if (!fSuccess)
4941 return win32_error("DuplicateHandle", NULL);
4942
4943 /* Close the inheritable version of ChildStdin
4944 that we're using. */
4945 CloseHandle(hChildStdinWr);
4946
4947 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4948 return win32_error("CreatePipe", NULL);
4949
4950 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004951 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4952 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004953 if (!fSuccess)
4954 return win32_error("DuplicateHandle", NULL);
4955
4956 /* Close the inheritable version of ChildStdout
4957 that we're using. */
4958 CloseHandle(hChildStdoutRd);
4959
4960 if (n != POPEN_4) {
4961 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4962 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004963 fSuccess = DuplicateHandle(GetCurrentProcess(),
4964 hChildStderrRd,
4965 GetCurrentProcess(),
4966 &hChildStderrRdDup, 0,
4967 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004968 if (!fSuccess)
4969 return win32_error("DuplicateHandle", NULL);
4970 /* Close the inheritable version of ChildStdErr that we're using. */
4971 CloseHandle(hChildStderrRd);
4972 }
Tim Peters5aa91602002-01-30 05:46:57 +00004973
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004974 switch (n) {
4975 case POPEN_1:
4976 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4977 case _O_WRONLY | _O_TEXT:
4978 /* Case for writing to child Stdin in text mode. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00004979 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004980 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004981 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004982 PyFile_SetBufSize(f, 0);
4983 /* We don't care about these pipes anymore, so close them. */
4984 CloseHandle(hChildStdoutRdDup);
4985 CloseHandle(hChildStderrRdDup);
4986 break;
4987
4988 case _O_RDONLY | _O_TEXT:
4989 /* Case for reading from child Stdout in text mode. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00004990 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004991 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004992 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004993 PyFile_SetBufSize(f, 0);
4994 /* We don't care about these pipes anymore, so close them. */
4995 CloseHandle(hChildStdinWrDup);
4996 CloseHandle(hChildStderrRdDup);
4997 break;
4998
4999 case _O_RDONLY | _O_BINARY:
5000 /* Case for readinig from child Stdout in binary mode. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00005001 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005002 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005003 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005004 PyFile_SetBufSize(f, 0);
5005 /* We don't care about these pipes anymore, so close them. */
5006 CloseHandle(hChildStdinWrDup);
5007 CloseHandle(hChildStderrRdDup);
5008 break;
5009
5010 case _O_WRONLY | _O_BINARY:
5011 /* Case for writing to child Stdin in binary mode. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00005012 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005013 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005014 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005015 PyFile_SetBufSize(f, 0);
5016 /* We don't care about these pipes anymore, so close them. */
5017 CloseHandle(hChildStdoutRdDup);
5018 CloseHandle(hChildStderrRdDup);
5019 break;
5020 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005021 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005022 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005023
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005024 case POPEN_2:
5025 case POPEN_4:
5026 {
5027 char *m1, *m2;
5028 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005029
Tim Peters7dca21e2002-08-19 00:42:29 +00005030 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005031 m1 = "r";
5032 m2 = "w";
5033 } else {
5034 m1 = "rb";
5035 m2 = "wb";
5036 }
5037
Thomas Wouters477c8d52006-05-27 19:21:47 +00005038 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005039 f1 = _fdopen(fd1, m2);
Thomas Wouters477c8d52006-05-27 19:21:47 +00005040 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005041 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005042 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005043 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00005044 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005045 PyFile_SetBufSize(p2, 0);
5046
5047 if (n != 4)
5048 CloseHandle(hChildStderrRdDup);
5049
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005050 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00005051 Py_XDECREF(p1);
5052 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00005053 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005054 break;
5055 }
Tim Peters5aa91602002-01-30 05:46:57 +00005056
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005057 case POPEN_3:
5058 {
5059 char *m1, *m2;
5060 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005061
Tim Peters7dca21e2002-08-19 00:42:29 +00005062 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005063 m1 = "r";
5064 m2 = "w";
5065 } else {
5066 m1 = "rb";
5067 m2 = "wb";
5068 }
5069
Thomas Wouters477c8d52006-05-27 19:21:47 +00005070 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005071 f1 = _fdopen(fd1, m2);
Thomas Wouters477c8d52006-05-27 19:21:47 +00005072 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005073 f2 = _fdopen(fd2, m1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00005074 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005075 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005076 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00005077 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5078 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005079 PyFile_SetBufSize(p1, 0);
5080 PyFile_SetBufSize(p2, 0);
5081 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005082 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00005083 Py_XDECREF(p1);
5084 Py_XDECREF(p2);
5085 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00005086 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005087 break;
5088 }
5089 }
5090
5091 if (n == POPEN_4) {
5092 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005093 hChildStdinRd,
5094 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005095 hChildStdoutWr,
5096 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005097 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005098 }
5099 else {
5100 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005101 hChildStdinRd,
5102 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005103 hChildStderrWr,
5104 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005105 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005106 }
5107
Mark Hammondb37a3732000-08-14 04:47:33 +00005108 /*
5109 * Insert the files we've created into the process dictionary
5110 * all referencing the list with the process handle and the
5111 * initial number of files (see description below in _PyPclose).
5112 * Since if _PyPclose later tried to wait on a process when all
5113 * handles weren't closed, it could create a deadlock with the
5114 * child, we spend some energy here to try to ensure that we
5115 * either insert all file handles into the dictionary or none
5116 * at all. It's a little clumsy with the various popen modes
5117 * and variable number of files involved.
5118 */
5119 if (!_PyPopenProcs) {
5120 _PyPopenProcs = PyDict_New();
5121 }
5122
5123 if (_PyPopenProcs) {
5124 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5125 int ins_rc[3];
5126
5127 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5128 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
5129
5130 procObj = PyList_New(2);
5131 hProcessObj = PyLong_FromVoidPtr(hProcess);
5132 intObj = PyInt_FromLong(file_count);
5133
5134 if (procObj && hProcessObj && intObj) {
5135 PyList_SetItem(procObj,0,hProcessObj);
5136 PyList_SetItem(procObj,1,intObj);
5137
5138 fileObj[0] = PyLong_FromVoidPtr(f1);
5139 if (fileObj[0]) {
5140 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5141 fileObj[0],
5142 procObj);
5143 }
5144 if (file_count >= 2) {
5145 fileObj[1] = PyLong_FromVoidPtr(f2);
5146 if (fileObj[1]) {
5147 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5148 fileObj[1],
5149 procObj);
5150 }
5151 }
5152 if (file_count >= 3) {
5153 fileObj[2] = PyLong_FromVoidPtr(f3);
5154 if (fileObj[2]) {
5155 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5156 fileObj[2],
5157 procObj);
5158 }
5159 }
5160
5161 if (ins_rc[0] < 0 || !fileObj[0] ||
5162 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5163 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5164 /* Something failed - remove any dictionary
5165 * entries that did make it.
5166 */
5167 if (!ins_rc[0] && fileObj[0]) {
5168 PyDict_DelItem(_PyPopenProcs,
5169 fileObj[0]);
5170 }
5171 if (!ins_rc[1] && fileObj[1]) {
5172 PyDict_DelItem(_PyPopenProcs,
5173 fileObj[1]);
5174 }
5175 if (!ins_rc[2] && fileObj[2]) {
5176 PyDict_DelItem(_PyPopenProcs,
5177 fileObj[2]);
5178 }
5179 }
5180 }
Tim Peters5aa91602002-01-30 05:46:57 +00005181
Mark Hammondb37a3732000-08-14 04:47:33 +00005182 /*
5183 * Clean up our localized references for the dictionary keys
5184 * and value since PyDict_SetItem will Py_INCREF any copies
5185 * that got placed in the dictionary.
5186 */
5187 Py_XDECREF(procObj);
5188 Py_XDECREF(fileObj[0]);
5189 Py_XDECREF(fileObj[1]);
5190 Py_XDECREF(fileObj[2]);
5191 }
5192
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005193 /* Child is launched. Close the parents copy of those pipe
5194 * handles that only the child should have open. You need to
5195 * make sure that no handles to the write end of the output pipe
5196 * are maintained in this process or else the pipe will not close
5197 * when the child process exits and the ReadFile will hang. */
5198
5199 if (!CloseHandle(hChildStdinRd))
5200 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005201
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005202 if (!CloseHandle(hChildStdoutWr))
5203 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005204
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005205 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5206 return win32_error("CloseHandle", NULL);
5207
5208 return f;
5209}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005210
5211/*
5212 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5213 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005214 *
5215 * This function uses the _PyPopenProcs dictionary in order to map the
5216 * input file pointer to information about the process that was
5217 * originally created by the popen* call that created the file pointer.
5218 * The dictionary uses the file pointer as a key (with one entry
5219 * inserted for each file returned by the original popen* call) and a
5220 * single list object as the value for all files from a single call.
5221 * The list object contains the Win32 process handle at [0], and a file
5222 * count at [1], which is initialized to the total number of file
5223 * handles using that list.
5224 *
5225 * This function closes whichever handle it is passed, and decrements
5226 * the file count in the dictionary for the process handle pointed to
5227 * by this file. On the last close (when the file count reaches zero),
5228 * this function will wait for the child process and then return its
5229 * exit code as the result of the close() operation. This permits the
5230 * files to be closed in any order - it is always the close() of the
5231 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005232 *
5233 * NOTE: This function is currently called with the GIL released.
5234 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005235 */
Tim Peters736aa322000-09-01 06:51:24 +00005236
Fredrik Lundh56055a42000-07-23 19:47:12 +00005237static int _PyPclose(FILE *file)
5238{
Fredrik Lundh20318932000-07-26 17:29:12 +00005239 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005240 DWORD exit_code;
5241 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00005242 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5243 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005244#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005245 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005246#endif
5247
Fredrik Lundh20318932000-07-26 17:29:12 +00005248 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00005249 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00005250 */
5251 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005252#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005253 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005254#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005255 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005256 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5257 (procObj = PyDict_GetItem(_PyPopenProcs,
5258 fileObj)) != NULL &&
5259 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5260 (intObj = PyList_GetItem(procObj,1)) != NULL) {
5261
5262 hProcess = PyLong_AsVoidPtr(hProcessObj);
5263 file_count = PyInt_AsLong(intObj);
5264
5265 if (file_count > 1) {
5266 /* Still other files referencing process */
5267 file_count--;
5268 PyList_SetItem(procObj,1,
5269 PyInt_FromLong(file_count));
5270 } else {
5271 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00005272 if (result != EOF &&
5273 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5274 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00005275 /* Possible truncation here in 16-bit environments, but
5276 * real exit codes are just the lower byte in any event.
5277 */
5278 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005279 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00005280 /* Indicate failure - this will cause the file object
5281 * to raise an I/O error and translate the last Win32
5282 * error code from errno. We do have a problem with
5283 * last errors that overlap the normal errno table,
5284 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005285 */
Fredrik Lundh20318932000-07-26 17:29:12 +00005286 if (result != EOF) {
5287 /* If the error wasn't from the fclose(), then
5288 * set errno for the file object error handling.
5289 */
5290 errno = GetLastError();
5291 }
5292 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005293 }
5294
5295 /* Free up the native handle at this point */
5296 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00005297 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005298
Mark Hammondb37a3732000-08-14 04:47:33 +00005299 /* Remove this file pointer from dictionary */
5300 PyDict_DelItem(_PyPopenProcs, fileObj);
5301
5302 if (PyDict_Size(_PyPopenProcs) == 0) {
5303 Py_DECREF(_PyPopenProcs);
5304 _PyPopenProcs = NULL;
5305 }
5306
5307 } /* if object retrieval ok */
5308
5309 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005310 } /* if _PyPopenProcs */
5311
Tim Peters736aa322000-09-01 06:51:24 +00005312#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005313 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005314#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005315 return result;
5316}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005317
5318#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005319static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005320posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005321{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005322 char *name;
5323 char *mode = "r";
5324 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00005325 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005326 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005327 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00005328 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00005329 /* Strip mode of binary or text modifiers */
5330 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5331 mode = "r";
5332 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5333 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00005334 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005335 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005336 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00005337 if (fp == NULL)
5338 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005339 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005340 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005341 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005342 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005343}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005344
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005345#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005346#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005347
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005348
Guido van Rossumb6775db1994-08-01 11:34:53 +00005349#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005350PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005351"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005352Set the current process's user id.");
5353
Barry Warsaw53699e91996-12-10 23:23:01 +00005354static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005355posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005356{
5357 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005358 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005359 return NULL;
5360 if (setuid(uid) < 0)
5361 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005362 Py_INCREF(Py_None);
5363 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005364}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005365#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005366
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005367
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005368#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005369PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005370"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005371Set the current process's effective user id.");
5372
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005373static PyObject *
5374posix_seteuid (PyObject *self, PyObject *args)
5375{
5376 int euid;
5377 if (!PyArg_ParseTuple(args, "i", &euid)) {
5378 return NULL;
5379 } else if (seteuid(euid) < 0) {
5380 return posix_error();
5381 } else {
5382 Py_INCREF(Py_None);
5383 return Py_None;
5384 }
5385}
5386#endif /* HAVE_SETEUID */
5387
5388#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005389PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005390"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005391Set the current process's effective group id.");
5392
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005393static PyObject *
5394posix_setegid (PyObject *self, PyObject *args)
5395{
5396 int egid;
5397 if (!PyArg_ParseTuple(args, "i", &egid)) {
5398 return NULL;
5399 } else if (setegid(egid) < 0) {
5400 return posix_error();
5401 } else {
5402 Py_INCREF(Py_None);
5403 return Py_None;
5404 }
5405}
5406#endif /* HAVE_SETEGID */
5407
5408#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005409PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005410"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005411Set the current process's real and effective user ids.");
5412
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005413static PyObject *
5414posix_setreuid (PyObject *self, PyObject *args)
5415{
5416 int ruid, euid;
5417 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
5418 return NULL;
5419 } else if (setreuid(ruid, euid) < 0) {
5420 return posix_error();
5421 } else {
5422 Py_INCREF(Py_None);
5423 return Py_None;
5424 }
5425}
5426#endif /* HAVE_SETREUID */
5427
5428#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005429PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005430"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005431Set the current process's real and effective group ids.");
5432
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005433static PyObject *
5434posix_setregid (PyObject *self, PyObject *args)
5435{
5436 int rgid, egid;
5437 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
5438 return NULL;
5439 } else if (setregid(rgid, egid) < 0) {
5440 return posix_error();
5441 } else {
5442 Py_INCREF(Py_None);
5443 return Py_None;
5444 }
5445}
5446#endif /* HAVE_SETREGID */
5447
Guido van Rossumb6775db1994-08-01 11:34:53 +00005448#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005449PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005450"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005451Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005452
Barry Warsaw53699e91996-12-10 23:23:01 +00005453static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005454posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005455{
5456 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005457 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005458 return NULL;
5459 if (setgid(gid) < 0)
5460 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005461 Py_INCREF(Py_None);
5462 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005463}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005464#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005465
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005466#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005467PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005468"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005469Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005470
5471static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005472posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005473{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005474 int i, len;
5475 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005476
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005477 if (!PySequence_Check(groups)) {
5478 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5479 return NULL;
5480 }
5481 len = PySequence_Size(groups);
5482 if (len > MAX_GROUPS) {
5483 PyErr_SetString(PyExc_ValueError, "too many groups");
5484 return NULL;
5485 }
5486 for(i = 0; i < len; i++) {
5487 PyObject *elem;
5488 elem = PySequence_GetItem(groups, i);
5489 if (!elem)
5490 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00005491 if (!PyLong_Check(elem)) {
5492 PyErr_SetString(PyExc_TypeError,
5493 "groups must be integers");
5494 Py_DECREF(elem);
5495 return NULL;
5496 } else {
5497 unsigned long x = PyLong_AsUnsignedLong(elem);
5498 if (PyErr_Occurred()) {
5499 PyErr_SetString(PyExc_TypeError,
5500 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00005501 Py_DECREF(elem);
5502 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00005503 }
Georg Brandla13c2442005-11-22 19:30:31 +00005504 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00005505 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00005506 if (grouplist[i] != x) {
5507 PyErr_SetString(PyExc_TypeError,
5508 "group id too big");
5509 Py_DECREF(elem);
5510 return NULL;
5511 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005512 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005513 Py_DECREF(elem);
5514 }
5515
5516 if (setgroups(len, grouplist) < 0)
5517 return posix_error();
5518 Py_INCREF(Py_None);
5519 return Py_None;
5520}
5521#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005522
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005523#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5524static PyObject *
5525wait_helper(int pid, int status, struct rusage *ru)
5526{
5527 PyObject *result;
5528 static PyObject *struct_rusage;
5529
5530 if (pid == -1)
5531 return posix_error();
5532
5533 if (struct_rusage == NULL) {
5534 PyObject *m = PyImport_ImportModule("resource");
5535 if (m == NULL)
5536 return NULL;
5537 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5538 Py_DECREF(m);
5539 if (struct_rusage == NULL)
5540 return NULL;
5541 }
5542
5543 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5544 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5545 if (!result)
5546 return NULL;
5547
5548#ifndef doubletime
5549#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5550#endif
5551
5552 PyStructSequence_SET_ITEM(result, 0,
5553 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5554 PyStructSequence_SET_ITEM(result, 1,
5555 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5556#define SET_INT(result, index, value)\
5557 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5558 SET_INT(result, 2, ru->ru_maxrss);
5559 SET_INT(result, 3, ru->ru_ixrss);
5560 SET_INT(result, 4, ru->ru_idrss);
5561 SET_INT(result, 5, ru->ru_isrss);
5562 SET_INT(result, 6, ru->ru_minflt);
5563 SET_INT(result, 7, ru->ru_majflt);
5564 SET_INT(result, 8, ru->ru_nswap);
5565 SET_INT(result, 9, ru->ru_inblock);
5566 SET_INT(result, 10, ru->ru_oublock);
5567 SET_INT(result, 11, ru->ru_msgsnd);
5568 SET_INT(result, 12, ru->ru_msgrcv);
5569 SET_INT(result, 13, ru->ru_nsignals);
5570 SET_INT(result, 14, ru->ru_nvcsw);
5571 SET_INT(result, 15, ru->ru_nivcsw);
5572#undef SET_INT
5573
5574 if (PyErr_Occurred()) {
5575 Py_DECREF(result);
5576 return NULL;
5577 }
5578
5579 return Py_BuildValue("iiN", pid, status, result);
5580}
5581#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5582
5583#ifdef HAVE_WAIT3
5584PyDoc_STRVAR(posix_wait3__doc__,
5585"wait3(options) -> (pid, status, rusage)\n\n\
5586Wait for completion of a child process.");
5587
5588static PyObject *
5589posix_wait3(PyObject *self, PyObject *args)
5590{
5591 int pid, options;
5592 struct rusage ru;
5593 WAIT_TYPE status;
5594 WAIT_STATUS_INT(status) = 0;
5595
5596 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5597 return NULL;
5598
5599 Py_BEGIN_ALLOW_THREADS
5600 pid = wait3(&status, options, &ru);
5601 Py_END_ALLOW_THREADS
5602
5603 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
5604}
5605#endif /* HAVE_WAIT3 */
5606
5607#ifdef HAVE_WAIT4
5608PyDoc_STRVAR(posix_wait4__doc__,
5609"wait4(pid, options) -> (pid, status, rusage)\n\n\
5610Wait for completion of a given child process.");
5611
5612static PyObject *
5613posix_wait4(PyObject *self, PyObject *args)
5614{
5615 int pid, options;
5616 struct rusage ru;
5617 WAIT_TYPE status;
5618 WAIT_STATUS_INT(status) = 0;
5619
5620 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
5621 return NULL;
5622
5623 Py_BEGIN_ALLOW_THREADS
5624 pid = wait4(pid, &status, options, &ru);
5625 Py_END_ALLOW_THREADS
5626
5627 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
5628}
5629#endif /* HAVE_WAIT4 */
5630
Guido van Rossumb6775db1994-08-01 11:34:53 +00005631#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005632PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005633"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005634Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005635
Barry Warsaw53699e91996-12-10 23:23:01 +00005636static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005637posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005638{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005639 int pid, options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005640 WAIT_TYPE status;
5641 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005642
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005643 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00005644 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005645 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00005646 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00005647 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00005648 if (pid == -1)
5649 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005650
5651 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005652}
5653
Tim Petersab034fa2002-02-01 11:27:43 +00005654#elif defined(HAVE_CWAIT)
5655
5656/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005657PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005658"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005659"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005660
5661static PyObject *
5662posix_waitpid(PyObject *self, PyObject *args)
5663{
Thomas Wouters477c8d52006-05-27 19:21:47 +00005664 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005665 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005666
5667 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5668 return NULL;
5669 Py_BEGIN_ALLOW_THREADS
5670 pid = _cwait(&status, pid, options);
5671 Py_END_ALLOW_THREADS
5672 if (pid == -1)
5673 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005674
5675 /* shift the status left a byte so this is more like the POSIX waitpid */
5676 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005677}
5678#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005679
Guido van Rossumad0ee831995-03-01 10:34:45 +00005680#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005681PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005682"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005683Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005684
Barry Warsaw53699e91996-12-10 23:23:01 +00005685static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005686posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005687{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005688 int pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005689 WAIT_TYPE status;
5690 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005691
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005692 Py_BEGIN_ALLOW_THREADS
5693 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00005694 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00005695 if (pid == -1)
5696 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005697
5698 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005699}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005700#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005701
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005702
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005703PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005704"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005705Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005706
Barry Warsaw53699e91996-12-10 23:23:01 +00005707static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005708posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005709{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005710#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005711 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005712#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005713#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00005714 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005715#else
5716 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5717#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005718#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005719}
5720
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005721
Guido van Rossumb6775db1994-08-01 11:34:53 +00005722#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005723PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005724"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005725Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005726
Barry Warsaw53699e91996-12-10 23:23:01 +00005727static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005728posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005729{
Thomas Wouters89f507f2006-12-13 04:49:30 +00005730 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00005731 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005732 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005733 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005734#ifdef Py_USING_UNICODE
5735 int arg_is_unicode = 0;
5736#endif
5737
5738 if (!PyArg_ParseTuple(args, "et:readlink",
5739 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005740 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005741#ifdef Py_USING_UNICODE
5742 v = PySequence_GetItem(args, 0);
5743 if (v == NULL) return NULL;
5744
5745 if (PyUnicode_Check(v)) {
5746 arg_is_unicode = 1;
5747 }
5748 Py_DECREF(v);
5749#endif
5750
Barry Warsaw53699e91996-12-10 23:23:01 +00005751 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00005752 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00005753 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005754 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00005755 return posix_error_with_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005756
5757 v = PyString_FromStringAndSize(buf, n);
5758#ifdef Py_USING_UNICODE
5759 if (arg_is_unicode) {
5760 PyObject *w;
5761
5762 w = PyUnicode_FromEncodedObject(v,
5763 Py_FileSystemDefaultEncoding,
5764 "strict");
5765 if (w != NULL) {
5766 Py_DECREF(v);
5767 v = w;
5768 }
5769 else {
5770 /* fall back to the original byte string, as
5771 discussed in patch #683592 */
5772 PyErr_Clear();
5773 }
5774 }
5775#endif
5776 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005777}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005778#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005779
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005780
Guido van Rossumb6775db1994-08-01 11:34:53 +00005781#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005782PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005783"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005784Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005785
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005786static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005787posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005788{
Thomas Wouters477c8d52006-05-27 19:21:47 +00005789 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005790}
5791#endif /* HAVE_SYMLINK */
5792
5793
5794#ifdef HAVE_TIMES
5795#ifndef HZ
5796#define HZ 60 /* Universal constant :-) */
5797#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005798
Guido van Rossumd48f2521997-12-05 22:19:34 +00005799#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5800static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005801system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005802{
5803 ULONG value = 0;
5804
5805 Py_BEGIN_ALLOW_THREADS
5806 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5807 Py_END_ALLOW_THREADS
5808
5809 return value;
5810}
5811
5812static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005813posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005814{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005815 /* Currently Only Uptime is Provided -- Others Later */
5816 return Py_BuildValue("ddddd",
5817 (double)0 /* t.tms_utime / HZ */,
5818 (double)0 /* t.tms_stime / HZ */,
5819 (double)0 /* t.tms_cutime / HZ */,
5820 (double)0 /* t.tms_cstime / HZ */,
5821 (double)system_uptime() / 1000);
5822}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005823#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005824static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005825posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005826{
5827 struct tms t;
5828 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005829 errno = 0;
5830 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005831 if (c == (clock_t) -1)
5832 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005833 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005834 (double)t.tms_utime / HZ,
5835 (double)t.tms_stime / HZ,
5836 (double)t.tms_cutime / HZ,
5837 (double)t.tms_cstime / HZ,
5838 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005839}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005840#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005841#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005842
5843
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005844#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005845#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005846static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005847posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005848{
5849 FILETIME create, exit, kernel, user;
5850 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005851 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005852 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5853 /* The fields of a FILETIME structure are the hi and lo part
5854 of a 64-bit value expressed in 100 nanosecond units.
5855 1e7 is one second in such units; 1e-7 the inverse.
5856 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5857 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005858 return Py_BuildValue(
5859 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005860 (double)(kernel.dwHighDateTime*429.4967296 +
5861 kernel.dwLowDateTime*1e-7),
5862 (double)(user.dwHighDateTime*429.4967296 +
5863 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005864 (double)0,
5865 (double)0,
5866 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005867}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005868#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005869
5870#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005871PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005872"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005873Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005874#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005875
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005876
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005877#ifdef HAVE_GETSID
5878PyDoc_STRVAR(posix_getsid__doc__,
5879"getsid(pid) -> sid\n\n\
5880Call the system call getsid().");
5881
5882static PyObject *
5883posix_getsid(PyObject *self, PyObject *args)
5884{
5885 int pid, sid;
5886 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5887 return NULL;
5888 sid = getsid(pid);
5889 if (sid < 0)
5890 return posix_error();
5891 return PyInt_FromLong((long)sid);
5892}
5893#endif /* HAVE_GETSID */
5894
5895
Guido van Rossumb6775db1994-08-01 11:34:53 +00005896#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005897PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005898"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005899Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005900
Barry Warsaw53699e91996-12-10 23:23:01 +00005901static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005902posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005903{
Guido van Rossum687dd131993-05-17 08:34:16 +00005904 if (setsid() < 0)
5905 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005906 Py_INCREF(Py_None);
5907 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005908}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005909#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005910
Guido van Rossumb6775db1994-08-01 11:34:53 +00005911#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005912PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005913"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005914Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005915
Barry Warsaw53699e91996-12-10 23:23:01 +00005916static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005917posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005918{
5919 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005920 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005921 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005922 if (setpgid(pid, pgrp) < 0)
5923 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005924 Py_INCREF(Py_None);
5925 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005926}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005927#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005928
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005929
Guido van Rossumb6775db1994-08-01 11:34:53 +00005930#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005931PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005932"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005933Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005934
Barry Warsaw53699e91996-12-10 23:23:01 +00005935static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005936posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005937{
5938 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005939 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005940 return NULL;
5941 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005942 if (pgid < 0)
5943 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005944 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005945}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005946#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005947
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005948
Guido van Rossumb6775db1994-08-01 11:34:53 +00005949#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005950PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005951"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005952Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005953
Barry Warsaw53699e91996-12-10 23:23:01 +00005954static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005955posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005956{
5957 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005958 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005959 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005960 if (tcsetpgrp(fd, pgid) < 0)
5961 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005962 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005963 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005964}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005965#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005966
Guido van Rossum687dd131993-05-17 08:34:16 +00005967/* Functions acting on file descriptors */
5968
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005969PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005970"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005971Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005972
Barry Warsaw53699e91996-12-10 23:23:01 +00005973static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005974posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005975{
Mark Hammondef8b6542001-05-13 08:04:26 +00005976 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005977 int flag;
5978 int mode = 0777;
5979 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005980
5981#ifdef MS_WINDOWS
5982 if (unicode_file_names()) {
5983 PyUnicodeObject *po;
5984 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5985 Py_BEGIN_ALLOW_THREADS
5986 /* PyUnicode_AS_UNICODE OK without thread
5987 lock as it is a simple dereference. */
5988 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5989 Py_END_ALLOW_THREADS
5990 if (fd < 0)
5991 return posix_error();
5992 return PyInt_FromLong((long)fd);
5993 }
5994 /* Drop the argument parsing error as narrow strings
5995 are also valid. */
5996 PyErr_Clear();
5997 }
5998#endif
5999
Tim Peters5aa91602002-01-30 05:46:57 +00006000 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00006001 Py_FileSystemDefaultEncoding, &file,
6002 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00006003 return NULL;
6004
Barry Warsaw53699e91996-12-10 23:23:01 +00006005 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006006 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00006007 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006008 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00006009 return posix_error_with_allocated_filename(file);
6010 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00006011 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006012}
6013
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006014
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006015PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006016"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006017Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006018
Barry Warsaw53699e91996-12-10 23:23:01 +00006019static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006020posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006021{
6022 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006023 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006024 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006025 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006026 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006027 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006028 if (res < 0)
6029 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006030 Py_INCREF(Py_None);
6031 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006032}
6033
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006034
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006035PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006036"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006037Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006038
Barry Warsaw53699e91996-12-10 23:23:01 +00006039static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006040posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006041{
6042 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006043 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006044 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006045 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006046 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006047 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006048 if (fd < 0)
6049 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006050 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006051}
6052
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006054PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006055"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006056Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006057
Barry Warsaw53699e91996-12-10 23:23:01 +00006058static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006059posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006060{
6061 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006062 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00006063 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006064 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006065 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00006066 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006067 if (res < 0)
6068 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006069 Py_INCREF(Py_None);
6070 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006071}
6072
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006073
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006074PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006075"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006076Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006077
Barry Warsaw53699e91996-12-10 23:23:01 +00006078static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006079posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006080{
6081 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006082#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006083 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006084#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00006085 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006086#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006087 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006088 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00006089 return NULL;
6090#ifdef SEEK_SET
6091 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6092 switch (how) {
6093 case 0: how = SEEK_SET; break;
6094 case 1: how = SEEK_CUR; break;
6095 case 2: how = SEEK_END; break;
6096 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006097#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006098
6099#if !defined(HAVE_LARGEFILE_SUPPORT)
6100 pos = PyInt_AsLong(posobj);
6101#else
6102 pos = PyLong_Check(posobj) ?
6103 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
6104#endif
6105 if (PyErr_Occurred())
6106 return NULL;
6107
Barry Warsaw53699e91996-12-10 23:23:01 +00006108 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006109#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00006110 res = _lseeki64(fd, pos, how);
6111#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006112 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006113#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006114 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006115 if (res < 0)
6116 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006117
6118#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00006119 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006120#else
6121 return PyLong_FromLongLong(res);
6122#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006123}
6124
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006126PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006127"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006128Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006129
Barry Warsaw53699e91996-12-10 23:23:01 +00006130static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006131posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006132{
Guido van Rossum8bac5461996-06-11 18:38:48 +00006133 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00006134 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006135 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006136 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00006137 if (size < 0) {
6138 errno = EINVAL;
6139 return posix_error();
6140 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006141 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006142 if (buffer == NULL)
6143 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006144 Py_BEGIN_ALLOW_THREADS
6145 n = read(fd, PyString_AsString(buffer), size);
6146 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00006147 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006148 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00006149 return posix_error();
6150 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00006151 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00006152 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00006153 return buffer;
6154}
6155
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006156
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006157PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006158"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006159Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006160
Barry Warsaw53699e91996-12-10 23:23:01 +00006161static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006162posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006163{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006164 int fd;
6165 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00006166 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006167
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006168 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006169 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006170 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006171 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00006172 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006173 if (size < 0)
6174 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006175 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006176}
6177
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006178
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006179PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006180"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006181Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006182
Barry Warsaw53699e91996-12-10 23:23:01 +00006183static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006184posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006185{
6186 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00006187 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00006188 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006189 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006190 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006191#ifdef __VMS
6192 /* on OpenVMS we must ensure that all bytes are written to the file */
6193 fsync(fd);
6194#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006195 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00006196 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00006197 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00006198 if (res != 0) {
6199#ifdef MS_WINDOWS
6200 return win32_error("fstat", NULL);
6201#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006202 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006203#endif
6204 }
Tim Peters5aa91602002-01-30 05:46:57 +00006205
Martin v. Löwis14694662006-02-03 12:54:16 +00006206 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006207}
6208
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006210PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006211"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006212Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006213
Barry Warsaw53699e91996-12-10 23:23:01 +00006214static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006215posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006216{
Guido van Rossum687dd131993-05-17 08:34:16 +00006217 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006218 char *mode = "r";
6219 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00006220 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00006221 PyObject *f;
6222 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00006223 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006224
Thomas Heller1f043e22002-11-07 16:00:59 +00006225 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
6226 PyErr_Format(PyExc_ValueError,
6227 "invalid file mode '%s'", mode);
6228 return NULL;
6229 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006230 Py_BEGIN_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006231#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
6232 if (mode[0] == 'a') {
6233 /* try to make sure the O_APPEND flag is set */
6234 int flags;
6235 flags = fcntl(fd, F_GETFL);
6236 if (flags != -1)
6237 fcntl(fd, F_SETFL, flags | O_APPEND);
6238 fp = fdopen(fd, mode);
6239 if (fp == NULL && flags != -1)
6240 /* restore old mode if fdopen failed */
6241 fcntl(fd, F_SETFL, flags);
6242 } else {
6243 fp = fdopen(fd, mode);
6244 }
6245#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006246 fp = fdopen(fd, mode);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006247#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006248 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006249 if (fp == NULL)
6250 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00006251 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006252 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00006253 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006254 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006255}
6256
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006257PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006258"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006259Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006260connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006261
6262static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006263posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006264{
6265 int fd;
6266 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6267 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006268 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006269}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006270
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006271#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006272PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006273"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006274Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006275
Barry Warsaw53699e91996-12-10 23:23:01 +00006276static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006277posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006278{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006279#if defined(PYOS_OS2)
6280 HFILE read, write;
6281 APIRET rc;
6282
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006283 Py_BEGIN_ALLOW_THREADS
6284 rc = DosCreatePipe( &read, &write, 4096);
6285 Py_END_ALLOW_THREADS
6286 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006287 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006288
6289 return Py_BuildValue("(ii)", read, write);
6290#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006291#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00006292 int fds[2];
6293 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00006294 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006295 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00006296 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006297 if (res != 0)
6298 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006299 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006300#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00006301 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006302 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00006303 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00006304 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006305 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00006306 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00006307 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006308 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00006309 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6310 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006311 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006312#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006313#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006314}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006315#endif /* HAVE_PIPE */
6316
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006317
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006318#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006319PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006320"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006321Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006322
Barry Warsaw53699e91996-12-10 23:23:01 +00006323static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006324posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006325{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006326 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006327 int mode = 0666;
6328 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006329 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006330 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006331 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006332 res = mkfifo(filename, mode);
6333 Py_END_ALLOW_THREADS
6334 if (res < 0)
6335 return posix_error();
6336 Py_INCREF(Py_None);
6337 return Py_None;
6338}
6339#endif
6340
6341
Neal Norwitz11690112002-07-30 01:08:28 +00006342#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006343PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006344"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006345Create a filesystem node (file, device special file or named pipe)\n\
6346named filename. mode specifies both the permissions to use and the\n\
6347type of node to be created, being combined (bitwise OR) with one of\n\
6348S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006349device defines the newly created device special file (probably using\n\
6350os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006351
6352
6353static PyObject *
6354posix_mknod(PyObject *self, PyObject *args)
6355{
6356 char *filename;
6357 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006358 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006359 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00006360 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006361 return NULL;
6362 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006363 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00006364 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006365 if (res < 0)
6366 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006367 Py_INCREF(Py_None);
6368 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006369}
6370#endif
6371
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006372#ifdef HAVE_DEVICE_MACROS
6373PyDoc_STRVAR(posix_major__doc__,
6374"major(device) -> major number\n\
6375Extracts a device major number from a raw device number.");
6376
6377static PyObject *
6378posix_major(PyObject *self, PyObject *args)
6379{
6380 int device;
6381 if (!PyArg_ParseTuple(args, "i:major", &device))
6382 return NULL;
6383 return PyInt_FromLong((long)major(device));
6384}
6385
6386PyDoc_STRVAR(posix_minor__doc__,
6387"minor(device) -> minor number\n\
6388Extracts a device minor number from a raw device number.");
6389
6390static PyObject *
6391posix_minor(PyObject *self, PyObject *args)
6392{
6393 int device;
6394 if (!PyArg_ParseTuple(args, "i:minor", &device))
6395 return NULL;
6396 return PyInt_FromLong((long)minor(device));
6397}
6398
6399PyDoc_STRVAR(posix_makedev__doc__,
6400"makedev(major, minor) -> device number\n\
6401Composes a raw device number from the major and minor device numbers.");
6402
6403static PyObject *
6404posix_makedev(PyObject *self, PyObject *args)
6405{
6406 int major, minor;
6407 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6408 return NULL;
6409 return PyInt_FromLong((long)makedev(major, minor));
6410}
6411#endif /* device macros */
6412
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006413
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006414#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006415PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006416"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006417Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006418
Barry Warsaw53699e91996-12-10 23:23:01 +00006419static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006420posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006421{
6422 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006423 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006424 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006425 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006426
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006427 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006428 return NULL;
6429
6430#if !defined(HAVE_LARGEFILE_SUPPORT)
6431 length = PyInt_AsLong(lenobj);
6432#else
6433 length = PyLong_Check(lenobj) ?
6434 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6435#endif
6436 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006437 return NULL;
6438
Barry Warsaw53699e91996-12-10 23:23:01 +00006439 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006440 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00006441 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006442 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006443 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006444 return NULL;
6445 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006446 Py_INCREF(Py_None);
6447 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006448}
6449#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006450
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006451#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006452PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006453"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006454Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006455
Fred Drake762e2061999-08-26 17:23:54 +00006456/* Save putenv() parameters as values here, so we can collect them when they
6457 * get re-set with another call for the same key. */
6458static PyObject *posix_putenv_garbage;
6459
Tim Peters5aa91602002-01-30 05:46:57 +00006460static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006461posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006462{
6463 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006464 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00006465 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00006466 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006467
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006468 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006469 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006470
6471#if defined(PYOS_OS2)
6472 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6473 APIRET rc;
6474
Guido van Rossumd48f2521997-12-05 22:19:34 +00006475 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6476 if (rc != NO_ERROR)
6477 return os2_error(rc);
6478
6479 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6480 APIRET rc;
6481
Guido van Rossumd48f2521997-12-05 22:19:34 +00006482 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6483 if (rc != NO_ERROR)
6484 return os2_error(rc);
6485 } else {
6486#endif
6487
Fred Drake762e2061999-08-26 17:23:54 +00006488 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00006489 len = strlen(s1) + strlen(s2) + 2;
6490 /* len includes space for a trailing \0; the size arg to
6491 PyString_FromStringAndSize does not count that */
6492 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00006493 if (newstr == NULL)
6494 return PyErr_NoMemory();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006495 newenv = PyString_AS_STRING(newstr);
6496 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6497 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00006498 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006499 posix_error();
6500 return NULL;
6501 }
Fred Drake762e2061999-08-26 17:23:54 +00006502 /* Install the first arg and newstr in posix_putenv_garbage;
6503 * this will cause previous value to be collected. This has to
6504 * happen after the real putenv() call because the old value
6505 * was still accessible until then. */
6506 if (PyDict_SetItem(posix_putenv_garbage,
6507 PyTuple_GET_ITEM(args, 0), newstr)) {
6508 /* really not much we can do; just leak */
6509 PyErr_Clear();
6510 }
6511 else {
6512 Py_DECREF(newstr);
6513 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006514
6515#if defined(PYOS_OS2)
6516 }
6517#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006518 Py_INCREF(Py_None);
6519 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006520}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006521#endif /* putenv */
6522
Guido van Rossumc524d952001-10-19 01:31:59 +00006523#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006524PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006525"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006526Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006527
6528static PyObject *
6529posix_unsetenv(PyObject *self, PyObject *args)
6530{
6531 char *s1;
6532
6533 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6534 return NULL;
6535
6536 unsetenv(s1);
6537
6538 /* Remove the key from posix_putenv_garbage;
6539 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00006540 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00006541 * old value was still accessible until then.
6542 */
6543 if (PyDict_DelItem(posix_putenv_garbage,
6544 PyTuple_GET_ITEM(args, 0))) {
6545 /* really not much we can do; just leak */
6546 PyErr_Clear();
6547 }
6548
6549 Py_INCREF(Py_None);
6550 return Py_None;
6551}
6552#endif /* unsetenv */
6553
Guido van Rossumb6a47161997-09-15 22:54:34 +00006554#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006555PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006556"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006557Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006558
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006559static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006560posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006561{
6562 int code;
6563 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006564 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00006565 return NULL;
6566 message = strerror(code);
6567 if (message == NULL) {
6568 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00006569 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006570 return NULL;
6571 }
6572 return PyString_FromString(message);
6573}
6574#endif /* strerror */
6575
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006576
Guido van Rossumc9641791998-08-04 15:26:23 +00006577#ifdef HAVE_SYS_WAIT_H
6578
Fred Drake106c1a02002-04-23 15:58:02 +00006579#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006580PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006581"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006582Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006583
6584static PyObject *
6585posix_WCOREDUMP(PyObject *self, PyObject *args)
6586{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006587 WAIT_TYPE status;
6588 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006589
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006590 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006591 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006592
6593 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006594}
6595#endif /* WCOREDUMP */
6596
6597#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006598PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006599"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006600Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006601job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006602
6603static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006604posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006605{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006606 WAIT_TYPE status;
6607 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006608
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006609 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006610 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006611
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006612 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006613}
6614#endif /* WIFCONTINUED */
6615
Guido van Rossumc9641791998-08-04 15:26:23 +00006616#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006617PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006618"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006619Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006620
6621static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006622posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006623{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006624 WAIT_TYPE status;
6625 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006626
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006627 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006628 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006629
Fred Drake106c1a02002-04-23 15:58:02 +00006630 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006631}
6632#endif /* WIFSTOPPED */
6633
6634#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006635PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006636"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006637Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006638
6639static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006640posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006641{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006642 WAIT_TYPE status;
6643 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006644
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006645 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006646 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006647
Fred Drake106c1a02002-04-23 15:58:02 +00006648 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006649}
6650#endif /* WIFSIGNALED */
6651
6652#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006653PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006654"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006655Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006656system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006657
6658static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006659posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006660{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006661 WAIT_TYPE status;
6662 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006663
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006664 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006665 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006666
Fred Drake106c1a02002-04-23 15:58:02 +00006667 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006668}
6669#endif /* WIFEXITED */
6670
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006671#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006672PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006673"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006674Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006675
6676static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006677posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006678{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006679 WAIT_TYPE status;
6680 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006681
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006682 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006683 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006684
Guido van Rossumc9641791998-08-04 15:26:23 +00006685 return Py_BuildValue("i", WEXITSTATUS(status));
6686}
6687#endif /* WEXITSTATUS */
6688
6689#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006690PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006691"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006692Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006693value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006694
6695static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006696posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006697{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006698 WAIT_TYPE status;
6699 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006700
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006701 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006702 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006703
Guido van Rossumc9641791998-08-04 15:26:23 +00006704 return Py_BuildValue("i", WTERMSIG(status));
6705}
6706#endif /* WTERMSIG */
6707
6708#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006709PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006710"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006711Return the signal that stopped the process that provided\n\
6712the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006713
6714static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006715posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006716{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006717 WAIT_TYPE status;
6718 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006719
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006720 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006721 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006722
Guido van Rossumc9641791998-08-04 15:26:23 +00006723 return Py_BuildValue("i", WSTOPSIG(status));
6724}
6725#endif /* WSTOPSIG */
6726
6727#endif /* HAVE_SYS_WAIT_H */
6728
6729
Thomas Wouters477c8d52006-05-27 19:21:47 +00006730#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006731#ifdef _SCO_DS
6732/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6733 needed definitions in sys/statvfs.h */
6734#define _SVID3
6735#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006736#include <sys/statvfs.h>
6737
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006738static PyObject*
6739_pystatvfs_fromstructstatvfs(struct statvfs st) {
6740 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6741 if (v == NULL)
6742 return NULL;
6743
6744#if !defined(HAVE_LARGEFILE_SUPPORT)
6745 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6746 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6747 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6748 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6749 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6750 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6751 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6752 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6753 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6754 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6755#else
6756 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6757 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006758 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006759 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006760 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006761 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006762 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006763 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006764 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006765 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006766 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006767 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006768 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006769 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006770 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6771 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6772#endif
6773
6774 return v;
6775}
6776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006777PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006778"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006779Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006780
6781static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006782posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006783{
6784 int fd, res;
6785 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006786
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006787 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006788 return NULL;
6789 Py_BEGIN_ALLOW_THREADS
6790 res = fstatvfs(fd, &st);
6791 Py_END_ALLOW_THREADS
6792 if (res != 0)
6793 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006794
6795 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006796}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006797#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006798
6799
Thomas Wouters477c8d52006-05-27 19:21:47 +00006800#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006801#include <sys/statvfs.h>
6802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006803PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006804"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006805Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006806
6807static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006808posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006809{
6810 char *path;
6811 int res;
6812 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006813 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006814 return NULL;
6815 Py_BEGIN_ALLOW_THREADS
6816 res = statvfs(path, &st);
6817 Py_END_ALLOW_THREADS
6818 if (res != 0)
6819 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006820
6821 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006822}
6823#endif /* HAVE_STATVFS */
6824
6825
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006826#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006827PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006828"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006829Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006830The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006831or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006832
6833static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006834posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006835{
6836 PyObject *result = NULL;
6837 char *dir = NULL;
6838 char *pfx = NULL;
6839 char *name;
6840
6841 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6842 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006843
6844 if (PyErr_Warn(PyExc_RuntimeWarning,
6845 "tempnam is a potential security risk to your program") < 0)
6846 return NULL;
6847
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006848#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006849 name = _tempnam(dir, pfx);
6850#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006851 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006852#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006853 if (name == NULL)
6854 return PyErr_NoMemory();
6855 result = PyString_FromString(name);
6856 free(name);
6857 return result;
6858}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006859#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006860
6861
6862#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006863PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006864"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006865Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006866
6867static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006868posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006869{
6870 FILE *fp;
6871
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006872 fp = tmpfile();
6873 if (fp == NULL)
6874 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006875 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006876}
6877#endif
6878
6879
6880#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006881PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006882"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006883Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006884
6885static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006886posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006887{
6888 char buffer[L_tmpnam];
6889 char *name;
6890
Skip Montanaro95618b52001-08-18 18:52:10 +00006891 if (PyErr_Warn(PyExc_RuntimeWarning,
6892 "tmpnam is a potential security risk to your program") < 0)
6893 return NULL;
6894
Greg Wardb48bc172000-03-01 21:51:56 +00006895#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006896 name = tmpnam_r(buffer);
6897#else
6898 name = tmpnam(buffer);
6899#endif
6900 if (name == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006901 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006902#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006903 "unexpected NULL from tmpnam_r"
6904#else
6905 "unexpected NULL from tmpnam"
6906#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006907 );
6908 PyErr_SetObject(PyExc_OSError, err);
6909 Py_XDECREF(err);
6910 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006911 }
6912 return PyString_FromString(buffer);
6913}
6914#endif
6915
6916
Fred Drakec9680921999-12-13 16:37:25 +00006917/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6918 * It maps strings representing configuration variable names to
6919 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006920 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006921 * rarely-used constants. There are three separate tables that use
6922 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006923 *
6924 * This code is always included, even if none of the interfaces that
6925 * need it are included. The #if hackery needed to avoid it would be
6926 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006927 */
6928struct constdef {
6929 char *name;
6930 long value;
6931};
6932
Fred Drake12c6e2d1999-12-14 21:25:03 +00006933static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006934conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6935 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006936{
6937 if (PyInt_Check(arg)) {
6938 *valuep = PyInt_AS_LONG(arg);
6939 return 1;
6940 }
6941 if (PyString_Check(arg)) {
6942 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006943 size_t lo = 0;
6944 size_t mid;
6945 size_t hi = tablesize;
6946 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006947 char *confname = PyString_AS_STRING(arg);
6948 while (lo < hi) {
6949 mid = (lo + hi) / 2;
6950 cmp = strcmp(confname, table[mid].name);
6951 if (cmp < 0)
6952 hi = mid;
6953 else if (cmp > 0)
6954 lo = mid + 1;
6955 else {
6956 *valuep = table[mid].value;
6957 return 1;
6958 }
6959 }
6960 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6961 }
6962 else
6963 PyErr_SetString(PyExc_TypeError,
6964 "configuration names must be strings or integers");
6965 return 0;
6966}
6967
6968
6969#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6970static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006971#ifdef _PC_ABI_AIO_XFER_MAX
6972 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6973#endif
6974#ifdef _PC_ABI_ASYNC_IO
6975 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6976#endif
Fred Drakec9680921999-12-13 16:37:25 +00006977#ifdef _PC_ASYNC_IO
6978 {"PC_ASYNC_IO", _PC_ASYNC_IO},
6979#endif
6980#ifdef _PC_CHOWN_RESTRICTED
6981 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
6982#endif
6983#ifdef _PC_FILESIZEBITS
6984 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
6985#endif
6986#ifdef _PC_LAST
6987 {"PC_LAST", _PC_LAST},
6988#endif
6989#ifdef _PC_LINK_MAX
6990 {"PC_LINK_MAX", _PC_LINK_MAX},
6991#endif
6992#ifdef _PC_MAX_CANON
6993 {"PC_MAX_CANON", _PC_MAX_CANON},
6994#endif
6995#ifdef _PC_MAX_INPUT
6996 {"PC_MAX_INPUT", _PC_MAX_INPUT},
6997#endif
6998#ifdef _PC_NAME_MAX
6999 {"PC_NAME_MAX", _PC_NAME_MAX},
7000#endif
7001#ifdef _PC_NO_TRUNC
7002 {"PC_NO_TRUNC", _PC_NO_TRUNC},
7003#endif
7004#ifdef _PC_PATH_MAX
7005 {"PC_PATH_MAX", _PC_PATH_MAX},
7006#endif
7007#ifdef _PC_PIPE_BUF
7008 {"PC_PIPE_BUF", _PC_PIPE_BUF},
7009#endif
7010#ifdef _PC_PRIO_IO
7011 {"PC_PRIO_IO", _PC_PRIO_IO},
7012#endif
7013#ifdef _PC_SOCK_MAXBUF
7014 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
7015#endif
7016#ifdef _PC_SYNC_IO
7017 {"PC_SYNC_IO", _PC_SYNC_IO},
7018#endif
7019#ifdef _PC_VDISABLE
7020 {"PC_VDISABLE", _PC_VDISABLE},
7021#endif
7022};
7023
Fred Drakec9680921999-12-13 16:37:25 +00007024static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007025conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007026{
7027 return conv_confname(arg, valuep, posix_constants_pathconf,
7028 sizeof(posix_constants_pathconf)
7029 / sizeof(struct constdef));
7030}
7031#endif
7032
7033#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007034PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007035"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007036Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007037If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007038
7039static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007040posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007041{
7042 PyObject *result = NULL;
7043 int name, fd;
7044
Fred Drake12c6e2d1999-12-14 21:25:03 +00007045 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7046 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00007047 long limit;
7048
7049 errno = 0;
7050 limit = fpathconf(fd, name);
7051 if (limit == -1 && errno != 0)
7052 posix_error();
7053 else
7054 result = PyInt_FromLong(limit);
7055 }
7056 return result;
7057}
7058#endif
7059
7060
7061#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007062PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007063"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007064Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007065If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007066
7067static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007068posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007069{
7070 PyObject *result = NULL;
7071 int name;
7072 char *path;
7073
7074 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7075 conv_path_confname, &name)) {
7076 long limit;
7077
7078 errno = 0;
7079 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007080 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00007081 if (errno == EINVAL)
7082 /* could be a path or name problem */
7083 posix_error();
7084 else
7085 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007086 }
Fred Drakec9680921999-12-13 16:37:25 +00007087 else
7088 result = PyInt_FromLong(limit);
7089 }
7090 return result;
7091}
7092#endif
7093
7094#ifdef HAVE_CONFSTR
7095static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007096#ifdef _CS_ARCHITECTURE
7097 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
7098#endif
7099#ifdef _CS_HOSTNAME
7100 {"CS_HOSTNAME", _CS_HOSTNAME},
7101#endif
7102#ifdef _CS_HW_PROVIDER
7103 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
7104#endif
7105#ifdef _CS_HW_SERIAL
7106 {"CS_HW_SERIAL", _CS_HW_SERIAL},
7107#endif
7108#ifdef _CS_INITTAB_NAME
7109 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
7110#endif
Fred Drakec9680921999-12-13 16:37:25 +00007111#ifdef _CS_LFS64_CFLAGS
7112 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
7113#endif
7114#ifdef _CS_LFS64_LDFLAGS
7115 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
7116#endif
7117#ifdef _CS_LFS64_LIBS
7118 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
7119#endif
7120#ifdef _CS_LFS64_LINTFLAGS
7121 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
7122#endif
7123#ifdef _CS_LFS_CFLAGS
7124 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
7125#endif
7126#ifdef _CS_LFS_LDFLAGS
7127 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
7128#endif
7129#ifdef _CS_LFS_LIBS
7130 {"CS_LFS_LIBS", _CS_LFS_LIBS},
7131#endif
7132#ifdef _CS_LFS_LINTFLAGS
7133 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
7134#endif
Fred Draked86ed291999-12-15 15:34:33 +00007135#ifdef _CS_MACHINE
7136 {"CS_MACHINE", _CS_MACHINE},
7137#endif
Fred Drakec9680921999-12-13 16:37:25 +00007138#ifdef _CS_PATH
7139 {"CS_PATH", _CS_PATH},
7140#endif
Fred Draked86ed291999-12-15 15:34:33 +00007141#ifdef _CS_RELEASE
7142 {"CS_RELEASE", _CS_RELEASE},
7143#endif
7144#ifdef _CS_SRPC_DOMAIN
7145 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
7146#endif
7147#ifdef _CS_SYSNAME
7148 {"CS_SYSNAME", _CS_SYSNAME},
7149#endif
7150#ifdef _CS_VERSION
7151 {"CS_VERSION", _CS_VERSION},
7152#endif
Fred Drakec9680921999-12-13 16:37:25 +00007153#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7154 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
7155#endif
7156#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7157 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
7158#endif
7159#ifdef _CS_XBS5_ILP32_OFF32_LIBS
7160 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
7161#endif
7162#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7163 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
7164#endif
7165#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7166 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
7167#endif
7168#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7169 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
7170#endif
7171#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7172 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
7173#endif
7174#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7175 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
7176#endif
7177#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7178 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
7179#endif
7180#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7181 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
7182#endif
7183#ifdef _CS_XBS5_LP64_OFF64_LIBS
7184 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
7185#endif
7186#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7187 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
7188#endif
7189#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7190 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
7191#endif
7192#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7193 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
7194#endif
7195#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7196 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
7197#endif
7198#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7199 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
7200#endif
Fred Draked86ed291999-12-15 15:34:33 +00007201#ifdef _MIPS_CS_AVAIL_PROCESSORS
7202 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
7203#endif
7204#ifdef _MIPS_CS_BASE
7205 {"MIPS_CS_BASE", _MIPS_CS_BASE},
7206#endif
7207#ifdef _MIPS_CS_HOSTID
7208 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
7209#endif
7210#ifdef _MIPS_CS_HW_NAME
7211 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7212#endif
7213#ifdef _MIPS_CS_NUM_PROCESSORS
7214 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7215#endif
7216#ifdef _MIPS_CS_OSREL_MAJ
7217 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7218#endif
7219#ifdef _MIPS_CS_OSREL_MIN
7220 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7221#endif
7222#ifdef _MIPS_CS_OSREL_PATCH
7223 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7224#endif
7225#ifdef _MIPS_CS_OS_NAME
7226 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7227#endif
7228#ifdef _MIPS_CS_OS_PROVIDER
7229 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7230#endif
7231#ifdef _MIPS_CS_PROCESSORS
7232 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7233#endif
7234#ifdef _MIPS_CS_SERIAL
7235 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7236#endif
7237#ifdef _MIPS_CS_VENDOR
7238 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7239#endif
Fred Drakec9680921999-12-13 16:37:25 +00007240};
7241
7242static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007243conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007244{
7245 return conv_confname(arg, valuep, posix_constants_confstr,
7246 sizeof(posix_constants_confstr)
7247 / sizeof(struct constdef));
7248}
7249
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007250PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007251"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007252Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007253
7254static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007255posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007256{
7257 PyObject *result = NULL;
7258 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007259 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007260
7261 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007262 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007263
Fred Drakec9680921999-12-13 16:37:25 +00007264 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007265 len = confstr(name, buffer, sizeof(buffer));
7266 if (len == 0) {
7267 if (errno) {
7268 posix_error();
7269 }
7270 else {
7271 result = Py_None;
7272 Py_INCREF(Py_None);
7273 }
Fred Drakec9680921999-12-13 16:37:25 +00007274 }
7275 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007276 if ((unsigned int)len >= sizeof(buffer)) {
7277 result = PyString_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007278 if (result != NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007279 confstr(name, PyString_AS_STRING(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00007280 }
7281 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007282 result = PyString_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007283 }
7284 }
7285 return result;
7286}
7287#endif
7288
7289
7290#ifdef HAVE_SYSCONF
7291static struct constdef posix_constants_sysconf[] = {
7292#ifdef _SC_2_CHAR_TERM
7293 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7294#endif
7295#ifdef _SC_2_C_BIND
7296 {"SC_2_C_BIND", _SC_2_C_BIND},
7297#endif
7298#ifdef _SC_2_C_DEV
7299 {"SC_2_C_DEV", _SC_2_C_DEV},
7300#endif
7301#ifdef _SC_2_C_VERSION
7302 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7303#endif
7304#ifdef _SC_2_FORT_DEV
7305 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7306#endif
7307#ifdef _SC_2_FORT_RUN
7308 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7309#endif
7310#ifdef _SC_2_LOCALEDEF
7311 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7312#endif
7313#ifdef _SC_2_SW_DEV
7314 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7315#endif
7316#ifdef _SC_2_UPE
7317 {"SC_2_UPE", _SC_2_UPE},
7318#endif
7319#ifdef _SC_2_VERSION
7320 {"SC_2_VERSION", _SC_2_VERSION},
7321#endif
Fred Draked86ed291999-12-15 15:34:33 +00007322#ifdef _SC_ABI_ASYNCHRONOUS_IO
7323 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7324#endif
7325#ifdef _SC_ACL
7326 {"SC_ACL", _SC_ACL},
7327#endif
Fred Drakec9680921999-12-13 16:37:25 +00007328#ifdef _SC_AIO_LISTIO_MAX
7329 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7330#endif
Fred Drakec9680921999-12-13 16:37:25 +00007331#ifdef _SC_AIO_MAX
7332 {"SC_AIO_MAX", _SC_AIO_MAX},
7333#endif
7334#ifdef _SC_AIO_PRIO_DELTA_MAX
7335 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7336#endif
7337#ifdef _SC_ARG_MAX
7338 {"SC_ARG_MAX", _SC_ARG_MAX},
7339#endif
7340#ifdef _SC_ASYNCHRONOUS_IO
7341 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7342#endif
7343#ifdef _SC_ATEXIT_MAX
7344 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7345#endif
Fred Draked86ed291999-12-15 15:34:33 +00007346#ifdef _SC_AUDIT
7347 {"SC_AUDIT", _SC_AUDIT},
7348#endif
Fred Drakec9680921999-12-13 16:37:25 +00007349#ifdef _SC_AVPHYS_PAGES
7350 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7351#endif
7352#ifdef _SC_BC_BASE_MAX
7353 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7354#endif
7355#ifdef _SC_BC_DIM_MAX
7356 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7357#endif
7358#ifdef _SC_BC_SCALE_MAX
7359 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7360#endif
7361#ifdef _SC_BC_STRING_MAX
7362 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7363#endif
Fred Draked86ed291999-12-15 15:34:33 +00007364#ifdef _SC_CAP
7365 {"SC_CAP", _SC_CAP},
7366#endif
Fred Drakec9680921999-12-13 16:37:25 +00007367#ifdef _SC_CHARCLASS_NAME_MAX
7368 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7369#endif
7370#ifdef _SC_CHAR_BIT
7371 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7372#endif
7373#ifdef _SC_CHAR_MAX
7374 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7375#endif
7376#ifdef _SC_CHAR_MIN
7377 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7378#endif
7379#ifdef _SC_CHILD_MAX
7380 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7381#endif
7382#ifdef _SC_CLK_TCK
7383 {"SC_CLK_TCK", _SC_CLK_TCK},
7384#endif
7385#ifdef _SC_COHER_BLKSZ
7386 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7387#endif
7388#ifdef _SC_COLL_WEIGHTS_MAX
7389 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7390#endif
7391#ifdef _SC_DCACHE_ASSOC
7392 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7393#endif
7394#ifdef _SC_DCACHE_BLKSZ
7395 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7396#endif
7397#ifdef _SC_DCACHE_LINESZ
7398 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7399#endif
7400#ifdef _SC_DCACHE_SZ
7401 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7402#endif
7403#ifdef _SC_DCACHE_TBLKSZ
7404 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7405#endif
7406#ifdef _SC_DELAYTIMER_MAX
7407 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7408#endif
7409#ifdef _SC_EQUIV_CLASS_MAX
7410 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7411#endif
7412#ifdef _SC_EXPR_NEST_MAX
7413 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7414#endif
7415#ifdef _SC_FSYNC
7416 {"SC_FSYNC", _SC_FSYNC},
7417#endif
7418#ifdef _SC_GETGR_R_SIZE_MAX
7419 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7420#endif
7421#ifdef _SC_GETPW_R_SIZE_MAX
7422 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7423#endif
7424#ifdef _SC_ICACHE_ASSOC
7425 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7426#endif
7427#ifdef _SC_ICACHE_BLKSZ
7428 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7429#endif
7430#ifdef _SC_ICACHE_LINESZ
7431 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7432#endif
7433#ifdef _SC_ICACHE_SZ
7434 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7435#endif
Fred Draked86ed291999-12-15 15:34:33 +00007436#ifdef _SC_INF
7437 {"SC_INF", _SC_INF},
7438#endif
Fred Drakec9680921999-12-13 16:37:25 +00007439#ifdef _SC_INT_MAX
7440 {"SC_INT_MAX", _SC_INT_MAX},
7441#endif
7442#ifdef _SC_INT_MIN
7443 {"SC_INT_MIN", _SC_INT_MIN},
7444#endif
7445#ifdef _SC_IOV_MAX
7446 {"SC_IOV_MAX", _SC_IOV_MAX},
7447#endif
Fred Draked86ed291999-12-15 15:34:33 +00007448#ifdef _SC_IP_SECOPTS
7449 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7450#endif
Fred Drakec9680921999-12-13 16:37:25 +00007451#ifdef _SC_JOB_CONTROL
7452 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7453#endif
Fred Draked86ed291999-12-15 15:34:33 +00007454#ifdef _SC_KERN_POINTERS
7455 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7456#endif
7457#ifdef _SC_KERN_SIM
7458 {"SC_KERN_SIM", _SC_KERN_SIM},
7459#endif
Fred Drakec9680921999-12-13 16:37:25 +00007460#ifdef _SC_LINE_MAX
7461 {"SC_LINE_MAX", _SC_LINE_MAX},
7462#endif
7463#ifdef _SC_LOGIN_NAME_MAX
7464 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7465#endif
7466#ifdef _SC_LOGNAME_MAX
7467 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7468#endif
7469#ifdef _SC_LONG_BIT
7470 {"SC_LONG_BIT", _SC_LONG_BIT},
7471#endif
Fred Draked86ed291999-12-15 15:34:33 +00007472#ifdef _SC_MAC
7473 {"SC_MAC", _SC_MAC},
7474#endif
Fred Drakec9680921999-12-13 16:37:25 +00007475#ifdef _SC_MAPPED_FILES
7476 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7477#endif
7478#ifdef _SC_MAXPID
7479 {"SC_MAXPID", _SC_MAXPID},
7480#endif
7481#ifdef _SC_MB_LEN_MAX
7482 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7483#endif
7484#ifdef _SC_MEMLOCK
7485 {"SC_MEMLOCK", _SC_MEMLOCK},
7486#endif
7487#ifdef _SC_MEMLOCK_RANGE
7488 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7489#endif
7490#ifdef _SC_MEMORY_PROTECTION
7491 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7492#endif
7493#ifdef _SC_MESSAGE_PASSING
7494 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7495#endif
Fred Draked86ed291999-12-15 15:34:33 +00007496#ifdef _SC_MMAP_FIXED_ALIGNMENT
7497 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7498#endif
Fred Drakec9680921999-12-13 16:37:25 +00007499#ifdef _SC_MQ_OPEN_MAX
7500 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7501#endif
7502#ifdef _SC_MQ_PRIO_MAX
7503 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7504#endif
Fred Draked86ed291999-12-15 15:34:33 +00007505#ifdef _SC_NACLS_MAX
7506 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7507#endif
Fred Drakec9680921999-12-13 16:37:25 +00007508#ifdef _SC_NGROUPS_MAX
7509 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7510#endif
7511#ifdef _SC_NL_ARGMAX
7512 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7513#endif
7514#ifdef _SC_NL_LANGMAX
7515 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7516#endif
7517#ifdef _SC_NL_MSGMAX
7518 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7519#endif
7520#ifdef _SC_NL_NMAX
7521 {"SC_NL_NMAX", _SC_NL_NMAX},
7522#endif
7523#ifdef _SC_NL_SETMAX
7524 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7525#endif
7526#ifdef _SC_NL_TEXTMAX
7527 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7528#endif
7529#ifdef _SC_NPROCESSORS_CONF
7530 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7531#endif
7532#ifdef _SC_NPROCESSORS_ONLN
7533 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7534#endif
Fred Draked86ed291999-12-15 15:34:33 +00007535#ifdef _SC_NPROC_CONF
7536 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7537#endif
7538#ifdef _SC_NPROC_ONLN
7539 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7540#endif
Fred Drakec9680921999-12-13 16:37:25 +00007541#ifdef _SC_NZERO
7542 {"SC_NZERO", _SC_NZERO},
7543#endif
7544#ifdef _SC_OPEN_MAX
7545 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7546#endif
7547#ifdef _SC_PAGESIZE
7548 {"SC_PAGESIZE", _SC_PAGESIZE},
7549#endif
7550#ifdef _SC_PAGE_SIZE
7551 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7552#endif
7553#ifdef _SC_PASS_MAX
7554 {"SC_PASS_MAX", _SC_PASS_MAX},
7555#endif
7556#ifdef _SC_PHYS_PAGES
7557 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7558#endif
7559#ifdef _SC_PII
7560 {"SC_PII", _SC_PII},
7561#endif
7562#ifdef _SC_PII_INTERNET
7563 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7564#endif
7565#ifdef _SC_PII_INTERNET_DGRAM
7566 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7567#endif
7568#ifdef _SC_PII_INTERNET_STREAM
7569 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7570#endif
7571#ifdef _SC_PII_OSI
7572 {"SC_PII_OSI", _SC_PII_OSI},
7573#endif
7574#ifdef _SC_PII_OSI_CLTS
7575 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7576#endif
7577#ifdef _SC_PII_OSI_COTS
7578 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7579#endif
7580#ifdef _SC_PII_OSI_M
7581 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7582#endif
7583#ifdef _SC_PII_SOCKET
7584 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7585#endif
7586#ifdef _SC_PII_XTI
7587 {"SC_PII_XTI", _SC_PII_XTI},
7588#endif
7589#ifdef _SC_POLL
7590 {"SC_POLL", _SC_POLL},
7591#endif
7592#ifdef _SC_PRIORITIZED_IO
7593 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7594#endif
7595#ifdef _SC_PRIORITY_SCHEDULING
7596 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7597#endif
7598#ifdef _SC_REALTIME_SIGNALS
7599 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7600#endif
7601#ifdef _SC_RE_DUP_MAX
7602 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7603#endif
7604#ifdef _SC_RTSIG_MAX
7605 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7606#endif
7607#ifdef _SC_SAVED_IDS
7608 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7609#endif
7610#ifdef _SC_SCHAR_MAX
7611 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7612#endif
7613#ifdef _SC_SCHAR_MIN
7614 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7615#endif
7616#ifdef _SC_SELECT
7617 {"SC_SELECT", _SC_SELECT},
7618#endif
7619#ifdef _SC_SEMAPHORES
7620 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7621#endif
7622#ifdef _SC_SEM_NSEMS_MAX
7623 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7624#endif
7625#ifdef _SC_SEM_VALUE_MAX
7626 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7627#endif
7628#ifdef _SC_SHARED_MEMORY_OBJECTS
7629 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7630#endif
7631#ifdef _SC_SHRT_MAX
7632 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7633#endif
7634#ifdef _SC_SHRT_MIN
7635 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7636#endif
7637#ifdef _SC_SIGQUEUE_MAX
7638 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7639#endif
7640#ifdef _SC_SIGRT_MAX
7641 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7642#endif
7643#ifdef _SC_SIGRT_MIN
7644 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7645#endif
Fred Draked86ed291999-12-15 15:34:33 +00007646#ifdef _SC_SOFTPOWER
7647 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7648#endif
Fred Drakec9680921999-12-13 16:37:25 +00007649#ifdef _SC_SPLIT_CACHE
7650 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7651#endif
7652#ifdef _SC_SSIZE_MAX
7653 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7654#endif
7655#ifdef _SC_STACK_PROT
7656 {"SC_STACK_PROT", _SC_STACK_PROT},
7657#endif
7658#ifdef _SC_STREAM_MAX
7659 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7660#endif
7661#ifdef _SC_SYNCHRONIZED_IO
7662 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7663#endif
7664#ifdef _SC_THREADS
7665 {"SC_THREADS", _SC_THREADS},
7666#endif
7667#ifdef _SC_THREAD_ATTR_STACKADDR
7668 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7669#endif
7670#ifdef _SC_THREAD_ATTR_STACKSIZE
7671 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7672#endif
7673#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7674 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7675#endif
7676#ifdef _SC_THREAD_KEYS_MAX
7677 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7678#endif
7679#ifdef _SC_THREAD_PRIORITY_SCHEDULING
7680 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7681#endif
7682#ifdef _SC_THREAD_PRIO_INHERIT
7683 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7684#endif
7685#ifdef _SC_THREAD_PRIO_PROTECT
7686 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7687#endif
7688#ifdef _SC_THREAD_PROCESS_SHARED
7689 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7690#endif
7691#ifdef _SC_THREAD_SAFE_FUNCTIONS
7692 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7693#endif
7694#ifdef _SC_THREAD_STACK_MIN
7695 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7696#endif
7697#ifdef _SC_THREAD_THREADS_MAX
7698 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7699#endif
7700#ifdef _SC_TIMERS
7701 {"SC_TIMERS", _SC_TIMERS},
7702#endif
7703#ifdef _SC_TIMER_MAX
7704 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7705#endif
7706#ifdef _SC_TTY_NAME_MAX
7707 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7708#endif
7709#ifdef _SC_TZNAME_MAX
7710 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7711#endif
7712#ifdef _SC_T_IOV_MAX
7713 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7714#endif
7715#ifdef _SC_UCHAR_MAX
7716 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7717#endif
7718#ifdef _SC_UINT_MAX
7719 {"SC_UINT_MAX", _SC_UINT_MAX},
7720#endif
7721#ifdef _SC_UIO_MAXIOV
7722 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7723#endif
7724#ifdef _SC_ULONG_MAX
7725 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7726#endif
7727#ifdef _SC_USHRT_MAX
7728 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7729#endif
7730#ifdef _SC_VERSION
7731 {"SC_VERSION", _SC_VERSION},
7732#endif
7733#ifdef _SC_WORD_BIT
7734 {"SC_WORD_BIT", _SC_WORD_BIT},
7735#endif
7736#ifdef _SC_XBS5_ILP32_OFF32
7737 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7738#endif
7739#ifdef _SC_XBS5_ILP32_OFFBIG
7740 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7741#endif
7742#ifdef _SC_XBS5_LP64_OFF64
7743 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7744#endif
7745#ifdef _SC_XBS5_LPBIG_OFFBIG
7746 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7747#endif
7748#ifdef _SC_XOPEN_CRYPT
7749 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7750#endif
7751#ifdef _SC_XOPEN_ENH_I18N
7752 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7753#endif
7754#ifdef _SC_XOPEN_LEGACY
7755 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7756#endif
7757#ifdef _SC_XOPEN_REALTIME
7758 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7759#endif
7760#ifdef _SC_XOPEN_REALTIME_THREADS
7761 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7762#endif
7763#ifdef _SC_XOPEN_SHM
7764 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7765#endif
7766#ifdef _SC_XOPEN_UNIX
7767 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7768#endif
7769#ifdef _SC_XOPEN_VERSION
7770 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7771#endif
7772#ifdef _SC_XOPEN_XCU_VERSION
7773 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7774#endif
7775#ifdef _SC_XOPEN_XPG2
7776 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7777#endif
7778#ifdef _SC_XOPEN_XPG3
7779 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7780#endif
7781#ifdef _SC_XOPEN_XPG4
7782 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7783#endif
7784};
7785
7786static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007787conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007788{
7789 return conv_confname(arg, valuep, posix_constants_sysconf,
7790 sizeof(posix_constants_sysconf)
7791 / sizeof(struct constdef));
7792}
7793
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007794PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007795"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007796Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007797
7798static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007799posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007800{
7801 PyObject *result = NULL;
7802 int name;
7803
7804 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7805 int value;
7806
7807 errno = 0;
7808 value = sysconf(name);
7809 if (value == -1 && errno != 0)
7810 posix_error();
7811 else
7812 result = PyInt_FromLong(value);
7813 }
7814 return result;
7815}
7816#endif
7817
7818
Fred Drakebec628d1999-12-15 18:31:10 +00007819/* This code is used to ensure that the tables of configuration value names
7820 * are in sorted order as required by conv_confname(), and also to build the
7821 * the exported dictionaries that are used to publish information about the
7822 * names available on the host platform.
7823 *
7824 * Sorting the table at runtime ensures that the table is properly ordered
7825 * when used, even for platforms we're not able to test on. It also makes
7826 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007827 */
Fred Drakebec628d1999-12-15 18:31:10 +00007828
7829static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007830cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007831{
7832 const struct constdef *c1 =
7833 (const struct constdef *) v1;
7834 const struct constdef *c2 =
7835 (const struct constdef *) v2;
7836
7837 return strcmp(c1->name, c2->name);
7838}
7839
7840static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007841setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007842 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007843{
Fred Drakebec628d1999-12-15 18:31:10 +00007844 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007845 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007846
7847 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7848 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007849 if (d == NULL)
7850 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007851
Barry Warsaw3155db32000-04-13 15:20:40 +00007852 for (i=0; i < tablesize; ++i) {
7853 PyObject *o = PyInt_FromLong(table[i].value);
7854 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7855 Py_XDECREF(o);
7856 Py_DECREF(d);
7857 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007858 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007859 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007860 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007861 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007862}
7863
Fred Drakebec628d1999-12-15 18:31:10 +00007864/* Return -1 on failure, 0 on success. */
7865static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007866setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007867{
7868#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007869 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007870 sizeof(posix_constants_pathconf)
7871 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007872 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007873 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007874#endif
7875#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007876 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007877 sizeof(posix_constants_confstr)
7878 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007879 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007880 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007881#endif
7882#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007883 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007884 sizeof(posix_constants_sysconf)
7885 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007886 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007887 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007888#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007889 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007890}
Fred Draked86ed291999-12-15 15:34:33 +00007891
7892
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007893PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007894"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007895Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007896in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007897
7898static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007899posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007900{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007901 abort();
7902 /*NOTREACHED*/
7903 Py_FatalError("abort() called from Python code didn't abort!");
7904 return NULL;
7905}
Fred Drakebec628d1999-12-15 18:31:10 +00007906
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007907#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007908PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007909"startfile(filepath [, operation]) - Start a file with its associated\n\
7910application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007911\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007912When \"operation\" is not specified or \"open\", this acts like\n\
7913double-clicking the file in Explorer, or giving the file name as an\n\
7914argument to the DOS \"start\" command: the file is opened with whatever\n\
7915application (if any) its extension is associated.\n\
7916When another \"operation\" is given, it specifies what should be done with\n\
7917the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007918\n\
7919startfile returns as soon as the associated application is launched.\n\
7920There is no option to wait for the application to close, and no way\n\
7921to retrieve the application's exit status.\n\
7922\n\
7923The filepath is relative to the current directory. If you want to use\n\
7924an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007925the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007926
7927static PyObject *
7928win32_startfile(PyObject *self, PyObject *args)
7929{
7930 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00007931 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007932 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007933#ifdef Py_WIN_WIDE_FILENAMES
7934 if (unicode_file_names()) {
7935 PyObject *unipath, *woperation = NULL;
7936 if (!PyArg_ParseTuple(args, "U|s:startfile",
7937 &unipath, &operation)) {
7938 PyErr_Clear();
7939 goto normal;
7940 }
7941
7942
7943 if (operation) {
7944 woperation = PyUnicode_DecodeASCII(operation,
7945 strlen(operation), NULL);
7946 if (!woperation) {
7947 PyErr_Clear();
7948 operation = NULL;
7949 goto normal;
7950 }
7951 }
7952
7953 Py_BEGIN_ALLOW_THREADS
7954 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7955 PyUnicode_AS_UNICODE(unipath),
7956 NULL, NULL, SW_SHOWNORMAL);
7957 Py_END_ALLOW_THREADS
7958
7959 Py_XDECREF(woperation);
7960 if (rc <= (HINSTANCE)32) {
7961 PyObject *errval = win32_error_unicode("startfile",
7962 PyUnicode_AS_UNICODE(unipath));
7963 return errval;
7964 }
7965 Py_INCREF(Py_None);
7966 return Py_None;
7967 }
7968#endif
7969
7970normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00007971 if (!PyArg_ParseTuple(args, "et|s:startfile",
7972 Py_FileSystemDefaultEncoding, &filepath,
7973 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00007974 return NULL;
7975 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00007976 rc = ShellExecute((HWND)0, operation, filepath,
7977 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007978 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00007979 if (rc <= (HINSTANCE)32) {
7980 PyObject *errval = win32_error("startfile", filepath);
7981 PyMem_Free(filepath);
7982 return errval;
7983 }
7984 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007985 Py_INCREF(Py_None);
7986 return Py_None;
7987}
7988#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007989
Martin v. Löwis438b5342002-12-27 10:16:42 +00007990#ifdef HAVE_GETLOADAVG
7991PyDoc_STRVAR(posix_getloadavg__doc__,
7992"getloadavg() -> (float, float, float)\n\n\
7993Return the number of processes in the system run queue averaged over\n\
7994the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7995was unobtainable");
7996
7997static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007998posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007999{
8000 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008001 if (getloadavg(loadavg, 3)!=3) {
8002 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8003 return NULL;
8004 } else
8005 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
8006}
8007#endif
8008
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008009#ifdef MS_WINDOWS
8010
8011PyDoc_STRVAR(win32_urandom__doc__,
8012"urandom(n) -> str\n\n\
8013Return a string of n random bytes suitable for cryptographic use.");
8014
8015typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8016 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8017 DWORD dwFlags );
8018typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8019 BYTE *pbBuffer );
8020
8021static CRYPTGENRANDOM pCryptGenRandom = NULL;
8022static HCRYPTPROV hCryptProv = 0;
8023
Tim Peters4ad82172004-08-30 17:02:04 +00008024static PyObject*
8025win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008026{
Tim Petersd3115382004-08-30 17:36:46 +00008027 int howMany;
8028 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008029
Tim Peters4ad82172004-08-30 17:02:04 +00008030 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00008031 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00008032 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00008033 if (howMany < 0)
8034 return PyErr_Format(PyExc_ValueError,
8035 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008036
Tim Peters4ad82172004-08-30 17:02:04 +00008037 if (hCryptProv == 0) {
8038 HINSTANCE hAdvAPI32 = NULL;
8039 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008040
Tim Peters4ad82172004-08-30 17:02:04 +00008041 /* Obtain handle to the DLL containing CryptoAPI
8042 This should not fail */
8043 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8044 if(hAdvAPI32 == NULL)
8045 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008046
Tim Peters4ad82172004-08-30 17:02:04 +00008047 /* Obtain pointers to the CryptoAPI functions
8048 This will fail on some early versions of Win95 */
8049 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8050 hAdvAPI32,
8051 "CryptAcquireContextA");
8052 if (pCryptAcquireContext == NULL)
8053 return PyErr_Format(PyExc_NotImplementedError,
8054 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008055
Tim Peters4ad82172004-08-30 17:02:04 +00008056 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8057 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00008058 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00008059 return PyErr_Format(PyExc_NotImplementedError,
8060 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008061
Tim Peters4ad82172004-08-30 17:02:04 +00008062 /* Acquire context */
8063 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8064 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8065 return win32_error("CryptAcquireContext", NULL);
8066 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008067
Tim Peters4ad82172004-08-30 17:02:04 +00008068 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00008069 result = PyString_FromStringAndSize(NULL, howMany);
8070 if (result != NULL) {
8071 /* Get random data */
8072 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8073 PyString_AS_STRING(result))) {
8074 Py_DECREF(result);
8075 return win32_error("CryptGenRandom", NULL);
8076 }
Tim Peters4ad82172004-08-30 17:02:04 +00008077 }
Tim Petersd3115382004-08-30 17:36:46 +00008078 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008079}
8080#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008081
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008082#ifdef __VMS
8083/* Use openssl random routine */
8084#include <openssl/rand.h>
8085PyDoc_STRVAR(vms_urandom__doc__,
8086"urandom(n) -> str\n\n\
8087Return a string of n random bytes suitable for cryptographic use.");
8088
8089static PyObject*
8090vms_urandom(PyObject *self, PyObject *args)
8091{
8092 int howMany;
8093 PyObject* result;
8094
8095 /* Read arguments */
8096 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8097 return NULL;
8098 if (howMany < 0)
8099 return PyErr_Format(PyExc_ValueError,
8100 "negative argument not allowed");
8101
8102 /* Allocate bytes */
8103 result = PyString_FromStringAndSize(NULL, howMany);
8104 if (result != NULL) {
8105 /* Get random data */
8106 if (RAND_pseudo_bytes((unsigned char*)
8107 PyString_AS_STRING(result),
8108 howMany) < 0) {
8109 Py_DECREF(result);
8110 return PyErr_Format(PyExc_ValueError,
8111 "RAND_pseudo_bytes");
8112 }
8113 }
8114 return result;
8115}
8116#endif
8117
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008118static PyMethodDef posix_methods[] = {
8119 {"access", posix_access, METH_VARARGS, posix_access__doc__},
8120#ifdef HAVE_TTYNAME
8121 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
8122#endif
8123 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00008124#ifdef HAVE_CHFLAGS
8125 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
8126#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008127 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008128#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008129 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008130#endif /* HAVE_CHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00008131#ifdef HAVE_LCHFLAGS
8132 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
8133#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008134#ifdef HAVE_LCHOWN
8135 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
8136#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008137#ifdef HAVE_CHROOT
8138 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
8139#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008140#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00008141 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008142#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008143#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00008144 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008145#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00008146 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008147#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008148#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008149#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008150 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008151#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008152 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8153 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8154 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008155#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008156 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008157#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008158#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008159 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008160#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008161 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8162 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8163 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00008164 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008165#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008166 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008167#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008168#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008169 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008170#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008171 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008172#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00008173 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008174#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008175 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8176 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8177 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008178#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00008179 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008180#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008181 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008182#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008183 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8184 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008185#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008186#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008187 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8188 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008189#if defined(PYOS_OS2)
8190 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8191 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
8192#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008193#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008194#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00008195 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008196#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008197#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00008198 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008199#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008200#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00008201 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008202#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008203#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00008204 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008205#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008206#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008207 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008208#endif /* HAVE_GETEGID */
8209#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008210 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008211#endif /* HAVE_GETEUID */
8212#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008213 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008214#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008215#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00008216 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008217#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008218 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008219#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008220 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008221#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008222#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00008223 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008224#endif /* HAVE_GETPPID */
8225#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008226 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008227#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008228#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00008229 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008230#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008231#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008232 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008233#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008234#ifdef HAVE_KILLPG
8235 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
8236#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008237#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008238 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008239#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008240#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008241 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008242#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008243 {"popen2", win32_popen2, METH_VARARGS},
8244 {"popen3", win32_popen3, METH_VARARGS},
8245 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00008246 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008247#else
8248#if defined(PYOS_OS2) && defined(PYCC_GCC)
8249 {"popen2", os2emx_popen2, METH_VARARGS},
8250 {"popen3", os2emx_popen3, METH_VARARGS},
8251 {"popen4", os2emx_popen4, METH_VARARGS},
8252#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008253#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008254#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008255#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008256 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008257#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008258#ifdef HAVE_SETEUID
8259 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8260#endif /* HAVE_SETEUID */
8261#ifdef HAVE_SETEGID
8262 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8263#endif /* HAVE_SETEGID */
8264#ifdef HAVE_SETREUID
8265 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8266#endif /* HAVE_SETREUID */
8267#ifdef HAVE_SETREGID
8268 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8269#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008270#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008271 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008272#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008273#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00008274 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008275#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008276#ifdef HAVE_GETPGID
8277 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8278#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008279#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008280 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008281#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008282#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00008283 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008284#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008285#ifdef HAVE_WAIT3
8286 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8287#endif /* HAVE_WAIT3 */
8288#ifdef HAVE_WAIT4
8289 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8290#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008291#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008292 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008293#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008294#ifdef HAVE_GETSID
8295 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8296#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008297#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00008298 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008299#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008300#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008301 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008302#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008303#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008304 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008305#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008306#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008307 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008308#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008309 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8310 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8311 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8312 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8313 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8314 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8315 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8316 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8317 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00008318 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008319#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00008320 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008321#endif
8322#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008323 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008324#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008325#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008326 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8327#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008328#ifdef HAVE_DEVICE_MACROS
8329 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8330 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8331 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8332#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008333#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008334 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008335#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008336#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008337 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008338#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008339#ifdef HAVE_UNSETENV
8340 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8341#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00008342#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008343 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00008344#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00008345#ifdef HAVE_FCHDIR
8346 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8347#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008348#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008349 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008350#endif
8351#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008352 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008353#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008354#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008355#ifdef WCOREDUMP
8356 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8357#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008358#ifdef WIFCONTINUED
8359 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8360#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008361#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008362 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008363#endif /* WIFSTOPPED */
8364#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008365 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008366#endif /* WIFSIGNALED */
8367#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008368 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008369#endif /* WIFEXITED */
8370#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008371 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008372#endif /* WEXITSTATUS */
8373#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008374 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008375#endif /* WTERMSIG */
8376#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008377 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008378#endif /* WSTOPSIG */
8379#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008380#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008381 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008382#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008383#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008384 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008385#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008386#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00008387 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008388#endif
8389#ifdef HAVE_TEMPNAM
8390 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8391#endif
8392#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00008393 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008394#endif
Fred Drakec9680921999-12-13 16:37:25 +00008395#ifdef HAVE_CONFSTR
8396 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8397#endif
8398#ifdef HAVE_SYSCONF
8399 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8400#endif
8401#ifdef HAVE_FPATHCONF
8402 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8403#endif
8404#ifdef HAVE_PATHCONF
8405 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8406#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008407 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008408#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00008409 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8410#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008411#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00008412 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008413#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008414 #ifdef MS_WINDOWS
8415 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8416 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008417 #ifdef __VMS
8418 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
8419 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008420 {NULL, NULL} /* Sentinel */
8421};
8422
8423
Barry Warsaw4a342091996-12-19 23:50:02 +00008424static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008425ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008426{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008427 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008428}
8429
Guido van Rossumd48f2521997-12-05 22:19:34 +00008430#if defined(PYOS_OS2)
8431/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008432static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008433{
8434 APIRET rc;
8435 ULONG values[QSV_MAX+1];
8436 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008437 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008438
8439 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008440 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008441 Py_END_ALLOW_THREADS
8442
8443 if (rc != NO_ERROR) {
8444 os2_error(rc);
8445 return -1;
8446 }
8447
Fred Drake4d1e64b2002-04-15 19:40:07 +00008448 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8449 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8450 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8451 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8452 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8453 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8454 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008455
8456 switch (values[QSV_VERSION_MINOR]) {
8457 case 0: ver = "2.00"; break;
8458 case 10: ver = "2.10"; break;
8459 case 11: ver = "2.11"; break;
8460 case 30: ver = "3.00"; break;
8461 case 40: ver = "4.00"; break;
8462 case 50: ver = "5.00"; break;
8463 default:
Tim Peters885d4572001-11-28 20:27:42 +00008464 PyOS_snprintf(tmp, sizeof(tmp),
8465 "%d-%d", values[QSV_VERSION_MAJOR],
8466 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008467 ver = &tmp[0];
8468 }
8469
8470 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008471 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008472 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008473
8474 /* Add Indicator of Which Drive was Used to Boot the System */
8475 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8476 tmp[1] = ':';
8477 tmp[2] = '\0';
8478
Fred Drake4d1e64b2002-04-15 19:40:07 +00008479 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008480}
8481#endif
8482
Barry Warsaw4a342091996-12-19 23:50:02 +00008483static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008484all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008485{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008486#ifdef F_OK
8487 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008488#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008489#ifdef R_OK
8490 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008491#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008492#ifdef W_OK
8493 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008494#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008495#ifdef X_OK
8496 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008497#endif
Fred Drakec9680921999-12-13 16:37:25 +00008498#ifdef NGROUPS_MAX
8499 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8500#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008501#ifdef TMP_MAX
8502 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8503#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008504#ifdef WCONTINUED
8505 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8506#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008507#ifdef WNOHANG
8508 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008509#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008510#ifdef WUNTRACED
8511 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8512#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008513#ifdef O_RDONLY
8514 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8515#endif
8516#ifdef O_WRONLY
8517 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8518#endif
8519#ifdef O_RDWR
8520 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8521#endif
8522#ifdef O_NDELAY
8523 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8524#endif
8525#ifdef O_NONBLOCK
8526 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8527#endif
8528#ifdef O_APPEND
8529 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8530#endif
8531#ifdef O_DSYNC
8532 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8533#endif
8534#ifdef O_RSYNC
8535 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8536#endif
8537#ifdef O_SYNC
8538 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8539#endif
8540#ifdef O_NOCTTY
8541 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8542#endif
8543#ifdef O_CREAT
8544 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8545#endif
8546#ifdef O_EXCL
8547 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8548#endif
8549#ifdef O_TRUNC
8550 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8551#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008552#ifdef O_BINARY
8553 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8554#endif
8555#ifdef O_TEXT
8556 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8557#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008558#ifdef O_LARGEFILE
8559 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8560#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008561#ifdef O_SHLOCK
8562 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8563#endif
8564#ifdef O_EXLOCK
8565 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8566#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008567
Tim Peters5aa91602002-01-30 05:46:57 +00008568/* MS Windows */
8569#ifdef O_NOINHERIT
8570 /* Don't inherit in child processes. */
8571 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8572#endif
8573#ifdef _O_SHORT_LIVED
8574 /* Optimize for short life (keep in memory). */
8575 /* MS forgot to define this one with a non-underscore form too. */
8576 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8577#endif
8578#ifdef O_TEMPORARY
8579 /* Automatically delete when last handle is closed. */
8580 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8581#endif
8582#ifdef O_RANDOM
8583 /* Optimize for random access. */
8584 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8585#endif
8586#ifdef O_SEQUENTIAL
8587 /* Optimize for sequential access. */
8588 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8589#endif
8590
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008591/* GNU extensions. */
8592#ifdef O_DIRECT
8593 /* Direct disk access. */
8594 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8595#endif
8596#ifdef O_DIRECTORY
8597 /* Must be a directory. */
8598 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8599#endif
8600#ifdef O_NOFOLLOW
8601 /* Do not follow links. */
8602 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8603#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008604
Barry Warsaw5676bd12003-01-07 20:57:09 +00008605 /* These come from sysexits.h */
8606#ifdef EX_OK
8607 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008608#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008609#ifdef EX_USAGE
8610 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008611#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008612#ifdef EX_DATAERR
8613 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008614#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008615#ifdef EX_NOINPUT
8616 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008617#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008618#ifdef EX_NOUSER
8619 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008620#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008621#ifdef EX_NOHOST
8622 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008623#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008624#ifdef EX_UNAVAILABLE
8625 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008626#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008627#ifdef EX_SOFTWARE
8628 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008629#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008630#ifdef EX_OSERR
8631 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008632#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008633#ifdef EX_OSFILE
8634 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008635#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008636#ifdef EX_CANTCREAT
8637 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008638#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008639#ifdef EX_IOERR
8640 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008641#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008642#ifdef EX_TEMPFAIL
8643 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008644#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008645#ifdef EX_PROTOCOL
8646 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008647#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008648#ifdef EX_NOPERM
8649 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008650#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008651#ifdef EX_CONFIG
8652 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008653#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008654#ifdef EX_NOTFOUND
8655 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008656#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008657
Guido van Rossum246bc171999-02-01 23:54:31 +00008658#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008659#if defined(PYOS_OS2) && defined(PYCC_GCC)
8660 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8661 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8662 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8663 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8664 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8665 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8666 if (ins(d, "P_PM", (long)P_PM)) return -1;
8667 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8668 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8669 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8670 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8671 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8672 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8673 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8674 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8675 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8676 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8677 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8678 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8679 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8680#else
Guido van Rossum7d385291999-02-16 19:38:04 +00008681 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8682 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8683 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8684 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8685 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008686#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008687#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008688
Guido van Rossumd48f2521997-12-05 22:19:34 +00008689#if defined(PYOS_OS2)
8690 if (insertvalues(d)) return -1;
8691#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008692 return 0;
8693}
8694
8695
Tim Peters5aa91602002-01-30 05:46:57 +00008696#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008697#define INITFUNC initnt
8698#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008699
8700#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008701#define INITFUNC initos2
8702#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008703
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008704#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008705#define INITFUNC initposix
8706#define MODNAME "posix"
8707#endif
8708
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008709PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008710INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008711{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008712 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008713
Fred Drake4d1e64b2002-04-15 19:40:07 +00008714 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008715 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008716 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00008717 if (m == NULL)
8718 return;
Tim Peters5aa91602002-01-30 05:46:57 +00008719
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008720 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008721 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00008722 Py_XINCREF(v);
8723 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008724 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00008725 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008726
Fred Drake4d1e64b2002-04-15 19:40:07 +00008727 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00008728 return;
8729
Fred Drake4d1e64b2002-04-15 19:40:07 +00008730 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00008731 return;
8732
Fred Drake4d1e64b2002-04-15 19:40:07 +00008733 Py_INCREF(PyExc_OSError);
8734 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008735
Guido van Rossumb3d39562000-01-31 18:41:26 +00008736#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00008737 if (posix_putenv_garbage == NULL)
8738 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008739#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008740
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008741 if (!initialized) {
8742 stat_result_desc.name = MODNAME ".stat_result";
8743 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8744 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8745 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8746 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8747 structseq_new = StatResultType.tp_new;
8748 StatResultType.tp_new = statresult_new;
8749
8750 statvfs_result_desc.name = MODNAME ".statvfs_result";
8751 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
8752 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008753 Py_INCREF((PyObject*) &StatResultType);
8754 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00008755 Py_INCREF((PyObject*) &StatVFSResultType);
8756 PyModule_AddObject(m, "statvfs_result",
8757 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008758 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008759
8760#ifdef __APPLE__
8761 /*
8762 * Step 2 of weak-linking support on Mac OS X.
8763 *
8764 * The code below removes functions that are not available on the
8765 * currently active platform.
8766 *
8767 * This block allow one to use a python binary that was build on
8768 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8769 * OSX 10.4.
8770 */
8771#ifdef HAVE_FSTATVFS
8772 if (fstatvfs == NULL) {
8773 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8774 return;
8775 }
8776 }
8777#endif /* HAVE_FSTATVFS */
8778
8779#ifdef HAVE_STATVFS
8780 if (statvfs == NULL) {
8781 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8782 return;
8783 }
8784 }
8785#endif /* HAVE_STATVFS */
8786
8787# ifdef HAVE_LCHOWN
8788 if (lchown == NULL) {
8789 if (PyObject_DelAttrString(m, "lchown") == -1) {
8790 return;
8791 }
8792 }
8793#endif /* HAVE_LCHOWN */
8794
8795
8796#endif /* __APPLE__ */
8797
Guido van Rossumb6775db1994-08-01 11:34:53 +00008798}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008799
8800#ifdef __cplusplus
8801}
8802#endif
8803
Thomas Wouters89f507f2006-12-13 04:49:30 +00008804