blob: 5fcc521d7a478fbeb643f79a0c2408461faebdf8 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Ronald Oussorend06b6f22006-04-23 11:59:25 +000016#ifdef __APPLE__
17 /*
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Anthony Baxterac6bd462006-04-13 02:06:09 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047#ifndef Py_USING_UNICODE
48/* This is used in signatures of functions. */
49#define Py_UNICODE void
50#endif
51
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000052#if defined(PYOS_OS2)
53#define INCL_DOS
54#define INCL_DOSERRORS
55#define INCL_DOSPROCESS
56#define INCL_NOPMAPI
57#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#if defined(PYCC_GCC)
59#include <ctype.h>
60#include <io.h>
61#include <stdio.h>
62#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000063#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000064#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000065#endif
66
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000067#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/types.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000069#endif /* HAVE_SYS_TYPES_H */
70
71#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000072#include <sys/stat.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000073#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000074
Guido van Rossum36bc6801995-06-14 22:54:23 +000075#ifdef HAVE_SYS_WAIT_H
76#include <sys/wait.h> /* For WNOHANG */
77#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000078
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000079#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000080#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000081#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000082
Guido van Rossumb6775db1994-08-01 11:34:53 +000083#ifdef HAVE_FCNTL_H
84#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000085#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000086
Guido van Rossuma6535fd2001-10-18 19:44:10 +000087#ifdef HAVE_GRP_H
88#include <grp.h>
89#endif
90
Barry Warsaw5676bd12003-01-07 20:57:09 +000091#ifdef HAVE_SYSEXITS_H
92#include <sysexits.h>
93#endif /* HAVE_SYSEXITS_H */
94
Anthony Baxter8a560de2004-10-13 15:30:56 +000095#ifdef HAVE_SYS_LOADAVG_H
96#include <sys/loadavg.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
107#define HAVE_SYSTEM 1
108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
114#ifdef __BORLANDC__ /* Borland compiler */
115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
119#define HAVE_POPEN 1
120#define HAVE_SYSTEM 1
121#define HAVE_WAIT 1
122#else
123#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000124#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000125#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_EXECV 1
127#define HAVE_PIPE 1
128#define HAVE_POPEN 1
129#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000130#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000131#define HAVE_FSYNC 1
132#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000133#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000134#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
135/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136#else /* all other compilers */
137/* Unix functions that the configure script doesn't check for */
138#define HAVE_EXECV 1
139#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000140#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
141#define HAVE_FORK1 1
142#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000143#define HAVE_GETCWD 1
144#define HAVE_GETEGID 1
145#define HAVE_GETEUID 1
146#define HAVE_GETGID 1
147#define HAVE_GETPPID 1
148#define HAVE_GETUID 1
149#define HAVE_KILL 1
150#define HAVE_OPENDIR 1
151#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000152#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000154#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000155#define HAVE_SYSTEM 1
156#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000157#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000158#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#endif /* _MSC_VER */
160#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000161#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000162#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000163
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000164#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000165
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000166#if defined(__sgi)&&_COMPILER_VERSION>=700
167/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
168 (default) */
169extern char *ctermid_r(char *);
170#endif
171
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000172#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000173#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000175#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000176#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000177extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000180#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#endif
182#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000183extern int chdir(char *);
184extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000185#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000186extern int chdir(const char *);
187extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000188#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000189#ifdef __BORLANDC__
190extern int chmod(const char *, int);
191#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000192extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000193#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000194extern int chown(const char *, uid_t, gid_t);
195extern char *getcwd(char *, int);
196extern char *strerror(int);
197extern int link(const char *, const char *);
198extern int rename(const char *, const char *);
199extern int stat(const char *, struct stat *);
200extern int unlink(const char *);
201extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000203extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000204#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000206extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000207#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000209
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000210#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#ifdef HAVE_UTIME_H
213#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000216#ifdef HAVE_SYS_UTIME_H
217#include <sys/utime.h>
218#define HAVE_UTIME_H /* pretend we do for the rest of this file */
219#endif /* HAVE_SYS_UTIME_H */
220
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#ifdef HAVE_SYS_TIMES_H
222#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000223#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224
225#ifdef HAVE_SYS_PARAM_H
226#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000227#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228
229#ifdef HAVE_SYS_UTSNAME_H
230#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000231#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#define NAMLEN(dirent) strlen((dirent)->d_name)
236#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000237#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#include <direct.h>
239#define NAMLEN(dirent) strlen((dirent)->d_name)
240#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000243#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000244#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000249#endif
250#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000255#ifdef _MSC_VER
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000256#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <direct.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000258#endif
259#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <io.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000261#endif
262#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <process.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000264#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000265#include "osdefs.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000267#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000268#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000269#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000270#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000271
Guido van Rossumd48f2521997-12-05 22:19:34 +0000272#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000273#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000274#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275
Tim Petersbc2e10e2002-03-03 23:17:02 +0000276#ifndef MAXPATHLEN
Thomas Wouters1ddba602006-04-25 15:29:46 +0000277#if defined(PATH_MAX) && PATH_MAX > 1024
278#define MAXPATHLEN PATH_MAX
279#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000280#define MAXPATHLEN 1024
Thomas Wouters1ddba602006-04-25 15:29:46 +0000281#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000282#endif /* MAXPATHLEN */
283
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000284#ifdef UNION_WAIT
285/* Emulate some macros on systems that have a union instead of macros */
286
287#ifndef WIFEXITED
288#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
289#endif
290
291#ifndef WEXITSTATUS
292#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
293#endif
294
295#ifndef WTERMSIG
296#define WTERMSIG(u_wait) ((u_wait).w_termsig)
297#endif
298
Neal Norwitzd5a37542006-03-20 06:48:34 +0000299#define WAIT_TYPE union wait
300#define WAIT_STATUS_INT(s) (s.w_status)
301
302#else /* !UNION_WAIT */
303#define WAIT_TYPE int
304#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000305#endif /* UNION_WAIT */
306
Greg Wardb48bc172000-03-01 21:51:56 +0000307/* Don't use the "_r" form if we don't need it (also, won't have a
308 prototype for it, at least on Solaris -- maybe others as well?). */
309#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
310#define USE_CTERMID_R
311#endif
312
313#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
314#define USE_TMPNAM_R
315#endif
316
Fred Drake699f3522000-06-29 21:12:41 +0000317/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000318#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000319#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000320# define STAT win32_stat
321# define FSTAT win32_fstat
322# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000323#else
324# define STAT stat
325# define FSTAT fstat
326# define STRUCT_STAT struct stat
327#endif
328
Tim Peters11b23062003-04-23 02:39:17 +0000329#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000330#include <sys/mkdev.h>
331#else
332#if defined(MAJOR_IN_SYSMACROS)
333#include <sys/sysmacros.h>
334#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000335#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
336#include <sys/mkdev.h>
337#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000338#endif
Fred Drake699f3522000-06-29 21:12:41 +0000339
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000341#ifdef WITH_NEXT_FRAMEWORK
342/* On Darwin/MacOSX a shared library or framework has no access to
343** environ directly, we must obtain it with _NSGetEnviron().
344*/
345#include <crt_externs.h>
346static char **environ;
347#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000349#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350
Barry Warsaw53699e91996-12-10 23:23:01 +0000351static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000352convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000353{
Barry Warsaw53699e91996-12-10 23:23:01 +0000354 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000356 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357 if (d == NULL)
358 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000359#ifdef WITH_NEXT_FRAMEWORK
360 if (environ == NULL)
361 environ = *_NSGetEnviron();
362#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363 if (environ == NULL)
364 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000365 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000367 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000368 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369 char *p = strchr(*e, '=');
370 if (p == NULL)
371 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000372 k = PyString_FromStringAndSize(*e, (int)(p-*e));
373 if (k == NULL) {
374 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000376 }
377 v = PyString_FromString(p+1);
378 if (v == NULL) {
379 PyErr_Clear();
380 Py_DECREF(k);
381 continue;
382 }
383 if (PyDict_GetItem(d, k) == NULL) {
384 if (PyDict_SetItem(d, k, v) != 0)
385 PyErr_Clear();
386 }
387 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000388 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000390#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000391 {
392 APIRET rc;
393 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
394
395 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000396 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000397 PyObject *v = PyString_FromString(buffer);
398 PyDict_SetItemString(d, "BEGINLIBPATH", v);
399 Py_DECREF(v);
400 }
401 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
402 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
403 PyObject *v = PyString_FromString(buffer);
404 PyDict_SetItemString(d, "ENDLIBPATH", v);
405 Py_DECREF(v);
406 }
407 }
408#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409 return d;
410}
411
412
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413/* Set a POSIX-specific error from errno, and return NULL */
414
Barry Warsawd58d7641998-07-23 16:14:40 +0000415static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000416posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000417{
Barry Warsawca74da41999-02-09 19:31:45 +0000418 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419}
Barry Warsawd58d7641998-07-23 16:14:40 +0000420static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000421posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000422{
Barry Warsawca74da41999-02-09 19:31:45 +0000423 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000424}
425
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000426#ifdef Py_WIN_WIDE_FILENAMES
427static PyObject *
428posix_error_with_unicode_filename(Py_UNICODE* name)
429{
430 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
431}
432#endif /* Py_WIN_WIDE_FILENAMES */
433
434
Mark Hammondef8b6542001-05-13 08:04:26 +0000435static PyObject *
436posix_error_with_allocated_filename(char* name)
437{
438 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
439 PyMem_Free(name);
440 return rc;
441}
442
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000443#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000444static PyObject *
445win32_error(char* function, char* filename)
446{
Mark Hammond33a6da92000-08-15 00:46:38 +0000447 /* XXX We should pass the function name along in the future.
448 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000449 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000450 Windows error object, which is non-trivial.
451 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000452 errno = GetLastError();
453 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000454 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000455 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000456 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000457}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000458
459#ifdef Py_WIN_WIDE_FILENAMES
460static PyObject *
461win32_error_unicode(char* function, Py_UNICODE* filename)
462{
463 /* XXX - see win32_error for comments on 'function' */
464 errno = GetLastError();
465 if (filename)
466 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
467 else
468 return PyErr_SetFromWindowsErr(errno);
469}
470
471static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
472{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000473}
474
475/* Function suitable for O& conversion */
476static int
477convert_to_unicode(PyObject *arg, void* _param)
478{
479 PyObject **param = (PyObject**)_param;
480 if (PyUnicode_CheckExact(arg)) {
481 Py_INCREF(arg);
482 *param = arg;
483 }
484 else if (PyUnicode_Check(arg)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000485 /* For a Unicode subtype that's not a Unicode object,
486 return a true Unicode object with the same data. */
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000487 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
488 PyUnicode_GET_SIZE(arg));
489 return *param != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000490 }
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000491 else
492 *param = PyUnicode_FromEncodedObject(arg,
493 Py_FileSystemDefaultEncoding,
494 "strict");
495 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000496}
497
498#endif /* Py_WIN_WIDE_FILENAMES */
499
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000500#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000501
Guido van Rossumd48f2521997-12-05 22:19:34 +0000502#if defined(PYOS_OS2)
503/**********************************************************************
504 * Helper Function to Trim and Format OS/2 Messages
505 **********************************************************************/
506 static void
507os2_formatmsg(char *msgbuf, int msglen, char *reason)
508{
509 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
510
511 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
512 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
513
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000514 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000515 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
516 }
517
518 /* Add Optional Reason Text */
519 if (reason) {
520 strcat(msgbuf, " : ");
521 strcat(msgbuf, reason);
522 }
523}
524
525/**********************************************************************
526 * Decode an OS/2 Operating System Error Code
527 *
528 * A convenience function to lookup an OS/2 error code and return a
529 * text message we can use to raise a Python exception.
530 *
531 * Notes:
532 * The messages for errors returned from the OS/2 kernel reside in
533 * the file OSO001.MSG in the \OS2 directory hierarchy.
534 *
535 **********************************************************************/
536 static char *
537os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
538{
539 APIRET rc;
540 ULONG msglen;
541
542 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
543 Py_BEGIN_ALLOW_THREADS
544 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
545 errorcode, "oso001.msg", &msglen);
546 Py_END_ALLOW_THREADS
547
548 if (rc == NO_ERROR)
549 os2_formatmsg(msgbuf, msglen, reason);
550 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000551 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000552 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000553
554 return msgbuf;
555}
556
557/* Set an OS/2-specific error and return NULL. OS/2 kernel
558 errors are not in a global variable e.g. 'errno' nor are
559 they congruent with posix error numbers. */
560
561static PyObject * os2_error(int code)
562{
563 char text[1024];
564 PyObject *v;
565
566 os2_strerror(text, sizeof(text), code, "");
567
568 v = Py_BuildValue("(is)", code, text);
569 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000570 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000571 Py_DECREF(v);
572 }
573 return NULL; /* Signal to Python that an Exception is Pending */
574}
575
576#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577
578/* POSIX generic methods */
579
Barry Warsaw53699e91996-12-10 23:23:01 +0000580static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000581posix_fildes(PyObject *fdobj, int (*func)(int))
582{
583 int fd;
584 int res;
585 fd = PyObject_AsFileDescriptor(fdobj);
586 if (fd < 0)
587 return NULL;
588 Py_BEGIN_ALLOW_THREADS
589 res = (*func)(fd);
590 Py_END_ALLOW_THREADS
591 if (res < 0)
592 return posix_error();
593 Py_INCREF(Py_None);
594 return Py_None;
595}
Guido van Rossum21142a01999-01-08 21:05:37 +0000596
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000597#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000598static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000599unicode_file_names(void)
600{
601 static int canusewide = -1;
602 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000603 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000604 the Windows NT family. */
605 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
606 }
607 return canusewide;
608}
609#endif
Tim Peters11b23062003-04-23 02:39:17 +0000610
Guido van Rossum21142a01999-01-08 21:05:37 +0000611static PyObject *
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000612posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000613{
Mark Hammondef8b6542001-05-13 08:04:26 +0000614 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000615 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000616 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000617 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000618 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000619 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000620 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000621 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000622 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000623 return posix_error_with_allocated_filename(path1);
624 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000625 Py_INCREF(Py_None);
626 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000627}
628
Barry Warsaw53699e91996-12-10 23:23:01 +0000629static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000630posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000631 char *format,
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000632 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000633{
Mark Hammondef8b6542001-05-13 08:04:26 +0000634 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000635 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000636 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000637 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000638 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000640 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000641 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000642 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000643 PyMem_Free(path1);
644 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000645 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000646 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000648 Py_INCREF(Py_None);
649 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000650}
651
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000652#ifdef Py_WIN_WIDE_FILENAMES
653static PyObject*
654win32_1str(PyObject* args, char* func,
655 char* format, BOOL (__stdcall *funcA)(LPCSTR),
656 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
657{
658 PyObject *uni;
659 char *ansi;
660 BOOL result;
661 if (unicode_file_names()) {
662 if (!PyArg_ParseTuple(args, wformat, &uni))
663 PyErr_Clear();
664 else {
665 Py_BEGIN_ALLOW_THREADS
666 result = funcW(PyUnicode_AsUnicode(uni));
667 Py_END_ALLOW_THREADS
668 if (!result)
669 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
670 Py_INCREF(Py_None);
671 return Py_None;
672 }
673 }
674 if (!PyArg_ParseTuple(args, format, &ansi))
675 return NULL;
676 Py_BEGIN_ALLOW_THREADS
677 result = funcA(ansi);
678 Py_END_ALLOW_THREADS
679 if (!result)
680 return win32_error(func, ansi);
681 Py_INCREF(Py_None);
682 return Py_None;
683
684}
685
686/* This is a reimplementation of the C library's chdir function,
687 but one that produces Win32 errors instead of DOS error codes.
688 chdir is essentially a wrapper around SetCurrentDirectory; however,
689 it also needs to set "magic" environment variables indicating
690 the per-drive current directory, which are of the form =<drive>: */
691BOOL __stdcall
692win32_chdir(LPCSTR path)
693{
694 char new_path[MAX_PATH+1];
695 int result;
696 char env[4] = "=x:";
697
698 if(!SetCurrentDirectoryA(path))
699 return FALSE;
700 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
701 if (!result)
702 return FALSE;
703 /* In the ANSI API, there should not be any paths longer
704 than MAX_PATH. */
705 assert(result <= MAX_PATH+1);
706 if (strncmp(new_path, "\\\\", 2) == 0 ||
707 strncmp(new_path, "//", 2) == 0)
708 /* UNC path, nothing to do. */
709 return TRUE;
710 env[1] = new_path[0];
711 return SetEnvironmentVariableA(env, new_path);
712}
713
714/* The Unicode version differs from the ANSI version
715 since the current directory might exceed MAX_PATH characters */
716BOOL __stdcall
717win32_wchdir(LPCWSTR path)
718{
719 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
720 int result;
721 wchar_t env[4] = L"=x:";
722
723 if(!SetCurrentDirectoryW(path))
724 return FALSE;
725 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
726 if (!result)
727 return FALSE;
728 if (result > MAX_PATH+1) {
729 new_path = malloc(result);
730 if (!new_path) {
731 SetLastError(ERROR_OUTOFMEMORY);
732 return FALSE;
733 }
734 }
735 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
736 wcsncmp(new_path, L"//", 2) == 0)
737 /* UNC path, nothing to do. */
738 return TRUE;
739 env[1] = new_path[0];
740 result = SetEnvironmentVariableW(env, new_path);
741 if (new_path != _new_path)
742 free(new_path);
743 return result;
744}
745#endif
746
Martin v. Löwis14694662006-02-03 12:54:16 +0000747#ifdef MS_WINDOWS
748/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
749 - time stamps are restricted to second resolution
750 - file modification times suffer from forth-and-back conversions between
751 UTC and local time
752 Therefore, we implement our own stat, based on the Win32 API directly.
753*/
754#define HAVE_STAT_NSEC 1
755
756struct win32_stat{
757 int st_dev;
758 __int64 st_ino;
759 unsigned short st_mode;
760 int st_nlink;
761 int st_uid;
762 int st_gid;
763 int st_rdev;
764 __int64 st_size;
765 int st_atime;
766 int st_atime_nsec;
767 int st_mtime;
768 int st_mtime_nsec;
769 int st_ctime;
770 int st_ctime_nsec;
771};
772
773static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
774
775static void
776FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
777{
Martin v. Löwis77c176d2006-05-12 17:22:04 +0000778 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
779 /* Cannot simply cast and dereference in_ptr,
780 since it might not be aligned properly */
781 __int64 in;
782 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000783 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
784 /* XXX Win32 supports time stamps past 2038; we currently don't */
785 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
786}
787
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000788static void
789time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
790{
791 /* XXX endianness */
792 __int64 out;
793 out = time_in + secs_between_epochs;
Martin v. Löwisf43893a2006-10-09 20:44:25 +0000794 out = out * 10000000 + nsec_in / 100;
Martin v. Löwis77c176d2006-05-12 17:22:04 +0000795 memcpy(out_ptr, &out, sizeof(out));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000796}
797
Martin v. Löwis14694662006-02-03 12:54:16 +0000798/* Below, we *know* that ugo+r is 0444 */
799#if _S_IREAD != 0400
800#error Unsupported C library
801#endif
802static int
803attributes_to_mode(DWORD attr)
804{
805 int m = 0;
806 if (attr & FILE_ATTRIBUTE_DIRECTORY)
807 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
808 else
809 m |= _S_IFREG;
810 if (attr & FILE_ATTRIBUTE_READONLY)
811 m |= 0444;
812 else
813 m |= 0666;
814 return m;
815}
816
817static int
818attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
819{
820 memset(result, 0, sizeof(*result));
821 result->st_mode = attributes_to_mode(info->dwFileAttributes);
822 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
823 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
824 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
825 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
826
827 return 0;
828}
829
Martin v. Löwis012bc722006-10-15 09:43:39 +0000830/* Emulate GetFileAttributesEx[AW] on Windows 95 */
831static int checked = 0;
832static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
833static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
834static void
835check_gfax()
836{
837 HINSTANCE hKernel32;
838 if (checked)
839 return;
840 checked = 1;
841 hKernel32 = GetModuleHandle("KERNEL32");
842 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
843 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
844}
845
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000846static BOOL
847attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
848{
849 HANDLE hFindFile;
850 WIN32_FIND_DATAA FileData;
851 hFindFile = FindFirstFileA(pszFile, &FileData);
852 if (hFindFile == INVALID_HANDLE_VALUE)
853 return FALSE;
854 FindClose(hFindFile);
855 pfad->dwFileAttributes = FileData.dwFileAttributes;
856 pfad->ftCreationTime = FileData.ftCreationTime;
857 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
858 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
859 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
860 pfad->nFileSizeLow = FileData.nFileSizeLow;
861 return TRUE;
862}
863
864static BOOL
865attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
866{
867 HANDLE hFindFile;
868 WIN32_FIND_DATAW FileData;
869 hFindFile = FindFirstFileW(pszFile, &FileData);
870 if (hFindFile == INVALID_HANDLE_VALUE)
871 return FALSE;
872 FindClose(hFindFile);
873 pfad->dwFileAttributes = FileData.dwFileAttributes;
874 pfad->ftCreationTime = FileData.ftCreationTime;
875 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
876 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
877 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
878 pfad->nFileSizeLow = FileData.nFileSizeLow;
879 return TRUE;
880}
881
Martin v. Löwis012bc722006-10-15 09:43:39 +0000882static BOOL WINAPI
883Py_GetFileAttributesExA(LPCSTR pszFile,
884 GET_FILEEX_INFO_LEVELS level,
885 LPVOID pv)
886{
887 BOOL result;
Martin v. Löwis012bc722006-10-15 09:43:39 +0000888 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
889 /* First try to use the system's implementation, if that is
890 available and either succeeds to gives an error other than
891 that it isn't implemented. */
892 check_gfax();
893 if (gfaxa) {
894 result = gfaxa(pszFile, level, pv);
895 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
896 return result;
897 }
898 /* It's either not present, or not implemented.
899 Emulate using FindFirstFile. */
900 if (level != GetFileExInfoStandard) {
901 SetLastError(ERROR_INVALID_PARAMETER);
902 return FALSE;
903 }
904 /* Use GetFileAttributes to validate that the file name
905 does not contain wildcards (which FindFirstFile would
906 accept). */
907 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
908 return FALSE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000909 return attributes_from_dir(pszFile, pfad);
Martin v. Löwis012bc722006-10-15 09:43:39 +0000910}
911
912static BOOL WINAPI
913Py_GetFileAttributesExW(LPCWSTR pszFile,
914 GET_FILEEX_INFO_LEVELS level,
915 LPVOID pv)
916{
917 BOOL result;
Martin v. Löwis012bc722006-10-15 09:43:39 +0000918 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
919 /* First try to use the system's implementation, if that is
920 available and either succeeds to gives an error other than
921 that it isn't implemented. */
922 check_gfax();
923 if (gfaxa) {
924 result = gfaxw(pszFile, level, pv);
925 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
926 return result;
927 }
928 /* It's either not present, or not implemented.
929 Emulate using FindFirstFile. */
930 if (level != GetFileExInfoStandard) {
931 SetLastError(ERROR_INVALID_PARAMETER);
932 return FALSE;
933 }
934 /* Use GetFileAttributes to validate that the file name
935 does not contain wildcards (which FindFirstFile would
936 accept). */
937 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
938 return FALSE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000939 return attributes_from_dir_w(pszFile, pfad);
Martin v. Löwis012bc722006-10-15 09:43:39 +0000940}
941
Martin v. Löwis14694662006-02-03 12:54:16 +0000942static int
943win32_stat(const char* path, struct win32_stat *result)
944{
945 WIN32_FILE_ATTRIBUTE_DATA info;
946 int code;
947 char *dot;
948 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis012bc722006-10-15 09:43:39 +0000949 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000950 if (GetLastError() != ERROR_SHARING_VIOLATION) {
951 /* Protocol violation: we explicitly clear errno, instead of
952 setting it to a POSIX error. Callers should use GetLastError. */
953 errno = 0;
954 return -1;
955 } else {
956 /* Could not get attributes on open file. Fall back to
957 reading the directory. */
958 if (!attributes_from_dir(path, &info)) {
959 /* Very strange. This should not fail now */
960 errno = 0;
961 return -1;
962 }
963 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000964 }
965 code = attribute_data_to_stat(&info, result);
966 if (code != 0)
967 return code;
968 /* Set S_IFEXEC if it is an .exe, .bat, ... */
969 dot = strrchr(path, '.');
970 if (dot) {
971 if (stricmp(dot, ".bat") == 0 ||
972 stricmp(dot, ".cmd") == 0 ||
973 stricmp(dot, ".exe") == 0 ||
974 stricmp(dot, ".com") == 0)
975 result->st_mode |= 0111;
976 }
977 return code;
978}
979
980static int
981win32_wstat(const wchar_t* path, struct win32_stat *result)
982{
983 int code;
984 const wchar_t *dot;
985 WIN32_FILE_ATTRIBUTE_DATA info;
986 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis012bc722006-10-15 09:43:39 +0000987 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000988 if (GetLastError() != ERROR_SHARING_VIOLATION) {
989 /* Protocol violation: we explicitly clear errno, instead of
990 setting it to a POSIX error. Callers should use GetLastError. */
991 errno = 0;
992 return -1;
993 } else {
994 /* Could not get attributes on open file. Fall back to
995 reading the directory. */
996 if (!attributes_from_dir_w(path, &info)) {
997 /* Very strange. This should not fail now */
998 errno = 0;
999 return -1;
1000 }
1001 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001002 }
1003 code = attribute_data_to_stat(&info, result);
1004 if (code < 0)
1005 return code;
1006 /* Set IFEXEC if it is an .exe, .bat, ... */
1007 dot = wcsrchr(path, '.');
1008 if (dot) {
1009 if (_wcsicmp(dot, L".bat") == 0 ||
1010 _wcsicmp(dot, L".cmd") == 0 ||
1011 _wcsicmp(dot, L".exe") == 0 ||
1012 _wcsicmp(dot, L".com") == 0)
1013 result->st_mode |= 0111;
1014 }
1015 return code;
1016}
1017
1018static int
1019win32_fstat(int file_number, struct win32_stat *result)
1020{
1021 BY_HANDLE_FILE_INFORMATION info;
1022 HANDLE h;
1023 int type;
1024
1025 h = (HANDLE)_get_osfhandle(file_number);
1026
1027 /* Protocol violation: we explicitly clear errno, instead of
1028 setting it to a POSIX error. Callers should use GetLastError. */
1029 errno = 0;
1030
1031 if (h == INVALID_HANDLE_VALUE) {
1032 /* This is really a C library error (invalid file handle).
1033 We set the Win32 error to the closes one matching. */
1034 SetLastError(ERROR_INVALID_HANDLE);
1035 return -1;
1036 }
1037 memset(result, 0, sizeof(*result));
1038
1039 type = GetFileType(h);
1040 if (type == FILE_TYPE_UNKNOWN) {
1041 DWORD error = GetLastError();
1042 if (error != 0) {
1043 return -1;
1044 }
1045 /* else: valid but unknown file */
1046 }
1047
1048 if (type != FILE_TYPE_DISK) {
1049 if (type == FILE_TYPE_CHAR)
1050 result->st_mode = _S_IFCHR;
1051 else if (type == FILE_TYPE_PIPE)
1052 result->st_mode = _S_IFIFO;
1053 return 0;
1054 }
1055
1056 if (!GetFileInformationByHandle(h, &info)) {
1057 return -1;
1058 }
1059
1060 /* similar to stat() */
1061 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1062 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1063 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1064 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1065 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1066 /* specific to fstat() */
1067 result->st_nlink = info.nNumberOfLinks;
1068 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1069 return 0;
1070}
1071
1072#endif /* MS_WINDOWS */
1073
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001074PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001075"stat_result: Result from stat or lstat.\n\n\
1076This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001077 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001078or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1079\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001080Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1081or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001082\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001083See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001084
1085static PyStructSequence_Field stat_result_fields[] = {
1086 {"st_mode", "protection bits"},
1087 {"st_ino", "inode"},
1088 {"st_dev", "device"},
1089 {"st_nlink", "number of hard links"},
1090 {"st_uid", "user ID of owner"},
1091 {"st_gid", "group ID of owner"},
1092 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001093 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1094 {NULL, "integer time of last access"},
1095 {NULL, "integer time of last modification"},
1096 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001097 {"st_atime", "time of last access"},
1098 {"st_mtime", "time of last modification"},
1099 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001100#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001101 {"st_blksize", "blocksize for filesystem I/O"},
1102#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001103#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001104 {"st_blocks", "number of blocks allocated"},
1105#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001106#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001107 {"st_rdev", "device type (if inode device)"},
1108#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001109#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1110 {"st_flags", "user defined flags for file"},
1111#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001112#ifdef HAVE_STRUCT_STAT_ST_GEN
1113 {"st_gen", "generation number"},
1114#endif
1115#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1116 {"st_birthtime", "time of creation"},
1117#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001118 {0}
1119};
1120
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001121#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001122#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001123#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001124#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001125#endif
1126
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001127#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001128#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1129#else
1130#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1131#endif
1132
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001133#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001134#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1135#else
1136#define ST_RDEV_IDX ST_BLOCKS_IDX
1137#endif
1138
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001139#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1140#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1141#else
1142#define ST_FLAGS_IDX ST_RDEV_IDX
1143#endif
1144
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001145#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001146#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001147#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001148#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001149#endif
1150
1151#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1152#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1153#else
1154#define ST_BIRTHTIME_IDX ST_GEN_IDX
1155#endif
1156
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001157static PyStructSequence_Desc stat_result_desc = {
1158 "stat_result", /* name */
1159 stat_result__doc__, /* doc */
1160 stat_result_fields,
1161 10
1162};
1163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001164PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001165"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1166This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001167 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001168or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001169\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001170See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001171
1172static PyStructSequence_Field statvfs_result_fields[] = {
1173 {"f_bsize", },
1174 {"f_frsize", },
1175 {"f_blocks", },
1176 {"f_bfree", },
1177 {"f_bavail", },
1178 {"f_files", },
1179 {"f_ffree", },
1180 {"f_favail", },
1181 {"f_flag", },
1182 {"f_namemax",},
1183 {0}
1184};
1185
1186static PyStructSequence_Desc statvfs_result_desc = {
1187 "statvfs_result", /* name */
1188 statvfs_result__doc__, /* doc */
1189 statvfs_result_fields,
1190 10
1191};
1192
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00001193static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001194static PyTypeObject StatResultType;
1195static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001196static newfunc structseq_new;
1197
1198static PyObject *
1199statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1200{
1201 PyStructSequence *result;
1202 int i;
1203
1204 result = (PyStructSequence*)structseq_new(type, args, kwds);
1205 if (!result)
1206 return NULL;
1207 /* If we have been initialized from a tuple,
1208 st_?time might be set to None. Initialize it
1209 from the int slots. */
1210 for (i = 7; i <= 9; i++) {
1211 if (result->ob_item[i+3] == Py_None) {
1212 Py_DECREF(Py_None);
1213 Py_INCREF(result->ob_item[i]);
1214 result->ob_item[i+3] = result->ob_item[i];
1215 }
1216 }
1217 return (PyObject*)result;
1218}
1219
1220
1221
1222/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001223static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001224
1225PyDoc_STRVAR(stat_float_times__doc__,
1226"stat_float_times([newval]) -> oldval\n\n\
1227Determine whether os.[lf]stat represents time stamps as float objects.\n\
1228If newval is True, future calls to stat() return floats, if it is False,\n\
1229future calls return ints. \n\
1230If newval is omitted, return the current setting.\n");
1231
1232static PyObject*
1233stat_float_times(PyObject* self, PyObject *args)
1234{
1235 int newval = -1;
1236 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1237 return NULL;
1238 if (newval == -1)
1239 /* Return old value */
1240 return PyBool_FromLong(_stat_float_times);
1241 _stat_float_times = newval;
1242 Py_INCREF(Py_None);
1243 return Py_None;
1244}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001245
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001246static void
1247fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1248{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001249 PyObject *fval,*ival;
1250#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001251 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001252#else
1253 ival = PyInt_FromLong((long)sec);
1254#endif
Neal Norwitze0a81af2006-08-12 01:50:38 +00001255 if (!ival)
1256 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001257 if (_stat_float_times) {
1258 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1259 } else {
1260 fval = ival;
1261 Py_INCREF(fval);
1262 }
1263 PyStructSequence_SET_ITEM(v, index, ival);
1264 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001265}
1266
Tim Peters5aa91602002-01-30 05:46:57 +00001267/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001268 (used by posix_stat() and posix_fstat()) */
1269static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001270_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001271{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001272 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001273 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001274 if (v == NULL)
1275 return NULL;
1276
Martin v. Löwis14694662006-02-03 12:54:16 +00001277 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001278#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001279 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001280 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001281#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001282 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001283#endif
1284#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001285 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001286 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001287#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001288 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001289#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001290 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1291 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1292 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001293#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001294 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001295 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001296#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001297 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001298#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001299
Martin v. Löwis14694662006-02-03 12:54:16 +00001300#if defined(HAVE_STAT_TV_NSEC)
1301 ansec = st->st_atim.tv_nsec;
1302 mnsec = st->st_mtim.tv_nsec;
1303 cnsec = st->st_ctim.tv_nsec;
1304#elif defined(HAVE_STAT_TV_NSEC2)
1305 ansec = st->st_atimespec.tv_nsec;
1306 mnsec = st->st_mtimespec.tv_nsec;
1307 cnsec = st->st_ctimespec.tv_nsec;
1308#elif defined(HAVE_STAT_NSEC)
1309 ansec = st->st_atime_nsec;
1310 mnsec = st->st_mtime_nsec;
1311 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001312#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001313 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001314#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001315 fill_time(v, 7, st->st_atime, ansec);
1316 fill_time(v, 8, st->st_mtime, mnsec);
1317 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001318
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001319#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001320 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001321 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001322#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001323#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001324 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001325 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001326#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001327#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001328 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001329 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001330#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001331#ifdef HAVE_STRUCT_STAT_ST_GEN
1332 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001333 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001334#endif
1335#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1336 {
1337 PyObject *val;
1338 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001339 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001340#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001341 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001342#else
1343 bnsec = 0;
1344#endif
1345 if (_stat_float_times) {
1346 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1347 } else {
1348 val = PyInt_FromLong((long)bsec);
1349 }
1350 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1351 val);
1352 }
1353#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001354#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1355 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001356 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001357#endif
Fred Drake699f3522000-06-29 21:12:41 +00001358
1359 if (PyErr_Occurred()) {
1360 Py_DECREF(v);
1361 return NULL;
1362 }
1363
1364 return v;
1365}
1366
Martin v. Löwisd8948722004-06-02 09:57:56 +00001367#ifdef MS_WINDOWS
1368
1369/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1370 where / can be used in place of \ and the trailing slash is optional.
1371 Both SERVER and SHARE must have at least one character.
1372*/
1373
1374#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1375#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001376#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001377#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001378#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001379
Tim Peters4ad82172004-08-30 17:02:04 +00001380static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001381IsUNCRootA(char *path, int pathlen)
1382{
1383 #define ISSLASH ISSLASHA
1384
1385 int i, share;
1386
1387 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1388 /* minimum UNCRoot is \\x\y */
1389 return FALSE;
1390 for (i = 2; i < pathlen ; i++)
1391 if (ISSLASH(path[i])) break;
1392 if (i == 2 || i == pathlen)
1393 /* do not allow \\\SHARE or \\SERVER */
1394 return FALSE;
1395 share = i+1;
1396 for (i = share; i < pathlen; i++)
1397 if (ISSLASH(path[i])) break;
1398 return (i != share && (i == pathlen || i == pathlen-1));
1399
1400 #undef ISSLASH
1401}
1402
1403#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001404static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001405IsUNCRootW(Py_UNICODE *path, int pathlen)
1406{
1407 #define ISSLASH ISSLASHW
1408
1409 int i, share;
1410
1411 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1412 /* minimum UNCRoot is \\x\y */
1413 return FALSE;
1414 for (i = 2; i < pathlen ; i++)
1415 if (ISSLASH(path[i])) break;
1416 if (i == 2 || i == pathlen)
1417 /* do not allow \\\SHARE or \\SERVER */
1418 return FALSE;
1419 share = i+1;
1420 for (i = share; i < pathlen; i++)
1421 if (ISSLASH(path[i])) break;
1422 return (i != share && (i == pathlen || i == pathlen-1));
1423
1424 #undef ISSLASH
1425}
1426#endif /* Py_WIN_WIDE_FILENAMES */
1427#endif /* MS_WINDOWS */
1428
Barry Warsaw53699e91996-12-10 23:23:01 +00001429static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001430posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001431 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001432#ifdef __VMS
1433 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1434#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001435 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001436#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001437 char *wformat,
1438 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001439{
Fred Drake699f3522000-06-29 21:12:41 +00001440 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001441 char *path = NULL; /* pass this to stat; do not free() it */
1442 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001443 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001444 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001445
1446#ifdef Py_WIN_WIDE_FILENAMES
1447 /* If on wide-character-capable OS see if argument
1448 is Unicode and if so use wide API. */
1449 if (unicode_file_names()) {
1450 PyUnicodeObject *po;
1451 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001452 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1453
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001454 Py_BEGIN_ALLOW_THREADS
1455 /* PyUnicode_AS_UNICODE result OK without
1456 thread lock as it is a simple dereference. */
1457 res = wstatfunc(wpath, &st);
1458 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001459
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001460 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001461 return win32_error_unicode("stat", wpath);
1462 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001463 }
1464 /* Drop the argument parsing error as narrow strings
1465 are also valid. */
1466 PyErr_Clear();
1467 }
1468#endif
1469
Tim Peters5aa91602002-01-30 05:46:57 +00001470 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001471 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001472 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001473 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001474
Barry Warsaw53699e91996-12-10 23:23:01 +00001475 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001476 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001477 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001478
1479 if (res != 0) {
1480#ifdef MS_WINDOWS
1481 result = win32_error("stat", pathfree);
1482#else
1483 result = posix_error_with_filename(pathfree);
1484#endif
1485 }
1486 else
1487 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001488
Tim Peters500bd032001-12-19 19:05:01 +00001489 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001490 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001491}
1492
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001493/* POSIX methods */
1494
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001495PyDoc_STRVAR(posix_access__doc__,
Georg Brandl7a284472007-01-27 19:38:50 +00001496"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001497Use the real uid/gid to test for access to a path. Note that most\n\
1498operations will use the effective uid/gid, therefore this routine can\n\
1499be used in a suid/sgid environment to test if the invoking user has the\n\
1500specified access to the path. The mode argument can be F_OK to test\n\
1501existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001502
1503static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001504posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001505{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001506 char *path;
1507 int mode;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001508
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001509#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001510 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001511 if (unicode_file_names()) {
1512 PyUnicodeObject *po;
1513 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1514 Py_BEGIN_ALLOW_THREADS
1515 /* PyUnicode_AS_UNICODE OK without thread lock as
1516 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001517 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001518 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001519 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001520 }
1521 /* Drop the argument parsing error as narrow strings
1522 are also valid. */
1523 PyErr_Clear();
1524 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001525 if (!PyArg_ParseTuple(args, "eti:access",
1526 Py_FileSystemDefaultEncoding, &path, &mode))
1527 return 0;
1528 Py_BEGIN_ALLOW_THREADS
1529 attr = GetFileAttributesA(path);
1530 Py_END_ALLOW_THREADS
1531 PyMem_Free(path);
1532finish:
1533 if (attr == 0xFFFFFFFF)
1534 /* File does not exist, or cannot read attributes */
1535 return PyBool_FromLong(0);
1536 /* Access is possible if either write access wasn't requested, or
1537 the file isn't read-only. */
Martin v. Löwisee1e06d2006-07-02 18:44:00 +00001538 return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001539#else
1540 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001541 if (!PyArg_ParseTuple(args, "eti:access",
1542 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001543 return NULL;
1544 Py_BEGIN_ALLOW_THREADS
1545 res = access(path, mode);
1546 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001547 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001548 return PyBool_FromLong(res == 0);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001549#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001550}
1551
Guido van Rossumd371ff11999-01-25 16:12:23 +00001552#ifndef F_OK
1553#define F_OK 0
1554#endif
1555#ifndef R_OK
1556#define R_OK 4
1557#endif
1558#ifndef W_OK
1559#define W_OK 2
1560#endif
1561#ifndef X_OK
1562#define X_OK 1
1563#endif
1564
1565#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001566PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001567"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001568Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001569
1570static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001571posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001572{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001573 int id;
1574 char *ret;
1575
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001576 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001577 return NULL;
1578
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001579#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001580 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001581 if (id == 0) {
1582 ret = ttyname();
1583 }
1584 else {
1585 ret = NULL;
1586 }
1587#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001588 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001589#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001590 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001591 return posix_error();
1592 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001593}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001594#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001595
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001596#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001597PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001598"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001599Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001600
1601static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001602posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001603{
1604 char *ret;
1605 char buffer[L_ctermid];
1606
Greg Wardb48bc172000-03-01 21:51:56 +00001607#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001608 ret = ctermid_r(buffer);
1609#else
1610 ret = ctermid(buffer);
1611#endif
1612 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001613 return posix_error();
1614 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001615}
1616#endif
1617
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001618PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001619"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001620Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001621
Barry Warsaw53699e91996-12-10 23:23:01 +00001622static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001623posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001624{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001625#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001626 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001627#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001628 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001629#elif defined(__VMS)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001630 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001631#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001632 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001633#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001634}
1635
Fred Drake4d1e64b2002-04-15 19:40:07 +00001636#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001637PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001638"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001639Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001640opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001641
1642static PyObject *
1643posix_fchdir(PyObject *self, PyObject *fdobj)
1644{
1645 return posix_fildes(fdobj, fchdir);
1646}
1647#endif /* HAVE_FCHDIR */
1648
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001649
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001650PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001651"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001652Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001653
Barry Warsaw53699e91996-12-10 23:23:01 +00001654static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001655posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001656{
Mark Hammondef8b6542001-05-13 08:04:26 +00001657 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001658 int i;
1659 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001660#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001661 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001662 if (unicode_file_names()) {
1663 PyUnicodeObject *po;
1664 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1665 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001666 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1667 if (attr != 0xFFFFFFFF) {
1668 if (i & _S_IWRITE)
1669 attr &= ~FILE_ATTRIBUTE_READONLY;
1670 else
1671 attr |= FILE_ATTRIBUTE_READONLY;
1672 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1673 }
1674 else
1675 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001676 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001677 if (!res)
1678 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001679 PyUnicode_AS_UNICODE(po));
1680 Py_INCREF(Py_None);
1681 return Py_None;
1682 }
1683 /* Drop the argument parsing error as narrow strings
1684 are also valid. */
1685 PyErr_Clear();
1686 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001687 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1688 &path, &i))
1689 return NULL;
1690 Py_BEGIN_ALLOW_THREADS
1691 attr = GetFileAttributesA(path);
1692 if (attr != 0xFFFFFFFF) {
1693 if (i & _S_IWRITE)
1694 attr &= ~FILE_ATTRIBUTE_READONLY;
1695 else
1696 attr |= FILE_ATTRIBUTE_READONLY;
1697 res = SetFileAttributesA(path, attr);
1698 }
1699 else
1700 res = 0;
1701 Py_END_ALLOW_THREADS
1702 if (!res) {
1703 win32_error("chmod", path);
1704 PyMem_Free(path);
1705 return NULL;
1706 }
Martin v. Löwis9f485bc2006-05-08 05:25:56 +00001707 PyMem_Free(path);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001708 Py_INCREF(Py_None);
1709 return Py_None;
1710#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001711 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001712 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001713 return NULL;
1714 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001715 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001716 Py_END_ALLOW_THREADS
1717 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001718 return posix_error_with_allocated_filename(path);
1719 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001720 Py_INCREF(Py_None);
1721 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001722#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001723}
1724
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001725
Martin v. Löwis382abef2007-02-19 10:55:19 +00001726#ifdef HAVE_CHFLAGS
1727PyDoc_STRVAR(posix_chflags__doc__,
1728"chflags(path, flags)\n\n\
1729Set file flags.");
1730
1731static PyObject *
1732posix_chflags(PyObject *self, PyObject *args)
1733{
1734 char *path;
1735 unsigned long flags;
1736 int res;
1737 if (!PyArg_ParseTuple(args, "etk:chflags",
1738 Py_FileSystemDefaultEncoding, &path, &flags))
1739 return NULL;
1740 Py_BEGIN_ALLOW_THREADS
1741 res = chflags(path, flags);
1742 Py_END_ALLOW_THREADS
1743 if (res < 0)
1744 return posix_error_with_allocated_filename(path);
1745 PyMem_Free(path);
1746 Py_INCREF(Py_None);
1747 return Py_None;
1748}
1749#endif /* HAVE_CHFLAGS */
1750
1751#ifdef HAVE_LCHFLAGS
1752PyDoc_STRVAR(posix_lchflags__doc__,
1753"lchflags(path, flags)\n\n\
1754Set file flags.\n\
1755This function will not follow symbolic links.");
1756
1757static PyObject *
1758posix_lchflags(PyObject *self, PyObject *args)
1759{
1760 char *path;
1761 unsigned long flags;
1762 int res;
1763 if (!PyArg_ParseTuple(args, "etk:lchflags",
1764 Py_FileSystemDefaultEncoding, &path, &flags))
1765 return NULL;
1766 Py_BEGIN_ALLOW_THREADS
1767 res = lchflags(path, flags);
1768 Py_END_ALLOW_THREADS
1769 if (res < 0)
1770 return posix_error_with_allocated_filename(path);
1771 PyMem_Free(path);
1772 Py_INCREF(Py_None);
1773 return Py_None;
1774}
1775#endif /* HAVE_LCHFLAGS */
1776
Martin v. Löwis244edc82001-10-04 22:44:26 +00001777#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001778PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001779"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001780Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001781
1782static PyObject *
1783posix_chroot(PyObject *self, PyObject *args)
1784{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001785 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001786}
1787#endif
1788
Guido van Rossum21142a01999-01-08 21:05:37 +00001789#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001790PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001791"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001792force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001793
1794static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001795posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001796{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001797 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001798}
1799#endif /* HAVE_FSYNC */
1800
1801#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001802
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001803#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001804extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1805#endif
1806
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001807PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001808"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001809force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001810 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001811
1812static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001813posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001814{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001815 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001816}
1817#endif /* HAVE_FDATASYNC */
1818
1819
Fredrik Lundh10723342000-07-10 16:38:09 +00001820#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001821PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001822"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001823Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001824
Barry Warsaw53699e91996-12-10 23:23:01 +00001825static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001826posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001827{
Mark Hammondef8b6542001-05-13 08:04:26 +00001828 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001829 int uid, gid;
1830 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001831 if (!PyArg_ParseTuple(args, "etii:chown",
1832 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001833 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001834 return NULL;
1835 Py_BEGIN_ALLOW_THREADS
1836 res = chown(path, (uid_t) uid, (gid_t) gid);
1837 Py_END_ALLOW_THREADS
1838 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001839 return posix_error_with_allocated_filename(path);
1840 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001841 Py_INCREF(Py_None);
1842 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001843}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001844#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001845
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001846#ifdef HAVE_LCHOWN
1847PyDoc_STRVAR(posix_lchown__doc__,
1848"lchown(path, uid, gid)\n\n\
1849Change the owner and group id of path to the numeric uid and gid.\n\
1850This function will not follow symbolic links.");
1851
1852static PyObject *
1853posix_lchown(PyObject *self, PyObject *args)
1854{
1855 char *path = NULL;
1856 int uid, gid;
1857 int res;
1858 if (!PyArg_ParseTuple(args, "etii:lchown",
1859 Py_FileSystemDefaultEncoding, &path,
1860 &uid, &gid))
1861 return NULL;
1862 Py_BEGIN_ALLOW_THREADS
1863 res = lchown(path, (uid_t) uid, (gid_t) gid);
1864 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001865 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001866 return posix_error_with_allocated_filename(path);
1867 PyMem_Free(path);
1868 Py_INCREF(Py_None);
1869 return Py_None;
1870}
1871#endif /* HAVE_LCHOWN */
1872
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001873
Guido van Rossum36bc6801995-06-14 22:54:23 +00001874#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001875PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001876"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001877Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001878
Barry Warsaw53699e91996-12-10 23:23:01 +00001879static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001880posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001881{
1882 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001883 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001884
Barry Warsaw53699e91996-12-10 23:23:01 +00001885 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001886#if defined(PYOS_OS2) && defined(PYCC_GCC)
1887 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001888#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001889 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001890#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001891 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001892 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001893 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001894 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001895}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001896
Walter Dörwald3b918c32002-11-21 20:18:46 +00001897#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001898PyDoc_STRVAR(posix_getcwdu__doc__,
1899"getcwdu() -> path\n\n\
1900Return a unicode string representing the current working directory.");
1901
1902static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001903posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001904{
1905 char buf[1026];
1906 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001907
1908#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001909 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001910 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001911 wchar_t wbuf[1026];
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001912 wchar_t *wbuf2 = wbuf;
1913 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001914 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001915 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1916 /* If the buffer is large enough, len does not include the
1917 terminating \0. If the buffer is too small, len includes
1918 the space needed for the terminator. */
1919 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1920 wbuf2 = malloc(len * sizeof(wchar_t));
1921 if (wbuf2)
1922 len = GetCurrentDirectoryW(len, wbuf2);
1923 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001924 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001925 if (!wbuf2) {
1926 PyErr_NoMemory();
1927 return NULL;
1928 }
1929 if (!len) {
1930 if (wbuf2 != wbuf) free(wbuf2);
1931 return win32_error("getcwdu", NULL);
1932 }
1933 resobj = PyUnicode_FromWideChar(wbuf2, len);
1934 if (wbuf2 != wbuf) free(wbuf2);
1935 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001936 }
1937#endif
1938
1939 Py_BEGIN_ALLOW_THREADS
1940#if defined(PYOS_OS2) && defined(PYCC_GCC)
1941 res = _getcwd2(buf, sizeof buf);
1942#else
1943 res = getcwd(buf, sizeof buf);
1944#endif
1945 Py_END_ALLOW_THREADS
1946 if (res == NULL)
1947 return posix_error();
1948 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1949}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001950#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001951#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001952
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001953
Guido van Rossumb6775db1994-08-01 11:34:53 +00001954#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001955PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001956"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001957Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001958
Barry Warsaw53699e91996-12-10 23:23:01 +00001959static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001960posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001961{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00001962 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001963}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001964#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001965
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001966
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001967PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001968"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001969Return a list containing the names of the entries in the directory.\n\
1970\n\
1971 path: path of directory to list\n\
1972\n\
1973The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001974entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001975
Barry Warsaw53699e91996-12-10 23:23:01 +00001976static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001977posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001978{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001979 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001980 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001981#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001982
Barry Warsaw53699e91996-12-10 23:23:01 +00001983 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001984 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001985 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001986 WIN32_FIND_DATA FileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001987 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00001988 char *bufptr = namebuf;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001989 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001990
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001991#ifdef Py_WIN_WIDE_FILENAMES
1992 /* If on wide-character-capable OS see if argument
1993 is Unicode and if so use wide API. */
1994 if (unicode_file_names()) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001995 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001996 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1997 WIN32_FIND_DATAW wFileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001998 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001999 Py_UNICODE wch;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002000 /* Overallocate for \\*.*\0 */
2001 len = PyUnicode_GET_SIZE(po);
2002 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2003 if (!wnamebuf) {
2004 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002005 return NULL;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002006 }
2007 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2008 wch = len > 0 ? wnamebuf[len-1] : '\0';
2009 if (wch != L'/' && wch != L'\\' && wch != L':')
2010 wnamebuf[len++] = L'\\';
2011 wcscpy(wnamebuf + len, L"*.*");
2012 if ((d = PyList_New(0)) == NULL) {
2013 free(wnamebuf);
2014 return NULL;
2015 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002016 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2017 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002018 int error = GetLastError();
2019 if (error == ERROR_FILE_NOT_FOUND) {
2020 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002021 return d;
2022 }
2023 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002024 win32_error_unicode("FindFirstFileW", wnamebuf);
2025 free(wnamebuf);
2026 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002027 }
2028 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002029 /* Skip over . and .. */
2030 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2031 wcscmp(wFileData.cFileName, L"..") != 0) {
2032 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2033 if (v == NULL) {
2034 Py_DECREF(d);
2035 d = NULL;
2036 break;
2037 }
2038 if (PyList_Append(d, v) != 0) {
2039 Py_DECREF(v);
2040 Py_DECREF(d);
2041 d = NULL;
2042 break;
2043 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002044 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002045 }
Georg Brandl622927b2006-03-07 12:48:03 +00002046 Py_BEGIN_ALLOW_THREADS
2047 result = FindNextFileW(hFindFile, &wFileData);
2048 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00002049 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2050 it got to the end of the directory. */
2051 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2052 Py_DECREF(d);
2053 win32_error_unicode("FindNextFileW", wnamebuf);
2054 FindClose(hFindFile);
2055 free(wnamebuf);
2056 return NULL;
2057 }
Georg Brandl622927b2006-03-07 12:48:03 +00002058 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002059
2060 if (FindClose(hFindFile) == FALSE) {
2061 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002062 win32_error_unicode("FindClose", wnamebuf);
2063 free(wnamebuf);
2064 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002065 }
Martin v. Löwise3edaea2006-05-15 05:51:36 +00002066 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002067 return d;
2068 }
2069 /* Drop the argument parsing error as narrow strings
2070 are also valid. */
2071 PyErr_Clear();
2072 }
2073#endif
2074
Tim Peters5aa91602002-01-30 05:46:57 +00002075 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002076 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002077 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002078 if (len > 0) {
2079 char ch = namebuf[len-1];
2080 if (ch != SEP && ch != ALTSEP && ch != ':')
2081 namebuf[len++] = '/';
2082 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002083 strcpy(namebuf + len, "*.*");
2084
Barry Warsaw53699e91996-12-10 23:23:01 +00002085 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002086 return NULL;
2087
2088 hFindFile = FindFirstFile(namebuf, &FileData);
2089 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002090 int error = GetLastError();
2091 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002092 return d;
2093 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002094 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002095 }
2096 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002097 /* Skip over . and .. */
2098 if (strcmp(FileData.cFileName, ".") != 0 &&
2099 strcmp(FileData.cFileName, "..") != 0) {
2100 v = PyString_FromString(FileData.cFileName);
2101 if (v == NULL) {
2102 Py_DECREF(d);
2103 d = NULL;
2104 break;
2105 }
2106 if (PyList_Append(d, v) != 0) {
2107 Py_DECREF(v);
2108 Py_DECREF(d);
2109 d = NULL;
2110 break;
2111 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002112 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002113 }
Georg Brandl622927b2006-03-07 12:48:03 +00002114 Py_BEGIN_ALLOW_THREADS
2115 result = FindNextFile(hFindFile, &FileData);
2116 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00002117 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2118 it got to the end of the directory. */
2119 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2120 Py_DECREF(d);
2121 win32_error("FindNextFile", namebuf);
2122 FindClose(hFindFile);
2123 return NULL;
2124 }
Georg Brandl622927b2006-03-07 12:48:03 +00002125 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002126
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002127 if (FindClose(hFindFile) == FALSE) {
2128 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002129 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002130 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002131
2132 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002133
Tim Peters0bb44a42000-09-15 07:44:49 +00002134#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002135
2136#ifndef MAX_PATH
2137#define MAX_PATH CCHMAXPATH
2138#endif
2139 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002140 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002141 PyObject *d, *v;
2142 char namebuf[MAX_PATH+5];
2143 HDIR hdir = 1;
2144 ULONG srchcnt = 1;
2145 FILEFINDBUF3 ep;
2146 APIRET rc;
2147
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002148 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002149 return NULL;
2150 if (len >= MAX_PATH) {
2151 PyErr_SetString(PyExc_ValueError, "path too long");
2152 return NULL;
2153 }
2154 strcpy(namebuf, name);
2155 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002156 if (*pt == ALTSEP)
2157 *pt = SEP;
2158 if (namebuf[len-1] != SEP)
2159 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002160 strcpy(namebuf + len, "*.*");
2161
2162 if ((d = PyList_New(0)) == NULL)
2163 return NULL;
2164
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002165 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2166 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002167 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002168 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2169 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2170 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002171
2172 if (rc != NO_ERROR) {
2173 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002174 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002175 }
2176
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002177 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002178 do {
2179 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002180 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002181 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002182
2183 strcpy(namebuf, ep.achName);
2184
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002185 /* Leave Case of Name Alone -- In Native Form */
2186 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002187
2188 v = PyString_FromString(namebuf);
2189 if (v == NULL) {
2190 Py_DECREF(d);
2191 d = NULL;
2192 break;
2193 }
2194 if (PyList_Append(d, v) != 0) {
2195 Py_DECREF(v);
2196 Py_DECREF(d);
2197 d = NULL;
2198 break;
2199 }
2200 Py_DECREF(v);
2201 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2202 }
2203
2204 return d;
2205#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002206
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002207 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002208 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002209 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002210 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002211 int arg_is_unicode = 1;
2212
Georg Brandl05e89b82006-04-11 07:04:06 +00002213 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002214 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2215 arg_is_unicode = 0;
2216 PyErr_Clear();
2217 }
2218 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002219 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002220 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002221 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002222 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002223 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002224 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002225 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002226 return NULL;
2227 }
Georg Brandl622927b2006-03-07 12:48:03 +00002228 for (;;) {
2229 Py_BEGIN_ALLOW_THREADS
2230 ep = readdir(dirp);
2231 Py_END_ALLOW_THREADS
2232 if (ep == NULL)
2233 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002234 if (ep->d_name[0] == '.' &&
2235 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002236 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002237 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00002238 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002239 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002240 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002241 d = NULL;
2242 break;
2243 }
Just van Rossum46c97842003-02-25 21:42:15 +00002244#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00002245 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002246 PyObject *w;
2247
2248 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002249 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002250 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002251 if (w != NULL) {
2252 Py_DECREF(v);
2253 v = w;
2254 }
2255 else {
2256 /* fall back to the original byte string, as
2257 discussed in patch #683592 */
2258 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002259 }
Just van Rossum46c97842003-02-25 21:42:15 +00002260 }
2261#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002262 if (PyList_Append(d, v) != 0) {
2263 Py_DECREF(v);
2264 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002265 d = NULL;
2266 break;
2267 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002268 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002269 }
Georg Brandlbbfe4fa2006-04-11 06:47:43 +00002270 if (errno != 0 && d != NULL) {
2271 /* readdir() returned NULL and set errno */
2272 closedir(dirp);
2273 Py_DECREF(d);
2274 return posix_error_with_allocated_filename(name);
2275 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002276 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002277 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002278
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002279 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002280
Tim Peters0bb44a42000-09-15 07:44:49 +00002281#endif /* which OS */
2282} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002283
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002284#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002285/* A helper function for abspath on win32 */
2286static PyObject *
2287posix__getfullpathname(PyObject *self, PyObject *args)
2288{
2289 /* assume encoded strings wont more than double no of chars */
2290 char inbuf[MAX_PATH*2];
2291 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002292 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002293 char outbuf[MAX_PATH*2];
2294 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002295#ifdef Py_WIN_WIDE_FILENAMES
2296 if (unicode_file_names()) {
2297 PyUnicodeObject *po;
2298 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2299 Py_UNICODE woutbuf[MAX_PATH*2];
2300 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002301 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002302 sizeof(woutbuf)/sizeof(woutbuf[0]),
2303 woutbuf, &wtemp))
2304 return win32_error("GetFullPathName", "");
2305 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2306 }
2307 /* Drop the argument parsing error as narrow strings
2308 are also valid. */
2309 PyErr_Clear();
2310 }
2311#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002312 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2313 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002314 &insize))
2315 return NULL;
2316 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2317 outbuf, &temp))
2318 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002319 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2320 return PyUnicode_Decode(outbuf, strlen(outbuf),
2321 Py_FileSystemDefaultEncoding, NULL);
2322 }
Mark Hammondef8b6542001-05-13 08:04:26 +00002323 return PyString_FromString(outbuf);
2324} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002325#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002326
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002327PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002328"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002329Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002330
Barry Warsaw53699e91996-12-10 23:23:01 +00002331static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002332posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002333{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002334 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002335 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002336 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002337
2338#ifdef Py_WIN_WIDE_FILENAMES
2339 if (unicode_file_names()) {
2340 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002341 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002342 Py_BEGIN_ALLOW_THREADS
2343 /* PyUnicode_AS_UNICODE OK without thread lock as
2344 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002345 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002346 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002347 if (!res)
2348 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002349 Py_INCREF(Py_None);
2350 return Py_None;
2351 }
2352 /* Drop the argument parsing error as narrow strings
2353 are also valid. */
2354 PyErr_Clear();
2355 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002356 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2357 Py_FileSystemDefaultEncoding, &path, &mode))
2358 return NULL;
2359 Py_BEGIN_ALLOW_THREADS
2360 /* PyUnicode_AS_UNICODE OK without thread lock as
2361 it is a simple dereference. */
2362 res = CreateDirectoryA(path, NULL);
2363 Py_END_ALLOW_THREADS
2364 if (!res) {
2365 win32_error("mkdir", path);
2366 PyMem_Free(path);
2367 return NULL;
2368 }
2369 PyMem_Free(path);
2370 Py_INCREF(Py_None);
2371 return Py_None;
2372#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002373
Tim Peters5aa91602002-01-30 05:46:57 +00002374 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002375 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002376 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002377 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002378#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002379 res = mkdir(path);
2380#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002381 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002382#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002383 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002384 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002385 return posix_error_with_allocated_filename(path);
2386 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002387 Py_INCREF(Py_None);
2388 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002389#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002390}
2391
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002392
Neal Norwitz1818ed72006-03-26 00:29:48 +00002393/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2394#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002395#include <sys/resource.h>
2396#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002397
Neal Norwitz1818ed72006-03-26 00:29:48 +00002398
2399#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002400PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002401"nice(inc) -> new_priority\n\n\
2402Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002403
Barry Warsaw53699e91996-12-10 23:23:01 +00002404static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002405posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002406{
2407 int increment, value;
2408
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002409 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002410 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002411
2412 /* There are two flavours of 'nice': one that returns the new
2413 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002414 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2415 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002416
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002417 If we are of the nice family that returns the new priority, we
2418 need to clear errno before the call, and check if errno is filled
2419 before calling posix_error() on a returnvalue of -1, because the
2420 -1 may be the actual new priority! */
2421
2422 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002423 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002424#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002425 if (value == 0)
2426 value = getpriority(PRIO_PROCESS, 0);
2427#endif
2428 if (value == -1 && errno != 0)
2429 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002430 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002431 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002432}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002433#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002434
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002435PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002436"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002437Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002438
Barry Warsaw53699e91996-12-10 23:23:01 +00002439static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002440posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002441{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002442#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002443 PyObject *o1, *o2;
2444 char *p1, *p2;
2445 BOOL result;
2446 if (unicode_file_names()) {
2447 if (!PyArg_ParseTuple(args, "O&O&:rename",
2448 convert_to_unicode, &o1,
2449 convert_to_unicode, &o2))
2450 PyErr_Clear();
2451 else {
2452 Py_BEGIN_ALLOW_THREADS
2453 result = MoveFileW(PyUnicode_AsUnicode(o1),
2454 PyUnicode_AsUnicode(o2));
2455 Py_END_ALLOW_THREADS
2456 Py_DECREF(o1);
2457 Py_DECREF(o2);
2458 if (!result)
2459 return win32_error("rename", NULL);
2460 Py_INCREF(Py_None);
2461 return Py_None;
2462 }
2463 }
2464 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2465 return NULL;
2466 Py_BEGIN_ALLOW_THREADS
2467 result = MoveFileA(p1, p2);
2468 Py_END_ALLOW_THREADS
2469 if (!result)
2470 return win32_error("rename", NULL);
2471 Py_INCREF(Py_None);
2472 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002473#else
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00002474 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002475#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002476}
2477
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002479PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002480"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002481Remove a directory.");
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_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002485{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002486#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002487 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002488#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002489 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002490#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002491}
2492
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002494PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002495"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002496Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002497
Barry Warsaw53699e91996-12-10 23:23:01 +00002498static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002499posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002500{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002501#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002502 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002503#else
2504 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2505#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002506}
2507
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002508
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002509#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002510PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002511"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002512Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002513
Barry Warsaw53699e91996-12-10 23:23:01 +00002514static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002515posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002516{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002517 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002518 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002519 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002520 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002521 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002522 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002523 Py_END_ALLOW_THREADS
2524 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002525}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002526#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002527
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002528
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002529PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002530"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002531Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002532
Barry Warsaw53699e91996-12-10 23:23:01 +00002533static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002534posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002535{
2536 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002537 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002538 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002539 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002540 if (i < 0)
2541 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002542 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002543}
2544
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002545
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002546PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002547"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002548Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002550PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002551"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002552Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002553
Barry Warsaw53699e91996-12-10 23:23:01 +00002554static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002555posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002556{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002557#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002558 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002559#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002560 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002561#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002562}
2563
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002564
Guido van Rossumb6775db1994-08-01 11:34:53 +00002565#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002566PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002567"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002568Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002569
Barry Warsaw53699e91996-12-10 23:23:01 +00002570static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002571posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002572{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002573 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002574 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002575
Barry Warsaw53699e91996-12-10 23:23:01 +00002576 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002577 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002578 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002579 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002580 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002581 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002582 u.sysname,
2583 u.nodename,
2584 u.release,
2585 u.version,
2586 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002587}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002588#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002589
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002590static int
2591extract_time(PyObject *t, long* sec, long* usec)
2592{
2593 long intval;
2594 if (PyFloat_Check(t)) {
2595 double tval = PyFloat_AsDouble(t);
Martin v. Löwis68192102007-07-21 06:55:02 +00002596 PyObject *intobj = Py_Type(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002597 if (!intobj)
2598 return -1;
2599 intval = PyInt_AsLong(intobj);
2600 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002601 if (intval == -1 && PyErr_Occurred())
2602 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002603 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002604 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002605 if (*usec < 0)
2606 /* If rounding gave us a negative number,
2607 truncate. */
2608 *usec = 0;
2609 return 0;
2610 }
2611 intval = PyInt_AsLong(t);
2612 if (intval == -1 && PyErr_Occurred())
2613 return -1;
2614 *sec = intval;
2615 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002616 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002617}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002618
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002619PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002620"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002621utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002622Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002623second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002624
Barry Warsaw53699e91996-12-10 23:23:01 +00002625static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002626posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002627{
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002628#ifdef Py_WIN_WIDE_FILENAMES
2629 PyObject *arg;
2630 PyUnicodeObject *obwpath;
2631 wchar_t *wpath = NULL;
2632 char *apath = NULL;
2633 HANDLE hFile;
2634 long atimesec, mtimesec, ausec, musec;
2635 FILETIME atime, mtime;
2636 PyObject *result = NULL;
2637
2638 if (unicode_file_names()) {
2639 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2640 wpath = PyUnicode_AS_UNICODE(obwpath);
2641 Py_BEGIN_ALLOW_THREADS
2642 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Martin v. Löwis18aaa562006-10-15 08:43:33 +00002643 NULL, OPEN_EXISTING,
2644 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002645 Py_END_ALLOW_THREADS
2646 if (hFile == INVALID_HANDLE_VALUE)
2647 return win32_error_unicode("utime", wpath);
2648 } else
2649 /* Drop the argument parsing error as narrow strings
2650 are also valid. */
2651 PyErr_Clear();
2652 }
2653 if (!wpath) {
2654 if (!PyArg_ParseTuple(args, "etO:utime",
2655 Py_FileSystemDefaultEncoding, &apath, &arg))
2656 return NULL;
2657 Py_BEGIN_ALLOW_THREADS
2658 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Martin v. Löwis18aaa562006-10-15 08:43:33 +00002659 NULL, OPEN_EXISTING,
2660 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002661 Py_END_ALLOW_THREADS
2662 if (hFile == INVALID_HANDLE_VALUE) {
2663 win32_error("utime", apath);
2664 PyMem_Free(apath);
2665 return NULL;
2666 }
2667 PyMem_Free(apath);
2668 }
2669
2670 if (arg == Py_None) {
2671 SYSTEMTIME now;
2672 GetSystemTime(&now);
2673 if (!SystemTimeToFileTime(&now, &mtime) ||
2674 !SystemTimeToFileTime(&now, &atime)) {
2675 win32_error("utime", NULL);
2676 goto done;
2677 }
2678 }
2679 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2680 PyErr_SetString(PyExc_TypeError,
2681 "utime() arg 2 must be a tuple (atime, mtime)");
2682 goto done;
2683 }
2684 else {
2685 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2686 &atimesec, &ausec) == -1)
2687 goto done;
Martin v. Löwisf43893a2006-10-09 20:44:25 +00002688 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002689 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2690 &mtimesec, &musec) == -1)
2691 goto done;
Martin v. Löwisf43893a2006-10-09 20:44:25 +00002692 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002693 }
2694 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2695 /* Avoid putting the file name into the error here,
2696 as that may confuse the user into believing that
2697 something is wrong with the file, when it also
2698 could be the time stamp that gives a problem. */
2699 win32_error("utime", NULL);
2700 }
2701 Py_INCREF(Py_None);
2702 result = Py_None;
2703done:
2704 CloseHandle(hFile);
2705 return result;
2706#else /* Py_WIN_WIDE_FILENAMES */
2707
Neal Norwitz2adf2102004-06-09 01:46:02 +00002708 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002709 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002710 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002711 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002712
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002713#if defined(HAVE_UTIMES)
2714 struct timeval buf[2];
2715#define ATIME buf[0].tv_sec
2716#define MTIME buf[1].tv_sec
2717#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002718/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002719 struct utimbuf buf;
2720#define ATIME buf.actime
2721#define MTIME buf.modtime
2722#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002723#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002724 time_t buf[2];
2725#define ATIME buf[0]
2726#define MTIME buf[1]
2727#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002728#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002729
Mark Hammond817c9292003-12-03 01:22:38 +00002730
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002731 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002732 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002733 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002734 if (arg == Py_None) {
2735 /* optional time values not given */
2736 Py_BEGIN_ALLOW_THREADS
2737 res = utime(path, NULL);
2738 Py_END_ALLOW_THREADS
2739 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002740 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002741 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002742 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002743 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002744 return NULL;
2745 }
2746 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002747 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002748 &atime, &ausec) == -1) {
2749 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002750 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002751 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002752 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002753 &mtime, &musec) == -1) {
2754 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002755 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002756 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002757 ATIME = atime;
2758 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002759#ifdef HAVE_UTIMES
2760 buf[0].tv_usec = ausec;
2761 buf[1].tv_usec = musec;
2762 Py_BEGIN_ALLOW_THREADS
2763 res = utimes(path, buf);
2764 Py_END_ALLOW_THREADS
2765#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002766 Py_BEGIN_ALLOW_THREADS
2767 res = utime(path, UTIME_ARG);
2768 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002769#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002770 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002771 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002772 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002773 }
Neal Norwitz96652712004-06-06 20:40:27 +00002774 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002775 Py_INCREF(Py_None);
2776 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002777#undef UTIME_ARG
2778#undef ATIME
2779#undef MTIME
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002780#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002781}
2782
Guido van Rossum85e3b011991-06-03 12:42:10 +00002783
Guido van Rossum3b066191991-06-04 19:40:25 +00002784/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002785
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002786PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002787"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002788Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002789
Barry Warsaw53699e91996-12-10 23:23:01 +00002790static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002791posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002792{
2793 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002794 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002795 return NULL;
2796 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002797 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002798}
2799
Martin v. Löwis114619e2002-10-07 06:44:21 +00002800#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2801static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002802free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002803{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002804 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002805 for (i = 0; i < count; i++)
2806 PyMem_Free(array[i]);
2807 PyMem_DEL(array);
2808}
2809#endif
2810
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002811
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002812#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002813PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002814"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002815Execute an executable path with arguments, replacing current process.\n\
2816\n\
2817 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002818 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002819
Barry Warsaw53699e91996-12-10 23:23:01 +00002820static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002821posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002822{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002823 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002824 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002825 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002826 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002827 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002828
Guido van Rossum89b33251993-10-22 14:26:06 +00002829 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002830 argv is a list or tuple of strings. */
2831
Martin v. Löwis114619e2002-10-07 06:44:21 +00002832 if (!PyArg_ParseTuple(args, "etO:execv",
2833 Py_FileSystemDefaultEncoding,
2834 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002835 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002836 if (PyList_Check(argv)) {
2837 argc = PyList_Size(argv);
2838 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002839 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002840 else if (PyTuple_Check(argv)) {
2841 argc = PyTuple_Size(argv);
2842 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002843 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002844 else {
Fred Drake661ea262000-10-24 19:57:45 +00002845 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002846 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002847 return NULL;
2848 }
2849
Barry Warsaw53699e91996-12-10 23:23:01 +00002850 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002851 if (argvlist == NULL) {
2852 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002853 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002854 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002855 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002856 if (!PyArg_Parse((*getitem)(argv, i), "et",
2857 Py_FileSystemDefaultEncoding,
2858 &argvlist[i])) {
2859 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002860 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002861 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002862 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002863 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002864
Guido van Rossum85e3b011991-06-03 12:42:10 +00002865 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002866 }
2867 argvlist[argc] = NULL;
2868
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002869 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002870
Guido van Rossum85e3b011991-06-03 12:42:10 +00002871 /* If we get here it's definitely an error */
2872
Martin v. Löwis114619e2002-10-07 06:44:21 +00002873 free_string_array(argvlist, argc);
2874 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002875 return posix_error();
2876}
2877
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002878
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002879PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002880"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002881Execute a path with arguments and environment, replacing current process.\n\
2882\n\
2883 path: path of executable file\n\
2884 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002885 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002886
Barry Warsaw53699e91996-12-10 23:23:01 +00002887static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002888posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002889{
2890 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002891 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002892 char **argvlist;
2893 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002894 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002895 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002896 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00002897 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002898
2899 /* execve has three arguments: (path, argv, env), where
2900 argv is a list or tuple of strings and env is a dictionary
2901 like posix.environ. */
2902
Martin v. Löwis114619e2002-10-07 06:44:21 +00002903 if (!PyArg_ParseTuple(args, "etOO:execve",
2904 Py_FileSystemDefaultEncoding,
2905 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002906 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002907 if (PyList_Check(argv)) {
2908 argc = PyList_Size(argv);
2909 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002910 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002911 else if (PyTuple_Check(argv)) {
2912 argc = PyTuple_Size(argv);
2913 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002914 }
2915 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002916 PyErr_SetString(PyExc_TypeError,
2917 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002918 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002919 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002920 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002921 PyErr_SetString(PyExc_TypeError,
2922 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002923 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002924 }
2925
Barry Warsaw53699e91996-12-10 23:23:01 +00002926 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002927 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002928 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002929 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002930 }
2931 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002932 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002933 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002934 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002935 &argvlist[i]))
2936 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002937 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002938 goto fail_1;
2939 }
2940 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002941 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002942 argvlist[argc] = NULL;
2943
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002944 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002945 if (i < 0)
2946 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002947 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002948 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002949 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002950 goto fail_1;
2951 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002952 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002953 keys = PyMapping_Keys(env);
2954 vals = PyMapping_Values(env);
2955 if (!keys || !vals)
2956 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002957 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2958 PyErr_SetString(PyExc_TypeError,
2959 "execve(): env.keys() or env.values() is not a list");
2960 goto fail_2;
2961 }
Tim Peters5aa91602002-01-30 05:46:57 +00002962
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002963 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002964 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002965 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002966
2967 key = PyList_GetItem(keys, pos);
2968 val = PyList_GetItem(vals, pos);
2969 if (!key || !val)
2970 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002971
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002972 if (!PyArg_Parse(
2973 key,
2974 "s;execve() arg 3 contains a non-string key",
2975 &k) ||
2976 !PyArg_Parse(
2977 val,
2978 "s;execve() arg 3 contains a non-string value",
2979 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002980 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002981 goto fail_2;
2982 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002983
2984#if defined(PYOS_OS2)
2985 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2986 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2987#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002988 len = PyString_Size(key) + PyString_Size(val) + 2;
2989 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002990 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002991 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002992 goto fail_2;
2993 }
Tim Petersc8996f52001-12-03 20:41:00 +00002994 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002995 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002996#if defined(PYOS_OS2)
2997 }
2998#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002999 }
3000 envlist[envc] = 0;
3001
3002 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003003
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003004 /* If we get here it's definitely an error */
3005
3006 (void) posix_error();
3007
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003008 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003009 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003010 PyMem_DEL(envlist[envc]);
3011 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003012 fail_1:
3013 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003014 Py_XDECREF(vals);
3015 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003016 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003017 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003018 return NULL;
3019}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003020#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003021
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003022
Guido van Rossuma1065681999-01-25 23:20:23 +00003023#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003024PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003025"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003026Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003027\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003028 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003029 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003030 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003031
3032static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003033posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003034{
3035 char *path;
3036 PyObject *argv;
3037 char **argvlist;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003038 int mode, i;
3039 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003040 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003041 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003042
3043 /* spawnv has three arguments: (mode, path, argv), where
3044 argv is a list or tuple of strings. */
3045
Martin v. Löwis114619e2002-10-07 06:44:21 +00003046 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3047 Py_FileSystemDefaultEncoding,
3048 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003049 return NULL;
3050 if (PyList_Check(argv)) {
3051 argc = PyList_Size(argv);
3052 getitem = PyList_GetItem;
3053 }
3054 else if (PyTuple_Check(argv)) {
3055 argc = PyTuple_Size(argv);
3056 getitem = PyTuple_GetItem;
3057 }
3058 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003059 PyErr_SetString(PyExc_TypeError,
3060 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003061 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003062 return NULL;
3063 }
3064
3065 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003066 if (argvlist == NULL) {
3067 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003068 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003069 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003070 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003071 if (!PyArg_Parse((*getitem)(argv, i), "et",
3072 Py_FileSystemDefaultEncoding,
3073 &argvlist[i])) {
3074 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003075 PyErr_SetString(
3076 PyExc_TypeError,
3077 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003078 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003079 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003080 }
3081 }
3082 argvlist[argc] = NULL;
3083
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003084#if defined(PYOS_OS2) && defined(PYCC_GCC)
3085 Py_BEGIN_ALLOW_THREADS
3086 spawnval = spawnv(mode, path, argvlist);
3087 Py_END_ALLOW_THREADS
3088#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003089 if (mode == _OLD_P_OVERLAY)
3090 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003091
Tim Peters25059d32001-12-07 20:35:43 +00003092 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003093 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003094 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003095#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003096
Martin v. Löwis114619e2002-10-07 06:44:21 +00003097 free_string_array(argvlist, argc);
3098 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003099
Fred Drake699f3522000-06-29 21:12:41 +00003100 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003101 return posix_error();
3102 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003103#if SIZEOF_LONG == SIZEOF_VOID_P
3104 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003105#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003106 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003107#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003108}
3109
3110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003111PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003112"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003113Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003114\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003115 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003116 path: path of executable file\n\
3117 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003118 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003119
3120static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003121posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003122{
3123 char *path;
3124 PyObject *argv, *env;
3125 char **argvlist;
3126 char **envlist;
3127 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003128 int mode, pos, envc;
3129 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003130 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003131 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003132 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003133
3134 /* spawnve has four arguments: (mode, path, argv, env), where
3135 argv is a list or tuple of strings and env is a dictionary
3136 like posix.environ. */
3137
Martin v. Löwis114619e2002-10-07 06:44:21 +00003138 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3139 Py_FileSystemDefaultEncoding,
3140 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003141 return NULL;
3142 if (PyList_Check(argv)) {
3143 argc = PyList_Size(argv);
3144 getitem = PyList_GetItem;
3145 }
3146 else if (PyTuple_Check(argv)) {
3147 argc = PyTuple_Size(argv);
3148 getitem = PyTuple_GetItem;
3149 }
3150 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003151 PyErr_SetString(PyExc_TypeError,
3152 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003153 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003154 }
3155 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003156 PyErr_SetString(PyExc_TypeError,
3157 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003158 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003159 }
3160
3161 argvlist = PyMem_NEW(char *, argc+1);
3162 if (argvlist == NULL) {
3163 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003164 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003165 }
3166 for (i = 0; i < argc; i++) {
3167 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003168 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003169 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003170 &argvlist[i]))
3171 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003172 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003173 goto fail_1;
3174 }
3175 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003176 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003177 argvlist[argc] = NULL;
3178
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003179 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003180 if (i < 0)
3181 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003182 envlist = PyMem_NEW(char *, i + 1);
3183 if (envlist == NULL) {
3184 PyErr_NoMemory();
3185 goto fail_1;
3186 }
3187 envc = 0;
3188 keys = PyMapping_Keys(env);
3189 vals = PyMapping_Values(env);
3190 if (!keys || !vals)
3191 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003192 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3193 PyErr_SetString(PyExc_TypeError,
3194 "spawnve(): env.keys() or env.values() is not a list");
3195 goto fail_2;
3196 }
Tim Peters5aa91602002-01-30 05:46:57 +00003197
Guido van Rossuma1065681999-01-25 23:20:23 +00003198 for (pos = 0; pos < i; pos++) {
3199 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003200 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003201
3202 key = PyList_GetItem(keys, pos);
3203 val = PyList_GetItem(vals, pos);
3204 if (!key || !val)
3205 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003206
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003207 if (!PyArg_Parse(
3208 key,
3209 "s;spawnve() arg 3 contains a non-string key",
3210 &k) ||
3211 !PyArg_Parse(
3212 val,
3213 "s;spawnve() arg 3 contains a non-string value",
3214 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003215 {
3216 goto fail_2;
3217 }
Tim Petersc8996f52001-12-03 20:41:00 +00003218 len = PyString_Size(key) + PyString_Size(val) + 2;
3219 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003220 if (p == NULL) {
3221 PyErr_NoMemory();
3222 goto fail_2;
3223 }
Tim Petersc8996f52001-12-03 20:41:00 +00003224 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003225 envlist[envc++] = p;
3226 }
3227 envlist[envc] = 0;
3228
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003229#if defined(PYOS_OS2) && defined(PYCC_GCC)
3230 Py_BEGIN_ALLOW_THREADS
3231 spawnval = spawnve(mode, path, argvlist, envlist);
3232 Py_END_ALLOW_THREADS
3233#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003234 if (mode == _OLD_P_OVERLAY)
3235 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003236
3237 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003238 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003239 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003240#endif
Tim Peters25059d32001-12-07 20:35:43 +00003241
Fred Drake699f3522000-06-29 21:12:41 +00003242 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003243 (void) posix_error();
3244 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003245#if SIZEOF_LONG == SIZEOF_VOID_P
3246 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003247#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003248 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003249#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003250
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003251 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003252 while (--envc >= 0)
3253 PyMem_DEL(envlist[envc]);
3254 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003255 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003256 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003257 Py_XDECREF(vals);
3258 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003259 fail_0:
3260 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003261 return res;
3262}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003263
3264/* OS/2 supports spawnvp & spawnvpe natively */
3265#if defined(PYOS_OS2)
3266PyDoc_STRVAR(posix_spawnvp__doc__,
3267"spawnvp(mode, file, args)\n\n\
3268Execute the program 'file' in a new process, using the environment\n\
3269search path to find the file.\n\
3270\n\
3271 mode: mode of process creation\n\
3272 file: executable file name\n\
3273 args: tuple or list of strings");
3274
3275static PyObject *
3276posix_spawnvp(PyObject *self, PyObject *args)
3277{
3278 char *path;
3279 PyObject *argv;
3280 char **argvlist;
3281 int mode, i, argc;
3282 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003283 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003284
3285 /* spawnvp has three arguments: (mode, path, argv), where
3286 argv is a list or tuple of strings. */
3287
3288 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3289 Py_FileSystemDefaultEncoding,
3290 &path, &argv))
3291 return NULL;
3292 if (PyList_Check(argv)) {
3293 argc = PyList_Size(argv);
3294 getitem = PyList_GetItem;
3295 }
3296 else if (PyTuple_Check(argv)) {
3297 argc = PyTuple_Size(argv);
3298 getitem = PyTuple_GetItem;
3299 }
3300 else {
3301 PyErr_SetString(PyExc_TypeError,
3302 "spawnvp() arg 2 must be a tuple or list");
3303 PyMem_Free(path);
3304 return NULL;
3305 }
3306
3307 argvlist = PyMem_NEW(char *, argc+1);
3308 if (argvlist == NULL) {
3309 PyMem_Free(path);
3310 return PyErr_NoMemory();
3311 }
3312 for (i = 0; i < argc; i++) {
3313 if (!PyArg_Parse((*getitem)(argv, i), "et",
3314 Py_FileSystemDefaultEncoding,
3315 &argvlist[i])) {
3316 free_string_array(argvlist, i);
3317 PyErr_SetString(
3318 PyExc_TypeError,
3319 "spawnvp() arg 2 must contain only strings");
3320 PyMem_Free(path);
3321 return NULL;
3322 }
3323 }
3324 argvlist[argc] = NULL;
3325
3326 Py_BEGIN_ALLOW_THREADS
3327#if defined(PYCC_GCC)
3328 spawnval = spawnvp(mode, path, argvlist);
3329#else
3330 spawnval = _spawnvp(mode, path, argvlist);
3331#endif
3332 Py_END_ALLOW_THREADS
3333
3334 free_string_array(argvlist, argc);
3335 PyMem_Free(path);
3336
3337 if (spawnval == -1)
3338 return posix_error();
3339 else
3340 return Py_BuildValue("l", (long) spawnval);
3341}
3342
3343
3344PyDoc_STRVAR(posix_spawnvpe__doc__,
3345"spawnvpe(mode, file, args, env)\n\n\
3346Execute the program 'file' in a new process, using the environment\n\
3347search path to find the file.\n\
3348\n\
3349 mode: mode of process creation\n\
3350 file: executable file name\n\
3351 args: tuple or list of arguments\n\
3352 env: dictionary of strings mapping to strings");
3353
3354static PyObject *
3355posix_spawnvpe(PyObject *self, PyObject *args)
3356{
3357 char *path;
3358 PyObject *argv, *env;
3359 char **argvlist;
3360 char **envlist;
3361 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3362 int mode, i, pos, argc, envc;
3363 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003364 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003365 int lastarg = 0;
3366
3367 /* spawnvpe has four arguments: (mode, path, argv, env), where
3368 argv is a list or tuple of strings and env is a dictionary
3369 like posix.environ. */
3370
3371 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3372 Py_FileSystemDefaultEncoding,
3373 &path, &argv, &env))
3374 return NULL;
3375 if (PyList_Check(argv)) {
3376 argc = PyList_Size(argv);
3377 getitem = PyList_GetItem;
3378 }
3379 else if (PyTuple_Check(argv)) {
3380 argc = PyTuple_Size(argv);
3381 getitem = PyTuple_GetItem;
3382 }
3383 else {
3384 PyErr_SetString(PyExc_TypeError,
3385 "spawnvpe() arg 2 must be a tuple or list");
3386 goto fail_0;
3387 }
3388 if (!PyMapping_Check(env)) {
3389 PyErr_SetString(PyExc_TypeError,
3390 "spawnvpe() arg 3 must be a mapping object");
3391 goto fail_0;
3392 }
3393
3394 argvlist = PyMem_NEW(char *, argc+1);
3395 if (argvlist == NULL) {
3396 PyErr_NoMemory();
3397 goto fail_0;
3398 }
3399 for (i = 0; i < argc; i++) {
3400 if (!PyArg_Parse((*getitem)(argv, i),
3401 "et;spawnvpe() arg 2 must contain only strings",
3402 Py_FileSystemDefaultEncoding,
3403 &argvlist[i]))
3404 {
3405 lastarg = i;
3406 goto fail_1;
3407 }
3408 }
3409 lastarg = argc;
3410 argvlist[argc] = NULL;
3411
3412 i = PyMapping_Size(env);
3413 if (i < 0)
3414 goto fail_1;
3415 envlist = PyMem_NEW(char *, i + 1);
3416 if (envlist == NULL) {
3417 PyErr_NoMemory();
3418 goto fail_1;
3419 }
3420 envc = 0;
3421 keys = PyMapping_Keys(env);
3422 vals = PyMapping_Values(env);
3423 if (!keys || !vals)
3424 goto fail_2;
3425 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3426 PyErr_SetString(PyExc_TypeError,
3427 "spawnvpe(): env.keys() or env.values() is not a list");
3428 goto fail_2;
3429 }
3430
3431 for (pos = 0; pos < i; pos++) {
3432 char *p, *k, *v;
3433 size_t len;
3434
3435 key = PyList_GetItem(keys, pos);
3436 val = PyList_GetItem(vals, pos);
3437 if (!key || !val)
3438 goto fail_2;
3439
3440 if (!PyArg_Parse(
3441 key,
3442 "s;spawnvpe() arg 3 contains a non-string key",
3443 &k) ||
3444 !PyArg_Parse(
3445 val,
3446 "s;spawnvpe() arg 3 contains a non-string value",
3447 &v))
3448 {
3449 goto fail_2;
3450 }
3451 len = PyString_Size(key) + PyString_Size(val) + 2;
3452 p = PyMem_NEW(char, len);
3453 if (p == NULL) {
3454 PyErr_NoMemory();
3455 goto fail_2;
3456 }
3457 PyOS_snprintf(p, len, "%s=%s", k, v);
3458 envlist[envc++] = p;
3459 }
3460 envlist[envc] = 0;
3461
3462 Py_BEGIN_ALLOW_THREADS
3463#if defined(PYCC_GCC)
3464 spawnval = spawnve(mode, path, argvlist, envlist);
3465#else
3466 spawnval = _spawnve(mode, path, argvlist, envlist);
3467#endif
3468 Py_END_ALLOW_THREADS
3469
3470 if (spawnval == -1)
3471 (void) posix_error();
3472 else
3473 res = Py_BuildValue("l", (long) spawnval);
3474
3475 fail_2:
3476 while (--envc >= 0)
3477 PyMem_DEL(envlist[envc]);
3478 PyMem_DEL(envlist);
3479 fail_1:
3480 free_string_array(argvlist, lastarg);
3481 Py_XDECREF(vals);
3482 Py_XDECREF(keys);
3483 fail_0:
3484 PyMem_Free(path);
3485 return res;
3486}
3487#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003488#endif /* HAVE_SPAWNV */
3489
3490
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003491#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003492PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003493"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003494Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3495\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003496Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003497
3498static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003499posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003500{
Neal Norwitze241ce82003-02-17 18:17:05 +00003501 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003502 if (pid == -1)
3503 return posix_error();
3504 PyOS_AfterFork();
3505 return PyInt_FromLong((long)pid);
3506}
3507#endif
3508
3509
Guido van Rossumad0ee831995-03-01 10:34:45 +00003510#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003511PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003512"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003513Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003514Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003515
Barry Warsaw53699e91996-12-10 23:23:01 +00003516static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003517posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003518{
Neal Norwitze241ce82003-02-17 18:17:05 +00003519 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003520 if (pid == -1)
3521 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003522 if (pid == 0)
3523 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003524 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003525}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003526#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003527
Neal Norwitzb59798b2003-03-21 01:43:31 +00003528/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003529/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3530#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003531#define DEV_PTY_FILE "/dev/ptc"
3532#define HAVE_DEV_PTMX
3533#else
3534#define DEV_PTY_FILE "/dev/ptmx"
3535#endif
3536
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003537#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003538#ifdef HAVE_PTY_H
3539#include <pty.h>
3540#else
3541#ifdef HAVE_LIBUTIL_H
3542#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003543#endif /* HAVE_LIBUTIL_H */
3544#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003545#ifdef HAVE_STROPTS_H
3546#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003547#endif
3548#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003549
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003550#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003551PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003552"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003553Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003554
3555static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003556posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003557{
3558 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003559#ifndef HAVE_OPENPTY
3560 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003561#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003562#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003563 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003564#ifdef sun
Neal Norwitz84a98e02006-04-10 07:44:23 +00003565 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003566#endif
3567#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003568
Thomas Wouters70c21a12000-07-14 14:28:33 +00003569#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003570 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3571 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003572#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003573 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3574 if (slave_name == NULL)
3575 return posix_error();
3576
3577 slave_fd = open(slave_name, O_RDWR);
3578 if (slave_fd < 0)
3579 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003580#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003581 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003582 if (master_fd < 0)
3583 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003584 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003585 /* change permission of slave */
3586 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003587 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003588 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003589 }
3590 /* unlock slave */
3591 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003592 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003593 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003594 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003595 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003596 slave_name = ptsname(master_fd); /* get name of slave */
3597 if (slave_name == NULL)
3598 return posix_error();
3599 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3600 if (slave_fd < 0)
3601 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003602#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003603 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3604 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003605#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003606 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003607#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003608#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003609#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003610
Fred Drake8cef4cf2000-06-28 16:40:38 +00003611 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003612
Fred Drake8cef4cf2000-06-28 16:40:38 +00003613}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003614#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003615
3616#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003617PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003618"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003619Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3620Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003621To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003622
3623static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003624posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003625{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003626 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003627
Fred Drake8cef4cf2000-06-28 16:40:38 +00003628 pid = forkpty(&master_fd, NULL, NULL, NULL);
3629 if (pid == -1)
3630 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003631 if (pid == 0)
3632 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003633 return Py_BuildValue("(ii)", pid, master_fd);
3634}
3635#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003636
Guido van Rossumad0ee831995-03-01 10:34:45 +00003637#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003638PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003639"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003640Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003641
Barry Warsaw53699e91996-12-10 23:23:01 +00003642static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003643posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003644{
Barry Warsaw53699e91996-12-10 23:23:01 +00003645 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003646}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003647#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003648
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003649
Guido van Rossumad0ee831995-03-01 10:34:45 +00003650#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003651PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003652"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003653Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003654
Barry Warsaw53699e91996-12-10 23:23:01 +00003655static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003656posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003657{
Barry Warsaw53699e91996-12-10 23:23:01 +00003658 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003659}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003660#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003661
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003662
Guido van Rossumad0ee831995-03-01 10:34:45 +00003663#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003664PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003665"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003666Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003667
Barry Warsaw53699e91996-12-10 23:23:01 +00003668static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003669posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003670{
Barry Warsaw53699e91996-12-10 23:23:01 +00003671 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003672}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003673#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003674
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003676PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003677"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003678Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003679
Barry Warsaw53699e91996-12-10 23:23:01 +00003680static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003681posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003682{
Barry Warsaw53699e91996-12-10 23:23:01 +00003683 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003684}
3685
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003686
Fred Drakec9680921999-12-13 16:37:25 +00003687#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003688PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003689"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003690Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003691
3692static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003693posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003694{
3695 PyObject *result = NULL;
3696
Fred Drakec9680921999-12-13 16:37:25 +00003697#ifdef NGROUPS_MAX
3698#define MAX_GROUPS NGROUPS_MAX
3699#else
3700 /* defined to be 16 on Solaris7, so this should be a small number */
3701#define MAX_GROUPS 64
3702#endif
3703 gid_t grouplist[MAX_GROUPS];
3704 int n;
3705
3706 n = getgroups(MAX_GROUPS, grouplist);
3707 if (n < 0)
3708 posix_error();
3709 else {
3710 result = PyList_New(n);
3711 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003712 int i;
3713 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003714 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003715 if (o == NULL) {
3716 Py_DECREF(result);
3717 result = NULL;
3718 break;
3719 }
3720 PyList_SET_ITEM(result, i, o);
3721 }
3722 }
3723 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003724
Fred Drakec9680921999-12-13 16:37:25 +00003725 return result;
3726}
3727#endif
3728
Martin v. Löwis606edc12002-06-13 21:09:11 +00003729#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003730PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003731"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003732Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003733
3734static PyObject *
3735posix_getpgid(PyObject *self, PyObject *args)
3736{
3737 int pid, pgid;
3738 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3739 return NULL;
3740 pgid = getpgid(pid);
3741 if (pgid < 0)
3742 return posix_error();
3743 return PyInt_FromLong((long)pgid);
3744}
3745#endif /* HAVE_GETPGID */
3746
3747
Guido van Rossumb6775db1994-08-01 11:34:53 +00003748#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003749PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003750"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003751Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003752
Barry Warsaw53699e91996-12-10 23:23:01 +00003753static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003754posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003755{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003756#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003757 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003758#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003759 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003760#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003761}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003762#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003763
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003764
Guido van Rossumb6775db1994-08-01 11:34:53 +00003765#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003766PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003767"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003768Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003769
Barry Warsaw53699e91996-12-10 23:23:01 +00003770static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003771posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003772{
Guido van Rossum64933891994-10-20 21:56:42 +00003773#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003774 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003775#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003776 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003777#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003778 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003779 Py_INCREF(Py_None);
3780 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003781}
3782
Guido van Rossumb6775db1994-08-01 11:34:53 +00003783#endif /* HAVE_SETPGRP */
3784
Guido van Rossumad0ee831995-03-01 10:34:45 +00003785#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003786PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003787"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003788Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003789
Barry Warsaw53699e91996-12-10 23:23:01 +00003790static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003791posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003792{
Barry Warsaw53699e91996-12-10 23:23:01 +00003793 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003794}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003795#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003796
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003797
Fred Drake12c6e2d1999-12-14 21:25:03 +00003798#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003799PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003800"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003801Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003802
3803static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003804posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003805{
Neal Norwitze241ce82003-02-17 18:17:05 +00003806 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003807 char *name;
3808 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003809
Fred Drakea30680b2000-12-06 21:24:28 +00003810 errno = 0;
3811 name = getlogin();
3812 if (name == NULL) {
3813 if (errno)
3814 posix_error();
3815 else
3816 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003817 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003818 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003819 else
3820 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003821 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003822
Fred Drake12c6e2d1999-12-14 21:25:03 +00003823 return result;
3824}
3825#endif
3826
Guido van Rossumad0ee831995-03-01 10:34:45 +00003827#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003828PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003829"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003830Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003831
Barry Warsaw53699e91996-12-10 23:23:01 +00003832static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003833posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003834{
Barry Warsaw53699e91996-12-10 23:23:01 +00003835 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003836}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003837#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003838
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003839
Guido van Rossumad0ee831995-03-01 10:34:45 +00003840#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003841PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003842"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003843Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003844
Barry Warsaw53699e91996-12-10 23:23:01 +00003845static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003846posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003847{
3848 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003849 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003850 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003851#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003852 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3853 APIRET rc;
3854 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003855 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003856
3857 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3858 APIRET rc;
3859 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003860 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003861
3862 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003863 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003864#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003865 if (kill(pid, sig) == -1)
3866 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003867#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003868 Py_INCREF(Py_None);
3869 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003870}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003871#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003872
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003873#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003874PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003875"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003876Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003877
3878static PyObject *
3879posix_killpg(PyObject *self, PyObject *args)
3880{
3881 int pgid, sig;
3882 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3883 return NULL;
3884 if (killpg(pgid, sig) == -1)
3885 return posix_error();
3886 Py_INCREF(Py_None);
3887 return Py_None;
3888}
3889#endif
3890
Guido van Rossumc0125471996-06-28 18:55:32 +00003891#ifdef HAVE_PLOCK
3892
3893#ifdef HAVE_SYS_LOCK_H
3894#include <sys/lock.h>
3895#endif
3896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003897PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003898"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003899Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003900
Barry Warsaw53699e91996-12-10 23:23:01 +00003901static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003902posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003903{
3904 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003905 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003906 return NULL;
3907 if (plock(op) == -1)
3908 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003909 Py_INCREF(Py_None);
3910 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003911}
3912#endif
3913
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003914
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003915#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003916PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003917"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003918Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003919
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003920#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003921#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003922static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003923async_system(const char *command)
3924{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003925 char errormsg[256], args[1024];
3926 RESULTCODES rcodes;
3927 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003928
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003929 char *shell = getenv("COMSPEC");
3930 if (!shell)
3931 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003932
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003933 /* avoid overflowing the argument buffer */
3934 if (strlen(shell) + 3 + strlen(command) >= 1024)
3935 return ERROR_NOT_ENOUGH_MEMORY
3936
3937 args[0] = '\0';
3938 strcat(args, shell);
3939 strcat(args, "/c ");
3940 strcat(args, command);
3941
3942 /* execute asynchronously, inheriting the environment */
3943 rc = DosExecPgm(errormsg,
3944 sizeof(errormsg),
3945 EXEC_ASYNC,
3946 args,
3947 NULL,
3948 &rcodes,
3949 shell);
3950 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003951}
3952
Guido van Rossumd48f2521997-12-05 22:19:34 +00003953static FILE *
3954popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003955{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003956 int oldfd, tgtfd;
3957 HFILE pipeh[2];
3958 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003959
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003960 /* mode determines which of stdin or stdout is reconnected to
3961 * the pipe to the child
3962 */
3963 if (strchr(mode, 'r') != NULL) {
3964 tgt_fd = 1; /* stdout */
3965 } else if (strchr(mode, 'w')) {
3966 tgt_fd = 0; /* stdin */
3967 } else {
3968 *err = ERROR_INVALID_ACCESS;
3969 return NULL;
3970 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003971
Andrew MacIntyrea3be2582004-12-18 09:51:05 +00003972 /* setup the pipe */
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003973 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
3974 *err = rc;
3975 return NULL;
3976 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003977
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003978 /* prevent other threads accessing stdio */
3979 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003980
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003981 /* reconnect stdio and execute child */
3982 oldfd = dup(tgtfd);
3983 close(tgtfd);
3984 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
3985 DosClose(pipeh[tgtfd]);
3986 rc = async_system(command);
3987 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003988
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003989 /* restore stdio */
3990 dup2(oldfd, tgtfd);
3991 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003992
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003993 /* allow other threads access to stdio */
3994 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003995
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003996 /* if execution of child was successful return file stream */
3997 if (rc == NO_ERROR)
3998 return fdopen(pipeh[1 - tgtfd], mode);
3999 else {
4000 DosClose(pipeh[1 - tgtfd]);
4001 *err = rc;
4002 return NULL;
4003 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004004}
4005
4006static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004007posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004008{
4009 char *name;
4010 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00004011 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004012 FILE *fp;
4013 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004014 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004015 return NULL;
4016 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00004017 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004018 Py_END_ALLOW_THREADS
4019 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004020 return os2_error(err);
4021
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004022 f = PyFile_FromFile(fp, name, mode, fclose);
4023 if (f != NULL)
4024 PyFile_SetBufSize(f, bufsize);
4025 return f;
4026}
4027
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004028#elif defined(PYCC_GCC)
4029
4030/* standard posix version of popen() support */
4031static PyObject *
4032posix_popen(PyObject *self, PyObject *args)
4033{
4034 char *name;
4035 char *mode = "r";
4036 int bufsize = -1;
4037 FILE *fp;
4038 PyObject *f;
4039 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4040 return NULL;
4041 Py_BEGIN_ALLOW_THREADS
4042 fp = popen(name, mode);
4043 Py_END_ALLOW_THREADS
4044 if (fp == NULL)
4045 return posix_error();
4046 f = PyFile_FromFile(fp, name, mode, pclose);
4047 if (f != NULL)
4048 PyFile_SetBufSize(f, bufsize);
4049 return f;
4050}
4051
4052/* fork() under OS/2 has lots'o'warts
4053 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4054 * most of this code is a ripoff of the win32 code, but using the
4055 * capabilities of EMX's C library routines
4056 */
4057
4058/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4059#define POPEN_1 1
4060#define POPEN_2 2
4061#define POPEN_3 3
4062#define POPEN_4 4
4063
4064static PyObject *_PyPopen(char *, int, int, int);
4065static int _PyPclose(FILE *file);
4066
4067/*
4068 * Internal dictionary mapping popen* file pointers to process handles,
4069 * for use when retrieving the process exit code. See _PyPclose() below
4070 * for more information on this dictionary's use.
4071 */
4072static PyObject *_PyPopenProcs = NULL;
4073
4074/* os2emx version of popen2()
4075 *
4076 * The result of this function is a pipe (file) connected to the
4077 * process's stdin, and a pipe connected to the process's stdout.
4078 */
4079
4080static PyObject *
4081os2emx_popen2(PyObject *self, PyObject *args)
4082{
4083 PyObject *f;
4084 int tm=0;
4085
4086 char *cmdstring;
4087 char *mode = "t";
4088 int bufsize = -1;
4089 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4090 return NULL;
4091
4092 if (*mode == 't')
4093 tm = O_TEXT;
4094 else if (*mode != 'b') {
4095 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4096 return NULL;
4097 } else
4098 tm = O_BINARY;
4099
4100 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
4101
4102 return f;
4103}
4104
4105/*
4106 * Variation on os2emx.popen2
4107 *
4108 * The result of this function is 3 pipes - the process's stdin,
4109 * stdout and stderr
4110 */
4111
4112static PyObject *
4113os2emx_popen3(PyObject *self, PyObject *args)
4114{
4115 PyObject *f;
4116 int tm = 0;
4117
4118 char *cmdstring;
4119 char *mode = "t";
4120 int bufsize = -1;
4121 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4122 return NULL;
4123
4124 if (*mode == 't')
4125 tm = O_TEXT;
4126 else if (*mode != 'b') {
4127 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4128 return NULL;
4129 } else
4130 tm = O_BINARY;
4131
4132 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
4133
4134 return f;
4135}
4136
4137/*
4138 * Variation on os2emx.popen2
4139 *
Tim Peters11b23062003-04-23 02:39:17 +00004140 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004141 * and stdout+stderr combined as a single pipe.
4142 */
4143
4144static PyObject *
4145os2emx_popen4(PyObject *self, PyObject *args)
4146{
4147 PyObject *f;
4148 int tm = 0;
4149
4150 char *cmdstring;
4151 char *mode = "t";
4152 int bufsize = -1;
4153 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4154 return NULL;
4155
4156 if (*mode == 't')
4157 tm = O_TEXT;
4158 else if (*mode != 'b') {
4159 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4160 return NULL;
4161 } else
4162 tm = O_BINARY;
4163
4164 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
4165
4166 return f;
4167}
4168
4169/* a couple of structures for convenient handling of multiple
4170 * file handles and pipes
4171 */
4172struct file_ref
4173{
4174 int handle;
4175 int flags;
4176};
4177
4178struct pipe_ref
4179{
4180 int rd;
4181 int wr;
4182};
4183
4184/* The following code is derived from the win32 code */
4185
4186static PyObject *
4187_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4188{
4189 struct file_ref stdio[3];
4190 struct pipe_ref p_fd[3];
4191 FILE *p_s[3];
4192 int file_count, i, pipe_err, pipe_pid;
4193 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4194 PyObject *f, *p_f[3];
4195
4196 /* file modes for subsequent fdopen's on pipe handles */
4197 if (mode == O_TEXT)
4198 {
4199 rd_mode = "rt";
4200 wr_mode = "wt";
4201 }
4202 else
4203 {
4204 rd_mode = "rb";
4205 wr_mode = "wb";
4206 }
4207
4208 /* prepare shell references */
4209 if ((shell = getenv("EMXSHELL")) == NULL)
4210 if ((shell = getenv("COMSPEC")) == NULL)
4211 {
4212 errno = ENOENT;
4213 return posix_error();
4214 }
4215
4216 sh_name = _getname(shell);
4217 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4218 opt = "/c";
4219 else
4220 opt = "-c";
4221
4222 /* save current stdio fds + their flags, and set not inheritable */
4223 i = pipe_err = 0;
4224 while (pipe_err >= 0 && i < 3)
4225 {
4226 pipe_err = stdio[i].handle = dup(i);
4227 stdio[i].flags = fcntl(i, F_GETFD, 0);
4228 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4229 i++;
4230 }
4231 if (pipe_err < 0)
4232 {
4233 /* didn't get them all saved - clean up and bail out */
4234 int saved_err = errno;
4235 while (i-- > 0)
4236 {
4237 close(stdio[i].handle);
4238 }
4239 errno = saved_err;
4240 return posix_error();
4241 }
4242
4243 /* create pipe ends */
4244 file_count = 2;
4245 if (n == POPEN_3)
4246 file_count = 3;
4247 i = pipe_err = 0;
4248 while ((pipe_err == 0) && (i < file_count))
4249 pipe_err = pipe((int *)&p_fd[i++]);
4250 if (pipe_err < 0)
4251 {
4252 /* didn't get them all made - clean up and bail out */
4253 while (i-- > 0)
4254 {
4255 close(p_fd[i].wr);
4256 close(p_fd[i].rd);
4257 }
4258 errno = EPIPE;
4259 return posix_error();
4260 }
4261
4262 /* change the actual standard IO streams over temporarily,
4263 * making the retained pipe ends non-inheritable
4264 */
4265 pipe_err = 0;
4266
4267 /* - stdin */
4268 if (dup2(p_fd[0].rd, 0) == 0)
4269 {
4270 close(p_fd[0].rd);
4271 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4272 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4273 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4274 {
4275 close(p_fd[0].wr);
4276 pipe_err = -1;
4277 }
4278 }
4279 else
4280 {
4281 pipe_err = -1;
4282 }
4283
4284 /* - stdout */
4285 if (pipe_err == 0)
4286 {
4287 if (dup2(p_fd[1].wr, 1) == 1)
4288 {
4289 close(p_fd[1].wr);
4290 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4291 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4292 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4293 {
4294 close(p_fd[1].rd);
4295 pipe_err = -1;
4296 }
4297 }
4298 else
4299 {
4300 pipe_err = -1;
4301 }
4302 }
4303
4304 /* - stderr, as required */
4305 if (pipe_err == 0)
4306 switch (n)
4307 {
4308 case POPEN_3:
4309 {
4310 if (dup2(p_fd[2].wr, 2) == 2)
4311 {
4312 close(p_fd[2].wr);
4313 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4314 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4315 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4316 {
4317 close(p_fd[2].rd);
4318 pipe_err = -1;
4319 }
4320 }
4321 else
4322 {
4323 pipe_err = -1;
4324 }
4325 break;
4326 }
4327
4328 case POPEN_4:
4329 {
4330 if (dup2(1, 2) != 2)
4331 {
4332 pipe_err = -1;
4333 }
4334 break;
4335 }
4336 }
4337
4338 /* spawn the child process */
4339 if (pipe_err == 0)
4340 {
4341 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4342 if (pipe_pid == -1)
4343 {
4344 pipe_err = -1;
4345 }
4346 else
4347 {
4348 /* save the PID into the FILE structure
4349 * NOTE: this implementation doesn't actually
4350 * take advantage of this, but do it for
4351 * completeness - AIM Apr01
4352 */
4353 for (i = 0; i < file_count; i++)
4354 p_s[i]->_pid = pipe_pid;
4355 }
4356 }
4357
4358 /* reset standard IO to normal */
4359 for (i = 0; i < 3; i++)
4360 {
4361 dup2(stdio[i].handle, i);
4362 fcntl(i, F_SETFD, stdio[i].flags);
4363 close(stdio[i].handle);
4364 }
4365
4366 /* if any remnant problems, clean up and bail out */
4367 if (pipe_err < 0)
4368 {
4369 for (i = 0; i < 3; i++)
4370 {
4371 close(p_fd[i].rd);
4372 close(p_fd[i].wr);
4373 }
4374 errno = EPIPE;
4375 return posix_error_with_filename(cmdstring);
4376 }
4377
4378 /* build tuple of file objects to return */
4379 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4380 PyFile_SetBufSize(p_f[0], bufsize);
4381 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4382 PyFile_SetBufSize(p_f[1], bufsize);
4383 if (n == POPEN_3)
4384 {
4385 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4386 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004387 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004388 }
4389 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004390 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004391
4392 /*
4393 * Insert the files we've created into the process dictionary
4394 * all referencing the list with the process handle and the
4395 * initial number of files (see description below in _PyPclose).
4396 * Since if _PyPclose later tried to wait on a process when all
4397 * handles weren't closed, it could create a deadlock with the
4398 * child, we spend some energy here to try to ensure that we
4399 * either insert all file handles into the dictionary or none
4400 * at all. It's a little clumsy with the various popen modes
4401 * and variable number of files involved.
4402 */
4403 if (!_PyPopenProcs)
4404 {
4405 _PyPopenProcs = PyDict_New();
4406 }
4407
4408 if (_PyPopenProcs)
4409 {
4410 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4411 int ins_rc[3];
4412
4413 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4414 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4415
4416 procObj = PyList_New(2);
4417 pidObj = PyInt_FromLong((long) pipe_pid);
4418 intObj = PyInt_FromLong((long) file_count);
4419
4420 if (procObj && pidObj && intObj)
4421 {
4422 PyList_SetItem(procObj, 0, pidObj);
4423 PyList_SetItem(procObj, 1, intObj);
4424
4425 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4426 if (fileObj[0])
4427 {
4428 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4429 fileObj[0],
4430 procObj);
4431 }
4432 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4433 if (fileObj[1])
4434 {
4435 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4436 fileObj[1],
4437 procObj);
4438 }
4439 if (file_count >= 3)
4440 {
4441 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4442 if (fileObj[2])
4443 {
4444 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4445 fileObj[2],
4446 procObj);
4447 }
4448 }
4449
4450 if (ins_rc[0] < 0 || !fileObj[0] ||
4451 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4452 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4453 {
4454 /* Something failed - remove any dictionary
4455 * entries that did make it.
4456 */
4457 if (!ins_rc[0] && fileObj[0])
4458 {
4459 PyDict_DelItem(_PyPopenProcs,
4460 fileObj[0]);
4461 }
4462 if (!ins_rc[1] && fileObj[1])
4463 {
4464 PyDict_DelItem(_PyPopenProcs,
4465 fileObj[1]);
4466 }
4467 if (!ins_rc[2] && fileObj[2])
4468 {
4469 PyDict_DelItem(_PyPopenProcs,
4470 fileObj[2]);
4471 }
4472 }
4473 }
Tim Peters11b23062003-04-23 02:39:17 +00004474
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004475 /*
4476 * Clean up our localized references for the dictionary keys
4477 * and value since PyDict_SetItem will Py_INCREF any copies
4478 * that got placed in the dictionary.
4479 */
4480 Py_XDECREF(procObj);
4481 Py_XDECREF(fileObj[0]);
4482 Py_XDECREF(fileObj[1]);
4483 Py_XDECREF(fileObj[2]);
4484 }
4485
4486 /* Child is launched. */
4487 return f;
4488}
4489
4490/*
4491 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4492 * exit code for the child process and return as a result of the close.
4493 *
4494 * This function uses the _PyPopenProcs dictionary in order to map the
4495 * input file pointer to information about the process that was
4496 * originally created by the popen* call that created the file pointer.
4497 * The dictionary uses the file pointer as a key (with one entry
4498 * inserted for each file returned by the original popen* call) and a
4499 * single list object as the value for all files from a single call.
4500 * The list object contains the Win32 process handle at [0], and a file
4501 * count at [1], which is initialized to the total number of file
4502 * handles using that list.
4503 *
4504 * This function closes whichever handle it is passed, and decrements
4505 * the file count in the dictionary for the process handle pointed to
4506 * by this file. On the last close (when the file count reaches zero),
4507 * this function will wait for the child process and then return its
4508 * exit code as the result of the close() operation. This permits the
4509 * files to be closed in any order - it is always the close() of the
4510 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004511 *
4512 * NOTE: This function is currently called with the GIL released.
4513 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004514 */
4515
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004516static int _PyPclose(FILE *file)
4517{
4518 int result;
4519 int exit_code;
4520 int pipe_pid;
4521 PyObject *procObj, *pidObj, *intObj, *fileObj;
4522 int file_count;
4523#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004524 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004525#endif
4526
4527 /* Close the file handle first, to ensure it can't block the
4528 * child from exiting if it's the last handle.
4529 */
4530 result = fclose(file);
4531
4532#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004533 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004534#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004535 if (_PyPopenProcs)
4536 {
4537 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4538 (procObj = PyDict_GetItem(_PyPopenProcs,
4539 fileObj)) != NULL &&
4540 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4541 (intObj = PyList_GetItem(procObj,1)) != NULL)
4542 {
4543 pipe_pid = (int) PyInt_AsLong(pidObj);
4544 file_count = (int) PyInt_AsLong(intObj);
4545
4546 if (file_count > 1)
4547 {
4548 /* Still other files referencing process */
4549 file_count--;
4550 PyList_SetItem(procObj,1,
4551 PyInt_FromLong((long) file_count));
4552 }
4553 else
4554 {
4555 /* Last file for this process */
4556 if (result != EOF &&
4557 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4558 {
4559 /* extract exit status */
4560 if (WIFEXITED(exit_code))
4561 {
4562 result = WEXITSTATUS(exit_code);
4563 }
4564 else
4565 {
4566 errno = EPIPE;
4567 result = -1;
4568 }
4569 }
4570 else
4571 {
4572 /* Indicate failure - this will cause the file object
4573 * to raise an I/O error and translate the last
4574 * error code from errno. We do have a problem with
4575 * last errors that overlap the normal errno table,
4576 * but that's a consistent problem with the file object.
4577 */
4578 result = -1;
4579 }
4580 }
4581
4582 /* Remove this file pointer from dictionary */
4583 PyDict_DelItem(_PyPopenProcs, fileObj);
4584
4585 if (PyDict_Size(_PyPopenProcs) == 0)
4586 {
4587 Py_DECREF(_PyPopenProcs);
4588 _PyPopenProcs = NULL;
4589 }
4590
4591 } /* if object retrieval ok */
4592
4593 Py_XDECREF(fileObj);
4594 } /* if _PyPopenProcs */
4595
4596#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004597 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004598#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004599 return result;
4600}
4601
4602#endif /* PYCC_??? */
4603
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004604#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004605
4606/*
4607 * Portable 'popen' replacement for Win32.
4608 *
4609 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4610 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004611 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004612 */
4613
4614#include <malloc.h>
4615#include <io.h>
4616#include <fcntl.h>
4617
4618/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4619#define POPEN_1 1
4620#define POPEN_2 2
4621#define POPEN_3 3
4622#define POPEN_4 4
4623
4624static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004625static int _PyPclose(FILE *file);
4626
4627/*
4628 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004629 * for use when retrieving the process exit code. See _PyPclose() below
4630 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004631 */
4632static PyObject *_PyPopenProcs = NULL;
4633
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004634
4635/* popen that works from a GUI.
4636 *
4637 * The result of this function is a pipe (file) connected to the
4638 * processes stdin or stdout, depending on the requested mode.
4639 */
4640
4641static PyObject *
4642posix_popen(PyObject *self, PyObject *args)
4643{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00004644 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004645 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004646
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004647 char *cmdstring;
4648 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004649 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004650 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004651 return NULL;
4652
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004653 if (*mode == 'r')
4654 tm = _O_RDONLY;
4655 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00004656 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004657 return NULL;
4658 } else
4659 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004660
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004661 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004662 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004663 return NULL;
4664 }
4665
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004666 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004667 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004668 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004669 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004670 else
4671 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4672
4673 return f;
4674}
4675
4676/* Variation on win32pipe.popen
4677 *
4678 * The result of this function is a pipe (file) connected to the
4679 * process's stdin, and a pipe connected to the process's stdout.
4680 */
4681
4682static PyObject *
4683win32_popen2(PyObject *self, PyObject *args)
4684{
4685 PyObject *f;
4686 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004687
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004688 char *cmdstring;
4689 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004690 int bufsize = -1;
4691 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004692 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004693
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004694 if (*mode == 't')
4695 tm = _O_TEXT;
4696 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004697 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004698 return NULL;
4699 } else
4700 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004701
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004702 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004703 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004704 return NULL;
4705 }
4706
4707 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004708
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004709 return f;
4710}
4711
4712/*
4713 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004714 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004715 * The result of this function is 3 pipes - the process's stdin,
4716 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004717 */
4718
4719static PyObject *
4720win32_popen3(PyObject *self, PyObject *args)
4721{
4722 PyObject *f;
4723 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004724
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004725 char *cmdstring;
4726 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004727 int bufsize = -1;
4728 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004729 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004730
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004731 if (*mode == 't')
4732 tm = _O_TEXT;
4733 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004734 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004735 return NULL;
4736 } else
4737 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004738
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004739 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004740 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004741 return NULL;
4742 }
4743
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004744 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004745
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004746 return f;
4747}
4748
4749/*
4750 * Variation on win32pipe.popen
4751 *
Tim Peters5aa91602002-01-30 05:46:57 +00004752 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004753 * and stdout+stderr combined as a single pipe.
4754 */
4755
4756static PyObject *
4757win32_popen4(PyObject *self, PyObject *args)
4758{
4759 PyObject *f;
4760 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004761
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004762 char *cmdstring;
4763 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004764 int bufsize = -1;
4765 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004766 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004767
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004768 if (*mode == 't')
4769 tm = _O_TEXT;
4770 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004771 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004772 return NULL;
4773 } else
4774 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004775
4776 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004777 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004778 return NULL;
4779 }
4780
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004781 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004782
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004783 return f;
4784}
4785
Mark Hammond08501372001-01-31 07:30:29 +00004786static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004787_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004788 HANDLE hStdin,
4789 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004790 HANDLE hStderr,
4791 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004792{
4793 PROCESS_INFORMATION piProcInfo;
4794 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004795 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004796 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004797 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004798 int i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004799 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004800
4801 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004802 char *comshell;
4803
Tim Peters92e4dd82002-10-05 01:47:34 +00004804 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004805 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
Martin v. Löwis18e16552006-02-15 17:27:45 +00004806 /* x < i, so x fits into an integer */
4807 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00004808
4809 /* Explicitly check if we are using COMMAND.COM. If we are
4810 * then use the w9xpopen hack.
4811 */
4812 comshell = s1 + x;
4813 while (comshell >= s1 && *comshell != '\\')
4814 --comshell;
4815 ++comshell;
4816
4817 if (GetVersion() < 0x80000000 &&
4818 _stricmp(comshell, "command.com") != 0) {
4819 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004820 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004821 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004822 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004823 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004824 }
4825 else {
4826 /*
Tim Peters402d5982001-08-27 06:37:48 +00004827 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4828 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004829 */
Mark Hammond08501372001-01-31 07:30:29 +00004830 char modulepath[_MAX_PATH];
4831 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004832 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
Martin v. Löwis26fd9602006-04-22 11:15:41 +00004833 for (x = i = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004834 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004835 x = i+1;
4836 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004837 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004838 strncat(modulepath,
4839 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004840 (sizeof(modulepath)/sizeof(modulepath[0]))
4841 -strlen(modulepath));
4842 if (stat(modulepath, &statinfo) != 0) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004843 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
Tim Peters5aa91602002-01-30 05:46:57 +00004844 /* Eeek - file-not-found - possibly an embedding
4845 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004846 */
Tim Peters5aa91602002-01-30 05:46:57 +00004847 strncpy(modulepath,
4848 Py_GetExecPrefix(),
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004849 mplen);
4850 modulepath[mplen-1] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004851 if (modulepath[strlen(modulepath)-1] != '\\')
4852 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004853 strncat(modulepath,
4854 szConsoleSpawn,
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004855 mplen-strlen(modulepath));
Mark Hammond08501372001-01-31 07:30:29 +00004856 /* No where else to look - raise an easily identifiable
4857 error, rather than leaving Windows to report
4858 "file not found" - as the user is probably blissfully
4859 unaware this shim EXE is used, and it will confuse them.
4860 (well, it confused me for a while ;-)
4861 */
4862 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004863 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004864 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004865 "for popen to work with your shell "
4866 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004867 szConsoleSpawn);
4868 return FALSE;
4869 }
4870 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004871 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004872 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004873 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004874
Tim Peters92e4dd82002-10-05 01:47:34 +00004875 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004876 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004877 /* To maintain correct argument passing semantics,
4878 we pass the command-line as it stands, and allow
4879 quoting to be applied. w9xpopen.exe will then
4880 use its argv vector, and re-quote the necessary
4881 args for the ultimate child process.
4882 */
Tim Peters75cdad52001-11-28 22:07:30 +00004883 PyOS_snprintf(
4884 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004885 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004886 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004887 s1,
4888 s3,
4889 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004890 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004891 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004892 dialog:
4893 "Your program accessed mem currently in use at xxx"
4894 and a hopeful warning about the stability of your
4895 system.
4896 Cost is Ctrl+C wont kill children, but anyone
4897 who cares can have a go!
4898 */
4899 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004900 }
4901 }
4902
4903 /* Could be an else here to try cmd.exe / command.com in the path
4904 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004905 else {
Tim Peters402d5982001-08-27 06:37:48 +00004906 PyErr_SetString(PyExc_RuntimeError,
4907 "Cannot locate a COMSPEC environment variable to "
4908 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004909 return FALSE;
4910 }
Tim Peters5aa91602002-01-30 05:46:57 +00004911
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004912 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4913 siStartInfo.cb = sizeof(STARTUPINFO);
4914 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4915 siStartInfo.hStdInput = hStdin;
4916 siStartInfo.hStdOutput = hStdout;
4917 siStartInfo.hStdError = hStderr;
4918 siStartInfo.wShowWindow = SW_HIDE;
4919
4920 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004921 s2,
4922 NULL,
4923 NULL,
4924 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004925 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004926 NULL,
4927 NULL,
4928 &siStartInfo,
4929 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004930 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004931 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004932
Mark Hammondb37a3732000-08-14 04:47:33 +00004933 /* Return process handle */
4934 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004935 return TRUE;
4936 }
Tim Peters402d5982001-08-27 06:37:48 +00004937 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004938 return FALSE;
4939}
4940
4941/* The following code is based off of KB: Q190351 */
4942
4943static PyObject *
4944_PyPopen(char *cmdstring, int mode, int n)
4945{
4946 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4947 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004948 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004949
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004950 SECURITY_ATTRIBUTES saAttr;
4951 BOOL fSuccess;
4952 int fd1, fd2, fd3;
4953 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004954 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004955 PyObject *f;
4956
4957 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4958 saAttr.bInheritHandle = TRUE;
4959 saAttr.lpSecurityDescriptor = NULL;
4960
4961 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4962 return win32_error("CreatePipe", NULL);
4963
4964 /* Create new output read handle and the input write handle. Set
4965 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004966 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004967 * being created. */
4968 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004969 GetCurrentProcess(), &hChildStdinWrDup, 0,
4970 FALSE,
4971 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004972 if (!fSuccess)
4973 return win32_error("DuplicateHandle", NULL);
4974
4975 /* Close the inheritable version of ChildStdin
4976 that we're using. */
4977 CloseHandle(hChildStdinWr);
4978
4979 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4980 return win32_error("CreatePipe", NULL);
4981
4982 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004983 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4984 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004985 if (!fSuccess)
4986 return win32_error("DuplicateHandle", NULL);
4987
4988 /* Close the inheritable version of ChildStdout
4989 that we're using. */
4990 CloseHandle(hChildStdoutRd);
4991
4992 if (n != POPEN_4) {
4993 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4994 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004995 fSuccess = DuplicateHandle(GetCurrentProcess(),
4996 hChildStderrRd,
4997 GetCurrentProcess(),
4998 &hChildStderrRdDup, 0,
4999 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005000 if (!fSuccess)
5001 return win32_error("DuplicateHandle", NULL);
5002 /* Close the inheritable version of ChildStdErr that we're using. */
5003 CloseHandle(hChildStderrRd);
5004 }
Tim Peters5aa91602002-01-30 05:46:57 +00005005
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005006 switch (n) {
5007 case POPEN_1:
5008 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5009 case _O_WRONLY | _O_TEXT:
5010 /* Case for writing to child Stdin in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005011 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005012 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005013 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005014 PyFile_SetBufSize(f, 0);
5015 /* We don't care about these pipes anymore, so close them. */
5016 CloseHandle(hChildStdoutRdDup);
5017 CloseHandle(hChildStderrRdDup);
5018 break;
5019
5020 case _O_RDONLY | _O_TEXT:
5021 /* Case for reading from child Stdout in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005022 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005023 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005024 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005025 PyFile_SetBufSize(f, 0);
5026 /* We don't care about these pipes anymore, so close them. */
5027 CloseHandle(hChildStdinWrDup);
5028 CloseHandle(hChildStderrRdDup);
5029 break;
5030
5031 case _O_RDONLY | _O_BINARY:
5032 /* Case for readinig from child Stdout in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005033 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005034 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005035 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005036 PyFile_SetBufSize(f, 0);
5037 /* We don't care about these pipes anymore, so close them. */
5038 CloseHandle(hChildStdinWrDup);
5039 CloseHandle(hChildStderrRdDup);
5040 break;
5041
5042 case _O_WRONLY | _O_BINARY:
5043 /* Case for writing to child Stdin in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005044 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005045 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005046 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005047 PyFile_SetBufSize(f, 0);
5048 /* We don't care about these pipes anymore, so close them. */
5049 CloseHandle(hChildStdoutRdDup);
5050 CloseHandle(hChildStderrRdDup);
5051 break;
5052 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005053 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005054 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005055
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005056 case POPEN_2:
5057 case POPEN_4:
5058 {
5059 char *m1, *m2;
5060 PyObject *p1, *p2;
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
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005070 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005071 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005072 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005073 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005074 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005075 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00005076 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005077 PyFile_SetBufSize(p2, 0);
5078
5079 if (n != 4)
5080 CloseHandle(hChildStderrRdDup);
5081
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005082 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00005083 Py_XDECREF(p1);
5084 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00005085 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005086 break;
5087 }
Tim Peters5aa91602002-01-30 05:46:57 +00005088
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005089 case POPEN_3:
5090 {
5091 char *m1, *m2;
5092 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005093
Tim Peters7dca21e2002-08-19 00:42:29 +00005094 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005095 m1 = "r";
5096 m2 = "w";
5097 } else {
5098 m1 = "rb";
5099 m2 = "wb";
5100 }
5101
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005102 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005103 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005104 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005105 f2 = _fdopen(fd2, m1);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005106 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005107 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005108 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00005109 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5110 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005111 PyFile_SetBufSize(p1, 0);
5112 PyFile_SetBufSize(p2, 0);
5113 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005114 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00005115 Py_XDECREF(p1);
5116 Py_XDECREF(p2);
5117 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00005118 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005119 break;
5120 }
5121 }
5122
5123 if (n == POPEN_4) {
5124 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005125 hChildStdinRd,
5126 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005127 hChildStdoutWr,
5128 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005129 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005130 }
5131 else {
5132 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005133 hChildStdinRd,
5134 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005135 hChildStderrWr,
5136 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005137 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005138 }
5139
Mark Hammondb37a3732000-08-14 04:47:33 +00005140 /*
5141 * Insert the files we've created into the process dictionary
5142 * all referencing the list with the process handle and the
5143 * initial number of files (see description below in _PyPclose).
5144 * Since if _PyPclose later tried to wait on a process when all
5145 * handles weren't closed, it could create a deadlock with the
5146 * child, we spend some energy here to try to ensure that we
5147 * either insert all file handles into the dictionary or none
5148 * at all. It's a little clumsy with the various popen modes
5149 * and variable number of files involved.
5150 */
5151 if (!_PyPopenProcs) {
5152 _PyPopenProcs = PyDict_New();
5153 }
5154
5155 if (_PyPopenProcs) {
5156 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5157 int ins_rc[3];
5158
5159 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5160 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
5161
5162 procObj = PyList_New(2);
5163 hProcessObj = PyLong_FromVoidPtr(hProcess);
5164 intObj = PyInt_FromLong(file_count);
5165
5166 if (procObj && hProcessObj && intObj) {
5167 PyList_SetItem(procObj,0,hProcessObj);
5168 PyList_SetItem(procObj,1,intObj);
5169
5170 fileObj[0] = PyLong_FromVoidPtr(f1);
5171 if (fileObj[0]) {
5172 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5173 fileObj[0],
5174 procObj);
5175 }
5176 if (file_count >= 2) {
5177 fileObj[1] = PyLong_FromVoidPtr(f2);
5178 if (fileObj[1]) {
5179 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5180 fileObj[1],
5181 procObj);
5182 }
5183 }
5184 if (file_count >= 3) {
5185 fileObj[2] = PyLong_FromVoidPtr(f3);
5186 if (fileObj[2]) {
5187 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5188 fileObj[2],
5189 procObj);
5190 }
5191 }
5192
5193 if (ins_rc[0] < 0 || !fileObj[0] ||
5194 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5195 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5196 /* Something failed - remove any dictionary
5197 * entries that did make it.
5198 */
5199 if (!ins_rc[0] && fileObj[0]) {
5200 PyDict_DelItem(_PyPopenProcs,
5201 fileObj[0]);
5202 }
5203 if (!ins_rc[1] && fileObj[1]) {
5204 PyDict_DelItem(_PyPopenProcs,
5205 fileObj[1]);
5206 }
5207 if (!ins_rc[2] && fileObj[2]) {
5208 PyDict_DelItem(_PyPopenProcs,
5209 fileObj[2]);
5210 }
5211 }
5212 }
Tim Peters5aa91602002-01-30 05:46:57 +00005213
Mark Hammondb37a3732000-08-14 04:47:33 +00005214 /*
5215 * Clean up our localized references for the dictionary keys
5216 * and value since PyDict_SetItem will Py_INCREF any copies
5217 * that got placed in the dictionary.
5218 */
5219 Py_XDECREF(procObj);
5220 Py_XDECREF(fileObj[0]);
5221 Py_XDECREF(fileObj[1]);
5222 Py_XDECREF(fileObj[2]);
5223 }
5224
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005225 /* Child is launched. Close the parents copy of those pipe
5226 * handles that only the child should have open. You need to
5227 * make sure that no handles to the write end of the output pipe
5228 * are maintained in this process or else the pipe will not close
5229 * when the child process exits and the ReadFile will hang. */
5230
5231 if (!CloseHandle(hChildStdinRd))
5232 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005233
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005234 if (!CloseHandle(hChildStdoutWr))
5235 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005236
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005237 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5238 return win32_error("CloseHandle", NULL);
5239
5240 return f;
5241}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005242
5243/*
5244 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5245 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005246 *
5247 * This function uses the _PyPopenProcs dictionary in order to map the
5248 * input file pointer to information about the process that was
5249 * originally created by the popen* call that created the file pointer.
5250 * The dictionary uses the file pointer as a key (with one entry
5251 * inserted for each file returned by the original popen* call) and a
5252 * single list object as the value for all files from a single call.
5253 * The list object contains the Win32 process handle at [0], and a file
5254 * count at [1], which is initialized to the total number of file
5255 * handles using that list.
5256 *
5257 * This function closes whichever handle it is passed, and decrements
5258 * the file count in the dictionary for the process handle pointed to
5259 * by this file. On the last close (when the file count reaches zero),
5260 * this function will wait for the child process and then return its
5261 * exit code as the result of the close() operation. This permits the
5262 * files to be closed in any order - it is always the close() of the
5263 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005264 *
5265 * NOTE: This function is currently called with the GIL released.
5266 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005267 */
Tim Peters736aa322000-09-01 06:51:24 +00005268
Fredrik Lundh56055a42000-07-23 19:47:12 +00005269static int _PyPclose(FILE *file)
5270{
Fredrik Lundh20318932000-07-26 17:29:12 +00005271 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005272 DWORD exit_code;
5273 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00005274 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5275 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005276#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005277 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005278#endif
5279
Fredrik Lundh20318932000-07-26 17:29:12 +00005280 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00005281 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00005282 */
5283 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005284#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005285 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005286#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005287 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005288 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5289 (procObj = PyDict_GetItem(_PyPopenProcs,
5290 fileObj)) != NULL &&
5291 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5292 (intObj = PyList_GetItem(procObj,1)) != NULL) {
5293
5294 hProcess = PyLong_AsVoidPtr(hProcessObj);
5295 file_count = PyInt_AsLong(intObj);
5296
5297 if (file_count > 1) {
5298 /* Still other files referencing process */
5299 file_count--;
5300 PyList_SetItem(procObj,1,
5301 PyInt_FromLong(file_count));
5302 } else {
5303 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00005304 if (result != EOF &&
5305 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5306 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00005307 /* Possible truncation here in 16-bit environments, but
5308 * real exit codes are just the lower byte in any event.
5309 */
5310 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005311 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00005312 /* Indicate failure - this will cause the file object
5313 * to raise an I/O error and translate the last Win32
5314 * error code from errno. We do have a problem with
5315 * last errors that overlap the normal errno table,
5316 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005317 */
Fredrik Lundh20318932000-07-26 17:29:12 +00005318 if (result != EOF) {
5319 /* If the error wasn't from the fclose(), then
5320 * set errno for the file object error handling.
5321 */
5322 errno = GetLastError();
5323 }
5324 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005325 }
5326
5327 /* Free up the native handle at this point */
5328 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00005329 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005330
Mark Hammondb37a3732000-08-14 04:47:33 +00005331 /* Remove this file pointer from dictionary */
5332 PyDict_DelItem(_PyPopenProcs, fileObj);
5333
5334 if (PyDict_Size(_PyPopenProcs) == 0) {
5335 Py_DECREF(_PyPopenProcs);
5336 _PyPopenProcs = NULL;
5337 }
5338
5339 } /* if object retrieval ok */
5340
5341 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005342 } /* if _PyPopenProcs */
5343
Tim Peters736aa322000-09-01 06:51:24 +00005344#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005345 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005346#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005347 return result;
5348}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005349
5350#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005351static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005352posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005353{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005354 char *name;
5355 char *mode = "r";
5356 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00005357 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005358 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005359 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00005360 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00005361 /* Strip mode of binary or text modifiers */
5362 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5363 mode = "r";
5364 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5365 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00005366 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005367 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005368 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00005369 if (fp == NULL)
5370 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005371 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005372 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005373 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005374 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005375}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005376
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005377#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005378#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005379
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005380
Guido van Rossumb6775db1994-08-01 11:34:53 +00005381#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005382PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005383"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005384Set the current process's user id.");
5385
Barry Warsaw53699e91996-12-10 23:23:01 +00005386static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005387posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005388{
5389 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005390 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005391 return NULL;
5392 if (setuid(uid) < 0)
5393 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005394 Py_INCREF(Py_None);
5395 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005396}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005397#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005398
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005399
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005400#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005401PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005402"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005403Set the current process's effective user id.");
5404
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005405static PyObject *
5406posix_seteuid (PyObject *self, PyObject *args)
5407{
5408 int euid;
5409 if (!PyArg_ParseTuple(args, "i", &euid)) {
5410 return NULL;
5411 } else if (seteuid(euid) < 0) {
5412 return posix_error();
5413 } else {
5414 Py_INCREF(Py_None);
5415 return Py_None;
5416 }
5417}
5418#endif /* HAVE_SETEUID */
5419
5420#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005421PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005422"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005423Set the current process's effective group id.");
5424
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005425static PyObject *
5426posix_setegid (PyObject *self, PyObject *args)
5427{
5428 int egid;
5429 if (!PyArg_ParseTuple(args, "i", &egid)) {
5430 return NULL;
5431 } else if (setegid(egid) < 0) {
5432 return posix_error();
5433 } else {
5434 Py_INCREF(Py_None);
5435 return Py_None;
5436 }
5437}
5438#endif /* HAVE_SETEGID */
5439
5440#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005441PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005442"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005443Set the current process's real and effective user ids.");
5444
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005445static PyObject *
5446posix_setreuid (PyObject *self, PyObject *args)
5447{
5448 int ruid, euid;
5449 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
5450 return NULL;
5451 } else if (setreuid(ruid, euid) < 0) {
5452 return posix_error();
5453 } else {
5454 Py_INCREF(Py_None);
5455 return Py_None;
5456 }
5457}
5458#endif /* HAVE_SETREUID */
5459
5460#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005461PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005462"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005463Set the current process's real and effective group ids.");
5464
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005465static PyObject *
5466posix_setregid (PyObject *self, PyObject *args)
5467{
5468 int rgid, egid;
5469 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
5470 return NULL;
5471 } else if (setregid(rgid, egid) < 0) {
5472 return posix_error();
5473 } else {
5474 Py_INCREF(Py_None);
5475 return Py_None;
5476 }
5477}
5478#endif /* HAVE_SETREGID */
5479
Guido van Rossumb6775db1994-08-01 11:34:53 +00005480#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005481PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005482"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005483Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005484
Barry Warsaw53699e91996-12-10 23:23:01 +00005485static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005486posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005487{
5488 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005489 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005490 return NULL;
5491 if (setgid(gid) < 0)
5492 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005493 Py_INCREF(Py_None);
5494 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005495}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005496#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005497
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005498#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005499PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005500"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005501Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005502
5503static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005504posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005505{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005506 int i, len;
5507 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005508
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005509 if (!PySequence_Check(groups)) {
5510 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5511 return NULL;
5512 }
5513 len = PySequence_Size(groups);
5514 if (len > MAX_GROUPS) {
5515 PyErr_SetString(PyExc_ValueError, "too many groups");
5516 return NULL;
5517 }
5518 for(i = 0; i < len; i++) {
5519 PyObject *elem;
5520 elem = PySequence_GetItem(groups, i);
5521 if (!elem)
5522 return NULL;
5523 if (!PyInt_Check(elem)) {
Georg Brandla13c2442005-11-22 19:30:31 +00005524 if (!PyLong_Check(elem)) {
5525 PyErr_SetString(PyExc_TypeError,
5526 "groups must be integers");
5527 Py_DECREF(elem);
5528 return NULL;
5529 } else {
5530 unsigned long x = PyLong_AsUnsignedLong(elem);
5531 if (PyErr_Occurred()) {
5532 PyErr_SetString(PyExc_TypeError,
5533 "group id too big");
5534 Py_DECREF(elem);
5535 return NULL;
5536 }
5537 grouplist[i] = x;
5538 /* read back the value to see if it fitted in gid_t */
5539 if (grouplist[i] != x) {
5540 PyErr_SetString(PyExc_TypeError,
5541 "group id too big");
5542 Py_DECREF(elem);
5543 return NULL;
5544 }
5545 }
5546 } else {
5547 long x = PyInt_AsLong(elem);
5548 grouplist[i] = x;
5549 if (grouplist[i] != x) {
5550 PyErr_SetString(PyExc_TypeError,
5551 "group id too big");
5552 Py_DECREF(elem);
5553 return NULL;
5554 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005555 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005556 Py_DECREF(elem);
5557 }
5558
5559 if (setgroups(len, grouplist) < 0)
5560 return posix_error();
5561 Py_INCREF(Py_None);
5562 return Py_None;
5563}
5564#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005565
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005566#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00005567static PyObject *
5568wait_helper(int pid, int status, struct rusage *ru)
5569{
5570 PyObject *result;
5571 static PyObject *struct_rusage;
5572
5573 if (pid == -1)
5574 return posix_error();
5575
5576 if (struct_rusage == NULL) {
5577 PyObject *m = PyImport_ImportModule("resource");
5578 if (m == NULL)
5579 return NULL;
5580 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5581 Py_DECREF(m);
5582 if (struct_rusage == NULL)
5583 return NULL;
5584 }
5585
5586 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5587 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5588 if (!result)
5589 return NULL;
5590
5591#ifndef doubletime
5592#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5593#endif
5594
5595 PyStructSequence_SET_ITEM(result, 0,
5596 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5597 PyStructSequence_SET_ITEM(result, 1,
5598 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5599#define SET_INT(result, index, value)\
5600 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5601 SET_INT(result, 2, ru->ru_maxrss);
5602 SET_INT(result, 3, ru->ru_ixrss);
5603 SET_INT(result, 4, ru->ru_idrss);
5604 SET_INT(result, 5, ru->ru_isrss);
5605 SET_INT(result, 6, ru->ru_minflt);
5606 SET_INT(result, 7, ru->ru_majflt);
5607 SET_INT(result, 8, ru->ru_nswap);
5608 SET_INT(result, 9, ru->ru_inblock);
5609 SET_INT(result, 10, ru->ru_oublock);
5610 SET_INT(result, 11, ru->ru_msgsnd);
5611 SET_INT(result, 12, ru->ru_msgrcv);
5612 SET_INT(result, 13, ru->ru_nsignals);
5613 SET_INT(result, 14, ru->ru_nvcsw);
5614 SET_INT(result, 15, ru->ru_nivcsw);
5615#undef SET_INT
5616
5617 if (PyErr_Occurred()) {
5618 Py_DECREF(result);
5619 return NULL;
5620 }
5621
Neal Norwitz9b00a562006-03-20 08:47:12 +00005622 return Py_BuildValue("iiN", pid, status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00005623}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005624#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00005625
5626#ifdef HAVE_WAIT3
5627PyDoc_STRVAR(posix_wait3__doc__,
5628"wait3(options) -> (pid, status, rusage)\n\n\
5629Wait for completion of a child process.");
5630
5631static PyObject *
5632posix_wait3(PyObject *self, PyObject *args)
5633{
5634 int pid, options;
5635 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005636 WAIT_TYPE status;
5637 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005638
5639 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5640 return NULL;
5641
5642 Py_BEGIN_ALLOW_THREADS
5643 pid = wait3(&status, options, &ru);
5644 Py_END_ALLOW_THREADS
5645
Neal Norwitzd5a37542006-03-20 06:48:34 +00005646 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005647}
5648#endif /* HAVE_WAIT3 */
5649
5650#ifdef HAVE_WAIT4
5651PyDoc_STRVAR(posix_wait4__doc__,
5652"wait4(pid, options) -> (pid, status, rusage)\n\n\
5653Wait for completion of a given child process.");
5654
5655static PyObject *
5656posix_wait4(PyObject *self, PyObject *args)
5657{
5658 int pid, options;
5659 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005660 WAIT_TYPE status;
5661 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005662
5663 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
5664 return NULL;
5665
5666 Py_BEGIN_ALLOW_THREADS
5667 pid = wait4(pid, &status, options, &ru);
5668 Py_END_ALLOW_THREADS
5669
Neal Norwitzd5a37542006-03-20 06:48:34 +00005670 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005671}
5672#endif /* HAVE_WAIT4 */
5673
Guido van Rossumb6775db1994-08-01 11:34:53 +00005674#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005675PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005676"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005677Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005678
Barry Warsaw53699e91996-12-10 23:23:01 +00005679static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005680posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005681{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005682 int pid, options;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005683 WAIT_TYPE status;
5684 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005685
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005686 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00005687 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005688 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00005689 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00005690 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00005691 if (pid == -1)
5692 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005693
5694 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005695}
5696
Tim Petersab034fa2002-02-01 11:27:43 +00005697#elif defined(HAVE_CWAIT)
5698
5699/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005700PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005701"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005702"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005703
5704static PyObject *
5705posix_waitpid(PyObject *self, PyObject *args)
5706{
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005707 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005708 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005709
5710 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5711 return NULL;
5712 Py_BEGIN_ALLOW_THREADS
5713 pid = _cwait(&status, pid, options);
5714 Py_END_ALLOW_THREADS
5715 if (pid == -1)
5716 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005717
5718 /* shift the status left a byte so this is more like the POSIX waitpid */
5719 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005720}
5721#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005722
Guido van Rossumad0ee831995-03-01 10:34:45 +00005723#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005724PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005725"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005726Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005727
Barry Warsaw53699e91996-12-10 23:23:01 +00005728static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005729posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005730{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005731 int pid;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005732 WAIT_TYPE status;
5733 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005734
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005735 Py_BEGIN_ALLOW_THREADS
5736 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00005737 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00005738 if (pid == -1)
5739 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005740
5741 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005742}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005743#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005744
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005746PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005747"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005748Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005749
Barry Warsaw53699e91996-12-10 23:23:01 +00005750static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005751posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005752{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005753#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005754 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005755#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005756#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00005757 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005758#else
5759 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5760#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005761#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005762}
5763
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005764
Guido van Rossumb6775db1994-08-01 11:34:53 +00005765#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005766PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005767"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005768Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005769
Barry Warsaw53699e91996-12-10 23:23:01 +00005770static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005771posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005772{
Ronald Oussoren10168f22006-10-22 10:45:18 +00005773 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00005774 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005775 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005776 int n;
Ronald Oussoren10168f22006-10-22 10:45:18 +00005777#ifdef Py_USING_UNICODE
5778 int arg_is_unicode = 0;
5779#endif
5780
5781 if (!PyArg_ParseTuple(args, "et:readlink",
5782 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005783 return NULL;
Ronald Oussoren10168f22006-10-22 10:45:18 +00005784#ifdef Py_USING_UNICODE
5785 v = PySequence_GetItem(args, 0);
Neal Norwitz91a57212007-08-12 17:11:13 +00005786 if (v == NULL) {
5787 PyMem_Free(path);
5788 return NULL;
5789 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00005790
5791 if (PyUnicode_Check(v)) {
5792 arg_is_unicode = 1;
5793 }
5794 Py_DECREF(v);
5795#endif
5796
Barry Warsaw53699e91996-12-10 23:23:01 +00005797 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00005798 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00005799 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005800 if (n < 0)
Neal Norwitz91a57212007-08-12 17:11:13 +00005801 return posix_error_with_allocated_filename(path);
Ronald Oussoren10168f22006-10-22 10:45:18 +00005802
Neal Norwitz91a57212007-08-12 17:11:13 +00005803 PyMem_Free(path);
Ronald Oussoren10168f22006-10-22 10:45:18 +00005804 v = PyString_FromStringAndSize(buf, n);
5805#ifdef Py_USING_UNICODE
5806 if (arg_is_unicode) {
5807 PyObject *w;
5808
5809 w = PyUnicode_FromEncodedObject(v,
5810 Py_FileSystemDefaultEncoding,
5811 "strict");
5812 if (w != NULL) {
5813 Py_DECREF(v);
5814 v = w;
5815 }
5816 else {
5817 /* fall back to the original byte string, as
5818 discussed in patch #683592 */
5819 PyErr_Clear();
5820 }
5821 }
5822#endif
5823 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005824}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005825#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005826
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005827
Guido van Rossumb6775db1994-08-01 11:34:53 +00005828#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005829PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005830"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005831Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005832
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005833static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005834posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005835{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00005836 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005837}
5838#endif /* HAVE_SYMLINK */
5839
5840
5841#ifdef HAVE_TIMES
5842#ifndef HZ
5843#define HZ 60 /* Universal constant :-) */
5844#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005845
Guido van Rossumd48f2521997-12-05 22:19:34 +00005846#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5847static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005848system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005849{
5850 ULONG value = 0;
5851
5852 Py_BEGIN_ALLOW_THREADS
5853 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5854 Py_END_ALLOW_THREADS
5855
5856 return value;
5857}
5858
5859static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005860posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005861{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005862 /* Currently Only Uptime is Provided -- Others Later */
5863 return Py_BuildValue("ddddd",
5864 (double)0 /* t.tms_utime / HZ */,
5865 (double)0 /* t.tms_stime / HZ */,
5866 (double)0 /* t.tms_cutime / HZ */,
5867 (double)0 /* t.tms_cstime / HZ */,
5868 (double)system_uptime() / 1000);
5869}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005870#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005871static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005872posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005873{
5874 struct tms t;
5875 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005876 errno = 0;
5877 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005878 if (c == (clock_t) -1)
5879 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005880 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005881 (double)t.tms_utime / HZ,
5882 (double)t.tms_stime / HZ,
5883 (double)t.tms_cutime / HZ,
5884 (double)t.tms_cstime / HZ,
5885 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005886}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005887#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005888#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005889
5890
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005891#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005892#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005893static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005894posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005895{
5896 FILETIME create, exit, kernel, user;
5897 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005898 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005899 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5900 /* The fields of a FILETIME structure are the hi and lo part
5901 of a 64-bit value expressed in 100 nanosecond units.
5902 1e7 is one second in such units; 1e-7 the inverse.
5903 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5904 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005905 return Py_BuildValue(
5906 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005907 (double)(kernel.dwHighDateTime*429.4967296 +
5908 kernel.dwLowDateTime*1e-7),
5909 (double)(user.dwHighDateTime*429.4967296 +
5910 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005911 (double)0,
5912 (double)0,
5913 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005914}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005915#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005916
5917#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005918PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005919"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005920Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005921#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005922
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005923
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005924#ifdef HAVE_GETSID
5925PyDoc_STRVAR(posix_getsid__doc__,
5926"getsid(pid) -> sid\n\n\
5927Call the system call getsid().");
5928
5929static PyObject *
5930posix_getsid(PyObject *self, PyObject *args)
5931{
5932 int pid, sid;
5933 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5934 return NULL;
5935 sid = getsid(pid);
5936 if (sid < 0)
5937 return posix_error();
5938 return PyInt_FromLong((long)sid);
5939}
5940#endif /* HAVE_GETSID */
5941
5942
Guido van Rossumb6775db1994-08-01 11:34:53 +00005943#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005944PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005945"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005946Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005947
Barry Warsaw53699e91996-12-10 23:23:01 +00005948static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005949posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005950{
Guido van Rossum687dd131993-05-17 08:34:16 +00005951 if (setsid() < 0)
5952 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005953 Py_INCREF(Py_None);
5954 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005955}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005956#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005957
Guido van Rossumb6775db1994-08-01 11:34:53 +00005958#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005959PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005960"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005961Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005962
Barry Warsaw53699e91996-12-10 23:23:01 +00005963static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005964posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005965{
5966 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005967 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005968 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005969 if (setpgid(pid, pgrp) < 0)
5970 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005971 Py_INCREF(Py_None);
5972 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005973}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005974#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005975
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005976
Guido van Rossumb6775db1994-08-01 11:34:53 +00005977#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005978PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005979"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005980Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005981
Barry Warsaw53699e91996-12-10 23:23:01 +00005982static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005983posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005984{
5985 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005986 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005987 return NULL;
5988 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005989 if (pgid < 0)
5990 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005991 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005992}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005993#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005994
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005995
Guido van Rossumb6775db1994-08-01 11:34:53 +00005996#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005997PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005998"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005999Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006000
Barry Warsaw53699e91996-12-10 23:23:01 +00006001static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006002posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006003{
6004 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006005 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00006006 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006007 if (tcsetpgrp(fd, pgid) < 0)
6008 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00006009 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00006010 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006011}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006012#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006013
Guido van Rossum687dd131993-05-17 08:34:16 +00006014/* Functions acting on file descriptors */
6015
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006016PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006017"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006018Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006019
Barry Warsaw53699e91996-12-10 23:23:01 +00006020static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006021posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006022{
Mark Hammondef8b6542001-05-13 08:04:26 +00006023 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006024 int flag;
6025 int mode = 0777;
6026 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006027
6028#ifdef MS_WINDOWS
6029 if (unicode_file_names()) {
6030 PyUnicodeObject *po;
6031 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6032 Py_BEGIN_ALLOW_THREADS
6033 /* PyUnicode_AS_UNICODE OK without thread
6034 lock as it is a simple dereference. */
6035 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6036 Py_END_ALLOW_THREADS
6037 if (fd < 0)
6038 return posix_error();
6039 return PyInt_FromLong((long)fd);
6040 }
6041 /* Drop the argument parsing error as narrow strings
6042 are also valid. */
6043 PyErr_Clear();
6044 }
6045#endif
6046
Tim Peters5aa91602002-01-30 05:46:57 +00006047 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00006048 Py_FileSystemDefaultEncoding, &file,
6049 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00006050 return NULL;
6051
Barry Warsaw53699e91996-12-10 23:23:01 +00006052 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006053 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00006054 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006055 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00006056 return posix_error_with_allocated_filename(file);
6057 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00006058 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006059}
6060
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006061
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006062PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006063"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006064Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006065
Barry Warsaw53699e91996-12-10 23:23:01 +00006066static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006067posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006068{
6069 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006070 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006071 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006072 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006073 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006074 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006075 if (res < 0)
6076 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006077 Py_INCREF(Py_None);
6078 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006079}
6080
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006082PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006083"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006084Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006085
Barry Warsaw53699e91996-12-10 23:23:01 +00006086static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006087posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006088{
6089 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006090 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006091 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006092 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006093 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006094 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006095 if (fd < 0)
6096 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006097 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006098}
6099
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006101PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006102"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006103Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006104
Barry Warsaw53699e91996-12-10 23:23:01 +00006105static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006106posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006107{
6108 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006109 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00006110 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006111 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006112 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00006113 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006114 if (res < 0)
6115 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006116 Py_INCREF(Py_None);
6117 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006118}
6119
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006121PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006122"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006123Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006124
Barry Warsaw53699e91996-12-10 23:23:01 +00006125static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006126posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006127{
6128 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006129#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006130 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006131#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00006132 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006133#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006134 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006135 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00006136 return NULL;
6137#ifdef SEEK_SET
6138 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6139 switch (how) {
6140 case 0: how = SEEK_SET; break;
6141 case 1: how = SEEK_CUR; break;
6142 case 2: how = SEEK_END; break;
6143 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006144#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006145
6146#if !defined(HAVE_LARGEFILE_SUPPORT)
6147 pos = PyInt_AsLong(posobj);
6148#else
6149 pos = PyLong_Check(posobj) ?
6150 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
6151#endif
6152 if (PyErr_Occurred())
6153 return NULL;
6154
Barry Warsaw53699e91996-12-10 23:23:01 +00006155 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006156#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00006157 res = _lseeki64(fd, pos, how);
6158#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006159 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006160#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006161 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006162 if (res < 0)
6163 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006164
6165#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00006166 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006167#else
6168 return PyLong_FromLongLong(res);
6169#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006170}
6171
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006172
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006173PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006174"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006175Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006176
Barry Warsaw53699e91996-12-10 23:23:01 +00006177static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006178posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006179{
Guido van Rossum8bac5461996-06-11 18:38:48 +00006180 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00006181 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006182 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006183 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00006184 if (size < 0) {
6185 errno = EINVAL;
6186 return posix_error();
6187 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006188 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006189 if (buffer == NULL)
6190 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006191 Py_BEGIN_ALLOW_THREADS
6192 n = read(fd, PyString_AsString(buffer), size);
6193 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00006194 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006195 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00006196 return posix_error();
6197 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00006198 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00006199 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00006200 return buffer;
6201}
6202
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006204PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006205"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006206Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006207
Barry Warsaw53699e91996-12-10 23:23:01 +00006208static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006209posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006210{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006211 int fd;
6212 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00006213 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006214
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006215 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006216 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006217 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006218 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00006219 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006220 if (size < 0)
6221 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006222 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006223}
6224
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006225
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006226PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006227"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006228Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006229
Barry Warsaw53699e91996-12-10 23:23:01 +00006230static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006231posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006232{
6233 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00006234 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00006235 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006236 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006237 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006238#ifdef __VMS
6239 /* on OpenVMS we must ensure that all bytes are written to the file */
6240 fsync(fd);
6241#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006242 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00006243 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00006244 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00006245 if (res != 0) {
6246#ifdef MS_WINDOWS
6247 return win32_error("fstat", NULL);
6248#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006249 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006250#endif
6251 }
Tim Peters5aa91602002-01-30 05:46:57 +00006252
Martin v. Löwis14694662006-02-03 12:54:16 +00006253 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006254}
6255
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006256
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006257PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006258"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006259Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006260
Barry Warsaw53699e91996-12-10 23:23:01 +00006261static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006262posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006263{
Guido van Rossum687dd131993-05-17 08:34:16 +00006264 int fd;
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006265 char *orgmode = "r";
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006266 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00006267 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00006268 PyObject *f;
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006269 char *mode;
6270 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00006271 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006272
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006273 /* Sanitize mode. See fileobject.c */
6274 mode = PyMem_MALLOC(strlen(orgmode)+3);
6275 if (!mode) {
6276 PyErr_NoMemory();
6277 return NULL;
6278 }
6279 strcpy(mode, orgmode);
6280 if (_PyFile_SanitizeMode(mode)) {
6281 PyMem_FREE(mode);
Thomas Heller1f043e22002-11-07 16:00:59 +00006282 return NULL;
6283 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006284 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006285#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Georg Brandl54a188a2006-03-31 20:00:11 +00006286 if (mode[0] == 'a') {
6287 /* try to make sure the O_APPEND flag is set */
6288 int flags;
6289 flags = fcntl(fd, F_GETFL);
6290 if (flags != -1)
6291 fcntl(fd, F_SETFL, flags | O_APPEND);
6292 fp = fdopen(fd, mode);
Thomas Wouters2a9a6b02006-03-31 22:38:19 +00006293 if (fp == NULL && flags != -1)
Georg Brandl54a188a2006-03-31 20:00:11 +00006294 /* restore old mode if fdopen failed */
6295 fcntl(fd, F_SETFL, flags);
6296 } else {
6297 fp = fdopen(fd, mode);
6298 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006299#else
6300 fp = fdopen(fd, mode);
6301#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006302 Py_END_ALLOW_THREADS
Neal Norwitzd83eb312007-05-02 04:47:55 +00006303 PyMem_FREE(mode);
Guido van Rossum687dd131993-05-17 08:34:16 +00006304 if (fp == NULL)
6305 return posix_error();
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006306 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006307 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00006308 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006309 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006310}
6311
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006312PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006313"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006314Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006315connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006316
6317static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006318posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006319{
6320 int fd;
6321 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6322 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006323 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006324}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006325
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006326#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006327PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006328"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006329Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006330
Barry Warsaw53699e91996-12-10 23:23:01 +00006331static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006332posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006333{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006334#if defined(PYOS_OS2)
6335 HFILE read, write;
6336 APIRET rc;
6337
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006338 Py_BEGIN_ALLOW_THREADS
6339 rc = DosCreatePipe( &read, &write, 4096);
6340 Py_END_ALLOW_THREADS
6341 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006342 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006343
6344 return Py_BuildValue("(ii)", read, write);
6345#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006346#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00006347 int fds[2];
6348 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00006349 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006350 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00006351 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006352 if (res != 0)
6353 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006354 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006355#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00006356 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006357 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00006358 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00006359 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006360 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00006361 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00006362 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006363 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00006364 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6365 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006366 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006367#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006368#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006369}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006370#endif /* HAVE_PIPE */
6371
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006372
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006373#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006374PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006375"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006376Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006377
Barry Warsaw53699e91996-12-10 23:23:01 +00006378static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006379posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006380{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006381 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006382 int mode = 0666;
6383 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006384 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006385 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006386 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006387 res = mkfifo(filename, mode);
6388 Py_END_ALLOW_THREADS
6389 if (res < 0)
6390 return posix_error();
6391 Py_INCREF(Py_None);
6392 return Py_None;
6393}
6394#endif
6395
6396
Neal Norwitz11690112002-07-30 01:08:28 +00006397#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006398PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006399"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006400Create a filesystem node (file, device special file or named pipe)\n\
6401named filename. mode specifies both the permissions to use and the\n\
6402type of node to be created, being combined (bitwise OR) with one of\n\
6403S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006404device defines the newly created device special file (probably using\n\
6405os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006406
6407
6408static PyObject *
6409posix_mknod(PyObject *self, PyObject *args)
6410{
6411 char *filename;
6412 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006413 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006414 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00006415 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006416 return NULL;
6417 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006418 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00006419 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006420 if (res < 0)
6421 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006422 Py_INCREF(Py_None);
6423 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006424}
6425#endif
6426
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006427#ifdef HAVE_DEVICE_MACROS
6428PyDoc_STRVAR(posix_major__doc__,
6429"major(device) -> major number\n\
6430Extracts a device major number from a raw device number.");
6431
6432static PyObject *
6433posix_major(PyObject *self, PyObject *args)
6434{
6435 int device;
6436 if (!PyArg_ParseTuple(args, "i:major", &device))
6437 return NULL;
6438 return PyInt_FromLong((long)major(device));
6439}
6440
6441PyDoc_STRVAR(posix_minor__doc__,
6442"minor(device) -> minor number\n\
6443Extracts a device minor number from a raw device number.");
6444
6445static PyObject *
6446posix_minor(PyObject *self, PyObject *args)
6447{
6448 int device;
6449 if (!PyArg_ParseTuple(args, "i:minor", &device))
6450 return NULL;
6451 return PyInt_FromLong((long)minor(device));
6452}
6453
6454PyDoc_STRVAR(posix_makedev__doc__,
6455"makedev(major, minor) -> device number\n\
6456Composes a raw device number from the major and minor device numbers.");
6457
6458static PyObject *
6459posix_makedev(PyObject *self, PyObject *args)
6460{
6461 int major, minor;
6462 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6463 return NULL;
6464 return PyInt_FromLong((long)makedev(major, minor));
6465}
6466#endif /* device macros */
6467
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006468
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006469#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006470PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006471"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006472Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006473
Barry Warsaw53699e91996-12-10 23:23:01 +00006474static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006475posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006476{
6477 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006478 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006479 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006480 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006481
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006482 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006483 return NULL;
6484
6485#if !defined(HAVE_LARGEFILE_SUPPORT)
6486 length = PyInt_AsLong(lenobj);
6487#else
6488 length = PyLong_Check(lenobj) ?
6489 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6490#endif
6491 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006492 return NULL;
6493
Barry Warsaw53699e91996-12-10 23:23:01 +00006494 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006495 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00006496 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006497 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006498 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006499 return NULL;
6500 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006501 Py_INCREF(Py_None);
6502 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006503}
6504#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006505
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006506#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006507PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006508"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006509Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006510
Fred Drake762e2061999-08-26 17:23:54 +00006511/* Save putenv() parameters as values here, so we can collect them when they
6512 * get re-set with another call for the same key. */
6513static PyObject *posix_putenv_garbage;
6514
Tim Peters5aa91602002-01-30 05:46:57 +00006515static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006516posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006517{
6518 char *s1, *s2;
Anthony Baxter64182fe2006-04-11 12:14:09 +00006519 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00006520 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00006521 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006522
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006523 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006524 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006525
6526#if defined(PYOS_OS2)
6527 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6528 APIRET rc;
6529
Guido van Rossumd48f2521997-12-05 22:19:34 +00006530 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6531 if (rc != NO_ERROR)
6532 return os2_error(rc);
6533
6534 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6535 APIRET rc;
6536
Guido van Rossumd48f2521997-12-05 22:19:34 +00006537 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6538 if (rc != NO_ERROR)
6539 return os2_error(rc);
6540 } else {
6541#endif
6542
Fred Drake762e2061999-08-26 17:23:54 +00006543 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00006544 len = strlen(s1) + strlen(s2) + 2;
6545 /* len includes space for a trailing \0; the size arg to
6546 PyString_FromStringAndSize does not count that */
6547 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00006548 if (newstr == NULL)
6549 return PyErr_NoMemory();
Anthony Baxter64182fe2006-04-11 12:14:09 +00006550 newenv = PyString_AS_STRING(newstr);
6551 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6552 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00006553 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006554 posix_error();
6555 return NULL;
6556 }
Fred Drake762e2061999-08-26 17:23:54 +00006557 /* Install the first arg and newstr in posix_putenv_garbage;
6558 * this will cause previous value to be collected. This has to
6559 * happen after the real putenv() call because the old value
6560 * was still accessible until then. */
6561 if (PyDict_SetItem(posix_putenv_garbage,
6562 PyTuple_GET_ITEM(args, 0), newstr)) {
6563 /* really not much we can do; just leak */
6564 PyErr_Clear();
6565 }
6566 else {
6567 Py_DECREF(newstr);
6568 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006569
6570#if defined(PYOS_OS2)
6571 }
6572#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006573 Py_INCREF(Py_None);
6574 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006575}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006576#endif /* putenv */
6577
Guido van Rossumc524d952001-10-19 01:31:59 +00006578#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006579PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006580"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006581Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006582
6583static PyObject *
6584posix_unsetenv(PyObject *self, PyObject *args)
6585{
6586 char *s1;
6587
6588 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6589 return NULL;
6590
6591 unsetenv(s1);
6592
6593 /* Remove the key from posix_putenv_garbage;
6594 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00006595 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00006596 * old value was still accessible until then.
6597 */
6598 if (PyDict_DelItem(posix_putenv_garbage,
6599 PyTuple_GET_ITEM(args, 0))) {
6600 /* really not much we can do; just leak */
6601 PyErr_Clear();
6602 }
6603
6604 Py_INCREF(Py_None);
6605 return Py_None;
6606}
6607#endif /* unsetenv */
6608
Guido van Rossumb6a47161997-09-15 22:54:34 +00006609#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006610PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006611"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006612Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006613
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006614static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006615posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006616{
6617 int code;
6618 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006619 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00006620 return NULL;
6621 message = strerror(code);
6622 if (message == NULL) {
6623 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00006624 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006625 return NULL;
6626 }
6627 return PyString_FromString(message);
6628}
6629#endif /* strerror */
6630
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006631
Guido van Rossumc9641791998-08-04 15:26:23 +00006632#ifdef HAVE_SYS_WAIT_H
6633
Fred Drake106c1a02002-04-23 15:58:02 +00006634#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006635PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006636"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006637Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006638
6639static PyObject *
6640posix_WCOREDUMP(PyObject *self, PyObject *args)
6641{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006642 WAIT_TYPE status;
6643 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006644
Neal Norwitzd5a37542006-03-20 06:48:34 +00006645 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006646 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006647
6648 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006649}
6650#endif /* WCOREDUMP */
6651
6652#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006653PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006654"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006655Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006656job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006657
6658static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006659posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006660{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006661 WAIT_TYPE status;
6662 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006663
Neal Norwitzd5a37542006-03-20 06:48:34 +00006664 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006665 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006666
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006667 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006668}
6669#endif /* WIFCONTINUED */
6670
Guido van Rossumc9641791998-08-04 15:26:23 +00006671#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006672PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006673"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006674Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006675
6676static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006677posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006678{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006679 WAIT_TYPE status;
6680 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006681
Neal Norwitzd5a37542006-03-20 06:48:34 +00006682 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006683 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006684
Fred Drake106c1a02002-04-23 15:58:02 +00006685 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006686}
6687#endif /* WIFSTOPPED */
6688
6689#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006690PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006691"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006692Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006693
6694static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006695posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006696{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006697 WAIT_TYPE status;
6698 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006699
Neal Norwitzd5a37542006-03-20 06:48:34 +00006700 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006701 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006702
Fred Drake106c1a02002-04-23 15:58:02 +00006703 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006704}
6705#endif /* WIFSIGNALED */
6706
6707#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006708PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006709"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006710Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006711system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006712
6713static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006714posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006715{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006716 WAIT_TYPE status;
6717 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006718
Neal Norwitzd5a37542006-03-20 06:48:34 +00006719 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006720 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006721
Fred Drake106c1a02002-04-23 15:58:02 +00006722 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006723}
6724#endif /* WIFEXITED */
6725
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006726#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006727PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006728"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006729Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006730
6731static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006732posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006733{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006734 WAIT_TYPE status;
6735 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006736
Neal Norwitzd5a37542006-03-20 06:48:34 +00006737 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006738 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006739
Guido van Rossumc9641791998-08-04 15:26:23 +00006740 return Py_BuildValue("i", WEXITSTATUS(status));
6741}
6742#endif /* WEXITSTATUS */
6743
6744#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006745PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006746"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006747Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006748value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006749
6750static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006751posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006752{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006753 WAIT_TYPE status;
6754 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006755
Neal Norwitzd5a37542006-03-20 06:48:34 +00006756 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006757 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006758
Guido van Rossumc9641791998-08-04 15:26:23 +00006759 return Py_BuildValue("i", WTERMSIG(status));
6760}
6761#endif /* WTERMSIG */
6762
6763#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006764PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006765"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006766Return the signal that stopped the process that provided\n\
6767the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006768
6769static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006770posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006771{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006772 WAIT_TYPE status;
6773 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006774
Neal Norwitzd5a37542006-03-20 06:48:34 +00006775 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006776 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006777
Guido van Rossumc9641791998-08-04 15:26:23 +00006778 return Py_BuildValue("i", WSTOPSIG(status));
6779}
6780#endif /* WSTOPSIG */
6781
6782#endif /* HAVE_SYS_WAIT_H */
6783
6784
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006785#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006786#ifdef _SCO_DS
6787/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6788 needed definitions in sys/statvfs.h */
6789#define _SVID3
6790#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006791#include <sys/statvfs.h>
6792
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006793static PyObject*
6794_pystatvfs_fromstructstatvfs(struct statvfs st) {
6795 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6796 if (v == NULL)
6797 return NULL;
6798
6799#if !defined(HAVE_LARGEFILE_SUPPORT)
6800 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6801 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6802 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6803 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6804 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6805 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6806 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6807 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6808 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6809 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6810#else
6811 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6812 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006813 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006814 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006815 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006816 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006817 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006818 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006819 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006820 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006821 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006822 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006823 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006824 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006825 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6826 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6827#endif
6828
6829 return v;
6830}
6831
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006832PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006833"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006834Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006835
6836static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006837posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006838{
6839 int fd, res;
6840 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006841
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006842 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006843 return NULL;
6844 Py_BEGIN_ALLOW_THREADS
6845 res = fstatvfs(fd, &st);
6846 Py_END_ALLOW_THREADS
6847 if (res != 0)
6848 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006849
6850 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006851}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006852#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006853
6854
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006855#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006856#include <sys/statvfs.h>
6857
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006858PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006859"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006860Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006861
6862static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006863posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006864{
6865 char *path;
6866 int res;
6867 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006868 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006869 return NULL;
6870 Py_BEGIN_ALLOW_THREADS
6871 res = statvfs(path, &st);
6872 Py_END_ALLOW_THREADS
6873 if (res != 0)
6874 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006875
6876 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006877}
6878#endif /* HAVE_STATVFS */
6879
6880
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006881#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006882PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006883"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006884Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006885The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006886or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006887
6888static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006889posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006890{
6891 PyObject *result = NULL;
6892 char *dir = NULL;
6893 char *pfx = NULL;
6894 char *name;
6895
6896 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6897 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006898
6899 if (PyErr_Warn(PyExc_RuntimeWarning,
6900 "tempnam is a potential security risk to your program") < 0)
6901 return NULL;
6902
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006903#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006904 name = _tempnam(dir, pfx);
6905#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006906 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006907#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006908 if (name == NULL)
6909 return PyErr_NoMemory();
6910 result = PyString_FromString(name);
6911 free(name);
6912 return result;
6913}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006914#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006915
6916
6917#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006918PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006919"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006920Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006921
6922static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006923posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006924{
6925 FILE *fp;
6926
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006927 fp = tmpfile();
6928 if (fp == NULL)
6929 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006930 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006931}
6932#endif
6933
6934
6935#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006936PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006937"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006938Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006939
6940static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006941posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006942{
6943 char buffer[L_tmpnam];
6944 char *name;
6945
Skip Montanaro95618b52001-08-18 18:52:10 +00006946 if (PyErr_Warn(PyExc_RuntimeWarning,
6947 "tmpnam is a potential security risk to your program") < 0)
6948 return NULL;
6949
Greg Wardb48bc172000-03-01 21:51:56 +00006950#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006951 name = tmpnam_r(buffer);
6952#else
6953 name = tmpnam(buffer);
6954#endif
6955 if (name == NULL) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006956 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006957#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006958 "unexpected NULL from tmpnam_r"
6959#else
6960 "unexpected NULL from tmpnam"
6961#endif
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006962 );
6963 PyErr_SetObject(PyExc_OSError, err);
6964 Py_XDECREF(err);
6965 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006966 }
6967 return PyString_FromString(buffer);
6968}
6969#endif
6970
6971
Fred Drakec9680921999-12-13 16:37:25 +00006972/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6973 * It maps strings representing configuration variable names to
6974 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006975 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006976 * rarely-used constants. There are three separate tables that use
6977 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006978 *
6979 * This code is always included, even if none of the interfaces that
6980 * need it are included. The #if hackery needed to avoid it would be
6981 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006982 */
6983struct constdef {
6984 char *name;
6985 long value;
6986};
6987
Fred Drake12c6e2d1999-12-14 21:25:03 +00006988static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006989conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6990 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006991{
6992 if (PyInt_Check(arg)) {
6993 *valuep = PyInt_AS_LONG(arg);
6994 return 1;
6995 }
6996 if (PyString_Check(arg)) {
6997 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006998 size_t lo = 0;
6999 size_t mid;
7000 size_t hi = tablesize;
7001 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007002 char *confname = PyString_AS_STRING(arg);
7003 while (lo < hi) {
7004 mid = (lo + hi) / 2;
7005 cmp = strcmp(confname, table[mid].name);
7006 if (cmp < 0)
7007 hi = mid;
7008 else if (cmp > 0)
7009 lo = mid + 1;
7010 else {
7011 *valuep = table[mid].value;
7012 return 1;
7013 }
7014 }
7015 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
7016 }
7017 else
7018 PyErr_SetString(PyExc_TypeError,
7019 "configuration names must be strings or integers");
7020 return 0;
7021}
7022
7023
7024#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7025static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007026#ifdef _PC_ABI_AIO_XFER_MAX
7027 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
7028#endif
7029#ifdef _PC_ABI_ASYNC_IO
7030 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
7031#endif
Fred Drakec9680921999-12-13 16:37:25 +00007032#ifdef _PC_ASYNC_IO
7033 {"PC_ASYNC_IO", _PC_ASYNC_IO},
7034#endif
7035#ifdef _PC_CHOWN_RESTRICTED
7036 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
7037#endif
7038#ifdef _PC_FILESIZEBITS
7039 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
7040#endif
7041#ifdef _PC_LAST
7042 {"PC_LAST", _PC_LAST},
7043#endif
7044#ifdef _PC_LINK_MAX
7045 {"PC_LINK_MAX", _PC_LINK_MAX},
7046#endif
7047#ifdef _PC_MAX_CANON
7048 {"PC_MAX_CANON", _PC_MAX_CANON},
7049#endif
7050#ifdef _PC_MAX_INPUT
7051 {"PC_MAX_INPUT", _PC_MAX_INPUT},
7052#endif
7053#ifdef _PC_NAME_MAX
7054 {"PC_NAME_MAX", _PC_NAME_MAX},
7055#endif
7056#ifdef _PC_NO_TRUNC
7057 {"PC_NO_TRUNC", _PC_NO_TRUNC},
7058#endif
7059#ifdef _PC_PATH_MAX
7060 {"PC_PATH_MAX", _PC_PATH_MAX},
7061#endif
7062#ifdef _PC_PIPE_BUF
7063 {"PC_PIPE_BUF", _PC_PIPE_BUF},
7064#endif
7065#ifdef _PC_PRIO_IO
7066 {"PC_PRIO_IO", _PC_PRIO_IO},
7067#endif
7068#ifdef _PC_SOCK_MAXBUF
7069 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
7070#endif
7071#ifdef _PC_SYNC_IO
7072 {"PC_SYNC_IO", _PC_SYNC_IO},
7073#endif
7074#ifdef _PC_VDISABLE
7075 {"PC_VDISABLE", _PC_VDISABLE},
7076#endif
7077};
7078
Fred Drakec9680921999-12-13 16:37:25 +00007079static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007080conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007081{
7082 return conv_confname(arg, valuep, posix_constants_pathconf,
7083 sizeof(posix_constants_pathconf)
7084 / sizeof(struct constdef));
7085}
7086#endif
7087
7088#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007089PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007090"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007091Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007092If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007093
7094static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007095posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007096{
7097 PyObject *result = NULL;
7098 int name, fd;
7099
Fred Drake12c6e2d1999-12-14 21:25:03 +00007100 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7101 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00007102 long limit;
7103
7104 errno = 0;
7105 limit = fpathconf(fd, name);
7106 if (limit == -1 && errno != 0)
7107 posix_error();
7108 else
7109 result = PyInt_FromLong(limit);
7110 }
7111 return result;
7112}
7113#endif
7114
7115
7116#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007117PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007118"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007119Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007120If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007121
7122static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007123posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007124{
7125 PyObject *result = NULL;
7126 int name;
7127 char *path;
7128
7129 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7130 conv_path_confname, &name)) {
7131 long limit;
7132
7133 errno = 0;
7134 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007135 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00007136 if (errno == EINVAL)
7137 /* could be a path or name problem */
7138 posix_error();
7139 else
7140 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007141 }
Fred Drakec9680921999-12-13 16:37:25 +00007142 else
7143 result = PyInt_FromLong(limit);
7144 }
7145 return result;
7146}
7147#endif
7148
7149#ifdef HAVE_CONFSTR
7150static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007151#ifdef _CS_ARCHITECTURE
7152 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
7153#endif
7154#ifdef _CS_HOSTNAME
7155 {"CS_HOSTNAME", _CS_HOSTNAME},
7156#endif
7157#ifdef _CS_HW_PROVIDER
7158 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
7159#endif
7160#ifdef _CS_HW_SERIAL
7161 {"CS_HW_SERIAL", _CS_HW_SERIAL},
7162#endif
7163#ifdef _CS_INITTAB_NAME
7164 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
7165#endif
Fred Drakec9680921999-12-13 16:37:25 +00007166#ifdef _CS_LFS64_CFLAGS
7167 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
7168#endif
7169#ifdef _CS_LFS64_LDFLAGS
7170 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
7171#endif
7172#ifdef _CS_LFS64_LIBS
7173 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
7174#endif
7175#ifdef _CS_LFS64_LINTFLAGS
7176 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
7177#endif
7178#ifdef _CS_LFS_CFLAGS
7179 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
7180#endif
7181#ifdef _CS_LFS_LDFLAGS
7182 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
7183#endif
7184#ifdef _CS_LFS_LIBS
7185 {"CS_LFS_LIBS", _CS_LFS_LIBS},
7186#endif
7187#ifdef _CS_LFS_LINTFLAGS
7188 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
7189#endif
Fred Draked86ed291999-12-15 15:34:33 +00007190#ifdef _CS_MACHINE
7191 {"CS_MACHINE", _CS_MACHINE},
7192#endif
Fred Drakec9680921999-12-13 16:37:25 +00007193#ifdef _CS_PATH
7194 {"CS_PATH", _CS_PATH},
7195#endif
Fred Draked86ed291999-12-15 15:34:33 +00007196#ifdef _CS_RELEASE
7197 {"CS_RELEASE", _CS_RELEASE},
7198#endif
7199#ifdef _CS_SRPC_DOMAIN
7200 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
7201#endif
7202#ifdef _CS_SYSNAME
7203 {"CS_SYSNAME", _CS_SYSNAME},
7204#endif
7205#ifdef _CS_VERSION
7206 {"CS_VERSION", _CS_VERSION},
7207#endif
Fred Drakec9680921999-12-13 16:37:25 +00007208#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7209 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
7210#endif
7211#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7212 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
7213#endif
7214#ifdef _CS_XBS5_ILP32_OFF32_LIBS
7215 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
7216#endif
7217#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7218 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
7219#endif
7220#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7221 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
7222#endif
7223#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7224 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
7225#endif
7226#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7227 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
7228#endif
7229#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7230 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
7231#endif
7232#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7233 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
7234#endif
7235#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7236 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
7237#endif
7238#ifdef _CS_XBS5_LP64_OFF64_LIBS
7239 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
7240#endif
7241#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7242 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
7243#endif
7244#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7245 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
7246#endif
7247#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7248 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
7249#endif
7250#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7251 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
7252#endif
7253#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7254 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
7255#endif
Fred Draked86ed291999-12-15 15:34:33 +00007256#ifdef _MIPS_CS_AVAIL_PROCESSORS
7257 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
7258#endif
7259#ifdef _MIPS_CS_BASE
7260 {"MIPS_CS_BASE", _MIPS_CS_BASE},
7261#endif
7262#ifdef _MIPS_CS_HOSTID
7263 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
7264#endif
7265#ifdef _MIPS_CS_HW_NAME
7266 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7267#endif
7268#ifdef _MIPS_CS_NUM_PROCESSORS
7269 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7270#endif
7271#ifdef _MIPS_CS_OSREL_MAJ
7272 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7273#endif
7274#ifdef _MIPS_CS_OSREL_MIN
7275 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7276#endif
7277#ifdef _MIPS_CS_OSREL_PATCH
7278 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7279#endif
7280#ifdef _MIPS_CS_OS_NAME
7281 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7282#endif
7283#ifdef _MIPS_CS_OS_PROVIDER
7284 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7285#endif
7286#ifdef _MIPS_CS_PROCESSORS
7287 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7288#endif
7289#ifdef _MIPS_CS_SERIAL
7290 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7291#endif
7292#ifdef _MIPS_CS_VENDOR
7293 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7294#endif
Fred Drakec9680921999-12-13 16:37:25 +00007295};
7296
7297static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007298conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007299{
7300 return conv_confname(arg, valuep, posix_constants_confstr,
7301 sizeof(posix_constants_confstr)
7302 / sizeof(struct constdef));
7303}
7304
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007305PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007306"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007307Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007308
7309static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007310posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007311{
7312 PyObject *result = NULL;
7313 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007314 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007315
7316 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Skip Montanarodd527fc2006-04-18 00:49:49 +00007317 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007318
Fred Drakec9680921999-12-13 16:37:25 +00007319 errno = 0;
Skip Montanarodd527fc2006-04-18 00:49:49 +00007320 len = confstr(name, buffer, sizeof(buffer));
Skip Montanaro94785ef2006-04-20 01:29:48 +00007321 if (len == 0) {
7322 if (errno) {
7323 posix_error();
7324 }
7325 else {
7326 result = Py_None;
7327 Py_INCREF(Py_None);
7328 }
Fred Drakec9680921999-12-13 16:37:25 +00007329 }
7330 else {
Neal Norwitz0d21b1e2006-04-20 06:44:42 +00007331 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz449b24e2006-04-20 06:56:05 +00007332 result = PyString_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007333 if (result != NULL)
Neal Norwitz449b24e2006-04-20 06:56:05 +00007334 confstr(name, PyString_AS_STRING(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00007335 }
7336 else
Neal Norwitz449b24e2006-04-20 06:56:05 +00007337 result = PyString_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007338 }
7339 }
7340 return result;
7341}
7342#endif
7343
7344
7345#ifdef HAVE_SYSCONF
7346static struct constdef posix_constants_sysconf[] = {
7347#ifdef _SC_2_CHAR_TERM
7348 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7349#endif
7350#ifdef _SC_2_C_BIND
7351 {"SC_2_C_BIND", _SC_2_C_BIND},
7352#endif
7353#ifdef _SC_2_C_DEV
7354 {"SC_2_C_DEV", _SC_2_C_DEV},
7355#endif
7356#ifdef _SC_2_C_VERSION
7357 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7358#endif
7359#ifdef _SC_2_FORT_DEV
7360 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7361#endif
7362#ifdef _SC_2_FORT_RUN
7363 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7364#endif
7365#ifdef _SC_2_LOCALEDEF
7366 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7367#endif
7368#ifdef _SC_2_SW_DEV
7369 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7370#endif
7371#ifdef _SC_2_UPE
7372 {"SC_2_UPE", _SC_2_UPE},
7373#endif
7374#ifdef _SC_2_VERSION
7375 {"SC_2_VERSION", _SC_2_VERSION},
7376#endif
Fred Draked86ed291999-12-15 15:34:33 +00007377#ifdef _SC_ABI_ASYNCHRONOUS_IO
7378 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7379#endif
7380#ifdef _SC_ACL
7381 {"SC_ACL", _SC_ACL},
7382#endif
Fred Drakec9680921999-12-13 16:37:25 +00007383#ifdef _SC_AIO_LISTIO_MAX
7384 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7385#endif
Fred Drakec9680921999-12-13 16:37:25 +00007386#ifdef _SC_AIO_MAX
7387 {"SC_AIO_MAX", _SC_AIO_MAX},
7388#endif
7389#ifdef _SC_AIO_PRIO_DELTA_MAX
7390 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7391#endif
7392#ifdef _SC_ARG_MAX
7393 {"SC_ARG_MAX", _SC_ARG_MAX},
7394#endif
7395#ifdef _SC_ASYNCHRONOUS_IO
7396 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7397#endif
7398#ifdef _SC_ATEXIT_MAX
7399 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7400#endif
Fred Draked86ed291999-12-15 15:34:33 +00007401#ifdef _SC_AUDIT
7402 {"SC_AUDIT", _SC_AUDIT},
7403#endif
Fred Drakec9680921999-12-13 16:37:25 +00007404#ifdef _SC_AVPHYS_PAGES
7405 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7406#endif
7407#ifdef _SC_BC_BASE_MAX
7408 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7409#endif
7410#ifdef _SC_BC_DIM_MAX
7411 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7412#endif
7413#ifdef _SC_BC_SCALE_MAX
7414 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7415#endif
7416#ifdef _SC_BC_STRING_MAX
7417 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7418#endif
Fred Draked86ed291999-12-15 15:34:33 +00007419#ifdef _SC_CAP
7420 {"SC_CAP", _SC_CAP},
7421#endif
Fred Drakec9680921999-12-13 16:37:25 +00007422#ifdef _SC_CHARCLASS_NAME_MAX
7423 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7424#endif
7425#ifdef _SC_CHAR_BIT
7426 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7427#endif
7428#ifdef _SC_CHAR_MAX
7429 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7430#endif
7431#ifdef _SC_CHAR_MIN
7432 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7433#endif
7434#ifdef _SC_CHILD_MAX
7435 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7436#endif
7437#ifdef _SC_CLK_TCK
7438 {"SC_CLK_TCK", _SC_CLK_TCK},
7439#endif
7440#ifdef _SC_COHER_BLKSZ
7441 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7442#endif
7443#ifdef _SC_COLL_WEIGHTS_MAX
7444 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7445#endif
7446#ifdef _SC_DCACHE_ASSOC
7447 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7448#endif
7449#ifdef _SC_DCACHE_BLKSZ
7450 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7451#endif
7452#ifdef _SC_DCACHE_LINESZ
7453 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7454#endif
7455#ifdef _SC_DCACHE_SZ
7456 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7457#endif
7458#ifdef _SC_DCACHE_TBLKSZ
7459 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7460#endif
7461#ifdef _SC_DELAYTIMER_MAX
7462 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7463#endif
7464#ifdef _SC_EQUIV_CLASS_MAX
7465 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7466#endif
7467#ifdef _SC_EXPR_NEST_MAX
7468 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7469#endif
7470#ifdef _SC_FSYNC
7471 {"SC_FSYNC", _SC_FSYNC},
7472#endif
7473#ifdef _SC_GETGR_R_SIZE_MAX
7474 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7475#endif
7476#ifdef _SC_GETPW_R_SIZE_MAX
7477 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7478#endif
7479#ifdef _SC_ICACHE_ASSOC
7480 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7481#endif
7482#ifdef _SC_ICACHE_BLKSZ
7483 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7484#endif
7485#ifdef _SC_ICACHE_LINESZ
7486 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7487#endif
7488#ifdef _SC_ICACHE_SZ
7489 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7490#endif
Fred Draked86ed291999-12-15 15:34:33 +00007491#ifdef _SC_INF
7492 {"SC_INF", _SC_INF},
7493#endif
Fred Drakec9680921999-12-13 16:37:25 +00007494#ifdef _SC_INT_MAX
7495 {"SC_INT_MAX", _SC_INT_MAX},
7496#endif
7497#ifdef _SC_INT_MIN
7498 {"SC_INT_MIN", _SC_INT_MIN},
7499#endif
7500#ifdef _SC_IOV_MAX
7501 {"SC_IOV_MAX", _SC_IOV_MAX},
7502#endif
Fred Draked86ed291999-12-15 15:34:33 +00007503#ifdef _SC_IP_SECOPTS
7504 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7505#endif
Fred Drakec9680921999-12-13 16:37:25 +00007506#ifdef _SC_JOB_CONTROL
7507 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7508#endif
Fred Draked86ed291999-12-15 15:34:33 +00007509#ifdef _SC_KERN_POINTERS
7510 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7511#endif
7512#ifdef _SC_KERN_SIM
7513 {"SC_KERN_SIM", _SC_KERN_SIM},
7514#endif
Fred Drakec9680921999-12-13 16:37:25 +00007515#ifdef _SC_LINE_MAX
7516 {"SC_LINE_MAX", _SC_LINE_MAX},
7517#endif
7518#ifdef _SC_LOGIN_NAME_MAX
7519 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7520#endif
7521#ifdef _SC_LOGNAME_MAX
7522 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7523#endif
7524#ifdef _SC_LONG_BIT
7525 {"SC_LONG_BIT", _SC_LONG_BIT},
7526#endif
Fred Draked86ed291999-12-15 15:34:33 +00007527#ifdef _SC_MAC
7528 {"SC_MAC", _SC_MAC},
7529#endif
Fred Drakec9680921999-12-13 16:37:25 +00007530#ifdef _SC_MAPPED_FILES
7531 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7532#endif
7533#ifdef _SC_MAXPID
7534 {"SC_MAXPID", _SC_MAXPID},
7535#endif
7536#ifdef _SC_MB_LEN_MAX
7537 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7538#endif
7539#ifdef _SC_MEMLOCK
7540 {"SC_MEMLOCK", _SC_MEMLOCK},
7541#endif
7542#ifdef _SC_MEMLOCK_RANGE
7543 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7544#endif
7545#ifdef _SC_MEMORY_PROTECTION
7546 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7547#endif
7548#ifdef _SC_MESSAGE_PASSING
7549 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7550#endif
Fred Draked86ed291999-12-15 15:34:33 +00007551#ifdef _SC_MMAP_FIXED_ALIGNMENT
7552 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7553#endif
Fred Drakec9680921999-12-13 16:37:25 +00007554#ifdef _SC_MQ_OPEN_MAX
7555 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7556#endif
7557#ifdef _SC_MQ_PRIO_MAX
7558 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7559#endif
Fred Draked86ed291999-12-15 15:34:33 +00007560#ifdef _SC_NACLS_MAX
7561 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7562#endif
Fred Drakec9680921999-12-13 16:37:25 +00007563#ifdef _SC_NGROUPS_MAX
7564 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7565#endif
7566#ifdef _SC_NL_ARGMAX
7567 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7568#endif
7569#ifdef _SC_NL_LANGMAX
7570 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7571#endif
7572#ifdef _SC_NL_MSGMAX
7573 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7574#endif
7575#ifdef _SC_NL_NMAX
7576 {"SC_NL_NMAX", _SC_NL_NMAX},
7577#endif
7578#ifdef _SC_NL_SETMAX
7579 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7580#endif
7581#ifdef _SC_NL_TEXTMAX
7582 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7583#endif
7584#ifdef _SC_NPROCESSORS_CONF
7585 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7586#endif
7587#ifdef _SC_NPROCESSORS_ONLN
7588 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7589#endif
Fred Draked86ed291999-12-15 15:34:33 +00007590#ifdef _SC_NPROC_CONF
7591 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7592#endif
7593#ifdef _SC_NPROC_ONLN
7594 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7595#endif
Fred Drakec9680921999-12-13 16:37:25 +00007596#ifdef _SC_NZERO
7597 {"SC_NZERO", _SC_NZERO},
7598#endif
7599#ifdef _SC_OPEN_MAX
7600 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7601#endif
7602#ifdef _SC_PAGESIZE
7603 {"SC_PAGESIZE", _SC_PAGESIZE},
7604#endif
7605#ifdef _SC_PAGE_SIZE
7606 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7607#endif
7608#ifdef _SC_PASS_MAX
7609 {"SC_PASS_MAX", _SC_PASS_MAX},
7610#endif
7611#ifdef _SC_PHYS_PAGES
7612 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7613#endif
7614#ifdef _SC_PII
7615 {"SC_PII", _SC_PII},
7616#endif
7617#ifdef _SC_PII_INTERNET
7618 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7619#endif
7620#ifdef _SC_PII_INTERNET_DGRAM
7621 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7622#endif
7623#ifdef _SC_PII_INTERNET_STREAM
7624 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7625#endif
7626#ifdef _SC_PII_OSI
7627 {"SC_PII_OSI", _SC_PII_OSI},
7628#endif
7629#ifdef _SC_PII_OSI_CLTS
7630 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7631#endif
7632#ifdef _SC_PII_OSI_COTS
7633 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7634#endif
7635#ifdef _SC_PII_OSI_M
7636 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7637#endif
7638#ifdef _SC_PII_SOCKET
7639 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7640#endif
7641#ifdef _SC_PII_XTI
7642 {"SC_PII_XTI", _SC_PII_XTI},
7643#endif
7644#ifdef _SC_POLL
7645 {"SC_POLL", _SC_POLL},
7646#endif
7647#ifdef _SC_PRIORITIZED_IO
7648 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7649#endif
7650#ifdef _SC_PRIORITY_SCHEDULING
7651 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7652#endif
7653#ifdef _SC_REALTIME_SIGNALS
7654 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7655#endif
7656#ifdef _SC_RE_DUP_MAX
7657 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7658#endif
7659#ifdef _SC_RTSIG_MAX
7660 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7661#endif
7662#ifdef _SC_SAVED_IDS
7663 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7664#endif
7665#ifdef _SC_SCHAR_MAX
7666 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7667#endif
7668#ifdef _SC_SCHAR_MIN
7669 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7670#endif
7671#ifdef _SC_SELECT
7672 {"SC_SELECT", _SC_SELECT},
7673#endif
7674#ifdef _SC_SEMAPHORES
7675 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7676#endif
7677#ifdef _SC_SEM_NSEMS_MAX
7678 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7679#endif
7680#ifdef _SC_SEM_VALUE_MAX
7681 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7682#endif
7683#ifdef _SC_SHARED_MEMORY_OBJECTS
7684 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7685#endif
7686#ifdef _SC_SHRT_MAX
7687 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7688#endif
7689#ifdef _SC_SHRT_MIN
7690 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7691#endif
7692#ifdef _SC_SIGQUEUE_MAX
7693 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7694#endif
7695#ifdef _SC_SIGRT_MAX
7696 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7697#endif
7698#ifdef _SC_SIGRT_MIN
7699 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7700#endif
Fred Draked86ed291999-12-15 15:34:33 +00007701#ifdef _SC_SOFTPOWER
7702 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7703#endif
Fred Drakec9680921999-12-13 16:37:25 +00007704#ifdef _SC_SPLIT_CACHE
7705 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7706#endif
7707#ifdef _SC_SSIZE_MAX
7708 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7709#endif
7710#ifdef _SC_STACK_PROT
7711 {"SC_STACK_PROT", _SC_STACK_PROT},
7712#endif
7713#ifdef _SC_STREAM_MAX
7714 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7715#endif
7716#ifdef _SC_SYNCHRONIZED_IO
7717 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7718#endif
7719#ifdef _SC_THREADS
7720 {"SC_THREADS", _SC_THREADS},
7721#endif
7722#ifdef _SC_THREAD_ATTR_STACKADDR
7723 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7724#endif
7725#ifdef _SC_THREAD_ATTR_STACKSIZE
7726 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7727#endif
7728#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7729 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7730#endif
7731#ifdef _SC_THREAD_KEYS_MAX
7732 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7733#endif
7734#ifdef _SC_THREAD_PRIORITY_SCHEDULING
7735 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7736#endif
7737#ifdef _SC_THREAD_PRIO_INHERIT
7738 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7739#endif
7740#ifdef _SC_THREAD_PRIO_PROTECT
7741 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7742#endif
7743#ifdef _SC_THREAD_PROCESS_SHARED
7744 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7745#endif
7746#ifdef _SC_THREAD_SAFE_FUNCTIONS
7747 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7748#endif
7749#ifdef _SC_THREAD_STACK_MIN
7750 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7751#endif
7752#ifdef _SC_THREAD_THREADS_MAX
7753 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7754#endif
7755#ifdef _SC_TIMERS
7756 {"SC_TIMERS", _SC_TIMERS},
7757#endif
7758#ifdef _SC_TIMER_MAX
7759 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7760#endif
7761#ifdef _SC_TTY_NAME_MAX
7762 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7763#endif
7764#ifdef _SC_TZNAME_MAX
7765 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7766#endif
7767#ifdef _SC_T_IOV_MAX
7768 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7769#endif
7770#ifdef _SC_UCHAR_MAX
7771 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7772#endif
7773#ifdef _SC_UINT_MAX
7774 {"SC_UINT_MAX", _SC_UINT_MAX},
7775#endif
7776#ifdef _SC_UIO_MAXIOV
7777 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7778#endif
7779#ifdef _SC_ULONG_MAX
7780 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7781#endif
7782#ifdef _SC_USHRT_MAX
7783 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7784#endif
7785#ifdef _SC_VERSION
7786 {"SC_VERSION", _SC_VERSION},
7787#endif
7788#ifdef _SC_WORD_BIT
7789 {"SC_WORD_BIT", _SC_WORD_BIT},
7790#endif
7791#ifdef _SC_XBS5_ILP32_OFF32
7792 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7793#endif
7794#ifdef _SC_XBS5_ILP32_OFFBIG
7795 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7796#endif
7797#ifdef _SC_XBS5_LP64_OFF64
7798 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7799#endif
7800#ifdef _SC_XBS5_LPBIG_OFFBIG
7801 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7802#endif
7803#ifdef _SC_XOPEN_CRYPT
7804 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7805#endif
7806#ifdef _SC_XOPEN_ENH_I18N
7807 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7808#endif
7809#ifdef _SC_XOPEN_LEGACY
7810 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7811#endif
7812#ifdef _SC_XOPEN_REALTIME
7813 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7814#endif
7815#ifdef _SC_XOPEN_REALTIME_THREADS
7816 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7817#endif
7818#ifdef _SC_XOPEN_SHM
7819 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7820#endif
7821#ifdef _SC_XOPEN_UNIX
7822 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7823#endif
7824#ifdef _SC_XOPEN_VERSION
7825 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7826#endif
7827#ifdef _SC_XOPEN_XCU_VERSION
7828 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7829#endif
7830#ifdef _SC_XOPEN_XPG2
7831 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7832#endif
7833#ifdef _SC_XOPEN_XPG3
7834 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7835#endif
7836#ifdef _SC_XOPEN_XPG4
7837 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7838#endif
7839};
7840
7841static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007842conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007843{
7844 return conv_confname(arg, valuep, posix_constants_sysconf,
7845 sizeof(posix_constants_sysconf)
7846 / sizeof(struct constdef));
7847}
7848
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007849PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007850"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007851Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007852
7853static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007854posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007855{
7856 PyObject *result = NULL;
7857 int name;
7858
7859 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7860 int value;
7861
7862 errno = 0;
7863 value = sysconf(name);
7864 if (value == -1 && errno != 0)
7865 posix_error();
7866 else
7867 result = PyInt_FromLong(value);
7868 }
7869 return result;
7870}
7871#endif
7872
7873
Fred Drakebec628d1999-12-15 18:31:10 +00007874/* This code is used to ensure that the tables of configuration value names
7875 * are in sorted order as required by conv_confname(), and also to build the
7876 * the exported dictionaries that are used to publish information about the
7877 * names available on the host platform.
7878 *
7879 * Sorting the table at runtime ensures that the table is properly ordered
7880 * when used, even for platforms we're not able to test on. It also makes
7881 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007882 */
Fred Drakebec628d1999-12-15 18:31:10 +00007883
7884static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007885cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007886{
7887 const struct constdef *c1 =
7888 (const struct constdef *) v1;
7889 const struct constdef *c2 =
7890 (const struct constdef *) v2;
7891
7892 return strcmp(c1->name, c2->name);
7893}
7894
7895static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007896setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007897 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007898{
Fred Drakebec628d1999-12-15 18:31:10 +00007899 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007900 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007901
7902 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7903 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007904 if (d == NULL)
7905 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007906
Barry Warsaw3155db32000-04-13 15:20:40 +00007907 for (i=0; i < tablesize; ++i) {
7908 PyObject *o = PyInt_FromLong(table[i].value);
7909 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7910 Py_XDECREF(o);
7911 Py_DECREF(d);
7912 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007913 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007914 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007915 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007916 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007917}
7918
Fred Drakebec628d1999-12-15 18:31:10 +00007919/* Return -1 on failure, 0 on success. */
7920static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007921setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007922{
7923#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007924 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007925 sizeof(posix_constants_pathconf)
7926 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007927 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007928 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007929#endif
7930#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007931 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007932 sizeof(posix_constants_confstr)
7933 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007934 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007935 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007936#endif
7937#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007938 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007939 sizeof(posix_constants_sysconf)
7940 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007941 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007942 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007943#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007944 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007945}
Fred Draked86ed291999-12-15 15:34:33 +00007946
7947
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007948PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007949"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007950Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007951in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007952
7953static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007954posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007955{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007956 abort();
7957 /*NOTREACHED*/
7958 Py_FatalError("abort() called from Python code didn't abort!");
7959 return NULL;
7960}
Fred Drakebec628d1999-12-15 18:31:10 +00007961
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007962#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007963PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007964"startfile(filepath [, operation]) - Start a file with its associated\n\
7965application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007966\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007967When \"operation\" is not specified or \"open\", this acts like\n\
7968double-clicking the file in Explorer, or giving the file name as an\n\
7969argument to the DOS \"start\" command: the file is opened with whatever\n\
7970application (if any) its extension is associated.\n\
7971When another \"operation\" is given, it specifies what should be done with\n\
7972the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007973\n\
7974startfile returns as soon as the associated application is launched.\n\
7975There is no option to wait for the application to close, and no way\n\
7976to retrieve the application's exit status.\n\
7977\n\
7978The filepath is relative to the current directory. If you want to use\n\
7979an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007980the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007981
7982static PyObject *
7983win32_startfile(PyObject *self, PyObject *args)
7984{
7985 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00007986 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007987 HINSTANCE rc;
Georg Brandlad89dc82006-04-03 12:26:26 +00007988#ifdef Py_WIN_WIDE_FILENAMES
7989 if (unicode_file_names()) {
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007990 PyObject *unipath, *woperation = NULL;
Georg Brandlad89dc82006-04-03 12:26:26 +00007991 if (!PyArg_ParseTuple(args, "U|s:startfile",
7992 &unipath, &operation)) {
7993 PyErr_Clear();
7994 goto normal;
7995 }
7996
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007997
7998 if (operation) {
7999 woperation = PyUnicode_DecodeASCII(operation,
8000 strlen(operation), NULL);
8001 if (!woperation) {
8002 PyErr_Clear();
8003 operation = NULL;
8004 goto normal;
8005 }
Georg Brandlad89dc82006-04-03 12:26:26 +00008006 }
8007
8008 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008009 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
Georg Brandlad89dc82006-04-03 12:26:26 +00008010 PyUnicode_AS_UNICODE(unipath),
Georg Brandlad89dc82006-04-03 12:26:26 +00008011 NULL, NULL, SW_SHOWNORMAL);
8012 Py_END_ALLOW_THREADS
8013
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008014 Py_XDECREF(woperation);
Georg Brandlad89dc82006-04-03 12:26:26 +00008015 if (rc <= (HINSTANCE)32) {
8016 PyObject *errval = win32_error_unicode("startfile",
8017 PyUnicode_AS_UNICODE(unipath));
8018 return errval;
8019 }
8020 Py_INCREF(Py_None);
8021 return Py_None;
8022 }
8023#endif
8024
8025normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00008026 if (!PyArg_ParseTuple(args, "et|s:startfile",
8027 Py_FileSystemDefaultEncoding, &filepath,
8028 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00008029 return NULL;
8030 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00008031 rc = ShellExecute((HWND)0, operation, filepath,
8032 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00008033 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00008034 if (rc <= (HINSTANCE)32) {
8035 PyObject *errval = win32_error("startfile", filepath);
8036 PyMem_Free(filepath);
8037 return errval;
8038 }
8039 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00008040 Py_INCREF(Py_None);
8041 return Py_None;
8042}
8043#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008044
Martin v. Löwis438b5342002-12-27 10:16:42 +00008045#ifdef HAVE_GETLOADAVG
8046PyDoc_STRVAR(posix_getloadavg__doc__,
8047"getloadavg() -> (float, float, float)\n\n\
8048Return the number of processes in the system run queue averaged over\n\
8049the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8050was unobtainable");
8051
8052static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008053posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008054{
8055 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008056 if (getloadavg(loadavg, 3)!=3) {
8057 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8058 return NULL;
8059 } else
8060 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
8061}
8062#endif
8063
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008064#ifdef MS_WINDOWS
8065
8066PyDoc_STRVAR(win32_urandom__doc__,
8067"urandom(n) -> str\n\n\
8068Return a string of n random bytes suitable for cryptographic use.");
8069
8070typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8071 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8072 DWORD dwFlags );
8073typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8074 BYTE *pbBuffer );
8075
8076static CRYPTGENRANDOM pCryptGenRandom = NULL;
Martin v. Löwisaf699dd2007-09-04 09:51:57 +00008077/* This handle is never explicitly released. Instead, the operating
8078 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008079static HCRYPTPROV hCryptProv = 0;
8080
Tim Peters4ad82172004-08-30 17:02:04 +00008081static PyObject*
8082win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008083{
Tim Petersd3115382004-08-30 17:36:46 +00008084 int howMany;
8085 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008086
Tim Peters4ad82172004-08-30 17:02:04 +00008087 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00008088 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00008089 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00008090 if (howMany < 0)
8091 return PyErr_Format(PyExc_ValueError,
8092 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008093
Tim Peters4ad82172004-08-30 17:02:04 +00008094 if (hCryptProv == 0) {
8095 HINSTANCE hAdvAPI32 = NULL;
8096 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008097
Tim Peters4ad82172004-08-30 17:02:04 +00008098 /* Obtain handle to the DLL containing CryptoAPI
8099 This should not fail */
8100 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8101 if(hAdvAPI32 == NULL)
8102 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008103
Tim Peters4ad82172004-08-30 17:02:04 +00008104 /* Obtain pointers to the CryptoAPI functions
8105 This will fail on some early versions of Win95 */
8106 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8107 hAdvAPI32,
8108 "CryptAcquireContextA");
8109 if (pCryptAcquireContext == NULL)
8110 return PyErr_Format(PyExc_NotImplementedError,
8111 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008112
Tim Peters4ad82172004-08-30 17:02:04 +00008113 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8114 hAdvAPI32, "CryptGenRandom");
Georg Brandl74bb7832006-09-06 06:03:59 +00008115 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00008116 return PyErr_Format(PyExc_NotImplementedError,
8117 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008118
Tim Peters4ad82172004-08-30 17:02:04 +00008119 /* Acquire context */
8120 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8121 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8122 return win32_error("CryptAcquireContext", NULL);
8123 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008124
Tim Peters4ad82172004-08-30 17:02:04 +00008125 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00008126 result = PyString_FromStringAndSize(NULL, howMany);
8127 if (result != NULL) {
8128 /* Get random data */
8129 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8130 PyString_AS_STRING(result))) {
8131 Py_DECREF(result);
8132 return win32_error("CryptGenRandom", NULL);
8133 }
Tim Peters4ad82172004-08-30 17:02:04 +00008134 }
Tim Petersd3115382004-08-30 17:36:46 +00008135 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008136}
8137#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008138
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008139#ifdef __VMS
8140/* Use openssl random routine */
8141#include <openssl/rand.h>
8142PyDoc_STRVAR(vms_urandom__doc__,
8143"urandom(n) -> str\n\n\
8144Return a string of n random bytes suitable for cryptographic use.");
8145
8146static PyObject*
8147vms_urandom(PyObject *self, PyObject *args)
8148{
8149 int howMany;
8150 PyObject* result;
8151
8152 /* Read arguments */
8153 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8154 return NULL;
8155 if (howMany < 0)
8156 return PyErr_Format(PyExc_ValueError,
8157 "negative argument not allowed");
8158
8159 /* Allocate bytes */
8160 result = PyString_FromStringAndSize(NULL, howMany);
8161 if (result != NULL) {
8162 /* Get random data */
8163 if (RAND_pseudo_bytes((unsigned char*)
8164 PyString_AS_STRING(result),
8165 howMany) < 0) {
8166 Py_DECREF(result);
8167 return PyErr_Format(PyExc_ValueError,
8168 "RAND_pseudo_bytes");
8169 }
8170 }
8171 return result;
8172}
8173#endif
8174
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008175static PyMethodDef posix_methods[] = {
8176 {"access", posix_access, METH_VARARGS, posix_access__doc__},
8177#ifdef HAVE_TTYNAME
8178 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
8179#endif
8180 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008181#ifdef HAVE_CHFLAGS
8182 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
8183#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008184 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008185#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008186 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008187#endif /* HAVE_CHOWN */
Martin v. Löwis382abef2007-02-19 10:55:19 +00008188#ifdef HAVE_LCHFLAGS
8189 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
8190#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008191#ifdef HAVE_LCHOWN
8192 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
8193#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008194#ifdef HAVE_CHROOT
8195 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
8196#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008197#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00008198 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008199#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008200#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00008201 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008202#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00008203 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008204#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008205#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008206#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008207 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008208#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008209 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8210 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8211 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008212#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008213 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008214#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008215#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008216 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008217#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008218 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8219 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8220 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00008221 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008222#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008223 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008224#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008225#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008226 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008227#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008228 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008229#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00008230 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008231#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008232 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8233 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8234 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008235#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00008236 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008237#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008238 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008239#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008240 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8241 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008242#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008243#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008244 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8245 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008246#if defined(PYOS_OS2)
8247 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8248 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
8249#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008250#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008251#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00008252 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008253#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008254#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00008255 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008256#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008257#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00008258 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008259#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008260#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00008261 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008262#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008263#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008264 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008265#endif /* HAVE_GETEGID */
8266#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008267 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008268#endif /* HAVE_GETEUID */
8269#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008270 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008271#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008272#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00008273 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008274#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008275 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008276#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008277 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008278#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008279#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00008280 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008281#endif /* HAVE_GETPPID */
8282#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008283 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008284#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008285#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00008286 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008287#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008288#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008289 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008290#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008291#ifdef HAVE_KILLPG
8292 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
8293#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008294#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008295 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008296#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008297#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008298 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008299#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008300 {"popen2", win32_popen2, METH_VARARGS},
8301 {"popen3", win32_popen3, METH_VARARGS},
8302 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00008303 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008304#else
8305#if defined(PYOS_OS2) && defined(PYCC_GCC)
8306 {"popen2", os2emx_popen2, METH_VARARGS},
8307 {"popen3", os2emx_popen3, METH_VARARGS},
8308 {"popen4", os2emx_popen4, METH_VARARGS},
8309#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008310#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008311#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008312#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008313 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008314#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008315#ifdef HAVE_SETEUID
8316 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8317#endif /* HAVE_SETEUID */
8318#ifdef HAVE_SETEGID
8319 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8320#endif /* HAVE_SETEGID */
8321#ifdef HAVE_SETREUID
8322 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8323#endif /* HAVE_SETREUID */
8324#ifdef HAVE_SETREGID
8325 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8326#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008327#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008328 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008329#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008330#ifdef HAVE_SETGROUPS
Georg Brandl96a8c392006-05-29 21:04:52 +00008331 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008332#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008333#ifdef HAVE_GETPGID
8334 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8335#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008336#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008337 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008338#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008339#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00008340 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008341#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008342#ifdef HAVE_WAIT3
8343 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8344#endif /* HAVE_WAIT3 */
8345#ifdef HAVE_WAIT4
8346 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8347#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008348#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008349 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008350#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008351#ifdef HAVE_GETSID
8352 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8353#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008354#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00008355 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008356#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008357#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008358 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008359#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008360#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008361 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008362#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008363#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008364 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008365#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008366 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8367 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8368 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8369 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8370 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8371 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8372 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8373 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8374 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00008375 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008376#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00008377 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008378#endif
8379#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008380 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008381#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008382#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008383 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8384#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008385#ifdef HAVE_DEVICE_MACROS
8386 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8387 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8388 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8389#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008390#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008391 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008392#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008393#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008394 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008395#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008396#ifdef HAVE_UNSETENV
8397 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8398#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00008399#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008400 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00008401#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00008402#ifdef HAVE_FCHDIR
8403 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8404#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008405#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008406 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008407#endif
8408#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008409 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008410#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008411#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008412#ifdef WCOREDUMP
8413 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8414#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008415#ifdef WIFCONTINUED
8416 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8417#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008418#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008419 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008420#endif /* WIFSTOPPED */
8421#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008422 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008423#endif /* WIFSIGNALED */
8424#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008425 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008426#endif /* WIFEXITED */
8427#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008428 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008429#endif /* WEXITSTATUS */
8430#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008431 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008432#endif /* WTERMSIG */
8433#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008434 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008435#endif /* WSTOPSIG */
8436#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008437#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008438 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008439#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008440#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008441 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008442#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008443#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00008444 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008445#endif
8446#ifdef HAVE_TEMPNAM
8447 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8448#endif
8449#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00008450 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008451#endif
Fred Drakec9680921999-12-13 16:37:25 +00008452#ifdef HAVE_CONFSTR
8453 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8454#endif
8455#ifdef HAVE_SYSCONF
8456 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8457#endif
8458#ifdef HAVE_FPATHCONF
8459 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8460#endif
8461#ifdef HAVE_PATHCONF
8462 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8463#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008464 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008465#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00008466 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8467#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008468#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00008469 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008470#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008471 #ifdef MS_WINDOWS
8472 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8473 #endif
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008474 #ifdef __VMS
8475 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
8476 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008477 {NULL, NULL} /* Sentinel */
8478};
8479
8480
Barry Warsaw4a342091996-12-19 23:50:02 +00008481static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008482ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008483{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008484 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008485}
8486
Guido van Rossumd48f2521997-12-05 22:19:34 +00008487#if defined(PYOS_OS2)
8488/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008489static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008490{
8491 APIRET rc;
8492 ULONG values[QSV_MAX+1];
8493 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008494 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008495
8496 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008497 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008498 Py_END_ALLOW_THREADS
8499
8500 if (rc != NO_ERROR) {
8501 os2_error(rc);
8502 return -1;
8503 }
8504
Fred Drake4d1e64b2002-04-15 19:40:07 +00008505 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8506 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8507 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8508 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8509 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8510 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8511 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008512
8513 switch (values[QSV_VERSION_MINOR]) {
8514 case 0: ver = "2.00"; break;
8515 case 10: ver = "2.10"; break;
8516 case 11: ver = "2.11"; break;
8517 case 30: ver = "3.00"; break;
8518 case 40: ver = "4.00"; break;
8519 case 50: ver = "5.00"; break;
8520 default:
Tim Peters885d4572001-11-28 20:27:42 +00008521 PyOS_snprintf(tmp, sizeof(tmp),
8522 "%d-%d", values[QSV_VERSION_MAJOR],
8523 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008524 ver = &tmp[0];
8525 }
8526
8527 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008528 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008529 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008530
8531 /* Add Indicator of Which Drive was Used to Boot the System */
8532 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8533 tmp[1] = ':';
8534 tmp[2] = '\0';
8535
Fred Drake4d1e64b2002-04-15 19:40:07 +00008536 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008537}
8538#endif
8539
Barry Warsaw4a342091996-12-19 23:50:02 +00008540static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008541all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008542{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008543#ifdef F_OK
8544 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008545#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008546#ifdef R_OK
8547 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008548#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008549#ifdef W_OK
8550 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008551#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008552#ifdef X_OK
8553 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008554#endif
Fred Drakec9680921999-12-13 16:37:25 +00008555#ifdef NGROUPS_MAX
8556 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8557#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008558#ifdef TMP_MAX
8559 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8560#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008561#ifdef WCONTINUED
8562 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8563#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008564#ifdef WNOHANG
8565 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008566#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008567#ifdef WUNTRACED
8568 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8569#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008570#ifdef O_RDONLY
8571 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8572#endif
8573#ifdef O_WRONLY
8574 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8575#endif
8576#ifdef O_RDWR
8577 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8578#endif
8579#ifdef O_NDELAY
8580 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8581#endif
8582#ifdef O_NONBLOCK
8583 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8584#endif
8585#ifdef O_APPEND
8586 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8587#endif
8588#ifdef O_DSYNC
8589 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8590#endif
8591#ifdef O_RSYNC
8592 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8593#endif
8594#ifdef O_SYNC
8595 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8596#endif
8597#ifdef O_NOCTTY
8598 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8599#endif
8600#ifdef O_CREAT
8601 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8602#endif
8603#ifdef O_EXCL
8604 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8605#endif
8606#ifdef O_TRUNC
8607 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8608#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008609#ifdef O_BINARY
8610 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8611#endif
8612#ifdef O_TEXT
8613 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8614#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008615#ifdef O_LARGEFILE
8616 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8617#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008618#ifdef O_SHLOCK
8619 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8620#endif
8621#ifdef O_EXLOCK
8622 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8623#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008624
Tim Peters5aa91602002-01-30 05:46:57 +00008625/* MS Windows */
8626#ifdef O_NOINHERIT
8627 /* Don't inherit in child processes. */
8628 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8629#endif
8630#ifdef _O_SHORT_LIVED
8631 /* Optimize for short life (keep in memory). */
8632 /* MS forgot to define this one with a non-underscore form too. */
8633 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8634#endif
8635#ifdef O_TEMPORARY
8636 /* Automatically delete when last handle is closed. */
8637 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8638#endif
8639#ifdef O_RANDOM
8640 /* Optimize for random access. */
8641 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8642#endif
8643#ifdef O_SEQUENTIAL
8644 /* Optimize for sequential access. */
8645 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8646#endif
8647
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008648/* GNU extensions. */
8649#ifdef O_DIRECT
8650 /* Direct disk access. */
8651 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8652#endif
8653#ifdef O_DIRECTORY
8654 /* Must be a directory. */
8655 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8656#endif
8657#ifdef O_NOFOLLOW
8658 /* Do not follow links. */
8659 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8660#endif
Georg Brandlb67da6e2007-11-24 13:56:09 +00008661#ifdef O_NOATIME
8662 /* Do not update the access time. */
8663 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
8664#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008665
Barry Warsaw5676bd12003-01-07 20:57:09 +00008666 /* These come from sysexits.h */
8667#ifdef EX_OK
8668 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008669#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008670#ifdef EX_USAGE
8671 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008672#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008673#ifdef EX_DATAERR
8674 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008675#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008676#ifdef EX_NOINPUT
8677 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008678#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008679#ifdef EX_NOUSER
8680 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008681#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008682#ifdef EX_NOHOST
8683 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008684#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008685#ifdef EX_UNAVAILABLE
8686 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008687#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008688#ifdef EX_SOFTWARE
8689 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008690#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008691#ifdef EX_OSERR
8692 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008693#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008694#ifdef EX_OSFILE
8695 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008696#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008697#ifdef EX_CANTCREAT
8698 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008699#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008700#ifdef EX_IOERR
8701 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008702#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008703#ifdef EX_TEMPFAIL
8704 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008705#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008706#ifdef EX_PROTOCOL
8707 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008708#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008709#ifdef EX_NOPERM
8710 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008711#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008712#ifdef EX_CONFIG
8713 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008714#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008715#ifdef EX_NOTFOUND
8716 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008717#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008718
Guido van Rossum246bc171999-02-01 23:54:31 +00008719#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008720#if defined(PYOS_OS2) && defined(PYCC_GCC)
8721 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8722 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8723 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8724 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8725 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8726 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8727 if (ins(d, "P_PM", (long)P_PM)) return -1;
8728 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8729 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8730 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8731 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8732 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8733 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8734 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8735 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8736 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8737 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8738 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8739 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8740 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8741#else
Guido van Rossum7d385291999-02-16 19:38:04 +00008742 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8743 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8744 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8745 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8746 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008747#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008748#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008749
Guido van Rossumd48f2521997-12-05 22:19:34 +00008750#if defined(PYOS_OS2)
8751 if (insertvalues(d)) return -1;
8752#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008753 return 0;
8754}
8755
8756
Tim Peters5aa91602002-01-30 05:46:57 +00008757#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008758#define INITFUNC initnt
8759#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008760
8761#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008762#define INITFUNC initos2
8763#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008764
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008765#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008766#define INITFUNC initposix
8767#define MODNAME "posix"
8768#endif
8769
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008770PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008771INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008772{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008773 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008774
Fred Drake4d1e64b2002-04-15 19:40:07 +00008775 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008776 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008777 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00008778 if (m == NULL)
8779 return;
Tim Peters5aa91602002-01-30 05:46:57 +00008780
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008781 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008782 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00008783 Py_XINCREF(v);
8784 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008785 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00008786 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008787
Fred Drake4d1e64b2002-04-15 19:40:07 +00008788 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00008789 return;
8790
Fred Drake4d1e64b2002-04-15 19:40:07 +00008791 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00008792 return;
8793
Fred Drake4d1e64b2002-04-15 19:40:07 +00008794 Py_INCREF(PyExc_OSError);
8795 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008796
Guido van Rossumb3d39562000-01-31 18:41:26 +00008797#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00008798 if (posix_putenv_garbage == NULL)
8799 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008800#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008801
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008802 if (!initialized) {
8803 stat_result_desc.name = MODNAME ".stat_result";
8804 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8805 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8806 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8807 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8808 structseq_new = StatResultType.tp_new;
8809 StatResultType.tp_new = statresult_new;
8810
8811 statvfs_result_desc.name = MODNAME ".statvfs_result";
8812 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
8813 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008814 Py_INCREF((PyObject*) &StatResultType);
8815 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00008816 Py_INCREF((PyObject*) &StatVFSResultType);
8817 PyModule_AddObject(m, "statvfs_result",
8818 (PyObject*) &StatVFSResultType);
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008819 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00008820
8821#ifdef __APPLE__
8822 /*
8823 * Step 2 of weak-linking support on Mac OS X.
8824 *
8825 * The code below removes functions that are not available on the
8826 * currently active platform.
8827 *
8828 * This block allow one to use a python binary that was build on
8829 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8830 * OSX 10.4.
8831 */
8832#ifdef HAVE_FSTATVFS
8833 if (fstatvfs == NULL) {
8834 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8835 return;
8836 }
8837 }
8838#endif /* HAVE_FSTATVFS */
8839
8840#ifdef HAVE_STATVFS
8841 if (statvfs == NULL) {
8842 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8843 return;
8844 }
8845 }
8846#endif /* HAVE_STATVFS */
8847
8848# ifdef HAVE_LCHOWN
8849 if (lchown == NULL) {
8850 if (PyObject_DelAttrString(m, "lchown") == -1) {
8851 return;
8852 }
8853 }
8854#endif /* HAVE_LCHOWN */
8855
8856
8857#endif /* __APPLE__ */
8858
Guido van Rossumb6775db1994-08-01 11:34:53 +00008859}
Anthony Baxterac6bd462006-04-13 02:06:09 +00008860
8861#ifdef __cplusplus
8862}
8863#endif
8864
Martin v. Löwis18aaa562006-10-15 08:43:33 +00008865