blob: 425fa5a1181b4c59b51bbf381f8f2f14f1c9e4af [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
49#define INCL_DOS
50#define INCL_DOSERRORS
51#define INCL_DOSPROCESS
52#define INCL_NOPMAPI
53#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000054#if defined(PYCC_GCC)
55#include <ctype.h>
56#include <io.h>
57#include <stdio.h>
58#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000060#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#endif
62
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#endif /* HAVE_SYS_TYPES_H */
66
67#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070
Guido van Rossum36bc6801995-06-14 22:54:23 +000071#ifdef HAVE_SYS_WAIT_H
72#include <sys/wait.h> /* For WNOHANG */
73#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000076#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000078
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#ifdef HAVE_FCNTL_H
80#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000081#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000082
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083#ifdef HAVE_GRP_H
84#include <grp.h>
85#endif
86
Barry Warsaw5676bd12003-01-07 20:57:09 +000087#ifdef HAVE_SYSEXITS_H
88#include <sysexits.h>
89#endif /* HAVE_SYSEXITS_H */
90
Anthony Baxter8a560de2004-10-13 15:30:56 +000091#ifdef HAVE_SYS_LOADAVG_H
92#include <sys/loadavg.h>
93#endif
94
Guido van Rossuma4916fa1996-05-23 22:58:55 +000095/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000096/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000097#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000098#include <process.h>
99#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000100#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000101#define HAVE_GETCWD 1
102#define HAVE_OPENDIR 1
103#define HAVE_SYSTEM 1
104#if defined(__OS2__)
105#define HAVE_EXECV 1
106#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000107#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000108#include <process.h>
109#else
110#ifdef __BORLANDC__ /* Borland compiler */
111#define HAVE_EXECV 1
112#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000113#define HAVE_OPENDIR 1
114#define HAVE_PIPE 1
115#define HAVE_POPEN 1
116#define HAVE_SYSTEM 1
117#define HAVE_WAIT 1
118#else
119#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000120#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000121#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000122#define HAVE_EXECV 1
123#define HAVE_PIPE 1
124#define HAVE_POPEN 1
125#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000126#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000127#define HAVE_FSYNC 1
128#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000129#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000130#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
131/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000132#else /* all other compilers */
133/* Unix functions that the configure script doesn't check for */
134#define HAVE_EXECV 1
135#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000136#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
137#define HAVE_FORK1 1
138#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000139#define HAVE_GETCWD 1
140#define HAVE_GETEGID 1
141#define HAVE_GETEUID 1
142#define HAVE_GETGID 1
143#define HAVE_GETPPID 1
144#define HAVE_GETUID 1
145#define HAVE_KILL 1
146#define HAVE_OPENDIR 1
147#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000148#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000149#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000150#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000151#define HAVE_SYSTEM 1
152#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000153#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000154#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000155#endif /* _MSC_VER */
156#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000157#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000158#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000159
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000161
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000162#if defined(__sgi)&&_COMPILER_VERSION>=700
163/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
164 (default) */
165extern char *ctermid_r(char *);
166#endif
167
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000168#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000169#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000170extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000171#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000172#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000173extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000174#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000175extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000176#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000177#endif
178#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int chdir(char *);
180extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000182extern int chdir(const char *);
183extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000184#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000185#ifdef __BORLANDC__
186extern int chmod(const char *, int);
187#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000188extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000189#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000190extern int chown(const char *, uid_t, gid_t);
191extern char *getcwd(char *, int);
192extern char *strerror(int);
193extern int link(const char *, const char *);
194extern int rename(const char *, const char *);
195extern int stat(const char *, struct stat *);
196extern int unlink(const char *);
197extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000199extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000200#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000201#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000202extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000203#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000205
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000206#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#ifdef HAVE_UTIME_H
209#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000210#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000212#ifdef HAVE_SYS_UTIME_H
213#include <sys/utime.h>
214#define HAVE_UTIME_H /* pretend we do for the rest of this file */
215#endif /* HAVE_SYS_UTIME_H */
216
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217#ifdef HAVE_SYS_TIMES_H
218#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000219#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220
221#ifdef HAVE_SYS_PARAM_H
222#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000223#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224
225#ifdef HAVE_SYS_UTSNAME_H
226#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000227#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000229#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000231#define NAMLEN(dirent) strlen((dirent)->d_name)
232#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000233#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000234#include <direct.h>
235#define NAMLEN(dirent) strlen((dirent)->d_name)
236#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000238#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000239#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000240#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#endif
243#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000244#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000245#endif
246#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000248#endif
249#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000250
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000251#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000252#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000253#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000254#endif
255#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000257#endif
258#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000260#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000261#include "osdefs.h"
Martin v. Löwisdc3883f2004-08-29 15:46:35 +0000262#define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000264#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000265#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000266#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000267#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268
Guido van Rossumd48f2521997-12-05 22:19:34 +0000269#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000271#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272
Tim Petersbc2e10e2002-03-03 23:17:02 +0000273#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000274#if defined(PATH_MAX) && PATH_MAX > 1024
275#define MAXPATHLEN PATH_MAX
276#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000277#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000278#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000279#endif /* MAXPATHLEN */
280
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000281#ifdef UNION_WAIT
282/* Emulate some macros on systems that have a union instead of macros */
283
284#ifndef WIFEXITED
285#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
286#endif
287
288#ifndef WEXITSTATUS
289#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
290#endif
291
292#ifndef WTERMSIG
293#define WTERMSIG(u_wait) ((u_wait).w_termsig)
294#endif
295
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000296#define WAIT_TYPE union wait
297#define WAIT_STATUS_INT(s) (s.w_status)
298
299#else /* !UNION_WAIT */
300#define WAIT_TYPE int
301#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000302#endif /* UNION_WAIT */
303
Greg Wardb48bc172000-03-01 21:51:56 +0000304/* Don't use the "_r" form if we don't need it (also, won't have a
305 prototype for it, at least on Solaris -- maybe others as well?). */
306#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
307#define USE_CTERMID_R
308#endif
309
310#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
311#define USE_TMPNAM_R
312#endif
313
Fred Drake699f3522000-06-29 21:12:41 +0000314/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000315#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000316#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000317# define STAT win32_stat
318# define FSTAT win32_fstat
319# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000320#else
321# define STAT stat
322# define FSTAT fstat
323# define STRUCT_STAT struct stat
324#endif
325
Tim Peters11b23062003-04-23 02:39:17 +0000326#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000327#include <sys/mkdev.h>
328#else
329#if defined(MAJOR_IN_SYSMACROS)
330#include <sys/sysmacros.h>
331#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000332#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
333#include <sys/mkdev.h>
334#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000335#endif
Fred Drake699f3522000-06-29 21:12:41 +0000336
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000338#ifdef WITH_NEXT_FRAMEWORK
339/* On Darwin/MacOSX a shared library or framework has no access to
340** environ directly, we must obtain it with _NSGetEnviron().
341*/
342#include <crt_externs.h>
343static char **environ;
344#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000346#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347
Barry Warsaw53699e91996-12-10 23:23:01 +0000348static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000349convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350{
Barry Warsaw53699e91996-12-10 23:23:01 +0000351 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000353 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354 if (d == NULL)
355 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000356#ifdef WITH_NEXT_FRAMEWORK
357 if (environ == NULL)
358 environ = *_NSGetEnviron();
359#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360 if (environ == NULL)
361 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000362 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000364 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000365 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366 char *p = strchr(*e, '=');
367 if (p == NULL)
368 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000369 k = PyString_FromStringAndSize(*e, (int)(p-*e));
370 if (k == NULL) {
371 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000373 }
374 v = PyString_FromString(p+1);
375 if (v == NULL) {
376 PyErr_Clear();
377 Py_DECREF(k);
378 continue;
379 }
380 if (PyDict_GetItem(d, k) == NULL) {
381 if (PyDict_SetItem(d, k, v) != 0)
382 PyErr_Clear();
383 }
384 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000385 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000387#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000388 {
389 APIRET rc;
390 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
391
392 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000393 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000394 PyObject *v = PyString_FromString(buffer);
395 PyDict_SetItemString(d, "BEGINLIBPATH", v);
396 Py_DECREF(v);
397 }
398 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
399 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
400 PyObject *v = PyString_FromString(buffer);
401 PyDict_SetItemString(d, "ENDLIBPATH", v);
402 Py_DECREF(v);
403 }
404 }
405#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406 return d;
407}
408
409
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410/* Set a POSIX-specific error from errno, and return NULL */
411
Barry Warsawd58d7641998-07-23 16:14:40 +0000412static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000413posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000414{
Barry Warsawca74da41999-02-09 19:31:45 +0000415 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416}
Barry Warsawd58d7641998-07-23 16:14:40 +0000417static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000418posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000419{
Barry Warsawca74da41999-02-09 19:31:45 +0000420 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000421}
422
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000423#ifdef Py_WIN_WIDE_FILENAMES
424static PyObject *
425posix_error_with_unicode_filename(Py_UNICODE* name)
426{
427 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
428}
429#endif /* Py_WIN_WIDE_FILENAMES */
430
431
Mark Hammondef8b6542001-05-13 08:04:26 +0000432static PyObject *
433posix_error_with_allocated_filename(char* name)
434{
435 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
436 PyMem_Free(name);
437 return rc;
438}
439
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000440#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000441static PyObject *
442win32_error(char* function, char* filename)
443{
Mark Hammond33a6da92000-08-15 00:46:38 +0000444 /* XXX We should pass the function name along in the future.
445 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000446 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000447 Windows error object, which is non-trivial.
448 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000449 errno = GetLastError();
450 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000451 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000452 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000453 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000454}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000455
456#ifdef Py_WIN_WIDE_FILENAMES
457static PyObject *
458win32_error_unicode(char* function, Py_UNICODE* filename)
459{
460 /* XXX - see win32_error for comments on 'function' */
461 errno = GetLastError();
462 if (filename)
463 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
464 else
465 return PyErr_SetFromWindowsErr(errno);
466}
467
468static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
469{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000470}
471
472/* Function suitable for O& conversion */
473static int
474convert_to_unicode(PyObject *arg, void* _param)
475{
476 PyObject **param = (PyObject**)_param;
477 if (PyUnicode_CheckExact(arg)) {
478 Py_INCREF(arg);
479 *param = arg;
480 }
481 else if (PyUnicode_Check(arg)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000482 /* For a Unicode subtype that's not a Unicode object,
483 return a true Unicode object with the same data. */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000484 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
485 PyUnicode_GET_SIZE(arg));
486 return *param != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000487 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000488 else
489 *param = PyUnicode_FromEncodedObject(arg,
490 Py_FileSystemDefaultEncoding,
491 "strict");
492 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000493}
494
495#endif /* Py_WIN_WIDE_FILENAMES */
496
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000497#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498
Guido van Rossumd48f2521997-12-05 22:19:34 +0000499#if defined(PYOS_OS2)
500/**********************************************************************
501 * Helper Function to Trim and Format OS/2 Messages
502 **********************************************************************/
503 static void
504os2_formatmsg(char *msgbuf, int msglen, char *reason)
505{
506 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
507
508 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
509 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
510
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000511 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000512 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
513 }
514
515 /* Add Optional Reason Text */
516 if (reason) {
517 strcat(msgbuf, " : ");
518 strcat(msgbuf, reason);
519 }
520}
521
522/**********************************************************************
523 * Decode an OS/2 Operating System Error Code
524 *
525 * A convenience function to lookup an OS/2 error code and return a
526 * text message we can use to raise a Python exception.
527 *
528 * Notes:
529 * The messages for errors returned from the OS/2 kernel reside in
530 * the file OSO001.MSG in the \OS2 directory hierarchy.
531 *
532 **********************************************************************/
533 static char *
534os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
535{
536 APIRET rc;
537 ULONG msglen;
538
539 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
540 Py_BEGIN_ALLOW_THREADS
541 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
542 errorcode, "oso001.msg", &msglen);
543 Py_END_ALLOW_THREADS
544
545 if (rc == NO_ERROR)
546 os2_formatmsg(msgbuf, msglen, reason);
547 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000548 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000549 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000550
551 return msgbuf;
552}
553
554/* Set an OS/2-specific error and return NULL. OS/2 kernel
555 errors are not in a global variable e.g. 'errno' nor are
556 they congruent with posix error numbers. */
557
558static PyObject * os2_error(int code)
559{
560 char text[1024];
561 PyObject *v;
562
563 os2_strerror(text, sizeof(text), code, "");
564
565 v = Py_BuildValue("(is)", code, text);
566 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000567 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000568 Py_DECREF(v);
569 }
570 return NULL; /* Signal to Python that an Exception is Pending */
571}
572
573#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574
575/* POSIX generic methods */
576
Barry Warsaw53699e91996-12-10 23:23:01 +0000577static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000578posix_fildes(PyObject *fdobj, int (*func)(int))
579{
580 int fd;
581 int res;
582 fd = PyObject_AsFileDescriptor(fdobj);
583 if (fd < 0)
584 return NULL;
585 Py_BEGIN_ALLOW_THREADS
586 res = (*func)(fd);
587 Py_END_ALLOW_THREADS
588 if (res < 0)
589 return posix_error();
590 Py_INCREF(Py_None);
591 return Py_None;
592}
Guido van Rossum21142a01999-01-08 21:05:37 +0000593
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000594#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000595static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000596unicode_file_names(void)
597{
598 static int canusewide = -1;
599 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000600 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000601 the Windows NT family. */
602 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
603 }
604 return canusewide;
605}
606#endif
Tim Peters11b23062003-04-23 02:39:17 +0000607
Guido van Rossum21142a01999-01-08 21:05:37 +0000608static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000609posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610{
Mark Hammondef8b6542001-05-13 08:04:26 +0000611 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000612 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000613 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000614 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000615 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000616 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000617 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000618 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000619 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000620 return posix_error_with_allocated_filename(path1);
621 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000622 Py_INCREF(Py_None);
623 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000624}
625
Barry Warsaw53699e91996-12-10 23:23:01 +0000626static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000627posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000628 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000629 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630{
Mark Hammondef8b6542001-05-13 08:04:26 +0000631 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000632 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000633 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000634 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000635 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000636 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000637 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000638 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000639 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000640 PyMem_Free(path1);
641 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000642 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000643 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000645 Py_INCREF(Py_None);
646 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647}
648
Thomas Wouters477c8d52006-05-27 19:21:47 +0000649#ifdef Py_WIN_WIDE_FILENAMES
650static PyObject*
651win32_1str(PyObject* args, char* func,
652 char* format, BOOL (__stdcall *funcA)(LPCSTR),
653 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
654{
655 PyObject *uni;
656 char *ansi;
657 BOOL result;
658 if (unicode_file_names()) {
659 if (!PyArg_ParseTuple(args, wformat, &uni))
660 PyErr_Clear();
661 else {
662 Py_BEGIN_ALLOW_THREADS
663 result = funcW(PyUnicode_AsUnicode(uni));
664 Py_END_ALLOW_THREADS
665 if (!result)
666 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
667 Py_INCREF(Py_None);
668 return Py_None;
669 }
670 }
671 if (!PyArg_ParseTuple(args, format, &ansi))
672 return NULL;
673 Py_BEGIN_ALLOW_THREADS
674 result = funcA(ansi);
675 Py_END_ALLOW_THREADS
676 if (!result)
677 return win32_error(func, ansi);
678 Py_INCREF(Py_None);
679 return Py_None;
680
681}
682
683/* This is a reimplementation of the C library's chdir function,
684 but one that produces Win32 errors instead of DOS error codes.
685 chdir is essentially a wrapper around SetCurrentDirectory; however,
686 it also needs to set "magic" environment variables indicating
687 the per-drive current directory, which are of the form =<drive>: */
688BOOL __stdcall
689win32_chdir(LPCSTR path)
690{
691 char new_path[MAX_PATH+1];
692 int result;
693 char env[4] = "=x:";
694
695 if(!SetCurrentDirectoryA(path))
696 return FALSE;
697 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
698 if (!result)
699 return FALSE;
700 /* In the ANSI API, there should not be any paths longer
701 than MAX_PATH. */
702 assert(result <= MAX_PATH+1);
703 if (strncmp(new_path, "\\\\", 2) == 0 ||
704 strncmp(new_path, "//", 2) == 0)
705 /* UNC path, nothing to do. */
706 return TRUE;
707 env[1] = new_path[0];
708 return SetEnvironmentVariableA(env, new_path);
709}
710
711/* The Unicode version differs from the ANSI version
712 since the current directory might exceed MAX_PATH characters */
713BOOL __stdcall
714win32_wchdir(LPCWSTR path)
715{
716 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
717 int result;
718 wchar_t env[4] = L"=x:";
719
720 if(!SetCurrentDirectoryW(path))
721 return FALSE;
722 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
723 if (!result)
724 return FALSE;
725 if (result > MAX_PATH+1) {
726 new_path = malloc(result);
727 if (!new_path) {
728 SetLastError(ERROR_OUTOFMEMORY);
729 return FALSE;
730 }
731 }
732 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
733 wcsncmp(new_path, L"//", 2) == 0)
734 /* UNC path, nothing to do. */
735 return TRUE;
736 env[1] = new_path[0];
737 result = SetEnvironmentVariableW(env, new_path);
738 if (new_path != _new_path)
739 free(new_path);
740 return result;
741}
742#endif
743
Martin v. Löwis14694662006-02-03 12:54:16 +0000744#ifdef MS_WINDOWS
745/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
746 - time stamps are restricted to second resolution
747 - file modification times suffer from forth-and-back conversions between
748 UTC and local time
749 Therefore, we implement our own stat, based on the Win32 API directly.
750*/
751#define HAVE_STAT_NSEC 1
752
753struct win32_stat{
754 int st_dev;
755 __int64 st_ino;
756 unsigned short st_mode;
757 int st_nlink;
758 int st_uid;
759 int st_gid;
760 int st_rdev;
761 __int64 st_size;
762 int st_atime;
763 int st_atime_nsec;
764 int st_mtime;
765 int st_mtime_nsec;
766 int st_ctime;
767 int st_ctime_nsec;
768};
769
770static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
771
772static void
773FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
774{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000775 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
776 /* Cannot simply cast and dereference in_ptr,
777 since it might not be aligned properly */
778 __int64 in;
779 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000780 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
781 /* XXX Win32 supports time stamps past 2038; we currently don't */
782 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
783}
784
Thomas Wouters477c8d52006-05-27 19:21:47 +0000785static void
786time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
787{
788 /* XXX endianness */
789 __int64 out;
790 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000791 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000792 memcpy(out_ptr, &out, sizeof(out));
793}
794
Martin v. Löwis14694662006-02-03 12:54:16 +0000795/* Below, we *know* that ugo+r is 0444 */
796#if _S_IREAD != 0400
797#error Unsupported C library
798#endif
799static int
800attributes_to_mode(DWORD attr)
801{
802 int m = 0;
803 if (attr & FILE_ATTRIBUTE_DIRECTORY)
804 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
805 else
806 m |= _S_IFREG;
807 if (attr & FILE_ATTRIBUTE_READONLY)
808 m |= 0444;
809 else
810 m |= 0666;
811 return m;
812}
813
814static int
815attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
816{
817 memset(result, 0, sizeof(*result));
818 result->st_mode = attributes_to_mode(info->dwFileAttributes);
819 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
820 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
821 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
822 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
823
824 return 0;
825}
826
Thomas Wouters89f507f2006-12-13 04:49:30 +0000827/* Emulate GetFileAttributesEx[AW] on Windows 95 */
828static int checked = 0;
829static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
830static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
831static void
832check_gfax()
833{
834 HINSTANCE hKernel32;
835 if (checked)
836 return;
837 checked = 1;
838 hKernel32 = GetModuleHandle("KERNEL32");
839 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
840 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
841}
842
Guido van Rossumd8faa362007-04-27 19:54:29 +0000843static BOOL
844attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
845{
846 HANDLE hFindFile;
847 WIN32_FIND_DATAA FileData;
848 hFindFile = FindFirstFileA(pszFile, &FileData);
849 if (hFindFile == INVALID_HANDLE_VALUE)
850 return FALSE;
851 FindClose(hFindFile);
852 pfad->dwFileAttributes = FileData.dwFileAttributes;
853 pfad->ftCreationTime = FileData.ftCreationTime;
854 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
855 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
856 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
857 pfad->nFileSizeLow = FileData.nFileSizeLow;
858 return TRUE;
859}
860
861static BOOL
862attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
863{
864 HANDLE hFindFile;
865 WIN32_FIND_DATAW FileData;
866 hFindFile = FindFirstFileW(pszFile, &FileData);
867 if (hFindFile == INVALID_HANDLE_VALUE)
868 return FALSE;
869 FindClose(hFindFile);
870 pfad->dwFileAttributes = FileData.dwFileAttributes;
871 pfad->ftCreationTime = FileData.ftCreationTime;
872 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
873 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
874 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
875 pfad->nFileSizeLow = FileData.nFileSizeLow;
876 return TRUE;
877}
878
Thomas Wouters89f507f2006-12-13 04:49:30 +0000879static BOOL WINAPI
880Py_GetFileAttributesExA(LPCSTR pszFile,
881 GET_FILEEX_INFO_LEVELS level,
882 LPVOID pv)
883{
884 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000885 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
886 /* First try to use the system's implementation, if that is
887 available and either succeeds to gives an error other than
888 that it isn't implemented. */
889 check_gfax();
890 if (gfaxa) {
891 result = gfaxa(pszFile, level, pv);
892 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
893 return result;
894 }
895 /* It's either not present, or not implemented.
896 Emulate using FindFirstFile. */
897 if (level != GetFileExInfoStandard) {
898 SetLastError(ERROR_INVALID_PARAMETER);
899 return FALSE;
900 }
901 /* Use GetFileAttributes to validate that the file name
902 does not contain wildcards (which FindFirstFile would
903 accept). */
904 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
905 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000906 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000907}
908
909static BOOL WINAPI
910Py_GetFileAttributesExW(LPCWSTR pszFile,
911 GET_FILEEX_INFO_LEVELS level,
912 LPVOID pv)
913{
914 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000915 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
916 /* First try to use the system's implementation, if that is
917 available and either succeeds to gives an error other than
918 that it isn't implemented. */
919 check_gfax();
920 if (gfaxa) {
921 result = gfaxw(pszFile, level, pv);
922 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
923 return result;
924 }
925 /* It's either not present, or not implemented.
926 Emulate using FindFirstFile. */
927 if (level != GetFileExInfoStandard) {
928 SetLastError(ERROR_INVALID_PARAMETER);
929 return FALSE;
930 }
931 /* Use GetFileAttributes to validate that the file name
932 does not contain wildcards (which FindFirstFile would
933 accept). */
934 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
935 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000936 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000937}
938
Martin v. Löwis14694662006-02-03 12:54:16 +0000939static int
940win32_stat(const char* path, struct win32_stat *result)
941{
942 WIN32_FILE_ATTRIBUTE_DATA info;
943 int code;
944 char *dot;
945 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000946 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000947 if (GetLastError() != ERROR_SHARING_VIOLATION) {
948 /* Protocol violation: we explicitly clear errno, instead of
949 setting it to a POSIX error. Callers should use GetLastError. */
950 errno = 0;
951 return -1;
952 } else {
953 /* Could not get attributes on open file. Fall back to
954 reading the directory. */
955 if (!attributes_from_dir(path, &info)) {
956 /* Very strange. This should not fail now */
957 errno = 0;
958 return -1;
959 }
960 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000961 }
962 code = attribute_data_to_stat(&info, result);
963 if (code != 0)
964 return code;
965 /* Set S_IFEXEC if it is an .exe, .bat, ... */
966 dot = strrchr(path, '.');
967 if (dot) {
968 if (stricmp(dot, ".bat") == 0 ||
969 stricmp(dot, ".cmd") == 0 ||
970 stricmp(dot, ".exe") == 0 ||
971 stricmp(dot, ".com") == 0)
972 result->st_mode |= 0111;
973 }
974 return code;
975}
976
977static int
978win32_wstat(const wchar_t* path, struct win32_stat *result)
979{
980 int code;
981 const wchar_t *dot;
982 WIN32_FILE_ATTRIBUTE_DATA info;
983 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000984 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000985 if (GetLastError() != ERROR_SHARING_VIOLATION) {
986 /* Protocol violation: we explicitly clear errno, instead of
987 setting it to a POSIX error. Callers should use GetLastError. */
988 errno = 0;
989 return -1;
990 } else {
991 /* Could not get attributes on open file. Fall back to
992 reading the directory. */
993 if (!attributes_from_dir_w(path, &info)) {
994 /* Very strange. This should not fail now */
995 errno = 0;
996 return -1;
997 }
998 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000999 }
1000 code = attribute_data_to_stat(&info, result);
1001 if (code < 0)
1002 return code;
1003 /* Set IFEXEC if it is an .exe, .bat, ... */
1004 dot = wcsrchr(path, '.');
1005 if (dot) {
1006 if (_wcsicmp(dot, L".bat") == 0 ||
1007 _wcsicmp(dot, L".cmd") == 0 ||
1008 _wcsicmp(dot, L".exe") == 0 ||
1009 _wcsicmp(dot, L".com") == 0)
1010 result->st_mode |= 0111;
1011 }
1012 return code;
1013}
1014
1015static int
1016win32_fstat(int file_number, struct win32_stat *result)
1017{
1018 BY_HANDLE_FILE_INFORMATION info;
1019 HANDLE h;
1020 int type;
1021
1022 h = (HANDLE)_get_osfhandle(file_number);
1023
1024 /* Protocol violation: we explicitly clear errno, instead of
1025 setting it to a POSIX error. Callers should use GetLastError. */
1026 errno = 0;
1027
1028 if (h == INVALID_HANDLE_VALUE) {
1029 /* This is really a C library error (invalid file handle).
1030 We set the Win32 error to the closes one matching. */
1031 SetLastError(ERROR_INVALID_HANDLE);
1032 return -1;
1033 }
1034 memset(result, 0, sizeof(*result));
1035
1036 type = GetFileType(h);
1037 if (type == FILE_TYPE_UNKNOWN) {
1038 DWORD error = GetLastError();
1039 if (error != 0) {
1040 return -1;
1041 }
1042 /* else: valid but unknown file */
1043 }
1044
1045 if (type != FILE_TYPE_DISK) {
1046 if (type == FILE_TYPE_CHAR)
1047 result->st_mode = _S_IFCHR;
1048 else if (type == FILE_TYPE_PIPE)
1049 result->st_mode = _S_IFIFO;
1050 return 0;
1051 }
1052
1053 if (!GetFileInformationByHandle(h, &info)) {
1054 return -1;
1055 }
1056
1057 /* similar to stat() */
1058 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1059 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1060 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1061 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1062 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1063 /* specific to fstat() */
1064 result->st_nlink = info.nNumberOfLinks;
1065 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1066 return 0;
1067}
1068
1069#endif /* MS_WINDOWS */
1070
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001071PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001072"stat_result: Result from stat or lstat.\n\n\
1073This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001074 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001075or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1076\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001077Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1078or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001079\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001080See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001081
1082static PyStructSequence_Field stat_result_fields[] = {
1083 {"st_mode", "protection bits"},
1084 {"st_ino", "inode"},
1085 {"st_dev", "device"},
1086 {"st_nlink", "number of hard links"},
1087 {"st_uid", "user ID of owner"},
1088 {"st_gid", "group ID of owner"},
1089 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001090 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1091 {NULL, "integer time of last access"},
1092 {NULL, "integer time of last modification"},
1093 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001094 {"st_atime", "time of last access"},
1095 {"st_mtime", "time of last modification"},
1096 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001097#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001098 {"st_blksize", "blocksize for filesystem I/O"},
1099#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001100#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001101 {"st_blocks", "number of blocks allocated"},
1102#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001103#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001104 {"st_rdev", "device type (if inode device)"},
1105#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001106#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1107 {"st_flags", "user defined flags for file"},
1108#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001109#ifdef HAVE_STRUCT_STAT_ST_GEN
1110 {"st_gen", "generation number"},
1111#endif
1112#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1113 {"st_birthtime", "time of creation"},
1114#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001115 {0}
1116};
1117
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001118#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001119#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001120#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001121#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001122#endif
1123
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001124#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001125#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1126#else
1127#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1128#endif
1129
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001130#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001131#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1132#else
1133#define ST_RDEV_IDX ST_BLOCKS_IDX
1134#endif
1135
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001136#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1137#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1138#else
1139#define ST_FLAGS_IDX ST_RDEV_IDX
1140#endif
1141
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001142#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001143#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001144#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001145#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001146#endif
1147
1148#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1149#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1150#else
1151#define ST_BIRTHTIME_IDX ST_GEN_IDX
1152#endif
1153
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001154static PyStructSequence_Desc stat_result_desc = {
1155 "stat_result", /* name */
1156 stat_result__doc__, /* doc */
1157 stat_result_fields,
1158 10
1159};
1160
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001161PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001162"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1163This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001164 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001165or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001166\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001167See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001168
1169static PyStructSequence_Field statvfs_result_fields[] = {
1170 {"f_bsize", },
1171 {"f_frsize", },
1172 {"f_blocks", },
1173 {"f_bfree", },
1174 {"f_bavail", },
1175 {"f_files", },
1176 {"f_ffree", },
1177 {"f_favail", },
1178 {"f_flag", },
1179 {"f_namemax",},
1180 {0}
1181};
1182
1183static PyStructSequence_Desc statvfs_result_desc = {
1184 "statvfs_result", /* name */
1185 statvfs_result__doc__, /* doc */
1186 statvfs_result_fields,
1187 10
1188};
1189
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001190static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001191static PyTypeObject StatResultType;
1192static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001193static newfunc structseq_new;
1194
1195static PyObject *
1196statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1197{
1198 PyStructSequence *result;
1199 int i;
1200
1201 result = (PyStructSequence*)structseq_new(type, args, kwds);
1202 if (!result)
1203 return NULL;
1204 /* If we have been initialized from a tuple,
1205 st_?time might be set to None. Initialize it
1206 from the int slots. */
1207 for (i = 7; i <= 9; i++) {
1208 if (result->ob_item[i+3] == Py_None) {
1209 Py_DECREF(Py_None);
1210 Py_INCREF(result->ob_item[i]);
1211 result->ob_item[i+3] = result->ob_item[i];
1212 }
1213 }
1214 return (PyObject*)result;
1215}
1216
1217
1218
1219/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001220static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001221
1222PyDoc_STRVAR(stat_float_times__doc__,
1223"stat_float_times([newval]) -> oldval\n\n\
1224Determine whether os.[lf]stat represents time stamps as float objects.\n\
1225If newval is True, future calls to stat() return floats, if it is False,\n\
1226future calls return ints. \n\
1227If newval is omitted, return the current setting.\n");
1228
1229static PyObject*
1230stat_float_times(PyObject* self, PyObject *args)
1231{
1232 int newval = -1;
1233 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1234 return NULL;
1235 if (newval == -1)
1236 /* Return old value */
1237 return PyBool_FromLong(_stat_float_times);
1238 _stat_float_times = newval;
1239 Py_INCREF(Py_None);
1240 return Py_None;
1241}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001242
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001243static void
1244fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1245{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001246 PyObject *fval,*ival;
1247#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001248 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001249#else
1250 ival = PyInt_FromLong((long)sec);
1251#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001252 if (!ival)
1253 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001254 if (_stat_float_times) {
1255 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1256 } else {
1257 fval = ival;
1258 Py_INCREF(fval);
1259 }
1260 PyStructSequence_SET_ITEM(v, index, ival);
1261 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001262}
1263
Tim Peters5aa91602002-01-30 05:46:57 +00001264/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001265 (used by posix_stat() and posix_fstat()) */
1266static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001267_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001268{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001269 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001270 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001271 if (v == NULL)
1272 return NULL;
1273
Martin v. Löwis14694662006-02-03 12:54:16 +00001274 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001275#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001276 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001277 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001278#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001279 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001280#endif
1281#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001282 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001283 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001284#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001285 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001286#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001287 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1288 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1289 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001290#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001291 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001292 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001293#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001294 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001295#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001296
Martin v. Löwis14694662006-02-03 12:54:16 +00001297#if defined(HAVE_STAT_TV_NSEC)
1298 ansec = st->st_atim.tv_nsec;
1299 mnsec = st->st_mtim.tv_nsec;
1300 cnsec = st->st_ctim.tv_nsec;
1301#elif defined(HAVE_STAT_TV_NSEC2)
1302 ansec = st->st_atimespec.tv_nsec;
1303 mnsec = st->st_mtimespec.tv_nsec;
1304 cnsec = st->st_ctimespec.tv_nsec;
1305#elif defined(HAVE_STAT_NSEC)
1306 ansec = st->st_atime_nsec;
1307 mnsec = st->st_mtime_nsec;
1308 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001309#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001310 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001311#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001312 fill_time(v, 7, st->st_atime, ansec);
1313 fill_time(v, 8, st->st_mtime, mnsec);
1314 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001315
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001316#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001317 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001318 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001319#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001320#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001321 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001322 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001323#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001324#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001325 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001326 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001327#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001328#ifdef HAVE_STRUCT_STAT_ST_GEN
1329 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001330 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001331#endif
1332#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1333 {
1334 PyObject *val;
1335 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001336 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001337#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001338 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001339#else
1340 bnsec = 0;
1341#endif
1342 if (_stat_float_times) {
1343 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1344 } else {
1345 val = PyInt_FromLong((long)bsec);
1346 }
1347 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1348 val);
1349 }
1350#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001351#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1352 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001353 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001354#endif
Fred Drake699f3522000-06-29 21:12:41 +00001355
1356 if (PyErr_Occurred()) {
1357 Py_DECREF(v);
1358 return NULL;
1359 }
1360
1361 return v;
1362}
1363
Martin v. Löwisd8948722004-06-02 09:57:56 +00001364#ifdef MS_WINDOWS
1365
1366/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1367 where / can be used in place of \ and the trailing slash is optional.
1368 Both SERVER and SHARE must have at least one character.
1369*/
1370
1371#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1372#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001373#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001374#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001375#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001376
Tim Peters4ad82172004-08-30 17:02:04 +00001377static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001378IsUNCRootA(char *path, int pathlen)
1379{
1380 #define ISSLASH ISSLASHA
1381
1382 int i, share;
1383
1384 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1385 /* minimum UNCRoot is \\x\y */
1386 return FALSE;
1387 for (i = 2; i < pathlen ; i++)
1388 if (ISSLASH(path[i])) break;
1389 if (i == 2 || i == pathlen)
1390 /* do not allow \\\SHARE or \\SERVER */
1391 return FALSE;
1392 share = i+1;
1393 for (i = share; i < pathlen; i++)
1394 if (ISSLASH(path[i])) break;
1395 return (i != share && (i == pathlen || i == pathlen-1));
1396
1397 #undef ISSLASH
1398}
1399
1400#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001401static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001402IsUNCRootW(Py_UNICODE *path, int pathlen)
1403{
1404 #define ISSLASH ISSLASHW
1405
1406 int i, share;
1407
1408 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1409 /* minimum UNCRoot is \\x\y */
1410 return FALSE;
1411 for (i = 2; i < pathlen ; i++)
1412 if (ISSLASH(path[i])) break;
1413 if (i == 2 || i == pathlen)
1414 /* do not allow \\\SHARE or \\SERVER */
1415 return FALSE;
1416 share = i+1;
1417 for (i = share; i < pathlen; i++)
1418 if (ISSLASH(path[i])) break;
1419 return (i != share && (i == pathlen || i == pathlen-1));
1420
1421 #undef ISSLASH
1422}
1423#endif /* Py_WIN_WIDE_FILENAMES */
1424#endif /* MS_WINDOWS */
1425
Barry Warsaw53699e91996-12-10 23:23:01 +00001426static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001427posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001428 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001429#ifdef __VMS
1430 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1431#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001432 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001433#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001434 char *wformat,
1435 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001436{
Fred Drake699f3522000-06-29 21:12:41 +00001437 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001438 char *path = NULL; /* pass this to stat; do not free() it */
1439 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001440 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001441 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001442
1443#ifdef Py_WIN_WIDE_FILENAMES
1444 /* If on wide-character-capable OS see if argument
1445 is Unicode and if so use wide API. */
1446 if (unicode_file_names()) {
1447 PyUnicodeObject *po;
1448 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001449 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1450
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001451 Py_BEGIN_ALLOW_THREADS
1452 /* PyUnicode_AS_UNICODE result OK without
1453 thread lock as it is a simple dereference. */
1454 res = wstatfunc(wpath, &st);
1455 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001456
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001457 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001458 return win32_error_unicode("stat", wpath);
1459 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001460 }
1461 /* Drop the argument parsing error as narrow strings
1462 are also valid. */
1463 PyErr_Clear();
1464 }
1465#endif
1466
Tim Peters5aa91602002-01-30 05:46:57 +00001467 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001468 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001469 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001470 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001471
Barry Warsaw53699e91996-12-10 23:23:01 +00001472 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001473 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001474 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001475
1476 if (res != 0) {
1477#ifdef MS_WINDOWS
1478 result = win32_error("stat", pathfree);
1479#else
1480 result = posix_error_with_filename(pathfree);
1481#endif
1482 }
1483 else
1484 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001485
Tim Peters500bd032001-12-19 19:05:01 +00001486 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001487 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001488}
1489
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001490/* POSIX methods */
1491
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001492PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001493"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001494Use the real uid/gid to test for access to a path. Note that most\n\
1495operations will use the effective uid/gid, therefore this routine can\n\
1496be used in a suid/sgid environment to test if the invoking user has the\n\
1497specified access to the path. The mode argument can be F_OK to test\n\
1498existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001499
1500static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001501posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001502{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001503 char *path;
1504 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001505
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001506#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001507 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001508 if (unicode_file_names()) {
1509 PyUnicodeObject *po;
1510 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1511 Py_BEGIN_ALLOW_THREADS
1512 /* PyUnicode_AS_UNICODE OK without thread lock as
1513 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001514 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001515 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001516 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001517 }
1518 /* Drop the argument parsing error as narrow strings
1519 are also valid. */
1520 PyErr_Clear();
1521 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001522 if (!PyArg_ParseTuple(args, "eti:access",
1523 Py_FileSystemDefaultEncoding, &path, &mode))
1524 return 0;
1525 Py_BEGIN_ALLOW_THREADS
1526 attr = GetFileAttributesA(path);
1527 Py_END_ALLOW_THREADS
1528 PyMem_Free(path);
1529finish:
1530 if (attr == 0xFFFFFFFF)
1531 /* File does not exist, or cannot read attributes */
1532 return PyBool_FromLong(0);
1533 /* Access is possible if either write access wasn't requested, or
1534 the file isn't read-only. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001535 return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001536#else
1537 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001538 if (!PyArg_ParseTuple(args, "eti:access",
1539 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001540 return NULL;
1541 Py_BEGIN_ALLOW_THREADS
1542 res = access(path, mode);
1543 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001544 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001545 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001546#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001547}
1548
Guido van Rossumd371ff11999-01-25 16:12:23 +00001549#ifndef F_OK
1550#define F_OK 0
1551#endif
1552#ifndef R_OK
1553#define R_OK 4
1554#endif
1555#ifndef W_OK
1556#define W_OK 2
1557#endif
1558#ifndef X_OK
1559#define X_OK 1
1560#endif
1561
1562#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001563PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001564"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001565Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001566
1567static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001568posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001569{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001570 int id;
1571 char *ret;
1572
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001573 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001574 return NULL;
1575
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001576#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001577 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001578 if (id == 0) {
1579 ret = ttyname();
1580 }
1581 else {
1582 ret = NULL;
1583 }
1584#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001585 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001586#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001587 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001588 return posix_error();
1589 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001590}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001591#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001592
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001593#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001594PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001595"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001596Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001597
1598static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001599posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001600{
1601 char *ret;
1602 char buffer[L_ctermid];
1603
Greg Wardb48bc172000-03-01 21:51:56 +00001604#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001605 ret = ctermid_r(buffer);
1606#else
1607 ret = ctermid(buffer);
1608#endif
1609 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001610 return posix_error();
1611 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001612}
1613#endif
1614
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001615PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001616"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001617Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001618
Barry Warsaw53699e91996-12-10 23:23:01 +00001619static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001620posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001621{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001622#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001623 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001624#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001625 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001626#elif defined(__VMS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001627 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001628#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00001629 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001630#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001631}
1632
Fred Drake4d1e64b2002-04-15 19:40:07 +00001633#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001634PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001635"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001636Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001637opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001638
1639static PyObject *
1640posix_fchdir(PyObject *self, PyObject *fdobj)
1641{
1642 return posix_fildes(fdobj, fchdir);
1643}
1644#endif /* HAVE_FCHDIR */
1645
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001646
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001647PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001648"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001649Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001650
Barry Warsaw53699e91996-12-10 23:23:01 +00001651static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001652posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001653{
Mark Hammondef8b6542001-05-13 08:04:26 +00001654 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001655 int i;
1656 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001657#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001658 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001659 if (unicode_file_names()) {
1660 PyUnicodeObject *po;
1661 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1662 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001663 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1664 if (attr != 0xFFFFFFFF) {
1665 if (i & _S_IWRITE)
1666 attr &= ~FILE_ATTRIBUTE_READONLY;
1667 else
1668 attr |= FILE_ATTRIBUTE_READONLY;
1669 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1670 }
1671 else
1672 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001673 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001674 if (!res)
1675 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001676 PyUnicode_AS_UNICODE(po));
1677 Py_INCREF(Py_None);
1678 return Py_None;
1679 }
1680 /* Drop the argument parsing error as narrow strings
1681 are also valid. */
1682 PyErr_Clear();
1683 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001684 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1685 &path, &i))
1686 return NULL;
1687 Py_BEGIN_ALLOW_THREADS
1688 attr = GetFileAttributesA(path);
1689 if (attr != 0xFFFFFFFF) {
1690 if (i & _S_IWRITE)
1691 attr &= ~FILE_ATTRIBUTE_READONLY;
1692 else
1693 attr |= FILE_ATTRIBUTE_READONLY;
1694 res = SetFileAttributesA(path, attr);
1695 }
1696 else
1697 res = 0;
1698 Py_END_ALLOW_THREADS
1699 if (!res) {
1700 win32_error("chmod", path);
1701 PyMem_Free(path);
1702 return NULL;
1703 }
1704 PyMem_Free(path);
1705 Py_INCREF(Py_None);
1706 return Py_None;
1707#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001708 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001709 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001710 return NULL;
1711 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001712 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001713 Py_END_ALLOW_THREADS
1714 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001715 return posix_error_with_allocated_filename(path);
1716 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001717 Py_INCREF(Py_None);
1718 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001719#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001720}
1721
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001722
Thomas Wouterscf297e42007-02-23 15:07:44 +00001723#ifdef HAVE_CHFLAGS
1724PyDoc_STRVAR(posix_chflags__doc__,
1725"chflags(path, flags)\n\n\
1726Set file flags.");
1727
1728static PyObject *
1729posix_chflags(PyObject *self, PyObject *args)
1730{
1731 char *path;
1732 unsigned long flags;
1733 int res;
1734 if (!PyArg_ParseTuple(args, "etk:chflags",
1735 Py_FileSystemDefaultEncoding, &path, &flags))
1736 return NULL;
1737 Py_BEGIN_ALLOW_THREADS
1738 res = chflags(path, flags);
1739 Py_END_ALLOW_THREADS
1740 if (res < 0)
1741 return posix_error_with_allocated_filename(path);
1742 PyMem_Free(path);
1743 Py_INCREF(Py_None);
1744 return Py_None;
1745}
1746#endif /* HAVE_CHFLAGS */
1747
1748#ifdef HAVE_LCHFLAGS
1749PyDoc_STRVAR(posix_lchflags__doc__,
1750"lchflags(path, flags)\n\n\
1751Set file flags.\n\
1752This function will not follow symbolic links.");
1753
1754static PyObject *
1755posix_lchflags(PyObject *self, PyObject *args)
1756{
1757 char *path;
1758 unsigned long flags;
1759 int res;
1760 if (!PyArg_ParseTuple(args, "etk:lchflags",
1761 Py_FileSystemDefaultEncoding, &path, &flags))
1762 return NULL;
1763 Py_BEGIN_ALLOW_THREADS
1764 res = lchflags(path, flags);
1765 Py_END_ALLOW_THREADS
1766 if (res < 0)
1767 return posix_error_with_allocated_filename(path);
1768 PyMem_Free(path);
1769 Py_INCREF(Py_None);
1770 return Py_None;
1771}
1772#endif /* HAVE_LCHFLAGS */
1773
Martin v. Löwis244edc82001-10-04 22:44:26 +00001774#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001775PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001776"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001777Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001778
1779static PyObject *
1780posix_chroot(PyObject *self, PyObject *args)
1781{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001782 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001783}
1784#endif
1785
Guido van Rossum21142a01999-01-08 21:05:37 +00001786#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001787PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001788"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001790
1791static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001792posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001793{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001794 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001795}
1796#endif /* HAVE_FSYNC */
1797
1798#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001799
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001800#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001801extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1802#endif
1803
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001804PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001805"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001806force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001807 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001808
1809static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001810posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001811{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001812 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001813}
1814#endif /* HAVE_FDATASYNC */
1815
1816
Fredrik Lundh10723342000-07-10 16:38:09 +00001817#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001818PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001819"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001820Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001821
Barry Warsaw53699e91996-12-10 23:23:01 +00001822static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001823posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001824{
Mark Hammondef8b6542001-05-13 08:04:26 +00001825 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001826 int uid, gid;
1827 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001828 if (!PyArg_ParseTuple(args, "etii:chown",
1829 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001830 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001831 return NULL;
1832 Py_BEGIN_ALLOW_THREADS
1833 res = chown(path, (uid_t) uid, (gid_t) gid);
1834 Py_END_ALLOW_THREADS
1835 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001836 return posix_error_with_allocated_filename(path);
1837 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001838 Py_INCREF(Py_None);
1839 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001840}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001841#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001842
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001843#ifdef HAVE_LCHOWN
1844PyDoc_STRVAR(posix_lchown__doc__,
1845"lchown(path, uid, gid)\n\n\
1846Change the owner and group id of path to the numeric uid and gid.\n\
1847This function will not follow symbolic links.");
1848
1849static PyObject *
1850posix_lchown(PyObject *self, PyObject *args)
1851{
1852 char *path = NULL;
1853 int uid, gid;
1854 int res;
1855 if (!PyArg_ParseTuple(args, "etii:lchown",
1856 Py_FileSystemDefaultEncoding, &path,
1857 &uid, &gid))
1858 return NULL;
1859 Py_BEGIN_ALLOW_THREADS
1860 res = lchown(path, (uid_t) uid, (gid_t) gid);
1861 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001862 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001863 return posix_error_with_allocated_filename(path);
1864 PyMem_Free(path);
1865 Py_INCREF(Py_None);
1866 return Py_None;
1867}
1868#endif /* HAVE_LCHOWN */
1869
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001870
Guido van Rossum36bc6801995-06-14 22:54:23 +00001871#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001872PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001873"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001874Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001875
Barry Warsaw53699e91996-12-10 23:23:01 +00001876static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001877posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001878{
1879 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001880 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001881
Barry Warsaw53699e91996-12-10 23:23:01 +00001882 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001883#if defined(PYOS_OS2) && defined(PYCC_GCC)
1884 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001885#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001886 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001887#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001888 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001889 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001890 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001891 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001892}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001893
1894PyDoc_STRVAR(posix_getcwdu__doc__,
1895"getcwdu() -> path\n\n\
1896Return a unicode string representing the current working directory.");
1897
1898static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001899posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001900{
1901 char buf[1026];
1902 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001903
1904#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001905 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001906 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001907 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001908 wchar_t *wbuf2 = wbuf;
1909 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001910 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001911 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1912 /* If the buffer is large enough, len does not include the
1913 terminating \0. If the buffer is too small, len includes
1914 the space needed for the terminator. */
1915 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1916 wbuf2 = malloc(len * sizeof(wchar_t));
1917 if (wbuf2)
1918 len = GetCurrentDirectoryW(len, wbuf2);
1919 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001920 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001921 if (!wbuf2) {
1922 PyErr_NoMemory();
1923 return NULL;
1924 }
1925 if (!len) {
1926 if (wbuf2 != wbuf) free(wbuf2);
1927 return win32_error("getcwdu", NULL);
1928 }
1929 resobj = PyUnicode_FromWideChar(wbuf2, len);
1930 if (wbuf2 != wbuf) free(wbuf2);
1931 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001932 }
1933#endif
1934
1935 Py_BEGIN_ALLOW_THREADS
1936#if defined(PYOS_OS2) && defined(PYCC_GCC)
1937 res = _getcwd2(buf, sizeof buf);
1938#else
1939 res = getcwd(buf, sizeof buf);
1940#endif
1941 Py_END_ALLOW_THREADS
1942 if (res == NULL)
1943 return posix_error();
1944 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1945}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001946#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001947
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001948
Guido van Rossumb6775db1994-08-01 11:34:53 +00001949#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001950PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001951"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001952Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001953
Barry Warsaw53699e91996-12-10 23:23:01 +00001954static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001955posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001956{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001957 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001958}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001959#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001960
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001961
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001962PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001963"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001964Return a list containing the names of the entries in the directory.\n\
1965\n\
1966 path: path of directory to list\n\
1967\n\
1968The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001969entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001970
Barry Warsaw53699e91996-12-10 23:23:01 +00001971static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001972posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001973{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001974 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001975 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001976#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001977
Barry Warsaw53699e91996-12-10 23:23:01 +00001978 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001979 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001980 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001981 WIN32_FIND_DATA FileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001982 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00001983 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001984 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001985
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001986#ifdef Py_WIN_WIDE_FILENAMES
1987 /* If on wide-character-capable OS see if argument
1988 is Unicode and if so use wide API. */
1989 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001990 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001991 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1992 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001993 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001994 Py_UNICODE wch;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001995 /* Overallocate for \\*.*\0 */
1996 len = PyUnicode_GET_SIZE(po);
1997 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
1998 if (!wnamebuf) {
1999 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002000 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002001 }
2002 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2003 wch = len > 0 ? wnamebuf[len-1] : '\0';
2004 if (wch != L'/' && wch != L'\\' && wch != L':')
2005 wnamebuf[len++] = L'\\';
2006 wcscpy(wnamebuf + len, L"*.*");
2007 if ((d = PyList_New(0)) == NULL) {
2008 free(wnamebuf);
2009 return NULL;
2010 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002011 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2012 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002013 int error = GetLastError();
2014 if (error == ERROR_FILE_NOT_FOUND) {
2015 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002016 return d;
2017 }
2018 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002019 win32_error_unicode("FindFirstFileW", wnamebuf);
2020 free(wnamebuf);
2021 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002022 }
2023 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002024 /* Skip over . and .. */
2025 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2026 wcscmp(wFileData.cFileName, L"..") != 0) {
2027 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2028 if (v == NULL) {
2029 Py_DECREF(d);
2030 d = NULL;
2031 break;
2032 }
2033 if (PyList_Append(d, v) != 0) {
2034 Py_DECREF(v);
2035 Py_DECREF(d);
2036 d = NULL;
2037 break;
2038 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002039 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002040 }
Georg Brandl622927b2006-03-07 12:48:03 +00002041 Py_BEGIN_ALLOW_THREADS
2042 result = FindNextFileW(hFindFile, &wFileData);
2043 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002044 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2045 it got to the end of the directory. */
2046 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2047 Py_DECREF(d);
2048 win32_error_unicode("FindNextFileW", wnamebuf);
2049 FindClose(hFindFile);
2050 free(wnamebuf);
2051 return NULL;
2052 }
Georg Brandl622927b2006-03-07 12:48:03 +00002053 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002054
2055 if (FindClose(hFindFile) == FALSE) {
2056 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002057 win32_error_unicode("FindClose", wnamebuf);
2058 free(wnamebuf);
2059 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002060 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002061 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002062 return d;
2063 }
2064 /* Drop the argument parsing error as narrow strings
2065 are also valid. */
2066 PyErr_Clear();
2067 }
2068#endif
2069
Tim Peters5aa91602002-01-30 05:46:57 +00002070 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002071 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002072 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002073 if (len > 0) {
2074 char ch = namebuf[len-1];
2075 if (ch != SEP && ch != ALTSEP && ch != ':')
2076 namebuf[len++] = '/';
2077 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002078 strcpy(namebuf + len, "*.*");
2079
Barry Warsaw53699e91996-12-10 23:23:01 +00002080 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002081 return NULL;
2082
2083 hFindFile = FindFirstFile(namebuf, &FileData);
2084 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002085 int error = GetLastError();
2086 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002087 return d;
2088 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002089 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002090 }
2091 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002092 /* Skip over . and .. */
2093 if (strcmp(FileData.cFileName, ".") != 0 &&
2094 strcmp(FileData.cFileName, "..") != 0) {
2095 v = PyString_FromString(FileData.cFileName);
2096 if (v == NULL) {
2097 Py_DECREF(d);
2098 d = NULL;
2099 break;
2100 }
2101 if (PyList_Append(d, v) != 0) {
2102 Py_DECREF(v);
2103 Py_DECREF(d);
2104 d = NULL;
2105 break;
2106 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002107 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002108 }
Georg Brandl622927b2006-03-07 12:48:03 +00002109 Py_BEGIN_ALLOW_THREADS
2110 result = FindNextFile(hFindFile, &FileData);
2111 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002112 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2113 it got to the end of the directory. */
2114 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2115 Py_DECREF(d);
2116 win32_error("FindNextFile", namebuf);
2117 FindClose(hFindFile);
2118 return NULL;
2119 }
Georg Brandl622927b2006-03-07 12:48:03 +00002120 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002121
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002122 if (FindClose(hFindFile) == FALSE) {
2123 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002124 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002125 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002126
2127 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002128
Tim Peters0bb44a42000-09-15 07:44:49 +00002129#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002130
2131#ifndef MAX_PATH
2132#define MAX_PATH CCHMAXPATH
2133#endif
2134 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002135 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002136 PyObject *d, *v;
2137 char namebuf[MAX_PATH+5];
2138 HDIR hdir = 1;
2139 ULONG srchcnt = 1;
2140 FILEFINDBUF3 ep;
2141 APIRET rc;
2142
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002143 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002144 return NULL;
2145 if (len >= MAX_PATH) {
2146 PyErr_SetString(PyExc_ValueError, "path too long");
2147 return NULL;
2148 }
2149 strcpy(namebuf, name);
2150 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002151 if (*pt == ALTSEP)
2152 *pt = SEP;
2153 if (namebuf[len-1] != SEP)
2154 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002155 strcpy(namebuf + len, "*.*");
2156
2157 if ((d = PyList_New(0)) == NULL)
2158 return NULL;
2159
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002160 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2161 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002162 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002163 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2164 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2165 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002166
2167 if (rc != NO_ERROR) {
2168 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002169 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002170 }
2171
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002172 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002173 do {
2174 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002175 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002176 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002177
2178 strcpy(namebuf, ep.achName);
2179
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002180 /* Leave Case of Name Alone -- In Native Form */
2181 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002182
2183 v = PyString_FromString(namebuf);
2184 if (v == NULL) {
2185 Py_DECREF(d);
2186 d = NULL;
2187 break;
2188 }
2189 if (PyList_Append(d, v) != 0) {
2190 Py_DECREF(v);
2191 Py_DECREF(d);
2192 d = NULL;
2193 break;
2194 }
2195 Py_DECREF(v);
2196 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2197 }
2198
2199 return d;
2200#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002201
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002202 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002203 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002204 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002205 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002206 int arg_is_unicode = 1;
2207
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002208 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002209 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2210 arg_is_unicode = 0;
2211 PyErr_Clear();
2212 }
2213 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002214 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002215 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002216 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002217 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002218 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002219 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002220 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002221 return NULL;
2222 }
Georg Brandl622927b2006-03-07 12:48:03 +00002223 for (;;) {
2224 Py_BEGIN_ALLOW_THREADS
2225 ep = readdir(dirp);
2226 Py_END_ALLOW_THREADS
2227 if (ep == NULL)
2228 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002229 if (ep->d_name[0] == '.' &&
2230 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002231 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002232 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00002233 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002234 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002235 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002236 d = NULL;
2237 break;
2238 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002239 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002240 PyObject *w;
2241
2242 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002243 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002244 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002245 if (w != NULL) {
2246 Py_DECREF(v);
2247 v = w;
2248 }
2249 else {
2250 /* fall back to the original byte string, as
2251 discussed in patch #683592 */
2252 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002253 }
Just van Rossum46c97842003-02-25 21:42:15 +00002254 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002255 if (PyList_Append(d, v) != 0) {
2256 Py_DECREF(v);
2257 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002258 d = NULL;
2259 break;
2260 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002261 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002262 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002263 if (errno != 0 && d != NULL) {
2264 /* readdir() returned NULL and set errno */
2265 closedir(dirp);
2266 Py_DECREF(d);
2267 return posix_error_with_allocated_filename(name);
2268 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002269 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002270 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002271
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002272 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002273
Tim Peters0bb44a42000-09-15 07:44:49 +00002274#endif /* which OS */
2275} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002276
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002277#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002278/* A helper function for abspath on win32 */
2279static PyObject *
2280posix__getfullpathname(PyObject *self, PyObject *args)
2281{
2282 /* assume encoded strings wont more than double no of chars */
2283 char inbuf[MAX_PATH*2];
2284 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002285 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002286 char outbuf[MAX_PATH*2];
2287 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002288#ifdef Py_WIN_WIDE_FILENAMES
2289 if (unicode_file_names()) {
2290 PyUnicodeObject *po;
2291 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2292 Py_UNICODE woutbuf[MAX_PATH*2];
2293 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002294 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002295 sizeof(woutbuf)/sizeof(woutbuf[0]),
2296 woutbuf, &wtemp))
2297 return win32_error("GetFullPathName", "");
2298 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2299 }
2300 /* Drop the argument parsing error as narrow strings
2301 are also valid. */
2302 PyErr_Clear();
2303 }
2304#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002305 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2306 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002307 &insize))
2308 return NULL;
2309 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2310 outbuf, &temp))
2311 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002312 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2313 return PyUnicode_Decode(outbuf, strlen(outbuf),
2314 Py_FileSystemDefaultEncoding, NULL);
2315 }
Mark Hammondef8b6542001-05-13 08:04:26 +00002316 return PyString_FromString(outbuf);
2317} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002318#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002320PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002321"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002322Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002323
Barry Warsaw53699e91996-12-10 23:23:01 +00002324static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002325posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002326{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002327 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002328 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002329 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002330
2331#ifdef Py_WIN_WIDE_FILENAMES
2332 if (unicode_file_names()) {
2333 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002334 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002335 Py_BEGIN_ALLOW_THREADS
2336 /* PyUnicode_AS_UNICODE OK without thread lock as
2337 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002338 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002339 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002340 if (!res)
2341 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002342 Py_INCREF(Py_None);
2343 return Py_None;
2344 }
2345 /* Drop the argument parsing error as narrow strings
2346 are also valid. */
2347 PyErr_Clear();
2348 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002349 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2350 Py_FileSystemDefaultEncoding, &path, &mode))
2351 return NULL;
2352 Py_BEGIN_ALLOW_THREADS
2353 /* PyUnicode_AS_UNICODE OK without thread lock as
2354 it is a simple dereference. */
2355 res = CreateDirectoryA(path, NULL);
2356 Py_END_ALLOW_THREADS
2357 if (!res) {
2358 win32_error("mkdir", path);
2359 PyMem_Free(path);
2360 return NULL;
2361 }
2362 PyMem_Free(path);
2363 Py_INCREF(Py_None);
2364 return Py_None;
2365#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002366
Tim Peters5aa91602002-01-30 05:46:57 +00002367 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002368 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002369 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002370 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002371#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002372 res = mkdir(path);
2373#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002374 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002375#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002376 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002377 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002378 return posix_error_with_allocated_filename(path);
2379 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002380 Py_INCREF(Py_None);
2381 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002382#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002383}
2384
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002385
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002386/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2387#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002388#include <sys/resource.h>
2389#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002390
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002391
2392#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002393PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002394"nice(inc) -> new_priority\n\n\
2395Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002396
Barry Warsaw53699e91996-12-10 23:23:01 +00002397static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002398posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002399{
2400 int increment, value;
2401
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002402 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002403 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002404
2405 /* There are two flavours of 'nice': one that returns the new
2406 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002407 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2408 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002409
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002410 If we are of the nice family that returns the new priority, we
2411 need to clear errno before the call, and check if errno is filled
2412 before calling posix_error() on a returnvalue of -1, because the
2413 -1 may be the actual new priority! */
2414
2415 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002416 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002417#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002418 if (value == 0)
2419 value = getpriority(PRIO_PROCESS, 0);
2420#endif
2421 if (value == -1 && errno != 0)
2422 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002423 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002424 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002425}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002426#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002427
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002428PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002429"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002430Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002431
Barry Warsaw53699e91996-12-10 23:23:01 +00002432static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002433posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002434{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002435#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002436 PyObject *o1, *o2;
2437 char *p1, *p2;
2438 BOOL result;
2439 if (unicode_file_names()) {
2440 if (!PyArg_ParseTuple(args, "O&O&:rename",
2441 convert_to_unicode, &o1,
2442 convert_to_unicode, &o2))
2443 PyErr_Clear();
2444 else {
2445 Py_BEGIN_ALLOW_THREADS
2446 result = MoveFileW(PyUnicode_AsUnicode(o1),
2447 PyUnicode_AsUnicode(o2));
2448 Py_END_ALLOW_THREADS
2449 Py_DECREF(o1);
2450 Py_DECREF(o2);
2451 if (!result)
2452 return win32_error("rename", NULL);
2453 Py_INCREF(Py_None);
2454 return Py_None;
2455 }
2456 }
2457 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2458 return NULL;
2459 Py_BEGIN_ALLOW_THREADS
2460 result = MoveFileA(p1, p2);
2461 Py_END_ALLOW_THREADS
2462 if (!result)
2463 return win32_error("rename", NULL);
2464 Py_INCREF(Py_None);
2465 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002466#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002467 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002468#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002469}
2470
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002472PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002473"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002474Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002475
Barry Warsaw53699e91996-12-10 23:23:01 +00002476static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002477posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002478{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002479#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002480 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002481#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002482 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002483#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002484}
2485
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002487PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002488"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002489Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002490
Barry Warsaw53699e91996-12-10 23:23:01 +00002491static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002492posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002493{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002494#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002495 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002496#else
2497 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2498#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002499}
2500
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002501
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002502#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002503PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002504"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002505Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002506
Barry Warsaw53699e91996-12-10 23:23:01 +00002507static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002508posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002509{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002510 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002511 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002512 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002513 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002514 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002515 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002516 Py_END_ALLOW_THREADS
2517 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002518}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002519#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002520
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002522PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002523"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002524Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002525
Barry Warsaw53699e91996-12-10 23:23:01 +00002526static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002527posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002528{
2529 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002530 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002531 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002532 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002533 if (i < 0)
2534 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002535 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002536}
2537
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002538
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002539PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002540"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002541Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002543PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002544"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002545Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002546
Barry Warsaw53699e91996-12-10 23:23:01 +00002547static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002548posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002549{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002550#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002551 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002552#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002553 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002554#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002555}
2556
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002557
Guido van Rossumb6775db1994-08-01 11:34:53 +00002558#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002559PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002560"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002561Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002562
Barry Warsaw53699e91996-12-10 23:23:01 +00002563static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002564posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002565{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002566 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002567 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002568
Barry Warsaw53699e91996-12-10 23:23:01 +00002569 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002570 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002571 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002572 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002573 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002574 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002575 u.sysname,
2576 u.nodename,
2577 u.release,
2578 u.version,
2579 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002580}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002581#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002582
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002583static int
2584extract_time(PyObject *t, long* sec, long* usec)
2585{
2586 long intval;
2587 if (PyFloat_Check(t)) {
2588 double tval = PyFloat_AsDouble(t);
2589 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
2590 if (!intobj)
2591 return -1;
2592 intval = PyInt_AsLong(intobj);
2593 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002594 if (intval == -1 && PyErr_Occurred())
2595 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002596 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002597 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002598 if (*usec < 0)
2599 /* If rounding gave us a negative number,
2600 truncate. */
2601 *usec = 0;
2602 return 0;
2603 }
2604 intval = PyInt_AsLong(t);
2605 if (intval == -1 && PyErr_Occurred())
2606 return -1;
2607 *sec = intval;
2608 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002609 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002610}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002611
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002612PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002613"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002614utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002615Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002616second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002617
Barry Warsaw53699e91996-12-10 23:23:01 +00002618static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002619posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002620{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002621#ifdef Py_WIN_WIDE_FILENAMES
2622 PyObject *arg;
2623 PyUnicodeObject *obwpath;
2624 wchar_t *wpath = NULL;
2625 char *apath = NULL;
2626 HANDLE hFile;
2627 long atimesec, mtimesec, ausec, musec;
2628 FILETIME atime, mtime;
2629 PyObject *result = NULL;
2630
2631 if (unicode_file_names()) {
2632 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2633 wpath = PyUnicode_AS_UNICODE(obwpath);
2634 Py_BEGIN_ALLOW_THREADS
2635 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002636 NULL, OPEN_EXISTING,
2637 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002638 Py_END_ALLOW_THREADS
2639 if (hFile == INVALID_HANDLE_VALUE)
2640 return win32_error_unicode("utime", wpath);
2641 } else
2642 /* Drop the argument parsing error as narrow strings
2643 are also valid. */
2644 PyErr_Clear();
2645 }
2646 if (!wpath) {
2647 if (!PyArg_ParseTuple(args, "etO:utime",
2648 Py_FileSystemDefaultEncoding, &apath, &arg))
2649 return NULL;
2650 Py_BEGIN_ALLOW_THREADS
2651 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002652 NULL, OPEN_EXISTING,
2653 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002654 Py_END_ALLOW_THREADS
2655 if (hFile == INVALID_HANDLE_VALUE) {
2656 win32_error("utime", apath);
2657 PyMem_Free(apath);
2658 return NULL;
2659 }
2660 PyMem_Free(apath);
2661 }
2662
2663 if (arg == Py_None) {
2664 SYSTEMTIME now;
2665 GetSystemTime(&now);
2666 if (!SystemTimeToFileTime(&now, &mtime) ||
2667 !SystemTimeToFileTime(&now, &atime)) {
2668 win32_error("utime", NULL);
2669 goto done;
2670 }
2671 }
2672 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2673 PyErr_SetString(PyExc_TypeError,
2674 "utime() arg 2 must be a tuple (atime, mtime)");
2675 goto done;
2676 }
2677 else {
2678 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2679 &atimesec, &ausec) == -1)
2680 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002681 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002682 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2683 &mtimesec, &musec) == -1)
2684 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002685 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002686 }
2687 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2688 /* Avoid putting the file name into the error here,
2689 as that may confuse the user into believing that
2690 something is wrong with the file, when it also
2691 could be the time stamp that gives a problem. */
2692 win32_error("utime", NULL);
2693 }
2694 Py_INCREF(Py_None);
2695 result = Py_None;
2696done:
2697 CloseHandle(hFile);
2698 return result;
2699#else /* Py_WIN_WIDE_FILENAMES */
2700
Neal Norwitz2adf2102004-06-09 01:46:02 +00002701 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002702 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002703 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002704 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002705
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002706#if defined(HAVE_UTIMES)
2707 struct timeval buf[2];
2708#define ATIME buf[0].tv_sec
2709#define MTIME buf[1].tv_sec
2710#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002711/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002712 struct utimbuf buf;
2713#define ATIME buf.actime
2714#define MTIME buf.modtime
2715#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002716#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002717 time_t buf[2];
2718#define ATIME buf[0]
2719#define MTIME buf[1]
2720#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002721#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002722
Mark Hammond817c9292003-12-03 01:22:38 +00002723
Thomas Wouters477c8d52006-05-27 19:21:47 +00002724 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002725 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002726 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002727 if (arg == Py_None) {
2728 /* optional time values not given */
2729 Py_BEGIN_ALLOW_THREADS
2730 res = utime(path, NULL);
2731 Py_END_ALLOW_THREADS
2732 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002733 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002734 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002735 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002736 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002737 return NULL;
2738 }
2739 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002740 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002741 &atime, &ausec) == -1) {
2742 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002743 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002744 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002745 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002746 &mtime, &musec) == -1) {
2747 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002748 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002749 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002750 ATIME = atime;
2751 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002752#ifdef HAVE_UTIMES
2753 buf[0].tv_usec = ausec;
2754 buf[1].tv_usec = musec;
2755 Py_BEGIN_ALLOW_THREADS
2756 res = utimes(path, buf);
2757 Py_END_ALLOW_THREADS
2758#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002759 Py_BEGIN_ALLOW_THREADS
2760 res = utime(path, UTIME_ARG);
2761 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002762#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002763 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002764 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002765 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002766 }
Neal Norwitz96652712004-06-06 20:40:27 +00002767 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002768 Py_INCREF(Py_None);
2769 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002770#undef UTIME_ARG
2771#undef ATIME
2772#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002773#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002774}
2775
Guido van Rossum85e3b011991-06-03 12:42:10 +00002776
Guido van Rossum3b066191991-06-04 19:40:25 +00002777/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002778
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002779PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002780"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002781Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002782
Barry Warsaw53699e91996-12-10 23:23:01 +00002783static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002784posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002785{
2786 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002787 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002788 return NULL;
2789 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002790 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002791}
2792
Martin v. Löwis114619e2002-10-07 06:44:21 +00002793#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2794static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002795free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002796{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002797 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002798 for (i = 0; i < count; i++)
2799 PyMem_Free(array[i]);
2800 PyMem_DEL(array);
2801}
2802#endif
2803
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002804
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002805#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002806PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002807"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002808Execute an executable path with arguments, replacing current process.\n\
2809\n\
2810 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002811 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002812
Barry Warsaw53699e91996-12-10 23:23:01 +00002813static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002814posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002815{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002816 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002817 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002818 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002819 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002820 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002821
Guido van Rossum89b33251993-10-22 14:26:06 +00002822 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002823 argv is a list or tuple of strings. */
2824
Martin v. Löwis114619e2002-10-07 06:44:21 +00002825 if (!PyArg_ParseTuple(args, "etO:execv",
2826 Py_FileSystemDefaultEncoding,
2827 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002828 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002829 if (PyList_Check(argv)) {
2830 argc = PyList_Size(argv);
2831 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002832 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002833 else if (PyTuple_Check(argv)) {
2834 argc = PyTuple_Size(argv);
2835 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002836 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002837 else {
Fred Drake661ea262000-10-24 19:57:45 +00002838 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002839 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002840 return NULL;
2841 }
2842
Barry Warsaw53699e91996-12-10 23:23:01 +00002843 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002844 if (argvlist == NULL) {
2845 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002846 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002847 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002848 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002849 if (!PyArg_Parse((*getitem)(argv, i), "et",
2850 Py_FileSystemDefaultEncoding,
2851 &argvlist[i])) {
2852 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002853 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002854 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002855 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002856 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002857
Guido van Rossum85e3b011991-06-03 12:42:10 +00002858 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002859 }
2860 argvlist[argc] = NULL;
2861
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002862 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002863
Guido van Rossum85e3b011991-06-03 12:42:10 +00002864 /* If we get here it's definitely an error */
2865
Martin v. Löwis114619e2002-10-07 06:44:21 +00002866 free_string_array(argvlist, argc);
2867 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002868 return posix_error();
2869}
2870
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002871
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002872PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002873"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002874Execute a path with arguments and environment, replacing current process.\n\
2875\n\
2876 path: path of executable file\n\
2877 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002878 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002879
Barry Warsaw53699e91996-12-10 23:23:01 +00002880static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002881posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002882{
2883 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002884 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002885 char **argvlist;
2886 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002887 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002888 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002889 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002890 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002891
2892 /* execve has three arguments: (path, argv, env), where
2893 argv is a list or tuple of strings and env is a dictionary
2894 like posix.environ. */
2895
Martin v. Löwis114619e2002-10-07 06:44:21 +00002896 if (!PyArg_ParseTuple(args, "etOO:execve",
2897 Py_FileSystemDefaultEncoding,
2898 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002899 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002900 if (PyList_Check(argv)) {
2901 argc = PyList_Size(argv);
2902 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002903 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002904 else if (PyTuple_Check(argv)) {
2905 argc = PyTuple_Size(argv);
2906 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002907 }
2908 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002909 PyErr_SetString(PyExc_TypeError,
2910 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002911 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002912 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002913 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002914 PyErr_SetString(PyExc_TypeError,
2915 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002916 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002917 }
2918
Barry Warsaw53699e91996-12-10 23:23:01 +00002919 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002920 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002921 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002922 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002923 }
2924 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002925 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002926 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002927 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002928 &argvlist[i]))
2929 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002930 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002931 goto fail_1;
2932 }
2933 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002934 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002935 argvlist[argc] = NULL;
2936
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002937 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002938 if (i < 0)
2939 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002940 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002941 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002942 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002943 goto fail_1;
2944 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002945 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002946 keys = PyMapping_Keys(env);
2947 vals = PyMapping_Values(env);
2948 if (!keys || !vals)
2949 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002950 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2951 PyErr_SetString(PyExc_TypeError,
2952 "execve(): env.keys() or env.values() is not a list");
2953 goto fail_2;
2954 }
Tim Peters5aa91602002-01-30 05:46:57 +00002955
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002956 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002957 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002958 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002959
2960 key = PyList_GetItem(keys, pos);
2961 val = PyList_GetItem(vals, pos);
2962 if (!key || !val)
2963 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002964
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002965 if (!PyArg_Parse(
2966 key,
2967 "s;execve() arg 3 contains a non-string key",
2968 &k) ||
2969 !PyArg_Parse(
2970 val,
2971 "s;execve() arg 3 contains a non-string value",
2972 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002973 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002974 goto fail_2;
2975 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002976
2977#if defined(PYOS_OS2)
2978 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2979 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2980#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002981 len = PyString_Size(key) + PyString_Size(val) + 2;
2982 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002983 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002984 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002985 goto fail_2;
2986 }
Tim Petersc8996f52001-12-03 20:41:00 +00002987 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002988 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002989#if defined(PYOS_OS2)
2990 }
2991#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002992 }
2993 envlist[envc] = 0;
2994
2995 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002996
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002997 /* If we get here it's definitely an error */
2998
2999 (void) posix_error();
3000
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003001 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003002 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003003 PyMem_DEL(envlist[envc]);
3004 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003005 fail_1:
3006 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003007 Py_XDECREF(vals);
3008 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003009 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003010 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003011 return NULL;
3012}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003013#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003014
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003015
Guido van Rossuma1065681999-01-25 23:20:23 +00003016#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003017PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003018"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003019Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003020\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003021 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003022 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003023 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003024
3025static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003026posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003027{
3028 char *path;
3029 PyObject *argv;
3030 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003031 int mode, i;
3032 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003033 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003034 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003035
3036 /* spawnv has three arguments: (mode, path, argv), where
3037 argv is a list or tuple of strings. */
3038
Martin v. Löwis114619e2002-10-07 06:44:21 +00003039 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3040 Py_FileSystemDefaultEncoding,
3041 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003042 return NULL;
3043 if (PyList_Check(argv)) {
3044 argc = PyList_Size(argv);
3045 getitem = PyList_GetItem;
3046 }
3047 else if (PyTuple_Check(argv)) {
3048 argc = PyTuple_Size(argv);
3049 getitem = PyTuple_GetItem;
3050 }
3051 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003052 PyErr_SetString(PyExc_TypeError,
3053 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003054 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003055 return NULL;
3056 }
3057
3058 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003059 if (argvlist == NULL) {
3060 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003061 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003062 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003063 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003064 if (!PyArg_Parse((*getitem)(argv, i), "et",
3065 Py_FileSystemDefaultEncoding,
3066 &argvlist[i])) {
3067 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003068 PyErr_SetString(
3069 PyExc_TypeError,
3070 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003071 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003072 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003073 }
3074 }
3075 argvlist[argc] = NULL;
3076
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003077#if defined(PYOS_OS2) && defined(PYCC_GCC)
3078 Py_BEGIN_ALLOW_THREADS
3079 spawnval = spawnv(mode, path, argvlist);
3080 Py_END_ALLOW_THREADS
3081#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003082 if (mode == _OLD_P_OVERLAY)
3083 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003084
Tim Peters25059d32001-12-07 20:35:43 +00003085 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003086 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003087 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003088#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003089
Martin v. Löwis114619e2002-10-07 06:44:21 +00003090 free_string_array(argvlist, argc);
3091 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003092
Fred Drake699f3522000-06-29 21:12:41 +00003093 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003094 return posix_error();
3095 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003096#if SIZEOF_LONG == SIZEOF_VOID_P
3097 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003098#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003099 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003100#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003101}
3102
3103
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003104PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003105"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003106Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003107\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003108 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003109 path: path of executable file\n\
3110 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003111 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003112
3113static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003114posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003115{
3116 char *path;
3117 PyObject *argv, *env;
3118 char **argvlist;
3119 char **envlist;
3120 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003121 int mode, pos, envc;
3122 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003123 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003124 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003125 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003126
3127 /* spawnve has four arguments: (mode, path, argv, env), where
3128 argv is a list or tuple of strings and env is a dictionary
3129 like posix.environ. */
3130
Martin v. Löwis114619e2002-10-07 06:44:21 +00003131 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3132 Py_FileSystemDefaultEncoding,
3133 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003134 return NULL;
3135 if (PyList_Check(argv)) {
3136 argc = PyList_Size(argv);
3137 getitem = PyList_GetItem;
3138 }
3139 else if (PyTuple_Check(argv)) {
3140 argc = PyTuple_Size(argv);
3141 getitem = PyTuple_GetItem;
3142 }
3143 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003144 PyErr_SetString(PyExc_TypeError,
3145 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003146 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003147 }
3148 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003149 PyErr_SetString(PyExc_TypeError,
3150 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003151 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003152 }
3153
3154 argvlist = PyMem_NEW(char *, argc+1);
3155 if (argvlist == NULL) {
3156 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003157 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003158 }
3159 for (i = 0; i < argc; i++) {
3160 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003161 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003162 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003163 &argvlist[i]))
3164 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003165 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003166 goto fail_1;
3167 }
3168 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003169 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003170 argvlist[argc] = NULL;
3171
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003172 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003173 if (i < 0)
3174 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003175 envlist = PyMem_NEW(char *, i + 1);
3176 if (envlist == NULL) {
3177 PyErr_NoMemory();
3178 goto fail_1;
3179 }
3180 envc = 0;
3181 keys = PyMapping_Keys(env);
3182 vals = PyMapping_Values(env);
3183 if (!keys || !vals)
3184 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003185 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3186 PyErr_SetString(PyExc_TypeError,
3187 "spawnve(): env.keys() or env.values() is not a list");
3188 goto fail_2;
3189 }
Tim Peters5aa91602002-01-30 05:46:57 +00003190
Guido van Rossuma1065681999-01-25 23:20:23 +00003191 for (pos = 0; pos < i; pos++) {
3192 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003193 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003194
3195 key = PyList_GetItem(keys, pos);
3196 val = PyList_GetItem(vals, pos);
3197 if (!key || !val)
3198 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003199
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003200 if (!PyArg_Parse(
3201 key,
3202 "s;spawnve() arg 3 contains a non-string key",
3203 &k) ||
3204 !PyArg_Parse(
3205 val,
3206 "s;spawnve() arg 3 contains a non-string value",
3207 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003208 {
3209 goto fail_2;
3210 }
Tim Petersc8996f52001-12-03 20:41:00 +00003211 len = PyString_Size(key) + PyString_Size(val) + 2;
3212 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003213 if (p == NULL) {
3214 PyErr_NoMemory();
3215 goto fail_2;
3216 }
Tim Petersc8996f52001-12-03 20:41:00 +00003217 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003218 envlist[envc++] = p;
3219 }
3220 envlist[envc] = 0;
3221
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003222#if defined(PYOS_OS2) && defined(PYCC_GCC)
3223 Py_BEGIN_ALLOW_THREADS
3224 spawnval = spawnve(mode, path, argvlist, envlist);
3225 Py_END_ALLOW_THREADS
3226#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003227 if (mode == _OLD_P_OVERLAY)
3228 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003229
3230 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003231 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003232 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003233#endif
Tim Peters25059d32001-12-07 20:35:43 +00003234
Fred Drake699f3522000-06-29 21:12:41 +00003235 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003236 (void) posix_error();
3237 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003238#if SIZEOF_LONG == SIZEOF_VOID_P
3239 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003240#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003241 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003242#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003243
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003244 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003245 while (--envc >= 0)
3246 PyMem_DEL(envlist[envc]);
3247 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003248 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003249 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003250 Py_XDECREF(vals);
3251 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003252 fail_0:
3253 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003254 return res;
3255}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003256
3257/* OS/2 supports spawnvp & spawnvpe natively */
3258#if defined(PYOS_OS2)
3259PyDoc_STRVAR(posix_spawnvp__doc__,
3260"spawnvp(mode, file, args)\n\n\
3261Execute the program 'file' in a new process, using the environment\n\
3262search path to find the file.\n\
3263\n\
3264 mode: mode of process creation\n\
3265 file: executable file name\n\
3266 args: tuple or list of strings");
3267
3268static PyObject *
3269posix_spawnvp(PyObject *self, PyObject *args)
3270{
3271 char *path;
3272 PyObject *argv;
3273 char **argvlist;
3274 int mode, i, argc;
3275 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003276 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003277
3278 /* spawnvp has three arguments: (mode, path, argv), where
3279 argv is a list or tuple of strings. */
3280
3281 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3282 Py_FileSystemDefaultEncoding,
3283 &path, &argv))
3284 return NULL;
3285 if (PyList_Check(argv)) {
3286 argc = PyList_Size(argv);
3287 getitem = PyList_GetItem;
3288 }
3289 else if (PyTuple_Check(argv)) {
3290 argc = PyTuple_Size(argv);
3291 getitem = PyTuple_GetItem;
3292 }
3293 else {
3294 PyErr_SetString(PyExc_TypeError,
3295 "spawnvp() arg 2 must be a tuple or list");
3296 PyMem_Free(path);
3297 return NULL;
3298 }
3299
3300 argvlist = PyMem_NEW(char *, argc+1);
3301 if (argvlist == NULL) {
3302 PyMem_Free(path);
3303 return PyErr_NoMemory();
3304 }
3305 for (i = 0; i < argc; i++) {
3306 if (!PyArg_Parse((*getitem)(argv, i), "et",
3307 Py_FileSystemDefaultEncoding,
3308 &argvlist[i])) {
3309 free_string_array(argvlist, i);
3310 PyErr_SetString(
3311 PyExc_TypeError,
3312 "spawnvp() arg 2 must contain only strings");
3313 PyMem_Free(path);
3314 return NULL;
3315 }
3316 }
3317 argvlist[argc] = NULL;
3318
3319 Py_BEGIN_ALLOW_THREADS
3320#if defined(PYCC_GCC)
3321 spawnval = spawnvp(mode, path, argvlist);
3322#else
3323 spawnval = _spawnvp(mode, path, argvlist);
3324#endif
3325 Py_END_ALLOW_THREADS
3326
3327 free_string_array(argvlist, argc);
3328 PyMem_Free(path);
3329
3330 if (spawnval == -1)
3331 return posix_error();
3332 else
3333 return Py_BuildValue("l", (long) spawnval);
3334}
3335
3336
3337PyDoc_STRVAR(posix_spawnvpe__doc__,
3338"spawnvpe(mode, file, args, env)\n\n\
3339Execute the program 'file' in a new process, using the environment\n\
3340search path to find the file.\n\
3341\n\
3342 mode: mode of process creation\n\
3343 file: executable file name\n\
3344 args: tuple or list of arguments\n\
3345 env: dictionary of strings mapping to strings");
3346
3347static PyObject *
3348posix_spawnvpe(PyObject *self, PyObject *args)
3349{
3350 char *path;
3351 PyObject *argv, *env;
3352 char **argvlist;
3353 char **envlist;
3354 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3355 int mode, i, pos, argc, envc;
3356 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003357 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003358 int lastarg = 0;
3359
3360 /* spawnvpe has four arguments: (mode, path, argv, env), where
3361 argv is a list or tuple of strings and env is a dictionary
3362 like posix.environ. */
3363
3364 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3365 Py_FileSystemDefaultEncoding,
3366 &path, &argv, &env))
3367 return NULL;
3368 if (PyList_Check(argv)) {
3369 argc = PyList_Size(argv);
3370 getitem = PyList_GetItem;
3371 }
3372 else if (PyTuple_Check(argv)) {
3373 argc = PyTuple_Size(argv);
3374 getitem = PyTuple_GetItem;
3375 }
3376 else {
3377 PyErr_SetString(PyExc_TypeError,
3378 "spawnvpe() arg 2 must be a tuple or list");
3379 goto fail_0;
3380 }
3381 if (!PyMapping_Check(env)) {
3382 PyErr_SetString(PyExc_TypeError,
3383 "spawnvpe() arg 3 must be a mapping object");
3384 goto fail_0;
3385 }
3386
3387 argvlist = PyMem_NEW(char *, argc+1);
3388 if (argvlist == NULL) {
3389 PyErr_NoMemory();
3390 goto fail_0;
3391 }
3392 for (i = 0; i < argc; i++) {
3393 if (!PyArg_Parse((*getitem)(argv, i),
3394 "et;spawnvpe() arg 2 must contain only strings",
3395 Py_FileSystemDefaultEncoding,
3396 &argvlist[i]))
3397 {
3398 lastarg = i;
3399 goto fail_1;
3400 }
3401 }
3402 lastarg = argc;
3403 argvlist[argc] = NULL;
3404
3405 i = PyMapping_Size(env);
3406 if (i < 0)
3407 goto fail_1;
3408 envlist = PyMem_NEW(char *, i + 1);
3409 if (envlist == NULL) {
3410 PyErr_NoMemory();
3411 goto fail_1;
3412 }
3413 envc = 0;
3414 keys = PyMapping_Keys(env);
3415 vals = PyMapping_Values(env);
3416 if (!keys || !vals)
3417 goto fail_2;
3418 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3419 PyErr_SetString(PyExc_TypeError,
3420 "spawnvpe(): env.keys() or env.values() is not a list");
3421 goto fail_2;
3422 }
3423
3424 for (pos = 0; pos < i; pos++) {
3425 char *p, *k, *v;
3426 size_t len;
3427
3428 key = PyList_GetItem(keys, pos);
3429 val = PyList_GetItem(vals, pos);
3430 if (!key || !val)
3431 goto fail_2;
3432
3433 if (!PyArg_Parse(
3434 key,
3435 "s;spawnvpe() arg 3 contains a non-string key",
3436 &k) ||
3437 !PyArg_Parse(
3438 val,
3439 "s;spawnvpe() arg 3 contains a non-string value",
3440 &v))
3441 {
3442 goto fail_2;
3443 }
3444 len = PyString_Size(key) + PyString_Size(val) + 2;
3445 p = PyMem_NEW(char, len);
3446 if (p == NULL) {
3447 PyErr_NoMemory();
3448 goto fail_2;
3449 }
3450 PyOS_snprintf(p, len, "%s=%s", k, v);
3451 envlist[envc++] = p;
3452 }
3453 envlist[envc] = 0;
3454
3455 Py_BEGIN_ALLOW_THREADS
3456#if defined(PYCC_GCC)
3457 spawnval = spawnve(mode, path, argvlist, envlist);
3458#else
3459 spawnval = _spawnve(mode, path, argvlist, envlist);
3460#endif
3461 Py_END_ALLOW_THREADS
3462
3463 if (spawnval == -1)
3464 (void) posix_error();
3465 else
3466 res = Py_BuildValue("l", (long) spawnval);
3467
3468 fail_2:
3469 while (--envc >= 0)
3470 PyMem_DEL(envlist[envc]);
3471 PyMem_DEL(envlist);
3472 fail_1:
3473 free_string_array(argvlist, lastarg);
3474 Py_XDECREF(vals);
3475 Py_XDECREF(keys);
3476 fail_0:
3477 PyMem_Free(path);
3478 return res;
3479}
3480#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003481#endif /* HAVE_SPAWNV */
3482
3483
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003484#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003485PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003486"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003487Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3488\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003489Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003490
3491static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003492posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003493{
Neal Norwitze241ce82003-02-17 18:17:05 +00003494 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003495 if (pid == -1)
3496 return posix_error();
3497 PyOS_AfterFork();
3498 return PyInt_FromLong((long)pid);
3499}
3500#endif
3501
3502
Guido van Rossumad0ee831995-03-01 10:34:45 +00003503#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003504PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003505"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003506Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003507Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003508
Barry Warsaw53699e91996-12-10 23:23:01 +00003509static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003510posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003511{
Neal Norwitze241ce82003-02-17 18:17:05 +00003512 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003513 if (pid == -1)
3514 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003515 if (pid == 0)
3516 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003517 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003518}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003519#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003520
Neal Norwitzb59798b2003-03-21 01:43:31 +00003521/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003522/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3523#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003524#define DEV_PTY_FILE "/dev/ptc"
3525#define HAVE_DEV_PTMX
3526#else
3527#define DEV_PTY_FILE "/dev/ptmx"
3528#endif
3529
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003530#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003531#ifdef HAVE_PTY_H
3532#include <pty.h>
3533#else
3534#ifdef HAVE_LIBUTIL_H
3535#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003536#endif /* HAVE_LIBUTIL_H */
3537#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003538#ifdef HAVE_STROPTS_H
3539#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003540#endif
3541#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003542
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003543#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003544PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003545"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003546Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003547
3548static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003549posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003550{
3551 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003552#ifndef HAVE_OPENPTY
3553 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003554#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003555#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003556 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003557#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003558 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003559#endif
3560#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003561
Thomas Wouters70c21a12000-07-14 14:28:33 +00003562#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003563 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3564 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003565#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003566 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3567 if (slave_name == NULL)
3568 return posix_error();
3569
3570 slave_fd = open(slave_name, O_RDWR);
3571 if (slave_fd < 0)
3572 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003573#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003574 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003575 if (master_fd < 0)
3576 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003577 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003578 /* change permission of slave */
3579 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003580 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003581 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003582 }
3583 /* unlock slave */
3584 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003585 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003586 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003587 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003588 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003589 slave_name = ptsname(master_fd); /* get name of slave */
3590 if (slave_name == NULL)
3591 return posix_error();
3592 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3593 if (slave_fd < 0)
3594 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003595#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003596 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3597 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003598#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003599 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003600#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003601#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003602#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003603
Fred Drake8cef4cf2000-06-28 16:40:38 +00003604 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003605
Fred Drake8cef4cf2000-06-28 16:40:38 +00003606}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003607#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003608
3609#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003610PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003611"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003612Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3613Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003614To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003615
3616static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003617posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003618{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003619 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003620
Fred Drake8cef4cf2000-06-28 16:40:38 +00003621 pid = forkpty(&master_fd, NULL, NULL, NULL);
3622 if (pid == -1)
3623 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003624 if (pid == 0)
3625 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003626 return Py_BuildValue("(ii)", pid, master_fd);
3627}
3628#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003629
Guido van Rossumad0ee831995-03-01 10:34:45 +00003630#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003631PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003632"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003633Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003634
Barry Warsaw53699e91996-12-10 23:23:01 +00003635static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003636posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003637{
Barry Warsaw53699e91996-12-10 23:23:01 +00003638 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003639}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003640#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003641
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003642
Guido van Rossumad0ee831995-03-01 10:34:45 +00003643#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003644PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003645"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003646Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003647
Barry Warsaw53699e91996-12-10 23:23:01 +00003648static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003649posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003650{
Barry Warsaw53699e91996-12-10 23:23:01 +00003651 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003652}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003653#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003654
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003655
Guido van Rossumad0ee831995-03-01 10:34:45 +00003656#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003657PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003658"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003659Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003660
Barry Warsaw53699e91996-12-10 23:23:01 +00003661static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003662posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003663{
Barry Warsaw53699e91996-12-10 23:23:01 +00003664 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003665}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003666#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003667
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003668
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003669PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003670"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003671Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003672
Barry Warsaw53699e91996-12-10 23:23:01 +00003673static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003674posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003675{
Barry Warsaw53699e91996-12-10 23:23:01 +00003676 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003677}
3678
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003679
Fred Drakec9680921999-12-13 16:37:25 +00003680#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003681PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003682"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003683Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003684
3685static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003686posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003687{
3688 PyObject *result = NULL;
3689
Fred Drakec9680921999-12-13 16:37:25 +00003690#ifdef NGROUPS_MAX
3691#define MAX_GROUPS NGROUPS_MAX
3692#else
3693 /* defined to be 16 on Solaris7, so this should be a small number */
3694#define MAX_GROUPS 64
3695#endif
3696 gid_t grouplist[MAX_GROUPS];
3697 int n;
3698
3699 n = getgroups(MAX_GROUPS, grouplist);
3700 if (n < 0)
3701 posix_error();
3702 else {
3703 result = PyList_New(n);
3704 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003705 int i;
3706 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003707 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003708 if (o == NULL) {
3709 Py_DECREF(result);
3710 result = NULL;
3711 break;
3712 }
3713 PyList_SET_ITEM(result, i, o);
3714 }
3715 }
3716 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003717
Fred Drakec9680921999-12-13 16:37:25 +00003718 return result;
3719}
3720#endif
3721
Martin v. Löwis606edc12002-06-13 21:09:11 +00003722#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003723PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003724"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003725Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003726
3727static PyObject *
3728posix_getpgid(PyObject *self, PyObject *args)
3729{
3730 int pid, pgid;
3731 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3732 return NULL;
3733 pgid = getpgid(pid);
3734 if (pgid < 0)
3735 return posix_error();
3736 return PyInt_FromLong((long)pgid);
3737}
3738#endif /* HAVE_GETPGID */
3739
3740
Guido van Rossumb6775db1994-08-01 11:34:53 +00003741#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003742PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003743"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003744Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003745
Barry Warsaw53699e91996-12-10 23:23:01 +00003746static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003747posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003748{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003749#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003750 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003751#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003752 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003753#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003754}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003755#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003756
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003757
Guido van Rossumb6775db1994-08-01 11:34:53 +00003758#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003759PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003760"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003761Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003762
Barry Warsaw53699e91996-12-10 23:23:01 +00003763static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003764posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003765{
Guido van Rossum64933891994-10-20 21:56:42 +00003766#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003767 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003768#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003769 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003770#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003771 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003772 Py_INCREF(Py_None);
3773 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003774}
3775
Guido van Rossumb6775db1994-08-01 11:34:53 +00003776#endif /* HAVE_SETPGRP */
3777
Guido van Rossumad0ee831995-03-01 10:34:45 +00003778#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003779PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003780"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003781Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003782
Barry Warsaw53699e91996-12-10 23:23:01 +00003783static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003784posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003785{
Barry Warsaw53699e91996-12-10 23:23:01 +00003786 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003787}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003788#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003789
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003790
Fred Drake12c6e2d1999-12-14 21:25:03 +00003791#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003792PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003793"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003794Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003795
3796static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003797posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003798{
Neal Norwitze241ce82003-02-17 18:17:05 +00003799 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003800 char *name;
3801 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003802
Fred Drakea30680b2000-12-06 21:24:28 +00003803 errno = 0;
3804 name = getlogin();
3805 if (name == NULL) {
3806 if (errno)
3807 posix_error();
3808 else
3809 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003810 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003811 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003812 else
3813 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003814 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003815
Fred Drake12c6e2d1999-12-14 21:25:03 +00003816 return result;
3817}
3818#endif
3819
Guido van Rossumad0ee831995-03-01 10:34:45 +00003820#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003821PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003822"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003823Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003824
Barry Warsaw53699e91996-12-10 23:23:01 +00003825static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003826posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003827{
Barry Warsaw53699e91996-12-10 23:23:01 +00003828 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003829}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003830#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003831
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003832
Guido van Rossumad0ee831995-03-01 10:34:45 +00003833#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003834PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003835"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003836Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003837
Barry Warsaw53699e91996-12-10 23:23:01 +00003838static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003839posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003840{
3841 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003842 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003843 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003844#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003845 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3846 APIRET rc;
3847 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003848 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003849
3850 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3851 APIRET rc;
3852 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003853 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003854
3855 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003856 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003857#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003858 if (kill(pid, sig) == -1)
3859 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003860#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003861 Py_INCREF(Py_None);
3862 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003863}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003864#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003865
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003866#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003867PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003868"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003869Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003870
3871static PyObject *
3872posix_killpg(PyObject *self, PyObject *args)
3873{
3874 int pgid, sig;
3875 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3876 return NULL;
3877 if (killpg(pgid, sig) == -1)
3878 return posix_error();
3879 Py_INCREF(Py_None);
3880 return Py_None;
3881}
3882#endif
3883
Guido van Rossumc0125471996-06-28 18:55:32 +00003884#ifdef HAVE_PLOCK
3885
3886#ifdef HAVE_SYS_LOCK_H
3887#include <sys/lock.h>
3888#endif
3889
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003890PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003891"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003892Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003893
Barry Warsaw53699e91996-12-10 23:23:01 +00003894static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003895posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003896{
3897 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003898 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003899 return NULL;
3900 if (plock(op) == -1)
3901 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003902 Py_INCREF(Py_None);
3903 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003904}
3905#endif
3906
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003907
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003908#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003909PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003910"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003911Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003912
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003913#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003914#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003915static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003916async_system(const char *command)
3917{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003918 char errormsg[256], args[1024];
3919 RESULTCODES rcodes;
3920 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003921
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003922 char *shell = getenv("COMSPEC");
3923 if (!shell)
3924 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003925
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003926 /* avoid overflowing the argument buffer */
3927 if (strlen(shell) + 3 + strlen(command) >= 1024)
3928 return ERROR_NOT_ENOUGH_MEMORY
3929
3930 args[0] = '\0';
3931 strcat(args, shell);
3932 strcat(args, "/c ");
3933 strcat(args, command);
3934
3935 /* execute asynchronously, inheriting the environment */
3936 rc = DosExecPgm(errormsg,
3937 sizeof(errormsg),
3938 EXEC_ASYNC,
3939 args,
3940 NULL,
3941 &rcodes,
3942 shell);
3943 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003944}
3945
Guido van Rossumd48f2521997-12-05 22:19:34 +00003946static FILE *
3947popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003948{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003949 int oldfd, tgtfd;
3950 HFILE pipeh[2];
3951 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003952
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003953 /* mode determines which of stdin or stdout is reconnected to
3954 * the pipe to the child
3955 */
3956 if (strchr(mode, 'r') != NULL) {
3957 tgt_fd = 1; /* stdout */
3958 } else if (strchr(mode, 'w')) {
3959 tgt_fd = 0; /* stdin */
3960 } else {
3961 *err = ERROR_INVALID_ACCESS;
3962 return NULL;
3963 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003964
Andrew MacIntyrea3be2582004-12-18 09:51:05 +00003965 /* setup the pipe */
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003966 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
3967 *err = rc;
3968 return NULL;
3969 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003970
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003971 /* prevent other threads accessing stdio */
3972 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003973
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003974 /* reconnect stdio and execute child */
3975 oldfd = dup(tgtfd);
3976 close(tgtfd);
3977 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
3978 DosClose(pipeh[tgtfd]);
3979 rc = async_system(command);
3980 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003981
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003982 /* restore stdio */
3983 dup2(oldfd, tgtfd);
3984 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003985
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003986 /* allow other threads access to stdio */
3987 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003988
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003989 /* if execution of child was successful return file stream */
3990 if (rc == NO_ERROR)
3991 return fdopen(pipeh[1 - tgtfd], mode);
3992 else {
3993 DosClose(pipeh[1 - tgtfd]);
3994 *err = rc;
3995 return NULL;
3996 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003997}
3998
3999static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004000posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004001{
4002 char *name;
4003 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00004004 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004005 FILE *fp;
4006 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004007 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004008 return NULL;
4009 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00004010 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004011 Py_END_ALLOW_THREADS
4012 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004013 return os2_error(err);
4014
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004015 f = PyFile_FromFile(fp, name, mode, fclose);
4016 if (f != NULL)
4017 PyFile_SetBufSize(f, bufsize);
4018 return f;
4019}
4020
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004021#elif defined(PYCC_GCC)
4022
4023/* standard posix version of popen() support */
4024static PyObject *
4025posix_popen(PyObject *self, PyObject *args)
4026{
4027 char *name;
4028 char *mode = "r";
4029 int bufsize = -1;
4030 FILE *fp;
4031 PyObject *f;
4032 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4033 return NULL;
4034 Py_BEGIN_ALLOW_THREADS
4035 fp = popen(name, mode);
4036 Py_END_ALLOW_THREADS
4037 if (fp == NULL)
4038 return posix_error();
4039 f = PyFile_FromFile(fp, name, mode, pclose);
4040 if (f != NULL)
4041 PyFile_SetBufSize(f, bufsize);
4042 return f;
4043}
4044
4045/* fork() under OS/2 has lots'o'warts
4046 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4047 * most of this code is a ripoff of the win32 code, but using the
4048 * capabilities of EMX's C library routines
4049 */
4050
4051/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4052#define POPEN_1 1
4053#define POPEN_2 2
4054#define POPEN_3 3
4055#define POPEN_4 4
4056
4057static PyObject *_PyPopen(char *, int, int, int);
4058static int _PyPclose(FILE *file);
4059
4060/*
4061 * Internal dictionary mapping popen* file pointers to process handles,
4062 * for use when retrieving the process exit code. See _PyPclose() below
4063 * for more information on this dictionary's use.
4064 */
4065static PyObject *_PyPopenProcs = NULL;
4066
4067/* os2emx version of popen2()
4068 *
4069 * The result of this function is a pipe (file) connected to the
4070 * process's stdin, and a pipe connected to the process's stdout.
4071 */
4072
4073static PyObject *
4074os2emx_popen2(PyObject *self, PyObject *args)
4075{
4076 PyObject *f;
4077 int tm=0;
4078
4079 char *cmdstring;
4080 char *mode = "t";
4081 int bufsize = -1;
4082 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4083 return NULL;
4084
4085 if (*mode == 't')
4086 tm = O_TEXT;
4087 else if (*mode != 'b') {
4088 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4089 return NULL;
4090 } else
4091 tm = O_BINARY;
4092
4093 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
4094
4095 return f;
4096}
4097
4098/*
4099 * Variation on os2emx.popen2
4100 *
4101 * The result of this function is 3 pipes - the process's stdin,
4102 * stdout and stderr
4103 */
4104
4105static PyObject *
4106os2emx_popen3(PyObject *self, PyObject *args)
4107{
4108 PyObject *f;
4109 int tm = 0;
4110
4111 char *cmdstring;
4112 char *mode = "t";
4113 int bufsize = -1;
4114 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4115 return NULL;
4116
4117 if (*mode == 't')
4118 tm = O_TEXT;
4119 else if (*mode != 'b') {
4120 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4121 return NULL;
4122 } else
4123 tm = O_BINARY;
4124
4125 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
4126
4127 return f;
4128}
4129
4130/*
4131 * Variation on os2emx.popen2
4132 *
Tim Peters11b23062003-04-23 02:39:17 +00004133 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004134 * and stdout+stderr combined as a single pipe.
4135 */
4136
4137static PyObject *
4138os2emx_popen4(PyObject *self, PyObject *args)
4139{
4140 PyObject *f;
4141 int tm = 0;
4142
4143 char *cmdstring;
4144 char *mode = "t";
4145 int bufsize = -1;
4146 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4147 return NULL;
4148
4149 if (*mode == 't')
4150 tm = O_TEXT;
4151 else if (*mode != 'b') {
4152 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4153 return NULL;
4154 } else
4155 tm = O_BINARY;
4156
4157 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
4158
4159 return f;
4160}
4161
4162/* a couple of structures for convenient handling of multiple
4163 * file handles and pipes
4164 */
4165struct file_ref
4166{
4167 int handle;
4168 int flags;
4169};
4170
4171struct pipe_ref
4172{
4173 int rd;
4174 int wr;
4175};
4176
4177/* The following code is derived from the win32 code */
4178
4179static PyObject *
4180_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4181{
4182 struct file_ref stdio[3];
4183 struct pipe_ref p_fd[3];
4184 FILE *p_s[3];
4185 int file_count, i, pipe_err, pipe_pid;
4186 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4187 PyObject *f, *p_f[3];
4188
4189 /* file modes for subsequent fdopen's on pipe handles */
4190 if (mode == O_TEXT)
4191 {
4192 rd_mode = "rt";
4193 wr_mode = "wt";
4194 }
4195 else
4196 {
4197 rd_mode = "rb";
4198 wr_mode = "wb";
4199 }
4200
4201 /* prepare shell references */
4202 if ((shell = getenv("EMXSHELL")) == NULL)
4203 if ((shell = getenv("COMSPEC")) == NULL)
4204 {
4205 errno = ENOENT;
4206 return posix_error();
4207 }
4208
4209 sh_name = _getname(shell);
4210 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4211 opt = "/c";
4212 else
4213 opt = "-c";
4214
4215 /* save current stdio fds + their flags, and set not inheritable */
4216 i = pipe_err = 0;
4217 while (pipe_err >= 0 && i < 3)
4218 {
4219 pipe_err = stdio[i].handle = dup(i);
4220 stdio[i].flags = fcntl(i, F_GETFD, 0);
4221 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4222 i++;
4223 }
4224 if (pipe_err < 0)
4225 {
4226 /* didn't get them all saved - clean up and bail out */
4227 int saved_err = errno;
4228 while (i-- > 0)
4229 {
4230 close(stdio[i].handle);
4231 }
4232 errno = saved_err;
4233 return posix_error();
4234 }
4235
4236 /* create pipe ends */
4237 file_count = 2;
4238 if (n == POPEN_3)
4239 file_count = 3;
4240 i = pipe_err = 0;
4241 while ((pipe_err == 0) && (i < file_count))
4242 pipe_err = pipe((int *)&p_fd[i++]);
4243 if (pipe_err < 0)
4244 {
4245 /* didn't get them all made - clean up and bail out */
4246 while (i-- > 0)
4247 {
4248 close(p_fd[i].wr);
4249 close(p_fd[i].rd);
4250 }
4251 errno = EPIPE;
4252 return posix_error();
4253 }
4254
4255 /* change the actual standard IO streams over temporarily,
4256 * making the retained pipe ends non-inheritable
4257 */
4258 pipe_err = 0;
4259
4260 /* - stdin */
4261 if (dup2(p_fd[0].rd, 0) == 0)
4262 {
4263 close(p_fd[0].rd);
4264 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4265 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4266 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4267 {
4268 close(p_fd[0].wr);
4269 pipe_err = -1;
4270 }
4271 }
4272 else
4273 {
4274 pipe_err = -1;
4275 }
4276
4277 /* - stdout */
4278 if (pipe_err == 0)
4279 {
4280 if (dup2(p_fd[1].wr, 1) == 1)
4281 {
4282 close(p_fd[1].wr);
4283 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4284 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4285 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4286 {
4287 close(p_fd[1].rd);
4288 pipe_err = -1;
4289 }
4290 }
4291 else
4292 {
4293 pipe_err = -1;
4294 }
4295 }
4296
4297 /* - stderr, as required */
4298 if (pipe_err == 0)
4299 switch (n)
4300 {
4301 case POPEN_3:
4302 {
4303 if (dup2(p_fd[2].wr, 2) == 2)
4304 {
4305 close(p_fd[2].wr);
4306 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4307 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4308 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4309 {
4310 close(p_fd[2].rd);
4311 pipe_err = -1;
4312 }
4313 }
4314 else
4315 {
4316 pipe_err = -1;
4317 }
4318 break;
4319 }
4320
4321 case POPEN_4:
4322 {
4323 if (dup2(1, 2) != 2)
4324 {
4325 pipe_err = -1;
4326 }
4327 break;
4328 }
4329 }
4330
4331 /* spawn the child process */
4332 if (pipe_err == 0)
4333 {
4334 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4335 if (pipe_pid == -1)
4336 {
4337 pipe_err = -1;
4338 }
4339 else
4340 {
4341 /* save the PID into the FILE structure
4342 * NOTE: this implementation doesn't actually
4343 * take advantage of this, but do it for
4344 * completeness - AIM Apr01
4345 */
4346 for (i = 0; i < file_count; i++)
4347 p_s[i]->_pid = pipe_pid;
4348 }
4349 }
4350
4351 /* reset standard IO to normal */
4352 for (i = 0; i < 3; i++)
4353 {
4354 dup2(stdio[i].handle, i);
4355 fcntl(i, F_SETFD, stdio[i].flags);
4356 close(stdio[i].handle);
4357 }
4358
4359 /* if any remnant problems, clean up and bail out */
4360 if (pipe_err < 0)
4361 {
4362 for (i = 0; i < 3; i++)
4363 {
4364 close(p_fd[i].rd);
4365 close(p_fd[i].wr);
4366 }
4367 errno = EPIPE;
4368 return posix_error_with_filename(cmdstring);
4369 }
4370
4371 /* build tuple of file objects to return */
4372 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4373 PyFile_SetBufSize(p_f[0], bufsize);
4374 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4375 PyFile_SetBufSize(p_f[1], bufsize);
4376 if (n == POPEN_3)
4377 {
4378 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4379 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004380 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004381 }
4382 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004383 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004384
4385 /*
4386 * Insert the files we've created into the process dictionary
4387 * all referencing the list with the process handle and the
4388 * initial number of files (see description below in _PyPclose).
4389 * Since if _PyPclose later tried to wait on a process when all
4390 * handles weren't closed, it could create a deadlock with the
4391 * child, we spend some energy here to try to ensure that we
4392 * either insert all file handles into the dictionary or none
4393 * at all. It's a little clumsy with the various popen modes
4394 * and variable number of files involved.
4395 */
4396 if (!_PyPopenProcs)
4397 {
4398 _PyPopenProcs = PyDict_New();
4399 }
4400
4401 if (_PyPopenProcs)
4402 {
4403 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4404 int ins_rc[3];
4405
4406 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4407 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4408
4409 procObj = PyList_New(2);
4410 pidObj = PyInt_FromLong((long) pipe_pid);
4411 intObj = PyInt_FromLong((long) file_count);
4412
4413 if (procObj && pidObj && intObj)
4414 {
4415 PyList_SetItem(procObj, 0, pidObj);
4416 PyList_SetItem(procObj, 1, intObj);
4417
4418 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4419 if (fileObj[0])
4420 {
4421 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4422 fileObj[0],
4423 procObj);
4424 }
4425 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4426 if (fileObj[1])
4427 {
4428 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4429 fileObj[1],
4430 procObj);
4431 }
4432 if (file_count >= 3)
4433 {
4434 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4435 if (fileObj[2])
4436 {
4437 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4438 fileObj[2],
4439 procObj);
4440 }
4441 }
4442
4443 if (ins_rc[0] < 0 || !fileObj[0] ||
4444 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4445 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4446 {
4447 /* Something failed - remove any dictionary
4448 * entries that did make it.
4449 */
4450 if (!ins_rc[0] && fileObj[0])
4451 {
4452 PyDict_DelItem(_PyPopenProcs,
4453 fileObj[0]);
4454 }
4455 if (!ins_rc[1] && fileObj[1])
4456 {
4457 PyDict_DelItem(_PyPopenProcs,
4458 fileObj[1]);
4459 }
4460 if (!ins_rc[2] && fileObj[2])
4461 {
4462 PyDict_DelItem(_PyPopenProcs,
4463 fileObj[2]);
4464 }
4465 }
4466 }
Tim Peters11b23062003-04-23 02:39:17 +00004467
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004468 /*
4469 * Clean up our localized references for the dictionary keys
4470 * and value since PyDict_SetItem will Py_INCREF any copies
4471 * that got placed in the dictionary.
4472 */
4473 Py_XDECREF(procObj);
4474 Py_XDECREF(fileObj[0]);
4475 Py_XDECREF(fileObj[1]);
4476 Py_XDECREF(fileObj[2]);
4477 }
4478
4479 /* Child is launched. */
4480 return f;
4481}
4482
4483/*
4484 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4485 * exit code for the child process and return as a result of the close.
4486 *
4487 * This function uses the _PyPopenProcs dictionary in order to map the
4488 * input file pointer to information about the process that was
4489 * originally created by the popen* call that created the file pointer.
4490 * The dictionary uses the file pointer as a key (with one entry
4491 * inserted for each file returned by the original popen* call) and a
4492 * single list object as the value for all files from a single call.
4493 * The list object contains the Win32 process handle at [0], and a file
4494 * count at [1], which is initialized to the total number of file
4495 * handles using that list.
4496 *
4497 * This function closes whichever handle it is passed, and decrements
4498 * the file count in the dictionary for the process handle pointed to
4499 * by this file. On the last close (when the file count reaches zero),
4500 * this function will wait for the child process and then return its
4501 * exit code as the result of the close() operation. This permits the
4502 * files to be closed in any order - it is always the close() of the
4503 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004504 *
4505 * NOTE: This function is currently called with the GIL released.
4506 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004507 */
4508
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004509static int _PyPclose(FILE *file)
4510{
4511 int result;
4512 int exit_code;
4513 int pipe_pid;
4514 PyObject *procObj, *pidObj, *intObj, *fileObj;
4515 int file_count;
4516#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004517 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004518#endif
4519
4520 /* Close the file handle first, to ensure it can't block the
4521 * child from exiting if it's the last handle.
4522 */
4523 result = fclose(file);
4524
4525#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004526 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004527#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004528 if (_PyPopenProcs)
4529 {
4530 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4531 (procObj = PyDict_GetItem(_PyPopenProcs,
4532 fileObj)) != NULL &&
4533 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4534 (intObj = PyList_GetItem(procObj,1)) != NULL)
4535 {
4536 pipe_pid = (int) PyInt_AsLong(pidObj);
4537 file_count = (int) PyInt_AsLong(intObj);
4538
4539 if (file_count > 1)
4540 {
4541 /* Still other files referencing process */
4542 file_count--;
4543 PyList_SetItem(procObj,1,
4544 PyInt_FromLong((long) file_count));
4545 }
4546 else
4547 {
4548 /* Last file for this process */
4549 if (result != EOF &&
4550 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4551 {
4552 /* extract exit status */
4553 if (WIFEXITED(exit_code))
4554 {
4555 result = WEXITSTATUS(exit_code);
4556 }
4557 else
4558 {
4559 errno = EPIPE;
4560 result = -1;
4561 }
4562 }
4563 else
4564 {
4565 /* Indicate failure - this will cause the file object
4566 * to raise an I/O error and translate the last
4567 * error code from errno. We do have a problem with
4568 * last errors that overlap the normal errno table,
4569 * but that's a consistent problem with the file object.
4570 */
4571 result = -1;
4572 }
4573 }
4574
4575 /* Remove this file pointer from dictionary */
4576 PyDict_DelItem(_PyPopenProcs, fileObj);
4577
4578 if (PyDict_Size(_PyPopenProcs) == 0)
4579 {
4580 Py_DECREF(_PyPopenProcs);
4581 _PyPopenProcs = NULL;
4582 }
4583
4584 } /* if object retrieval ok */
4585
4586 Py_XDECREF(fileObj);
4587 } /* if _PyPopenProcs */
4588
4589#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004590 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004591#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004592 return result;
4593}
4594
4595#endif /* PYCC_??? */
4596
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004597#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004598
4599/*
4600 * Portable 'popen' replacement for Win32.
4601 *
4602 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4603 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004604 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004605 */
4606
4607#include <malloc.h>
4608#include <io.h>
4609#include <fcntl.h>
4610
4611/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4612#define POPEN_1 1
4613#define POPEN_2 2
4614#define POPEN_3 3
4615#define POPEN_4 4
4616
4617static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004618static int _PyPclose(FILE *file);
4619
4620/*
4621 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004622 * for use when retrieving the process exit code. See _PyPclose() below
4623 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004624 */
4625static PyObject *_PyPopenProcs = NULL;
4626
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004627
4628/* popen that works from a GUI.
4629 *
4630 * The result of this function is a pipe (file) connected to the
4631 * processes stdin or stdout, depending on the requested mode.
4632 */
4633
4634static PyObject *
4635posix_popen(PyObject *self, PyObject *args)
4636{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00004637 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004638 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004639
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004640 char *cmdstring;
4641 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004642 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004643 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004644 return NULL;
4645
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004646 if (*mode == 'r')
4647 tm = _O_RDONLY;
4648 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00004649 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004650 return NULL;
4651 } else
4652 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004653
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004654 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004655 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004656 return NULL;
4657 }
4658
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004659 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004660 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004661 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004662 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004663 else
4664 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4665
4666 return f;
4667}
4668
4669/* Variation on win32pipe.popen
4670 *
4671 * The result of this function is a pipe (file) connected to the
4672 * process's stdin, and a pipe connected to the process's stdout.
4673 */
4674
4675static PyObject *
4676win32_popen2(PyObject *self, PyObject *args)
4677{
4678 PyObject *f;
4679 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004680
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004681 char *cmdstring;
4682 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004683 int bufsize = -1;
4684 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004685 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004686
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004687 if (*mode == 't')
4688 tm = _O_TEXT;
4689 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004690 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004691 return NULL;
4692 } else
4693 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004694
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004695 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004696 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004697 return NULL;
4698 }
4699
4700 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004701
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004702 return f;
4703}
4704
4705/*
4706 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004707 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004708 * The result of this function is 3 pipes - the process's stdin,
4709 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004710 */
4711
4712static PyObject *
4713win32_popen3(PyObject *self, PyObject *args)
4714{
4715 PyObject *f;
4716 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004717
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004718 char *cmdstring;
4719 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004720 int bufsize = -1;
4721 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004722 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004723
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004724 if (*mode == 't')
4725 tm = _O_TEXT;
4726 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004727 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004728 return NULL;
4729 } else
4730 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004731
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004732 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004733 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004734 return NULL;
4735 }
4736
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004737 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004738
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004739 return f;
4740}
4741
4742/*
4743 * Variation on win32pipe.popen
4744 *
Tim Peters5aa91602002-01-30 05:46:57 +00004745 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004746 * and stdout+stderr combined as a single pipe.
4747 */
4748
4749static PyObject *
4750win32_popen4(PyObject *self, PyObject *args)
4751{
4752 PyObject *f;
4753 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004754
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004755 char *cmdstring;
4756 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004757 int bufsize = -1;
4758 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004759 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004760
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004761 if (*mode == 't')
4762 tm = _O_TEXT;
4763 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004764 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004765 return NULL;
4766 } else
4767 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004768
4769 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004770 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004771 return NULL;
4772 }
4773
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004774 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004775
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004776 return f;
4777}
4778
Mark Hammond08501372001-01-31 07:30:29 +00004779static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004780_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004781 HANDLE hStdin,
4782 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004783 HANDLE hStderr,
4784 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004785{
4786 PROCESS_INFORMATION piProcInfo;
4787 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004788 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004789 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004790 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004791 int i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004792 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004793
4794 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004795 char *comshell;
4796
Tim Peters92e4dd82002-10-05 01:47:34 +00004797 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004798 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
Martin v. Löwis18e16552006-02-15 17:27:45 +00004799 /* x < i, so x fits into an integer */
4800 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00004801
4802 /* Explicitly check if we are using COMMAND.COM. If we are
4803 * then use the w9xpopen hack.
4804 */
4805 comshell = s1 + x;
4806 while (comshell >= s1 && *comshell != '\\')
4807 --comshell;
4808 ++comshell;
4809
4810 if (GetVersion() < 0x80000000 &&
4811 _stricmp(comshell, "command.com") != 0) {
4812 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004813 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004814 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004815 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004816 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004817 }
4818 else {
4819 /*
Tim Peters402d5982001-08-27 06:37:48 +00004820 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4821 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004822 */
Mark Hammond08501372001-01-31 07:30:29 +00004823 char modulepath[_MAX_PATH];
4824 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004825 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
Thomas Wouters477c8d52006-05-27 19:21:47 +00004826 for (x = i = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004827 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004828 x = i+1;
4829 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004830 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004831 strncat(modulepath,
4832 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004833 (sizeof(modulepath)/sizeof(modulepath[0]))
4834 -strlen(modulepath));
4835 if (stat(modulepath, &statinfo) != 0) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004836 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
Tim Peters5aa91602002-01-30 05:46:57 +00004837 /* Eeek - file-not-found - possibly an embedding
4838 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004839 */
Tim Peters5aa91602002-01-30 05:46:57 +00004840 strncpy(modulepath,
4841 Py_GetExecPrefix(),
Guido van Rossumd8faa362007-04-27 19:54:29 +00004842 mplen);
4843 modulepath[mplen-1] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004844 if (modulepath[strlen(modulepath)-1] != '\\')
4845 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004846 strncat(modulepath,
4847 szConsoleSpawn,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004848 mplen-strlen(modulepath));
Mark Hammond08501372001-01-31 07:30:29 +00004849 /* No where else to look - raise an easily identifiable
4850 error, rather than leaving Windows to report
4851 "file not found" - as the user is probably blissfully
4852 unaware this shim EXE is used, and it will confuse them.
4853 (well, it confused me for a while ;-)
4854 */
4855 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004856 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004857 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004858 "for popen to work with your shell "
4859 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004860 szConsoleSpawn);
4861 return FALSE;
4862 }
4863 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004864 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004865 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004866 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004867
Tim Peters92e4dd82002-10-05 01:47:34 +00004868 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004869 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004870 /* To maintain correct argument passing semantics,
4871 we pass the command-line as it stands, and allow
4872 quoting to be applied. w9xpopen.exe will then
4873 use its argv vector, and re-quote the necessary
4874 args for the ultimate child process.
4875 */
Tim Peters75cdad52001-11-28 22:07:30 +00004876 PyOS_snprintf(
4877 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004878 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004879 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004880 s1,
4881 s3,
4882 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004883 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004884 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004885 dialog:
4886 "Your program accessed mem currently in use at xxx"
4887 and a hopeful warning about the stability of your
4888 system.
4889 Cost is Ctrl+C wont kill children, but anyone
4890 who cares can have a go!
4891 */
4892 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004893 }
4894 }
4895
4896 /* Could be an else here to try cmd.exe / command.com in the path
4897 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004898 else {
Tim Peters402d5982001-08-27 06:37:48 +00004899 PyErr_SetString(PyExc_RuntimeError,
4900 "Cannot locate a COMSPEC environment variable to "
4901 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004902 return FALSE;
4903 }
Tim Peters5aa91602002-01-30 05:46:57 +00004904
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004905 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4906 siStartInfo.cb = sizeof(STARTUPINFO);
4907 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4908 siStartInfo.hStdInput = hStdin;
4909 siStartInfo.hStdOutput = hStdout;
4910 siStartInfo.hStdError = hStderr;
4911 siStartInfo.wShowWindow = SW_HIDE;
4912
4913 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004914 s2,
4915 NULL,
4916 NULL,
4917 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004918 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004919 NULL,
4920 NULL,
4921 &siStartInfo,
4922 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004923 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004924 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004925
Mark Hammondb37a3732000-08-14 04:47:33 +00004926 /* Return process handle */
4927 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004928 return TRUE;
4929 }
Tim Peters402d5982001-08-27 06:37:48 +00004930 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004931 return FALSE;
4932}
4933
4934/* The following code is based off of KB: Q190351 */
4935
4936static PyObject *
4937_PyPopen(char *cmdstring, int mode, int n)
4938{
4939 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4940 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004941 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004942
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004943 SECURITY_ATTRIBUTES saAttr;
4944 BOOL fSuccess;
4945 int fd1, fd2, fd3;
4946 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004947 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004948 PyObject *f;
4949
4950 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4951 saAttr.bInheritHandle = TRUE;
4952 saAttr.lpSecurityDescriptor = NULL;
4953
4954 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4955 return win32_error("CreatePipe", NULL);
4956
4957 /* Create new output read handle and the input write handle. Set
4958 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004959 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004960 * being created. */
4961 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004962 GetCurrentProcess(), &hChildStdinWrDup, 0,
4963 FALSE,
4964 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004965 if (!fSuccess)
4966 return win32_error("DuplicateHandle", NULL);
4967
4968 /* Close the inheritable version of ChildStdin
4969 that we're using. */
4970 CloseHandle(hChildStdinWr);
4971
4972 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4973 return win32_error("CreatePipe", NULL);
4974
4975 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004976 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4977 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004978 if (!fSuccess)
4979 return win32_error("DuplicateHandle", NULL);
4980
4981 /* Close the inheritable version of ChildStdout
4982 that we're using. */
4983 CloseHandle(hChildStdoutRd);
4984
4985 if (n != POPEN_4) {
4986 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4987 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004988 fSuccess = DuplicateHandle(GetCurrentProcess(),
4989 hChildStderrRd,
4990 GetCurrentProcess(),
4991 &hChildStderrRdDup, 0,
4992 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004993 if (!fSuccess)
4994 return win32_error("DuplicateHandle", NULL);
4995 /* Close the inheritable version of ChildStdErr that we're using. */
4996 CloseHandle(hChildStderrRd);
4997 }
Tim Peters5aa91602002-01-30 05:46:57 +00004998
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004999 switch (n) {
5000 case POPEN_1:
5001 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5002 case _O_WRONLY | _O_TEXT:
5003 /* Case for writing to child Stdin in text mode. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00005004 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005005 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005006 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005007 PyFile_SetBufSize(f, 0);
5008 /* We don't care about these pipes anymore, so close them. */
5009 CloseHandle(hChildStdoutRdDup);
5010 CloseHandle(hChildStderrRdDup);
5011 break;
5012
5013 case _O_RDONLY | _O_TEXT:
5014 /* Case for reading from child Stdout in text mode. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00005015 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005016 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005017 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005018 PyFile_SetBufSize(f, 0);
5019 /* We don't care about these pipes anymore, so close them. */
5020 CloseHandle(hChildStdinWrDup);
5021 CloseHandle(hChildStderrRdDup);
5022 break;
5023
5024 case _O_RDONLY | _O_BINARY:
5025 /* Case for readinig from child Stdout in binary mode. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00005026 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005027 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005028 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005029 PyFile_SetBufSize(f, 0);
5030 /* We don't care about these pipes anymore, so close them. */
5031 CloseHandle(hChildStdinWrDup);
5032 CloseHandle(hChildStderrRdDup);
5033 break;
5034
5035 case _O_WRONLY | _O_BINARY:
5036 /* Case for writing to child Stdin in binary mode. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00005037 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005038 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005039 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005040 PyFile_SetBufSize(f, 0);
5041 /* We don't care about these pipes anymore, so close them. */
5042 CloseHandle(hChildStdoutRdDup);
5043 CloseHandle(hChildStderrRdDup);
5044 break;
5045 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005046 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005047 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005048
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005049 case POPEN_2:
5050 case POPEN_4:
5051 {
5052 char *m1, *m2;
5053 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005054
Tim Peters7dca21e2002-08-19 00:42:29 +00005055 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005056 m1 = "r";
5057 m2 = "w";
5058 } else {
5059 m1 = "rb";
5060 m2 = "wb";
5061 }
5062
Thomas Wouters477c8d52006-05-27 19:21:47 +00005063 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005064 f1 = _fdopen(fd1, m2);
Thomas Wouters477c8d52006-05-27 19:21:47 +00005065 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005066 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005067 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005068 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00005069 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005070 PyFile_SetBufSize(p2, 0);
5071
5072 if (n != 4)
5073 CloseHandle(hChildStderrRdDup);
5074
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005075 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00005076 Py_XDECREF(p1);
5077 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00005078 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005079 break;
5080 }
Tim Peters5aa91602002-01-30 05:46:57 +00005081
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005082 case POPEN_3:
5083 {
5084 char *m1, *m2;
5085 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005086
Tim Peters7dca21e2002-08-19 00:42:29 +00005087 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005088 m1 = "r";
5089 m2 = "w";
5090 } else {
5091 m1 = "rb";
5092 m2 = "wb";
5093 }
5094
Thomas Wouters477c8d52006-05-27 19:21:47 +00005095 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005096 f1 = _fdopen(fd1, m2);
Thomas Wouters477c8d52006-05-27 19:21:47 +00005097 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005098 f2 = _fdopen(fd2, m1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00005099 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005100 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005101 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00005102 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5103 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005104 PyFile_SetBufSize(p1, 0);
5105 PyFile_SetBufSize(p2, 0);
5106 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005107 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00005108 Py_XDECREF(p1);
5109 Py_XDECREF(p2);
5110 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00005111 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005112 break;
5113 }
5114 }
5115
5116 if (n == POPEN_4) {
5117 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005118 hChildStdinRd,
5119 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005120 hChildStdoutWr,
5121 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005122 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005123 }
5124 else {
5125 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005126 hChildStdinRd,
5127 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005128 hChildStderrWr,
5129 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005130 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005131 }
5132
Mark Hammondb37a3732000-08-14 04:47:33 +00005133 /*
5134 * Insert the files we've created into the process dictionary
5135 * all referencing the list with the process handle and the
5136 * initial number of files (see description below in _PyPclose).
5137 * Since if _PyPclose later tried to wait on a process when all
5138 * handles weren't closed, it could create a deadlock with the
5139 * child, we spend some energy here to try to ensure that we
5140 * either insert all file handles into the dictionary or none
5141 * at all. It's a little clumsy with the various popen modes
5142 * and variable number of files involved.
5143 */
5144 if (!_PyPopenProcs) {
5145 _PyPopenProcs = PyDict_New();
5146 }
5147
5148 if (_PyPopenProcs) {
5149 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5150 int ins_rc[3];
5151
5152 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5153 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
5154
5155 procObj = PyList_New(2);
5156 hProcessObj = PyLong_FromVoidPtr(hProcess);
5157 intObj = PyInt_FromLong(file_count);
5158
5159 if (procObj && hProcessObj && intObj) {
5160 PyList_SetItem(procObj,0,hProcessObj);
5161 PyList_SetItem(procObj,1,intObj);
5162
5163 fileObj[0] = PyLong_FromVoidPtr(f1);
5164 if (fileObj[0]) {
5165 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5166 fileObj[0],
5167 procObj);
5168 }
5169 if (file_count >= 2) {
5170 fileObj[1] = PyLong_FromVoidPtr(f2);
5171 if (fileObj[1]) {
5172 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5173 fileObj[1],
5174 procObj);
5175 }
5176 }
5177 if (file_count >= 3) {
5178 fileObj[2] = PyLong_FromVoidPtr(f3);
5179 if (fileObj[2]) {
5180 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5181 fileObj[2],
5182 procObj);
5183 }
5184 }
5185
5186 if (ins_rc[0] < 0 || !fileObj[0] ||
5187 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5188 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5189 /* Something failed - remove any dictionary
5190 * entries that did make it.
5191 */
5192 if (!ins_rc[0] && fileObj[0]) {
5193 PyDict_DelItem(_PyPopenProcs,
5194 fileObj[0]);
5195 }
5196 if (!ins_rc[1] && fileObj[1]) {
5197 PyDict_DelItem(_PyPopenProcs,
5198 fileObj[1]);
5199 }
5200 if (!ins_rc[2] && fileObj[2]) {
5201 PyDict_DelItem(_PyPopenProcs,
5202 fileObj[2]);
5203 }
5204 }
5205 }
Tim Peters5aa91602002-01-30 05:46:57 +00005206
Mark Hammondb37a3732000-08-14 04:47:33 +00005207 /*
5208 * Clean up our localized references for the dictionary keys
5209 * and value since PyDict_SetItem will Py_INCREF any copies
5210 * that got placed in the dictionary.
5211 */
5212 Py_XDECREF(procObj);
5213 Py_XDECREF(fileObj[0]);
5214 Py_XDECREF(fileObj[1]);
5215 Py_XDECREF(fileObj[2]);
5216 }
5217
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005218 /* Child is launched. Close the parents copy of those pipe
5219 * handles that only the child should have open. You need to
5220 * make sure that no handles to the write end of the output pipe
5221 * are maintained in this process or else the pipe will not close
5222 * when the child process exits and the ReadFile will hang. */
5223
5224 if (!CloseHandle(hChildStdinRd))
5225 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005226
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005227 if (!CloseHandle(hChildStdoutWr))
5228 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005229
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005230 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5231 return win32_error("CloseHandle", NULL);
5232
5233 return f;
5234}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005235
5236/*
5237 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5238 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005239 *
5240 * This function uses the _PyPopenProcs dictionary in order to map the
5241 * input file pointer to information about the process that was
5242 * originally created by the popen* call that created the file pointer.
5243 * The dictionary uses the file pointer as a key (with one entry
5244 * inserted for each file returned by the original popen* call) and a
5245 * single list object as the value for all files from a single call.
5246 * The list object contains the Win32 process handle at [0], and a file
5247 * count at [1], which is initialized to the total number of file
5248 * handles using that list.
5249 *
5250 * This function closes whichever handle it is passed, and decrements
5251 * the file count in the dictionary for the process handle pointed to
5252 * by this file. On the last close (when the file count reaches zero),
5253 * this function will wait for the child process and then return its
5254 * exit code as the result of the close() operation. This permits the
5255 * files to be closed in any order - it is always the close() of the
5256 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005257 *
5258 * NOTE: This function is currently called with the GIL released.
5259 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005260 */
Tim Peters736aa322000-09-01 06:51:24 +00005261
Fredrik Lundh56055a42000-07-23 19:47:12 +00005262static int _PyPclose(FILE *file)
5263{
Fredrik Lundh20318932000-07-26 17:29:12 +00005264 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005265 DWORD exit_code;
5266 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00005267 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5268 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005269#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005270 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005271#endif
5272
Fredrik Lundh20318932000-07-26 17:29:12 +00005273 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00005274 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00005275 */
5276 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005277#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005278 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005279#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005280 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005281 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5282 (procObj = PyDict_GetItem(_PyPopenProcs,
5283 fileObj)) != NULL &&
5284 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5285 (intObj = PyList_GetItem(procObj,1)) != NULL) {
5286
5287 hProcess = PyLong_AsVoidPtr(hProcessObj);
5288 file_count = PyInt_AsLong(intObj);
5289
5290 if (file_count > 1) {
5291 /* Still other files referencing process */
5292 file_count--;
5293 PyList_SetItem(procObj,1,
5294 PyInt_FromLong(file_count));
5295 } else {
5296 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00005297 if (result != EOF &&
5298 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5299 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00005300 /* Possible truncation here in 16-bit environments, but
5301 * real exit codes are just the lower byte in any event.
5302 */
5303 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005304 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00005305 /* Indicate failure - this will cause the file object
5306 * to raise an I/O error and translate the last Win32
5307 * error code from errno. We do have a problem with
5308 * last errors that overlap the normal errno table,
5309 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005310 */
Fredrik Lundh20318932000-07-26 17:29:12 +00005311 if (result != EOF) {
5312 /* If the error wasn't from the fclose(), then
5313 * set errno for the file object error handling.
5314 */
5315 errno = GetLastError();
5316 }
5317 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005318 }
5319
5320 /* Free up the native handle at this point */
5321 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00005322 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005323
Mark Hammondb37a3732000-08-14 04:47:33 +00005324 /* Remove this file pointer from dictionary */
5325 PyDict_DelItem(_PyPopenProcs, fileObj);
5326
5327 if (PyDict_Size(_PyPopenProcs) == 0) {
5328 Py_DECREF(_PyPopenProcs);
5329 _PyPopenProcs = NULL;
5330 }
5331
5332 } /* if object retrieval ok */
5333
5334 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005335 } /* if _PyPopenProcs */
5336
Tim Peters736aa322000-09-01 06:51:24 +00005337#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005338 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005339#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005340 return result;
5341}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005342
5343#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005344static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005345posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005346{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005347 char *name;
5348 char *mode = "r";
5349 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00005350 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005351 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005352 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00005353 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00005354 /* Strip mode of binary or text modifiers */
5355 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5356 mode = "r";
5357 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5358 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00005359 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005360 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005361 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00005362 if (fp == NULL)
5363 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005364 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005365 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005366 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005367 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005368}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005369
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005370#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005371#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005372
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005373
Guido van Rossumb6775db1994-08-01 11:34:53 +00005374#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005375PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005376"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005377Set the current process's user id.");
5378
Barry Warsaw53699e91996-12-10 23:23:01 +00005379static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005380posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005381{
5382 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005383 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005384 return NULL;
5385 if (setuid(uid) < 0)
5386 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005387 Py_INCREF(Py_None);
5388 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005389}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005390#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005391
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005392
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005393#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005394PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005395"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005396Set the current process's effective user id.");
5397
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005398static PyObject *
5399posix_seteuid (PyObject *self, PyObject *args)
5400{
5401 int euid;
5402 if (!PyArg_ParseTuple(args, "i", &euid)) {
5403 return NULL;
5404 } else if (seteuid(euid) < 0) {
5405 return posix_error();
5406 } else {
5407 Py_INCREF(Py_None);
5408 return Py_None;
5409 }
5410}
5411#endif /* HAVE_SETEUID */
5412
5413#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005414PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005415"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005416Set the current process's effective group id.");
5417
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005418static PyObject *
5419posix_setegid (PyObject *self, PyObject *args)
5420{
5421 int egid;
5422 if (!PyArg_ParseTuple(args, "i", &egid)) {
5423 return NULL;
5424 } else if (setegid(egid) < 0) {
5425 return posix_error();
5426 } else {
5427 Py_INCREF(Py_None);
5428 return Py_None;
5429 }
5430}
5431#endif /* HAVE_SETEGID */
5432
5433#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005434PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005435"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005436Set the current process's real and effective user ids.");
5437
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005438static PyObject *
5439posix_setreuid (PyObject *self, PyObject *args)
5440{
5441 int ruid, euid;
5442 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
5443 return NULL;
5444 } else if (setreuid(ruid, euid) < 0) {
5445 return posix_error();
5446 } else {
5447 Py_INCREF(Py_None);
5448 return Py_None;
5449 }
5450}
5451#endif /* HAVE_SETREUID */
5452
5453#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005454PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005455"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005456Set the current process's real and effective group ids.");
5457
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005458static PyObject *
5459posix_setregid (PyObject *self, PyObject *args)
5460{
5461 int rgid, egid;
5462 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
5463 return NULL;
5464 } else if (setregid(rgid, egid) < 0) {
5465 return posix_error();
5466 } else {
5467 Py_INCREF(Py_None);
5468 return Py_None;
5469 }
5470}
5471#endif /* HAVE_SETREGID */
5472
Guido van Rossumb6775db1994-08-01 11:34:53 +00005473#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005474PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005475"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005476Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005477
Barry Warsaw53699e91996-12-10 23:23:01 +00005478static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005479posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005480{
5481 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005482 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005483 return NULL;
5484 if (setgid(gid) < 0)
5485 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005486 Py_INCREF(Py_None);
5487 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005488}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005489#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005490
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005491#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005492PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005493"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005494Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005495
5496static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005497posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005498{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005499 int i, len;
5500 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005501
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005502 if (!PySequence_Check(groups)) {
5503 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5504 return NULL;
5505 }
5506 len = PySequence_Size(groups);
5507 if (len > MAX_GROUPS) {
5508 PyErr_SetString(PyExc_ValueError, "too many groups");
5509 return NULL;
5510 }
5511 for(i = 0; i < len; i++) {
5512 PyObject *elem;
5513 elem = PySequence_GetItem(groups, i);
5514 if (!elem)
5515 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00005516 if (!PyLong_Check(elem)) {
5517 PyErr_SetString(PyExc_TypeError,
5518 "groups must be integers");
5519 Py_DECREF(elem);
5520 return NULL;
5521 } else {
5522 unsigned long x = PyLong_AsUnsignedLong(elem);
5523 if (PyErr_Occurred()) {
5524 PyErr_SetString(PyExc_TypeError,
5525 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00005526 Py_DECREF(elem);
5527 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00005528 }
Georg Brandla13c2442005-11-22 19:30:31 +00005529 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00005530 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00005531 if (grouplist[i] != x) {
5532 PyErr_SetString(PyExc_TypeError,
5533 "group id too big");
5534 Py_DECREF(elem);
5535 return NULL;
5536 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005537 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005538 Py_DECREF(elem);
5539 }
5540
5541 if (setgroups(len, grouplist) < 0)
5542 return posix_error();
5543 Py_INCREF(Py_None);
5544 return Py_None;
5545}
5546#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005547
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005548#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5549static PyObject *
5550wait_helper(int pid, int status, struct rusage *ru)
5551{
5552 PyObject *result;
5553 static PyObject *struct_rusage;
5554
5555 if (pid == -1)
5556 return posix_error();
5557
5558 if (struct_rusage == NULL) {
5559 PyObject *m = PyImport_ImportModule("resource");
5560 if (m == NULL)
5561 return NULL;
5562 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5563 Py_DECREF(m);
5564 if (struct_rusage == NULL)
5565 return NULL;
5566 }
5567
5568 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5569 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5570 if (!result)
5571 return NULL;
5572
5573#ifndef doubletime
5574#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5575#endif
5576
5577 PyStructSequence_SET_ITEM(result, 0,
5578 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5579 PyStructSequence_SET_ITEM(result, 1,
5580 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5581#define SET_INT(result, index, value)\
5582 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5583 SET_INT(result, 2, ru->ru_maxrss);
5584 SET_INT(result, 3, ru->ru_ixrss);
5585 SET_INT(result, 4, ru->ru_idrss);
5586 SET_INT(result, 5, ru->ru_isrss);
5587 SET_INT(result, 6, ru->ru_minflt);
5588 SET_INT(result, 7, ru->ru_majflt);
5589 SET_INT(result, 8, ru->ru_nswap);
5590 SET_INT(result, 9, ru->ru_inblock);
5591 SET_INT(result, 10, ru->ru_oublock);
5592 SET_INT(result, 11, ru->ru_msgsnd);
5593 SET_INT(result, 12, ru->ru_msgrcv);
5594 SET_INT(result, 13, ru->ru_nsignals);
5595 SET_INT(result, 14, ru->ru_nvcsw);
5596 SET_INT(result, 15, ru->ru_nivcsw);
5597#undef SET_INT
5598
5599 if (PyErr_Occurred()) {
5600 Py_DECREF(result);
5601 return NULL;
5602 }
5603
5604 return Py_BuildValue("iiN", pid, status, result);
5605}
5606#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5607
5608#ifdef HAVE_WAIT3
5609PyDoc_STRVAR(posix_wait3__doc__,
5610"wait3(options) -> (pid, status, rusage)\n\n\
5611Wait for completion of a child process.");
5612
5613static PyObject *
5614posix_wait3(PyObject *self, PyObject *args)
5615{
5616 int pid, options;
5617 struct rusage ru;
5618 WAIT_TYPE status;
5619 WAIT_STATUS_INT(status) = 0;
5620
5621 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5622 return NULL;
5623
5624 Py_BEGIN_ALLOW_THREADS
5625 pid = wait3(&status, options, &ru);
5626 Py_END_ALLOW_THREADS
5627
5628 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
5629}
5630#endif /* HAVE_WAIT3 */
5631
5632#ifdef HAVE_WAIT4
5633PyDoc_STRVAR(posix_wait4__doc__,
5634"wait4(pid, options) -> (pid, status, rusage)\n\n\
5635Wait for completion of a given child process.");
5636
5637static PyObject *
5638posix_wait4(PyObject *self, PyObject *args)
5639{
5640 int pid, options;
5641 struct rusage ru;
5642 WAIT_TYPE status;
5643 WAIT_STATUS_INT(status) = 0;
5644
5645 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
5646 return NULL;
5647
5648 Py_BEGIN_ALLOW_THREADS
5649 pid = wait4(pid, &status, options, &ru);
5650 Py_END_ALLOW_THREADS
5651
5652 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
5653}
5654#endif /* HAVE_WAIT4 */
5655
Guido van Rossumb6775db1994-08-01 11:34:53 +00005656#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005657PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005658"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005659Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005660
Barry Warsaw53699e91996-12-10 23:23:01 +00005661static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005662posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005663{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005664 int pid, options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005665 WAIT_TYPE status;
5666 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005667
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005668 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00005669 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005670 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00005671 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00005672 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00005673 if (pid == -1)
5674 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005675
5676 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005677}
5678
Tim Petersab034fa2002-02-01 11:27:43 +00005679#elif defined(HAVE_CWAIT)
5680
5681/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005682PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005683"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005684"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005685
5686static PyObject *
5687posix_waitpid(PyObject *self, PyObject *args)
5688{
Thomas Wouters477c8d52006-05-27 19:21:47 +00005689 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005690 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005691
5692 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5693 return NULL;
5694 Py_BEGIN_ALLOW_THREADS
5695 pid = _cwait(&status, pid, options);
5696 Py_END_ALLOW_THREADS
5697 if (pid == -1)
5698 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005699
5700 /* shift the status left a byte so this is more like the POSIX waitpid */
5701 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005702}
5703#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005704
Guido van Rossumad0ee831995-03-01 10:34:45 +00005705#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005706PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005707"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005708Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005709
Barry Warsaw53699e91996-12-10 23:23:01 +00005710static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005711posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005712{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005713 int pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005714 WAIT_TYPE status;
5715 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005716
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005717 Py_BEGIN_ALLOW_THREADS
5718 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00005719 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00005720 if (pid == -1)
5721 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005722
5723 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005724}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005725#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005726
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005727
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005728PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005729"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005730Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005731
Barry Warsaw53699e91996-12-10 23:23:01 +00005732static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005733posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005734{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005735#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005736 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005737#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005738#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00005739 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005740#else
5741 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5742#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005743#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005744}
5745
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005746
Guido van Rossumb6775db1994-08-01 11:34:53 +00005747#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005748PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005749"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005750Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005751
Barry Warsaw53699e91996-12-10 23:23:01 +00005752static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005753posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005754{
Thomas Wouters89f507f2006-12-13 04:49:30 +00005755 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00005756 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005757 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005758 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005759 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005760
5761 if (!PyArg_ParseTuple(args, "et:readlink",
5762 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005763 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005764 v = PySequence_GetItem(args, 0);
5765 if (v == NULL) return NULL;
5766
5767 if (PyUnicode_Check(v)) {
5768 arg_is_unicode = 1;
5769 }
5770 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005771
Barry Warsaw53699e91996-12-10 23:23:01 +00005772 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00005773 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00005774 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005775 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00005776 return posix_error_with_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005777
5778 v = PyString_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005779 if (arg_is_unicode) {
5780 PyObject *w;
5781
5782 w = PyUnicode_FromEncodedObject(v,
5783 Py_FileSystemDefaultEncoding,
5784 "strict");
5785 if (w != NULL) {
5786 Py_DECREF(v);
5787 v = w;
5788 }
5789 else {
5790 /* fall back to the original byte string, as
5791 discussed in patch #683592 */
5792 PyErr_Clear();
5793 }
5794 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005795 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005796}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005797#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005798
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005799
Guido van Rossumb6775db1994-08-01 11:34:53 +00005800#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005801PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005802"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005803Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005804
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005805static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005806posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005807{
Thomas Wouters477c8d52006-05-27 19:21:47 +00005808 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005809}
5810#endif /* HAVE_SYMLINK */
5811
5812
5813#ifdef HAVE_TIMES
5814#ifndef HZ
5815#define HZ 60 /* Universal constant :-) */
5816#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005817
Guido van Rossumd48f2521997-12-05 22:19:34 +00005818#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5819static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005820system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005821{
5822 ULONG value = 0;
5823
5824 Py_BEGIN_ALLOW_THREADS
5825 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5826 Py_END_ALLOW_THREADS
5827
5828 return value;
5829}
5830
5831static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005832posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005833{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005834 /* Currently Only Uptime is Provided -- Others Later */
5835 return Py_BuildValue("ddddd",
5836 (double)0 /* t.tms_utime / HZ */,
5837 (double)0 /* t.tms_stime / HZ */,
5838 (double)0 /* t.tms_cutime / HZ */,
5839 (double)0 /* t.tms_cstime / HZ */,
5840 (double)system_uptime() / 1000);
5841}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005842#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005843static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005844posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005845{
5846 struct tms t;
5847 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005848 errno = 0;
5849 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005850 if (c == (clock_t) -1)
5851 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005852 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005853 (double)t.tms_utime / HZ,
5854 (double)t.tms_stime / HZ,
5855 (double)t.tms_cutime / HZ,
5856 (double)t.tms_cstime / HZ,
5857 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005858}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005859#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005860#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005861
5862
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005863#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005864#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005865static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005866posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005867{
5868 FILETIME create, exit, kernel, user;
5869 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005870 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005871 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5872 /* The fields of a FILETIME structure are the hi and lo part
5873 of a 64-bit value expressed in 100 nanosecond units.
5874 1e7 is one second in such units; 1e-7 the inverse.
5875 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5876 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005877 return Py_BuildValue(
5878 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005879 (double)(kernel.dwHighDateTime*429.4967296 +
5880 kernel.dwLowDateTime*1e-7),
5881 (double)(user.dwHighDateTime*429.4967296 +
5882 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005883 (double)0,
5884 (double)0,
5885 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005886}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005887#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005888
5889#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005890PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005891"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005892Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005893#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005894
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005895
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005896#ifdef HAVE_GETSID
5897PyDoc_STRVAR(posix_getsid__doc__,
5898"getsid(pid) -> sid\n\n\
5899Call the system call getsid().");
5900
5901static PyObject *
5902posix_getsid(PyObject *self, PyObject *args)
5903{
5904 int pid, sid;
5905 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5906 return NULL;
5907 sid = getsid(pid);
5908 if (sid < 0)
5909 return posix_error();
5910 return PyInt_FromLong((long)sid);
5911}
5912#endif /* HAVE_GETSID */
5913
5914
Guido van Rossumb6775db1994-08-01 11:34:53 +00005915#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005916PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005917"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005918Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005919
Barry Warsaw53699e91996-12-10 23:23:01 +00005920static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005921posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005922{
Guido van Rossum687dd131993-05-17 08:34:16 +00005923 if (setsid() < 0)
5924 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005925 Py_INCREF(Py_None);
5926 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005927}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005928#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005929
Guido van Rossumb6775db1994-08-01 11:34:53 +00005930#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005931PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005932"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005933Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005934
Barry Warsaw53699e91996-12-10 23:23:01 +00005935static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005936posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005937{
5938 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005939 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005940 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005941 if (setpgid(pid, pgrp) < 0)
5942 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005943 Py_INCREF(Py_None);
5944 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005945}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005946#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005947
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005948
Guido van Rossumb6775db1994-08-01 11:34:53 +00005949#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005950PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005951"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005952Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005953
Barry Warsaw53699e91996-12-10 23:23:01 +00005954static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005955posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005956{
5957 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005958 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005959 return NULL;
5960 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005961 if (pgid < 0)
5962 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005963 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005964}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005965#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005966
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005967
Guido van Rossumb6775db1994-08-01 11:34:53 +00005968#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005969PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005970"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005971Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005972
Barry Warsaw53699e91996-12-10 23:23:01 +00005973static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005974posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005975{
5976 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005977 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005978 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005979 if (tcsetpgrp(fd, pgid) < 0)
5980 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005981 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005982 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005983}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005984#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005985
Guido van Rossum687dd131993-05-17 08:34:16 +00005986/* Functions acting on file descriptors */
5987
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005988PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005989"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005990Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005991
Barry Warsaw53699e91996-12-10 23:23:01 +00005992static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005993posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005994{
Mark Hammondef8b6542001-05-13 08:04:26 +00005995 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005996 int flag;
5997 int mode = 0777;
5998 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005999
6000#ifdef MS_WINDOWS
6001 if (unicode_file_names()) {
6002 PyUnicodeObject *po;
6003 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6004 Py_BEGIN_ALLOW_THREADS
6005 /* PyUnicode_AS_UNICODE OK without thread
6006 lock as it is a simple dereference. */
6007 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6008 Py_END_ALLOW_THREADS
6009 if (fd < 0)
6010 return posix_error();
6011 return PyInt_FromLong((long)fd);
6012 }
6013 /* Drop the argument parsing error as narrow strings
6014 are also valid. */
6015 PyErr_Clear();
6016 }
6017#endif
6018
Tim Peters5aa91602002-01-30 05:46:57 +00006019 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00006020 Py_FileSystemDefaultEncoding, &file,
6021 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00006022 return NULL;
6023
Barry Warsaw53699e91996-12-10 23:23:01 +00006024 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006025 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00006026 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006027 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00006028 return posix_error_with_allocated_filename(file);
6029 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00006030 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006031}
6032
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006033
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006034PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006035"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006036Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006037
Barry Warsaw53699e91996-12-10 23:23:01 +00006038static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006039posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006040{
6041 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006042 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006043 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006044 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006045 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006046 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006047 if (res < 0)
6048 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006049 Py_INCREF(Py_None);
6050 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006051}
6052
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006054PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006055"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006056Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006057
Barry Warsaw53699e91996-12-10 23:23:01 +00006058static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006059posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006060{
6061 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006062 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006063 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006064 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006065 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006066 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006067 if (fd < 0)
6068 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006069 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006070}
6071
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006072
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006073PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006074"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006075Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006076
Barry Warsaw53699e91996-12-10 23:23:01 +00006077static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006078posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006079{
6080 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006081 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00006082 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006083 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006084 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00006085 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006086 if (res < 0)
6087 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006088 Py_INCREF(Py_None);
6089 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006090}
6091
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006093PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006094"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006095Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006096
Barry Warsaw53699e91996-12-10 23:23:01 +00006097static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006098posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006099{
6100 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006101#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006102 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006103#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00006104 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006105#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006106 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006107 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00006108 return NULL;
6109#ifdef SEEK_SET
6110 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6111 switch (how) {
6112 case 0: how = SEEK_SET; break;
6113 case 1: how = SEEK_CUR; break;
6114 case 2: how = SEEK_END; break;
6115 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006116#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006117
6118#if !defined(HAVE_LARGEFILE_SUPPORT)
6119 pos = PyInt_AsLong(posobj);
6120#else
6121 pos = PyLong_Check(posobj) ?
6122 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
6123#endif
6124 if (PyErr_Occurred())
6125 return NULL;
6126
Barry Warsaw53699e91996-12-10 23:23:01 +00006127 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006128#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00006129 res = _lseeki64(fd, pos, how);
6130#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006131 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006132#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006133 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006134 if (res < 0)
6135 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006136
6137#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00006138 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006139#else
6140 return PyLong_FromLongLong(res);
6141#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006142}
6143
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006144
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006145PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006146"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006147Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006148
Barry Warsaw53699e91996-12-10 23:23:01 +00006149static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006150posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006151{
Guido van Rossum572dbf82007-04-27 23:53:51 +00006152 int fd, size;
6153 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00006154 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006155 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006156 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00006157 if (size < 0) {
6158 errno = EINVAL;
6159 return posix_error();
6160 }
Guido van Rossum572dbf82007-04-27 23:53:51 +00006161 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006162 if (buffer == NULL)
6163 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006164 Py_BEGIN_ALLOW_THREADS
Guido van Rossum572dbf82007-04-27 23:53:51 +00006165 n = read(fd, PyBytes_AsString(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00006166 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00006167 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006168 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00006169 return posix_error();
6170 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00006171 if (n != size)
Guido van Rossum572dbf82007-04-27 23:53:51 +00006172 PyBytes_Resize(buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00006173 return buffer;
6174}
6175
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006176
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006177PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006178"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006179Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006180
Barry Warsaw53699e91996-12-10 23:23:01 +00006181static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006182posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006183{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006184 int fd;
6185 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00006186 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006187
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006188 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006189 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006190 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006191 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00006192 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006193 if (size < 0)
6194 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006195 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006196}
6197
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006198
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006199PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006200"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006201Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006202
Barry Warsaw53699e91996-12-10 23:23:01 +00006203static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006204posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006205{
6206 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00006207 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00006208 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006209 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006210 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006211#ifdef __VMS
6212 /* on OpenVMS we must ensure that all bytes are written to the file */
6213 fsync(fd);
6214#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006215 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00006216 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00006217 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00006218 if (res != 0) {
6219#ifdef MS_WINDOWS
6220 return win32_error("fstat", NULL);
6221#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006222 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006223#endif
6224 }
Tim Peters5aa91602002-01-30 05:46:57 +00006225
Martin v. Löwis14694662006-02-03 12:54:16 +00006226 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006227}
6228
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006229
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006230PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006231"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006232Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006233
Barry Warsaw53699e91996-12-10 23:23:01 +00006234static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006235posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006236{
Guido van Rossum687dd131993-05-17 08:34:16 +00006237 int fd;
Guido van Rossumd8faa362007-04-27 19:54:29 +00006238 char *orgmode = "r";
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006239 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00006240 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00006241 PyObject *f;
Guido van Rossumd8faa362007-04-27 19:54:29 +00006242 char *mode;
6243 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00006244 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006245
Guido van Rossumd8faa362007-04-27 19:54:29 +00006246 /* Sanitize mode. See fileobject.c */
6247 mode = PyMem_MALLOC(strlen(orgmode)+3);
6248 if (!mode) {
6249 PyErr_NoMemory();
6250 return NULL;
6251 }
6252 strcpy(mode, orgmode);
6253 if (_PyFile_SanitizeMode(mode)) {
6254 PyMem_FREE(mode);
Thomas Heller1f043e22002-11-07 16:00:59 +00006255 return NULL;
6256 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006257 Py_BEGIN_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006258#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
6259 if (mode[0] == 'a') {
6260 /* try to make sure the O_APPEND flag is set */
6261 int flags;
6262 flags = fcntl(fd, F_GETFL);
6263 if (flags != -1)
6264 fcntl(fd, F_SETFL, flags | O_APPEND);
6265 fp = fdopen(fd, mode);
6266 if (fp == NULL && flags != -1)
6267 /* restore old mode if fdopen failed */
6268 fcntl(fd, F_SETFL, flags);
6269 } else {
6270 fp = fdopen(fd, mode);
6271 }
6272#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006273 fp = fdopen(fd, mode);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006274#endif
Guido van Rossumd8faa362007-04-27 19:54:29 +00006275 PyMem_FREE(mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00006276 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006277 if (fp == NULL)
6278 return posix_error();
Guido van Rossumd8faa362007-04-27 19:54:29 +00006279 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006280 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00006281 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006282 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006283}
6284
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006285PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006286"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006287Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006288connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006289
6290static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006291posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006292{
6293 int fd;
6294 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6295 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006296 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006297}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006298
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006299#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006300PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006301"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006302Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006303
Barry Warsaw53699e91996-12-10 23:23:01 +00006304static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006305posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006306{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006307#if defined(PYOS_OS2)
6308 HFILE read, write;
6309 APIRET rc;
6310
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006311 Py_BEGIN_ALLOW_THREADS
6312 rc = DosCreatePipe( &read, &write, 4096);
6313 Py_END_ALLOW_THREADS
6314 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006315 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006316
6317 return Py_BuildValue("(ii)", read, write);
6318#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006319#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00006320 int fds[2];
6321 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00006322 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006323 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00006324 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006325 if (res != 0)
6326 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006327 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006328#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00006329 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006330 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00006331 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00006332 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006333 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00006334 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00006335 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006336 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00006337 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6338 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006339 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006340#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006341#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006342}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006343#endif /* HAVE_PIPE */
6344
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006345
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006346#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006347PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006348"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006349Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006350
Barry Warsaw53699e91996-12-10 23:23:01 +00006351static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006352posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006353{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006354 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006355 int mode = 0666;
6356 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006357 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006358 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006359 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006360 res = mkfifo(filename, mode);
6361 Py_END_ALLOW_THREADS
6362 if (res < 0)
6363 return posix_error();
6364 Py_INCREF(Py_None);
6365 return Py_None;
6366}
6367#endif
6368
6369
Neal Norwitz11690112002-07-30 01:08:28 +00006370#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006371PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006372"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006373Create a filesystem node (file, device special file or named pipe)\n\
6374named filename. mode specifies both the permissions to use and the\n\
6375type of node to be created, being combined (bitwise OR) with one of\n\
6376S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006377device defines the newly created device special file (probably using\n\
6378os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006379
6380
6381static PyObject *
6382posix_mknod(PyObject *self, PyObject *args)
6383{
6384 char *filename;
6385 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006386 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006387 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00006388 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006389 return NULL;
6390 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006391 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00006392 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006393 if (res < 0)
6394 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006395 Py_INCREF(Py_None);
6396 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006397}
6398#endif
6399
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006400#ifdef HAVE_DEVICE_MACROS
6401PyDoc_STRVAR(posix_major__doc__,
6402"major(device) -> major number\n\
6403Extracts a device major number from a raw device number.");
6404
6405static PyObject *
6406posix_major(PyObject *self, PyObject *args)
6407{
6408 int device;
6409 if (!PyArg_ParseTuple(args, "i:major", &device))
6410 return NULL;
6411 return PyInt_FromLong((long)major(device));
6412}
6413
6414PyDoc_STRVAR(posix_minor__doc__,
6415"minor(device) -> minor number\n\
6416Extracts a device minor number from a raw device number.");
6417
6418static PyObject *
6419posix_minor(PyObject *self, PyObject *args)
6420{
6421 int device;
6422 if (!PyArg_ParseTuple(args, "i:minor", &device))
6423 return NULL;
6424 return PyInt_FromLong((long)minor(device));
6425}
6426
6427PyDoc_STRVAR(posix_makedev__doc__,
6428"makedev(major, minor) -> device number\n\
6429Composes a raw device number from the major and minor device numbers.");
6430
6431static PyObject *
6432posix_makedev(PyObject *self, PyObject *args)
6433{
6434 int major, minor;
6435 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6436 return NULL;
6437 return PyInt_FromLong((long)makedev(major, minor));
6438}
6439#endif /* device macros */
6440
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006441
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006442#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006443PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006444"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006445Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006446
Barry Warsaw53699e91996-12-10 23:23:01 +00006447static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006448posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006449{
6450 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006451 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006452 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006453 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006454
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006455 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006456 return NULL;
6457
6458#if !defined(HAVE_LARGEFILE_SUPPORT)
6459 length = PyInt_AsLong(lenobj);
6460#else
6461 length = PyLong_Check(lenobj) ?
6462 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6463#endif
6464 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006465 return NULL;
6466
Barry Warsaw53699e91996-12-10 23:23:01 +00006467 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006468 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00006469 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006470 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006471 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006472 return NULL;
6473 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006474 Py_INCREF(Py_None);
6475 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006476}
6477#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006478
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006479#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006480PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006481"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006482Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006483
Fred Drake762e2061999-08-26 17:23:54 +00006484/* Save putenv() parameters as values here, so we can collect them when they
6485 * get re-set with another call for the same key. */
6486static PyObject *posix_putenv_garbage;
6487
Tim Peters5aa91602002-01-30 05:46:57 +00006488static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006489posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006490{
6491 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006492 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00006493 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00006494 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006495
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006496 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006497 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006498
6499#if defined(PYOS_OS2)
6500 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6501 APIRET rc;
6502
Guido van Rossumd48f2521997-12-05 22:19:34 +00006503 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6504 if (rc != NO_ERROR)
6505 return os2_error(rc);
6506
6507 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6508 APIRET rc;
6509
Guido van Rossumd48f2521997-12-05 22:19:34 +00006510 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6511 if (rc != NO_ERROR)
6512 return os2_error(rc);
6513 } else {
6514#endif
6515
Fred Drake762e2061999-08-26 17:23:54 +00006516 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00006517 len = strlen(s1) + strlen(s2) + 2;
6518 /* len includes space for a trailing \0; the size arg to
6519 PyString_FromStringAndSize does not count that */
6520 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00006521 if (newstr == NULL)
6522 return PyErr_NoMemory();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006523 newenv = PyString_AS_STRING(newstr);
6524 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6525 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00006526 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006527 posix_error();
6528 return NULL;
6529 }
Fred Drake762e2061999-08-26 17:23:54 +00006530 /* Install the first arg and newstr in posix_putenv_garbage;
6531 * this will cause previous value to be collected. This has to
6532 * happen after the real putenv() call because the old value
6533 * was still accessible until then. */
6534 if (PyDict_SetItem(posix_putenv_garbage,
6535 PyTuple_GET_ITEM(args, 0), newstr)) {
6536 /* really not much we can do; just leak */
6537 PyErr_Clear();
6538 }
6539 else {
6540 Py_DECREF(newstr);
6541 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006542
6543#if defined(PYOS_OS2)
6544 }
6545#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006546 Py_INCREF(Py_None);
6547 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006548}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006549#endif /* putenv */
6550
Guido van Rossumc524d952001-10-19 01:31:59 +00006551#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006552PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006553"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006554Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006555
6556static PyObject *
6557posix_unsetenv(PyObject *self, PyObject *args)
6558{
6559 char *s1;
6560
6561 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6562 return NULL;
6563
6564 unsetenv(s1);
6565
6566 /* Remove the key from posix_putenv_garbage;
6567 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00006568 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00006569 * old value was still accessible until then.
6570 */
6571 if (PyDict_DelItem(posix_putenv_garbage,
6572 PyTuple_GET_ITEM(args, 0))) {
6573 /* really not much we can do; just leak */
6574 PyErr_Clear();
6575 }
6576
6577 Py_INCREF(Py_None);
6578 return Py_None;
6579}
6580#endif /* unsetenv */
6581
Guido van Rossumb6a47161997-09-15 22:54:34 +00006582#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006583PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006584"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006585Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006586
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006587static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006588posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006589{
6590 int code;
6591 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006592 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00006593 return NULL;
6594 message = strerror(code);
6595 if (message == NULL) {
6596 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00006597 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006598 return NULL;
6599 }
6600 return PyString_FromString(message);
6601}
6602#endif /* strerror */
6603
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006604
Guido van Rossumc9641791998-08-04 15:26:23 +00006605#ifdef HAVE_SYS_WAIT_H
6606
Fred Drake106c1a02002-04-23 15:58:02 +00006607#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006608PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006609"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006610Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006611
6612static PyObject *
6613posix_WCOREDUMP(PyObject *self, PyObject *args)
6614{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006615 WAIT_TYPE status;
6616 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006617
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006618 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006619 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006620
6621 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006622}
6623#endif /* WCOREDUMP */
6624
6625#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006626PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006627"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006628Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006629job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006630
6631static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006632posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006633{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006634 WAIT_TYPE status;
6635 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006636
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006637 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006638 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006639
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006640 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006641}
6642#endif /* WIFCONTINUED */
6643
Guido van Rossumc9641791998-08-04 15:26:23 +00006644#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006645PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006646"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006647Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006648
6649static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006650posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006651{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006652 WAIT_TYPE status;
6653 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006654
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006655 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006656 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006657
Fred Drake106c1a02002-04-23 15:58:02 +00006658 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006659}
6660#endif /* WIFSTOPPED */
6661
6662#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006663PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006664"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006665Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006666
6667static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006668posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006669{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006670 WAIT_TYPE status;
6671 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006672
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006673 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006674 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006675
Fred Drake106c1a02002-04-23 15:58:02 +00006676 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006677}
6678#endif /* WIFSIGNALED */
6679
6680#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006681PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006682"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006683Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006684system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006685
6686static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006687posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006688{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006689 WAIT_TYPE status;
6690 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006691
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006692 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006693 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006694
Fred Drake106c1a02002-04-23 15:58:02 +00006695 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006696}
6697#endif /* WIFEXITED */
6698
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006699#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006700PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006701"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006702Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006703
6704static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006705posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006706{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006707 WAIT_TYPE status;
6708 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006709
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006710 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006711 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006712
Guido van Rossumc9641791998-08-04 15:26:23 +00006713 return Py_BuildValue("i", WEXITSTATUS(status));
6714}
6715#endif /* WEXITSTATUS */
6716
6717#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006718PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006719"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006720Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006721value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006722
6723static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006724posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006725{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006726 WAIT_TYPE status;
6727 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006728
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006729 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006730 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006731
Guido van Rossumc9641791998-08-04 15:26:23 +00006732 return Py_BuildValue("i", WTERMSIG(status));
6733}
6734#endif /* WTERMSIG */
6735
6736#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006737PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006738"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006739Return the signal that stopped the process that provided\n\
6740the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006741
6742static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006743posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006744{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006745 WAIT_TYPE status;
6746 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006747
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006748 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006749 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006750
Guido van Rossumc9641791998-08-04 15:26:23 +00006751 return Py_BuildValue("i", WSTOPSIG(status));
6752}
6753#endif /* WSTOPSIG */
6754
6755#endif /* HAVE_SYS_WAIT_H */
6756
6757
Thomas Wouters477c8d52006-05-27 19:21:47 +00006758#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006759#ifdef _SCO_DS
6760/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6761 needed definitions in sys/statvfs.h */
6762#define _SVID3
6763#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006764#include <sys/statvfs.h>
6765
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006766static PyObject*
6767_pystatvfs_fromstructstatvfs(struct statvfs st) {
6768 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6769 if (v == NULL)
6770 return NULL;
6771
6772#if !defined(HAVE_LARGEFILE_SUPPORT)
6773 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6774 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6775 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6776 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6777 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6778 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6779 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6780 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6781 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6782 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6783#else
6784 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6785 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006786 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006787 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006788 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006789 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006790 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006791 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006792 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006793 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006794 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006795 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006796 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006797 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006798 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6799 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6800#endif
6801
6802 return v;
6803}
6804
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006805PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006806"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006807Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006808
6809static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006810posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006811{
6812 int fd, res;
6813 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006814
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006815 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006816 return NULL;
6817 Py_BEGIN_ALLOW_THREADS
6818 res = fstatvfs(fd, &st);
6819 Py_END_ALLOW_THREADS
6820 if (res != 0)
6821 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006822
6823 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006824}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006825#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006826
6827
Thomas Wouters477c8d52006-05-27 19:21:47 +00006828#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006829#include <sys/statvfs.h>
6830
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006831PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006832"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006833Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006834
6835static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006836posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006837{
6838 char *path;
6839 int res;
6840 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006841 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006842 return NULL;
6843 Py_BEGIN_ALLOW_THREADS
6844 res = statvfs(path, &st);
6845 Py_END_ALLOW_THREADS
6846 if (res != 0)
6847 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006848
6849 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006850}
6851#endif /* HAVE_STATVFS */
6852
6853
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006854#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006855PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006856"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006857Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006858The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006859or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006860
6861static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006862posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006863{
6864 PyObject *result = NULL;
6865 char *dir = NULL;
6866 char *pfx = NULL;
6867 char *name;
6868
6869 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6870 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006871
6872 if (PyErr_Warn(PyExc_RuntimeWarning,
6873 "tempnam is a potential security risk to your program") < 0)
6874 return NULL;
6875
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006876#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006877 name = _tempnam(dir, pfx);
6878#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006879 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006880#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006881 if (name == NULL)
6882 return PyErr_NoMemory();
6883 result = PyString_FromString(name);
6884 free(name);
6885 return result;
6886}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006887#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006888
6889
6890#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006891PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006892"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006893Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006894
6895static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006896posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006897{
6898 FILE *fp;
6899
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006900 fp = tmpfile();
6901 if (fp == NULL)
6902 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006903 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006904}
6905#endif
6906
6907
6908#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006909PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006910"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006911Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006912
6913static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006914posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006915{
6916 char buffer[L_tmpnam];
6917 char *name;
6918
Skip Montanaro95618b52001-08-18 18:52:10 +00006919 if (PyErr_Warn(PyExc_RuntimeWarning,
6920 "tmpnam is a potential security risk to your program") < 0)
6921 return NULL;
6922
Greg Wardb48bc172000-03-01 21:51:56 +00006923#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006924 name = tmpnam_r(buffer);
6925#else
6926 name = tmpnam(buffer);
6927#endif
6928 if (name == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006929 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006930#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006931 "unexpected NULL from tmpnam_r"
6932#else
6933 "unexpected NULL from tmpnam"
6934#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006935 );
6936 PyErr_SetObject(PyExc_OSError, err);
6937 Py_XDECREF(err);
6938 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006939 }
6940 return PyString_FromString(buffer);
6941}
6942#endif
6943
6944
Fred Drakec9680921999-12-13 16:37:25 +00006945/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6946 * It maps strings representing configuration variable names to
6947 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006948 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006949 * rarely-used constants. There are three separate tables that use
6950 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006951 *
6952 * This code is always included, even if none of the interfaces that
6953 * need it are included. The #if hackery needed to avoid it would be
6954 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006955 */
6956struct constdef {
6957 char *name;
6958 long value;
6959};
6960
Fred Drake12c6e2d1999-12-14 21:25:03 +00006961static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006962conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6963 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006964{
6965 if (PyInt_Check(arg)) {
6966 *valuep = PyInt_AS_LONG(arg);
6967 return 1;
6968 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006969 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00006970 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006971 size_t lo = 0;
6972 size_t mid;
6973 size_t hi = tablesize;
6974 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00006975 const char *confname;
6976 Py_ssize_t namelen;
6977 if (PyObject_AsCharBuffer(arg, &confname, &namelen) < 0) {
6978 PyErr_SetString(PyExc_TypeError,
6979 "configuration names must be strings or integers");
6980 return 0;
6981 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006982 while (lo < hi) {
6983 mid = (lo + hi) / 2;
6984 cmp = strcmp(confname, table[mid].name);
6985 if (cmp < 0)
6986 hi = mid;
6987 else if (cmp > 0)
6988 lo = mid + 1;
6989 else {
6990 *valuep = table[mid].value;
6991 return 1;
6992 }
6993 }
6994 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00006995 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006996 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006997}
6998
6999
7000#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7001static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007002#ifdef _PC_ABI_AIO_XFER_MAX
7003 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
7004#endif
7005#ifdef _PC_ABI_ASYNC_IO
7006 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
7007#endif
Fred Drakec9680921999-12-13 16:37:25 +00007008#ifdef _PC_ASYNC_IO
7009 {"PC_ASYNC_IO", _PC_ASYNC_IO},
7010#endif
7011#ifdef _PC_CHOWN_RESTRICTED
7012 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
7013#endif
7014#ifdef _PC_FILESIZEBITS
7015 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
7016#endif
7017#ifdef _PC_LAST
7018 {"PC_LAST", _PC_LAST},
7019#endif
7020#ifdef _PC_LINK_MAX
7021 {"PC_LINK_MAX", _PC_LINK_MAX},
7022#endif
7023#ifdef _PC_MAX_CANON
7024 {"PC_MAX_CANON", _PC_MAX_CANON},
7025#endif
7026#ifdef _PC_MAX_INPUT
7027 {"PC_MAX_INPUT", _PC_MAX_INPUT},
7028#endif
7029#ifdef _PC_NAME_MAX
7030 {"PC_NAME_MAX", _PC_NAME_MAX},
7031#endif
7032#ifdef _PC_NO_TRUNC
7033 {"PC_NO_TRUNC", _PC_NO_TRUNC},
7034#endif
7035#ifdef _PC_PATH_MAX
7036 {"PC_PATH_MAX", _PC_PATH_MAX},
7037#endif
7038#ifdef _PC_PIPE_BUF
7039 {"PC_PIPE_BUF", _PC_PIPE_BUF},
7040#endif
7041#ifdef _PC_PRIO_IO
7042 {"PC_PRIO_IO", _PC_PRIO_IO},
7043#endif
7044#ifdef _PC_SOCK_MAXBUF
7045 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
7046#endif
7047#ifdef _PC_SYNC_IO
7048 {"PC_SYNC_IO", _PC_SYNC_IO},
7049#endif
7050#ifdef _PC_VDISABLE
7051 {"PC_VDISABLE", _PC_VDISABLE},
7052#endif
7053};
7054
Fred Drakec9680921999-12-13 16:37:25 +00007055static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007056conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007057{
7058 return conv_confname(arg, valuep, posix_constants_pathconf,
7059 sizeof(posix_constants_pathconf)
7060 / sizeof(struct constdef));
7061}
7062#endif
7063
7064#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007065PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007066"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007067Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007068If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007069
7070static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007071posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007072{
7073 PyObject *result = NULL;
7074 int name, fd;
7075
Fred Drake12c6e2d1999-12-14 21:25:03 +00007076 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7077 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00007078 long limit;
7079
7080 errno = 0;
7081 limit = fpathconf(fd, name);
7082 if (limit == -1 && errno != 0)
7083 posix_error();
7084 else
7085 result = PyInt_FromLong(limit);
7086 }
7087 return result;
7088}
7089#endif
7090
7091
7092#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007093PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007094"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007095Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007096If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007097
7098static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007099posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007100{
7101 PyObject *result = NULL;
7102 int name;
7103 char *path;
7104
7105 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7106 conv_path_confname, &name)) {
7107 long limit;
7108
7109 errno = 0;
7110 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007111 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00007112 if (errno == EINVAL)
7113 /* could be a path or name problem */
7114 posix_error();
7115 else
7116 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007117 }
Fred Drakec9680921999-12-13 16:37:25 +00007118 else
7119 result = PyInt_FromLong(limit);
7120 }
7121 return result;
7122}
7123#endif
7124
7125#ifdef HAVE_CONFSTR
7126static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007127#ifdef _CS_ARCHITECTURE
7128 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
7129#endif
7130#ifdef _CS_HOSTNAME
7131 {"CS_HOSTNAME", _CS_HOSTNAME},
7132#endif
7133#ifdef _CS_HW_PROVIDER
7134 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
7135#endif
7136#ifdef _CS_HW_SERIAL
7137 {"CS_HW_SERIAL", _CS_HW_SERIAL},
7138#endif
7139#ifdef _CS_INITTAB_NAME
7140 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
7141#endif
Fred Drakec9680921999-12-13 16:37:25 +00007142#ifdef _CS_LFS64_CFLAGS
7143 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
7144#endif
7145#ifdef _CS_LFS64_LDFLAGS
7146 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
7147#endif
7148#ifdef _CS_LFS64_LIBS
7149 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
7150#endif
7151#ifdef _CS_LFS64_LINTFLAGS
7152 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
7153#endif
7154#ifdef _CS_LFS_CFLAGS
7155 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
7156#endif
7157#ifdef _CS_LFS_LDFLAGS
7158 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
7159#endif
7160#ifdef _CS_LFS_LIBS
7161 {"CS_LFS_LIBS", _CS_LFS_LIBS},
7162#endif
7163#ifdef _CS_LFS_LINTFLAGS
7164 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
7165#endif
Fred Draked86ed291999-12-15 15:34:33 +00007166#ifdef _CS_MACHINE
7167 {"CS_MACHINE", _CS_MACHINE},
7168#endif
Fred Drakec9680921999-12-13 16:37:25 +00007169#ifdef _CS_PATH
7170 {"CS_PATH", _CS_PATH},
7171#endif
Fred Draked86ed291999-12-15 15:34:33 +00007172#ifdef _CS_RELEASE
7173 {"CS_RELEASE", _CS_RELEASE},
7174#endif
7175#ifdef _CS_SRPC_DOMAIN
7176 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
7177#endif
7178#ifdef _CS_SYSNAME
7179 {"CS_SYSNAME", _CS_SYSNAME},
7180#endif
7181#ifdef _CS_VERSION
7182 {"CS_VERSION", _CS_VERSION},
7183#endif
Fred Drakec9680921999-12-13 16:37:25 +00007184#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7185 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
7186#endif
7187#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7188 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
7189#endif
7190#ifdef _CS_XBS5_ILP32_OFF32_LIBS
7191 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
7192#endif
7193#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7194 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
7195#endif
7196#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7197 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
7198#endif
7199#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7200 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
7201#endif
7202#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7203 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
7204#endif
7205#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7206 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
7207#endif
7208#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7209 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
7210#endif
7211#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7212 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
7213#endif
7214#ifdef _CS_XBS5_LP64_OFF64_LIBS
7215 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
7216#endif
7217#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7218 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
7219#endif
7220#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7221 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
7222#endif
7223#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7224 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
7225#endif
7226#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7227 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
7228#endif
7229#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7230 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
7231#endif
Fred Draked86ed291999-12-15 15:34:33 +00007232#ifdef _MIPS_CS_AVAIL_PROCESSORS
7233 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
7234#endif
7235#ifdef _MIPS_CS_BASE
7236 {"MIPS_CS_BASE", _MIPS_CS_BASE},
7237#endif
7238#ifdef _MIPS_CS_HOSTID
7239 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
7240#endif
7241#ifdef _MIPS_CS_HW_NAME
7242 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7243#endif
7244#ifdef _MIPS_CS_NUM_PROCESSORS
7245 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7246#endif
7247#ifdef _MIPS_CS_OSREL_MAJ
7248 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7249#endif
7250#ifdef _MIPS_CS_OSREL_MIN
7251 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7252#endif
7253#ifdef _MIPS_CS_OSREL_PATCH
7254 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7255#endif
7256#ifdef _MIPS_CS_OS_NAME
7257 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7258#endif
7259#ifdef _MIPS_CS_OS_PROVIDER
7260 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7261#endif
7262#ifdef _MIPS_CS_PROCESSORS
7263 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7264#endif
7265#ifdef _MIPS_CS_SERIAL
7266 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7267#endif
7268#ifdef _MIPS_CS_VENDOR
7269 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7270#endif
Fred Drakec9680921999-12-13 16:37:25 +00007271};
7272
7273static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007274conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007275{
7276 return conv_confname(arg, valuep, posix_constants_confstr,
7277 sizeof(posix_constants_confstr)
7278 / sizeof(struct constdef));
7279}
7280
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007281PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007282"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007283Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007284
7285static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007286posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007287{
7288 PyObject *result = NULL;
7289 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007290 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007291
7292 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007293 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007294
Fred Drakec9680921999-12-13 16:37:25 +00007295 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007296 len = confstr(name, buffer, sizeof(buffer));
7297 if (len == 0) {
7298 if (errno) {
7299 posix_error();
7300 }
7301 else {
7302 result = Py_None;
7303 Py_INCREF(Py_None);
7304 }
Fred Drakec9680921999-12-13 16:37:25 +00007305 }
7306 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007307 if ((unsigned int)len >= sizeof(buffer)) {
7308 result = PyString_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007309 if (result != NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007310 confstr(name, PyString_AS_STRING(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00007311 }
7312 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007313 result = PyString_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007314 }
7315 }
7316 return result;
7317}
7318#endif
7319
7320
7321#ifdef HAVE_SYSCONF
7322static struct constdef posix_constants_sysconf[] = {
7323#ifdef _SC_2_CHAR_TERM
7324 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7325#endif
7326#ifdef _SC_2_C_BIND
7327 {"SC_2_C_BIND", _SC_2_C_BIND},
7328#endif
7329#ifdef _SC_2_C_DEV
7330 {"SC_2_C_DEV", _SC_2_C_DEV},
7331#endif
7332#ifdef _SC_2_C_VERSION
7333 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7334#endif
7335#ifdef _SC_2_FORT_DEV
7336 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7337#endif
7338#ifdef _SC_2_FORT_RUN
7339 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7340#endif
7341#ifdef _SC_2_LOCALEDEF
7342 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7343#endif
7344#ifdef _SC_2_SW_DEV
7345 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7346#endif
7347#ifdef _SC_2_UPE
7348 {"SC_2_UPE", _SC_2_UPE},
7349#endif
7350#ifdef _SC_2_VERSION
7351 {"SC_2_VERSION", _SC_2_VERSION},
7352#endif
Fred Draked86ed291999-12-15 15:34:33 +00007353#ifdef _SC_ABI_ASYNCHRONOUS_IO
7354 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7355#endif
7356#ifdef _SC_ACL
7357 {"SC_ACL", _SC_ACL},
7358#endif
Fred Drakec9680921999-12-13 16:37:25 +00007359#ifdef _SC_AIO_LISTIO_MAX
7360 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7361#endif
Fred Drakec9680921999-12-13 16:37:25 +00007362#ifdef _SC_AIO_MAX
7363 {"SC_AIO_MAX", _SC_AIO_MAX},
7364#endif
7365#ifdef _SC_AIO_PRIO_DELTA_MAX
7366 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7367#endif
7368#ifdef _SC_ARG_MAX
7369 {"SC_ARG_MAX", _SC_ARG_MAX},
7370#endif
7371#ifdef _SC_ASYNCHRONOUS_IO
7372 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7373#endif
7374#ifdef _SC_ATEXIT_MAX
7375 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7376#endif
Fred Draked86ed291999-12-15 15:34:33 +00007377#ifdef _SC_AUDIT
7378 {"SC_AUDIT", _SC_AUDIT},
7379#endif
Fred Drakec9680921999-12-13 16:37:25 +00007380#ifdef _SC_AVPHYS_PAGES
7381 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7382#endif
7383#ifdef _SC_BC_BASE_MAX
7384 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7385#endif
7386#ifdef _SC_BC_DIM_MAX
7387 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7388#endif
7389#ifdef _SC_BC_SCALE_MAX
7390 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7391#endif
7392#ifdef _SC_BC_STRING_MAX
7393 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7394#endif
Fred Draked86ed291999-12-15 15:34:33 +00007395#ifdef _SC_CAP
7396 {"SC_CAP", _SC_CAP},
7397#endif
Fred Drakec9680921999-12-13 16:37:25 +00007398#ifdef _SC_CHARCLASS_NAME_MAX
7399 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7400#endif
7401#ifdef _SC_CHAR_BIT
7402 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7403#endif
7404#ifdef _SC_CHAR_MAX
7405 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7406#endif
7407#ifdef _SC_CHAR_MIN
7408 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7409#endif
7410#ifdef _SC_CHILD_MAX
7411 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7412#endif
7413#ifdef _SC_CLK_TCK
7414 {"SC_CLK_TCK", _SC_CLK_TCK},
7415#endif
7416#ifdef _SC_COHER_BLKSZ
7417 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7418#endif
7419#ifdef _SC_COLL_WEIGHTS_MAX
7420 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7421#endif
7422#ifdef _SC_DCACHE_ASSOC
7423 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7424#endif
7425#ifdef _SC_DCACHE_BLKSZ
7426 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7427#endif
7428#ifdef _SC_DCACHE_LINESZ
7429 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7430#endif
7431#ifdef _SC_DCACHE_SZ
7432 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7433#endif
7434#ifdef _SC_DCACHE_TBLKSZ
7435 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7436#endif
7437#ifdef _SC_DELAYTIMER_MAX
7438 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7439#endif
7440#ifdef _SC_EQUIV_CLASS_MAX
7441 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7442#endif
7443#ifdef _SC_EXPR_NEST_MAX
7444 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7445#endif
7446#ifdef _SC_FSYNC
7447 {"SC_FSYNC", _SC_FSYNC},
7448#endif
7449#ifdef _SC_GETGR_R_SIZE_MAX
7450 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7451#endif
7452#ifdef _SC_GETPW_R_SIZE_MAX
7453 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7454#endif
7455#ifdef _SC_ICACHE_ASSOC
7456 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7457#endif
7458#ifdef _SC_ICACHE_BLKSZ
7459 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7460#endif
7461#ifdef _SC_ICACHE_LINESZ
7462 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7463#endif
7464#ifdef _SC_ICACHE_SZ
7465 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7466#endif
Fred Draked86ed291999-12-15 15:34:33 +00007467#ifdef _SC_INF
7468 {"SC_INF", _SC_INF},
7469#endif
Fred Drakec9680921999-12-13 16:37:25 +00007470#ifdef _SC_INT_MAX
7471 {"SC_INT_MAX", _SC_INT_MAX},
7472#endif
7473#ifdef _SC_INT_MIN
7474 {"SC_INT_MIN", _SC_INT_MIN},
7475#endif
7476#ifdef _SC_IOV_MAX
7477 {"SC_IOV_MAX", _SC_IOV_MAX},
7478#endif
Fred Draked86ed291999-12-15 15:34:33 +00007479#ifdef _SC_IP_SECOPTS
7480 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7481#endif
Fred Drakec9680921999-12-13 16:37:25 +00007482#ifdef _SC_JOB_CONTROL
7483 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7484#endif
Fred Draked86ed291999-12-15 15:34:33 +00007485#ifdef _SC_KERN_POINTERS
7486 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7487#endif
7488#ifdef _SC_KERN_SIM
7489 {"SC_KERN_SIM", _SC_KERN_SIM},
7490#endif
Fred Drakec9680921999-12-13 16:37:25 +00007491#ifdef _SC_LINE_MAX
7492 {"SC_LINE_MAX", _SC_LINE_MAX},
7493#endif
7494#ifdef _SC_LOGIN_NAME_MAX
7495 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7496#endif
7497#ifdef _SC_LOGNAME_MAX
7498 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7499#endif
7500#ifdef _SC_LONG_BIT
7501 {"SC_LONG_BIT", _SC_LONG_BIT},
7502#endif
Fred Draked86ed291999-12-15 15:34:33 +00007503#ifdef _SC_MAC
7504 {"SC_MAC", _SC_MAC},
7505#endif
Fred Drakec9680921999-12-13 16:37:25 +00007506#ifdef _SC_MAPPED_FILES
7507 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7508#endif
7509#ifdef _SC_MAXPID
7510 {"SC_MAXPID", _SC_MAXPID},
7511#endif
7512#ifdef _SC_MB_LEN_MAX
7513 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7514#endif
7515#ifdef _SC_MEMLOCK
7516 {"SC_MEMLOCK", _SC_MEMLOCK},
7517#endif
7518#ifdef _SC_MEMLOCK_RANGE
7519 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7520#endif
7521#ifdef _SC_MEMORY_PROTECTION
7522 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7523#endif
7524#ifdef _SC_MESSAGE_PASSING
7525 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7526#endif
Fred Draked86ed291999-12-15 15:34:33 +00007527#ifdef _SC_MMAP_FIXED_ALIGNMENT
7528 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7529#endif
Fred Drakec9680921999-12-13 16:37:25 +00007530#ifdef _SC_MQ_OPEN_MAX
7531 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7532#endif
7533#ifdef _SC_MQ_PRIO_MAX
7534 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7535#endif
Fred Draked86ed291999-12-15 15:34:33 +00007536#ifdef _SC_NACLS_MAX
7537 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7538#endif
Fred Drakec9680921999-12-13 16:37:25 +00007539#ifdef _SC_NGROUPS_MAX
7540 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7541#endif
7542#ifdef _SC_NL_ARGMAX
7543 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7544#endif
7545#ifdef _SC_NL_LANGMAX
7546 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7547#endif
7548#ifdef _SC_NL_MSGMAX
7549 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7550#endif
7551#ifdef _SC_NL_NMAX
7552 {"SC_NL_NMAX", _SC_NL_NMAX},
7553#endif
7554#ifdef _SC_NL_SETMAX
7555 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7556#endif
7557#ifdef _SC_NL_TEXTMAX
7558 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7559#endif
7560#ifdef _SC_NPROCESSORS_CONF
7561 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7562#endif
7563#ifdef _SC_NPROCESSORS_ONLN
7564 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7565#endif
Fred Draked86ed291999-12-15 15:34:33 +00007566#ifdef _SC_NPROC_CONF
7567 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7568#endif
7569#ifdef _SC_NPROC_ONLN
7570 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7571#endif
Fred Drakec9680921999-12-13 16:37:25 +00007572#ifdef _SC_NZERO
7573 {"SC_NZERO", _SC_NZERO},
7574#endif
7575#ifdef _SC_OPEN_MAX
7576 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7577#endif
7578#ifdef _SC_PAGESIZE
7579 {"SC_PAGESIZE", _SC_PAGESIZE},
7580#endif
7581#ifdef _SC_PAGE_SIZE
7582 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7583#endif
7584#ifdef _SC_PASS_MAX
7585 {"SC_PASS_MAX", _SC_PASS_MAX},
7586#endif
7587#ifdef _SC_PHYS_PAGES
7588 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7589#endif
7590#ifdef _SC_PII
7591 {"SC_PII", _SC_PII},
7592#endif
7593#ifdef _SC_PII_INTERNET
7594 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7595#endif
7596#ifdef _SC_PII_INTERNET_DGRAM
7597 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7598#endif
7599#ifdef _SC_PII_INTERNET_STREAM
7600 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7601#endif
7602#ifdef _SC_PII_OSI
7603 {"SC_PII_OSI", _SC_PII_OSI},
7604#endif
7605#ifdef _SC_PII_OSI_CLTS
7606 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7607#endif
7608#ifdef _SC_PII_OSI_COTS
7609 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7610#endif
7611#ifdef _SC_PII_OSI_M
7612 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7613#endif
7614#ifdef _SC_PII_SOCKET
7615 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7616#endif
7617#ifdef _SC_PII_XTI
7618 {"SC_PII_XTI", _SC_PII_XTI},
7619#endif
7620#ifdef _SC_POLL
7621 {"SC_POLL", _SC_POLL},
7622#endif
7623#ifdef _SC_PRIORITIZED_IO
7624 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7625#endif
7626#ifdef _SC_PRIORITY_SCHEDULING
7627 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7628#endif
7629#ifdef _SC_REALTIME_SIGNALS
7630 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7631#endif
7632#ifdef _SC_RE_DUP_MAX
7633 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7634#endif
7635#ifdef _SC_RTSIG_MAX
7636 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7637#endif
7638#ifdef _SC_SAVED_IDS
7639 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7640#endif
7641#ifdef _SC_SCHAR_MAX
7642 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7643#endif
7644#ifdef _SC_SCHAR_MIN
7645 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7646#endif
7647#ifdef _SC_SELECT
7648 {"SC_SELECT", _SC_SELECT},
7649#endif
7650#ifdef _SC_SEMAPHORES
7651 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7652#endif
7653#ifdef _SC_SEM_NSEMS_MAX
7654 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7655#endif
7656#ifdef _SC_SEM_VALUE_MAX
7657 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7658#endif
7659#ifdef _SC_SHARED_MEMORY_OBJECTS
7660 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7661#endif
7662#ifdef _SC_SHRT_MAX
7663 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7664#endif
7665#ifdef _SC_SHRT_MIN
7666 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7667#endif
7668#ifdef _SC_SIGQUEUE_MAX
7669 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7670#endif
7671#ifdef _SC_SIGRT_MAX
7672 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7673#endif
7674#ifdef _SC_SIGRT_MIN
7675 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7676#endif
Fred Draked86ed291999-12-15 15:34:33 +00007677#ifdef _SC_SOFTPOWER
7678 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7679#endif
Fred Drakec9680921999-12-13 16:37:25 +00007680#ifdef _SC_SPLIT_CACHE
7681 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7682#endif
7683#ifdef _SC_SSIZE_MAX
7684 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7685#endif
7686#ifdef _SC_STACK_PROT
7687 {"SC_STACK_PROT", _SC_STACK_PROT},
7688#endif
7689#ifdef _SC_STREAM_MAX
7690 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7691#endif
7692#ifdef _SC_SYNCHRONIZED_IO
7693 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7694#endif
7695#ifdef _SC_THREADS
7696 {"SC_THREADS", _SC_THREADS},
7697#endif
7698#ifdef _SC_THREAD_ATTR_STACKADDR
7699 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7700#endif
7701#ifdef _SC_THREAD_ATTR_STACKSIZE
7702 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7703#endif
7704#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7705 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7706#endif
7707#ifdef _SC_THREAD_KEYS_MAX
7708 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7709#endif
7710#ifdef _SC_THREAD_PRIORITY_SCHEDULING
7711 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7712#endif
7713#ifdef _SC_THREAD_PRIO_INHERIT
7714 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7715#endif
7716#ifdef _SC_THREAD_PRIO_PROTECT
7717 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7718#endif
7719#ifdef _SC_THREAD_PROCESS_SHARED
7720 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7721#endif
7722#ifdef _SC_THREAD_SAFE_FUNCTIONS
7723 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7724#endif
7725#ifdef _SC_THREAD_STACK_MIN
7726 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7727#endif
7728#ifdef _SC_THREAD_THREADS_MAX
7729 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7730#endif
7731#ifdef _SC_TIMERS
7732 {"SC_TIMERS", _SC_TIMERS},
7733#endif
7734#ifdef _SC_TIMER_MAX
7735 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7736#endif
7737#ifdef _SC_TTY_NAME_MAX
7738 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7739#endif
7740#ifdef _SC_TZNAME_MAX
7741 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7742#endif
7743#ifdef _SC_T_IOV_MAX
7744 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7745#endif
7746#ifdef _SC_UCHAR_MAX
7747 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7748#endif
7749#ifdef _SC_UINT_MAX
7750 {"SC_UINT_MAX", _SC_UINT_MAX},
7751#endif
7752#ifdef _SC_UIO_MAXIOV
7753 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7754#endif
7755#ifdef _SC_ULONG_MAX
7756 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7757#endif
7758#ifdef _SC_USHRT_MAX
7759 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7760#endif
7761#ifdef _SC_VERSION
7762 {"SC_VERSION", _SC_VERSION},
7763#endif
7764#ifdef _SC_WORD_BIT
7765 {"SC_WORD_BIT", _SC_WORD_BIT},
7766#endif
7767#ifdef _SC_XBS5_ILP32_OFF32
7768 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7769#endif
7770#ifdef _SC_XBS5_ILP32_OFFBIG
7771 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7772#endif
7773#ifdef _SC_XBS5_LP64_OFF64
7774 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7775#endif
7776#ifdef _SC_XBS5_LPBIG_OFFBIG
7777 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7778#endif
7779#ifdef _SC_XOPEN_CRYPT
7780 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7781#endif
7782#ifdef _SC_XOPEN_ENH_I18N
7783 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7784#endif
7785#ifdef _SC_XOPEN_LEGACY
7786 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7787#endif
7788#ifdef _SC_XOPEN_REALTIME
7789 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7790#endif
7791#ifdef _SC_XOPEN_REALTIME_THREADS
7792 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7793#endif
7794#ifdef _SC_XOPEN_SHM
7795 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7796#endif
7797#ifdef _SC_XOPEN_UNIX
7798 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7799#endif
7800#ifdef _SC_XOPEN_VERSION
7801 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7802#endif
7803#ifdef _SC_XOPEN_XCU_VERSION
7804 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7805#endif
7806#ifdef _SC_XOPEN_XPG2
7807 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7808#endif
7809#ifdef _SC_XOPEN_XPG3
7810 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7811#endif
7812#ifdef _SC_XOPEN_XPG4
7813 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7814#endif
7815};
7816
7817static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007818conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007819{
7820 return conv_confname(arg, valuep, posix_constants_sysconf,
7821 sizeof(posix_constants_sysconf)
7822 / sizeof(struct constdef));
7823}
7824
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007825PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007826"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007827Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007828
7829static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007830posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007831{
7832 PyObject *result = NULL;
7833 int name;
7834
7835 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7836 int value;
7837
7838 errno = 0;
7839 value = sysconf(name);
7840 if (value == -1 && errno != 0)
7841 posix_error();
7842 else
7843 result = PyInt_FromLong(value);
7844 }
7845 return result;
7846}
7847#endif
7848
7849
Fred Drakebec628d1999-12-15 18:31:10 +00007850/* This code is used to ensure that the tables of configuration value names
7851 * are in sorted order as required by conv_confname(), and also to build the
7852 * the exported dictionaries that are used to publish information about the
7853 * names available on the host platform.
7854 *
7855 * Sorting the table at runtime ensures that the table is properly ordered
7856 * when used, even for platforms we're not able to test on. It also makes
7857 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007858 */
Fred Drakebec628d1999-12-15 18:31:10 +00007859
7860static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007861cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007862{
7863 const struct constdef *c1 =
7864 (const struct constdef *) v1;
7865 const struct constdef *c2 =
7866 (const struct constdef *) v2;
7867
7868 return strcmp(c1->name, c2->name);
7869}
7870
7871static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007872setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007873 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007874{
Fred Drakebec628d1999-12-15 18:31:10 +00007875 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007876 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007877
7878 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7879 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007880 if (d == NULL)
7881 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007882
Barry Warsaw3155db32000-04-13 15:20:40 +00007883 for (i=0; i < tablesize; ++i) {
7884 PyObject *o = PyInt_FromLong(table[i].value);
7885 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7886 Py_XDECREF(o);
7887 Py_DECREF(d);
7888 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007889 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007890 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007891 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007892 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007893}
7894
Fred Drakebec628d1999-12-15 18:31:10 +00007895/* Return -1 on failure, 0 on success. */
7896static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007897setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007898{
7899#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007900 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007901 sizeof(posix_constants_pathconf)
7902 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007903 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007904 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007905#endif
7906#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007907 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007908 sizeof(posix_constants_confstr)
7909 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007910 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007911 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007912#endif
7913#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007914 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007915 sizeof(posix_constants_sysconf)
7916 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007917 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007918 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007919#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007920 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007921}
Fred Draked86ed291999-12-15 15:34:33 +00007922
7923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007924PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007925"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007926Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007927in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007928
7929static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007930posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007931{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007932 abort();
7933 /*NOTREACHED*/
7934 Py_FatalError("abort() called from Python code didn't abort!");
7935 return NULL;
7936}
Fred Drakebec628d1999-12-15 18:31:10 +00007937
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007938#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007939PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007940"startfile(filepath [, operation]) - Start a file with its associated\n\
7941application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007942\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007943When \"operation\" is not specified or \"open\", this acts like\n\
7944double-clicking the file in Explorer, or giving the file name as an\n\
7945argument to the DOS \"start\" command: the file is opened with whatever\n\
7946application (if any) its extension is associated.\n\
7947When another \"operation\" is given, it specifies what should be done with\n\
7948the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007949\n\
7950startfile returns as soon as the associated application is launched.\n\
7951There is no option to wait for the application to close, and no way\n\
7952to retrieve the application's exit status.\n\
7953\n\
7954The filepath is relative to the current directory. If you want to use\n\
7955an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007956the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007957
7958static PyObject *
7959win32_startfile(PyObject *self, PyObject *args)
7960{
7961 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00007962 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007963 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007964#ifdef Py_WIN_WIDE_FILENAMES
7965 if (unicode_file_names()) {
7966 PyObject *unipath, *woperation = NULL;
7967 if (!PyArg_ParseTuple(args, "U|s:startfile",
7968 &unipath, &operation)) {
7969 PyErr_Clear();
7970 goto normal;
7971 }
7972
7973
7974 if (operation) {
7975 woperation = PyUnicode_DecodeASCII(operation,
7976 strlen(operation), NULL);
7977 if (!woperation) {
7978 PyErr_Clear();
7979 operation = NULL;
7980 goto normal;
7981 }
7982 }
7983
7984 Py_BEGIN_ALLOW_THREADS
7985 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7986 PyUnicode_AS_UNICODE(unipath),
7987 NULL, NULL, SW_SHOWNORMAL);
7988 Py_END_ALLOW_THREADS
7989
7990 Py_XDECREF(woperation);
7991 if (rc <= (HINSTANCE)32) {
7992 PyObject *errval = win32_error_unicode("startfile",
7993 PyUnicode_AS_UNICODE(unipath));
7994 return errval;
7995 }
7996 Py_INCREF(Py_None);
7997 return Py_None;
7998 }
7999#endif
8000
8001normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00008002 if (!PyArg_ParseTuple(args, "et|s:startfile",
8003 Py_FileSystemDefaultEncoding, &filepath,
8004 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00008005 return NULL;
8006 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00008007 rc = ShellExecute((HWND)0, operation, filepath,
8008 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00008009 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00008010 if (rc <= (HINSTANCE)32) {
8011 PyObject *errval = win32_error("startfile", filepath);
8012 PyMem_Free(filepath);
8013 return errval;
8014 }
8015 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00008016 Py_INCREF(Py_None);
8017 return Py_None;
8018}
8019#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008020
Martin v. Löwis438b5342002-12-27 10:16:42 +00008021#ifdef HAVE_GETLOADAVG
8022PyDoc_STRVAR(posix_getloadavg__doc__,
8023"getloadavg() -> (float, float, float)\n\n\
8024Return the number of processes in the system run queue averaged over\n\
8025the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8026was unobtainable");
8027
8028static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008029posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008030{
8031 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008032 if (getloadavg(loadavg, 3)!=3) {
8033 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8034 return NULL;
8035 } else
8036 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
8037}
8038#endif
8039
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008040#ifdef MS_WINDOWS
8041
8042PyDoc_STRVAR(win32_urandom__doc__,
8043"urandom(n) -> str\n\n\
8044Return a string of n random bytes suitable for cryptographic use.");
8045
8046typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8047 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8048 DWORD dwFlags );
8049typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8050 BYTE *pbBuffer );
8051
8052static CRYPTGENRANDOM pCryptGenRandom = NULL;
8053static HCRYPTPROV hCryptProv = 0;
8054
Tim Peters4ad82172004-08-30 17:02:04 +00008055static PyObject*
8056win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008057{
Tim Petersd3115382004-08-30 17:36:46 +00008058 int howMany;
8059 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008060
Tim Peters4ad82172004-08-30 17:02:04 +00008061 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00008062 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00008063 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00008064 if (howMany < 0)
8065 return PyErr_Format(PyExc_ValueError,
8066 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008067
Tim Peters4ad82172004-08-30 17:02:04 +00008068 if (hCryptProv == 0) {
8069 HINSTANCE hAdvAPI32 = NULL;
8070 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008071
Tim Peters4ad82172004-08-30 17:02:04 +00008072 /* Obtain handle to the DLL containing CryptoAPI
8073 This should not fail */
8074 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8075 if(hAdvAPI32 == NULL)
8076 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008077
Tim Peters4ad82172004-08-30 17:02:04 +00008078 /* Obtain pointers to the CryptoAPI functions
8079 This will fail on some early versions of Win95 */
8080 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8081 hAdvAPI32,
8082 "CryptAcquireContextA");
8083 if (pCryptAcquireContext == NULL)
8084 return PyErr_Format(PyExc_NotImplementedError,
8085 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008086
Tim Peters4ad82172004-08-30 17:02:04 +00008087 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8088 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00008089 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00008090 return PyErr_Format(PyExc_NotImplementedError,
8091 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008092
Tim Peters4ad82172004-08-30 17:02:04 +00008093 /* Acquire context */
8094 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8095 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8096 return win32_error("CryptAcquireContext", NULL);
8097 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008098
Tim Peters4ad82172004-08-30 17:02:04 +00008099 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00008100 result = PyString_FromStringAndSize(NULL, howMany);
8101 if (result != NULL) {
8102 /* Get random data */
8103 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8104 PyString_AS_STRING(result))) {
8105 Py_DECREF(result);
8106 return win32_error("CryptGenRandom", NULL);
8107 }
Tim Peters4ad82172004-08-30 17:02:04 +00008108 }
Tim Petersd3115382004-08-30 17:36:46 +00008109 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008110}
8111#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008112
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008113#ifdef __VMS
8114/* Use openssl random routine */
8115#include <openssl/rand.h>
8116PyDoc_STRVAR(vms_urandom__doc__,
8117"urandom(n) -> str\n\n\
8118Return a string of n random bytes suitable for cryptographic use.");
8119
8120static PyObject*
8121vms_urandom(PyObject *self, PyObject *args)
8122{
8123 int howMany;
8124 PyObject* result;
8125
8126 /* Read arguments */
8127 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8128 return NULL;
8129 if (howMany < 0)
8130 return PyErr_Format(PyExc_ValueError,
8131 "negative argument not allowed");
8132
8133 /* Allocate bytes */
8134 result = PyString_FromStringAndSize(NULL, howMany);
8135 if (result != NULL) {
8136 /* Get random data */
8137 if (RAND_pseudo_bytes((unsigned char*)
8138 PyString_AS_STRING(result),
8139 howMany) < 0) {
8140 Py_DECREF(result);
8141 return PyErr_Format(PyExc_ValueError,
8142 "RAND_pseudo_bytes");
8143 }
8144 }
8145 return result;
8146}
8147#endif
8148
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008149static PyMethodDef posix_methods[] = {
8150 {"access", posix_access, METH_VARARGS, posix_access__doc__},
8151#ifdef HAVE_TTYNAME
8152 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
8153#endif
8154 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00008155#ifdef HAVE_CHFLAGS
8156 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
8157#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008158 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008159#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008160 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008161#endif /* HAVE_CHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00008162#ifdef HAVE_LCHFLAGS
8163 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
8164#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008165#ifdef HAVE_LCHOWN
8166 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
8167#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008168#ifdef HAVE_CHROOT
8169 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
8170#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008171#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00008172 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008173#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008174#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00008175 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Neal Norwitze241ce82003-02-17 18:17:05 +00008176 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008177#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008178#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008179 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008180#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008181 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8182 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8183 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008184#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008185 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008186#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008187#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008188 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008189#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008190 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8191 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8192 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00008193 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008194#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008195 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008196#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008197#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008198 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008199#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008200 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008201#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00008202 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008203#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008204 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8205 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8206 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008207#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00008208 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008209#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008210 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008211#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008212 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8213 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008214#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008215#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008216 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8217 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008218#if defined(PYOS_OS2)
8219 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8220 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
8221#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008222#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008223#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00008224 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008225#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008226#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00008227 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008228#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008229#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00008230 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008231#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008232#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00008233 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008234#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008235#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008236 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008237#endif /* HAVE_GETEGID */
8238#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008239 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008240#endif /* HAVE_GETEUID */
8241#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008242 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008243#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008244#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00008245 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008246#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008247 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008248#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008249 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008250#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008251#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00008252 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008253#endif /* HAVE_GETPPID */
8254#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008255 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008256#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008257#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00008258 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008259#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008260#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008261 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008262#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008263#ifdef HAVE_KILLPG
8264 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
8265#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008266#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008267 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008268#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008269#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008270 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008271#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008272 {"popen2", win32_popen2, METH_VARARGS},
8273 {"popen3", win32_popen3, METH_VARARGS},
8274 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00008275 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008276#else
8277#if defined(PYOS_OS2) && defined(PYCC_GCC)
8278 {"popen2", os2emx_popen2, METH_VARARGS},
8279 {"popen3", os2emx_popen3, METH_VARARGS},
8280 {"popen4", os2emx_popen4, METH_VARARGS},
8281#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008282#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008283#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008284#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008285 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008286#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008287#ifdef HAVE_SETEUID
8288 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8289#endif /* HAVE_SETEUID */
8290#ifdef HAVE_SETEGID
8291 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8292#endif /* HAVE_SETEGID */
8293#ifdef HAVE_SETREUID
8294 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8295#endif /* HAVE_SETREUID */
8296#ifdef HAVE_SETREGID
8297 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8298#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008299#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008300 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008301#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008302#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00008303 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008304#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008305#ifdef HAVE_GETPGID
8306 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8307#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008308#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008309 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008310#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008311#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00008312 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008313#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008314#ifdef HAVE_WAIT3
8315 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8316#endif /* HAVE_WAIT3 */
8317#ifdef HAVE_WAIT4
8318 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8319#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008320#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008321 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008322#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008323#ifdef HAVE_GETSID
8324 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8325#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008326#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00008327 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008328#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008329#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008330 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008331#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008332#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008333 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008334#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008335#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008336 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008337#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008338 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8339 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8340 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8341 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8342 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8343 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8344 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8345 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8346 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00008347 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008348#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00008349 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008350#endif
8351#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008352 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008353#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008354#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008355 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8356#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008357#ifdef HAVE_DEVICE_MACROS
8358 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8359 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8360 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8361#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008362#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008363 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008364#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008365#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008366 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008367#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008368#ifdef HAVE_UNSETENV
8369 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8370#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00008371#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008372 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00008373#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00008374#ifdef HAVE_FCHDIR
8375 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8376#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008377#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008378 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008379#endif
8380#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008381 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008382#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008383#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008384#ifdef WCOREDUMP
8385 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8386#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008387#ifdef WIFCONTINUED
8388 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8389#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008390#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008391 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008392#endif /* WIFSTOPPED */
8393#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008394 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008395#endif /* WIFSIGNALED */
8396#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008397 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008398#endif /* WIFEXITED */
8399#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008400 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008401#endif /* WEXITSTATUS */
8402#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008403 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008404#endif /* WTERMSIG */
8405#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008406 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008407#endif /* WSTOPSIG */
8408#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008409#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008410 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008411#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008412#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008413 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008414#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008415#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00008416 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008417#endif
8418#ifdef HAVE_TEMPNAM
8419 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8420#endif
8421#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00008422 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008423#endif
Fred Drakec9680921999-12-13 16:37:25 +00008424#ifdef HAVE_CONFSTR
8425 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8426#endif
8427#ifdef HAVE_SYSCONF
8428 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8429#endif
8430#ifdef HAVE_FPATHCONF
8431 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8432#endif
8433#ifdef HAVE_PATHCONF
8434 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8435#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008436 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008437#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00008438 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8439#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008440#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00008441 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008442#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008443 #ifdef MS_WINDOWS
8444 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8445 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008446 #ifdef __VMS
8447 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
8448 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008449 {NULL, NULL} /* Sentinel */
8450};
8451
8452
Barry Warsaw4a342091996-12-19 23:50:02 +00008453static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008454ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008455{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008456 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008457}
8458
Guido van Rossumd48f2521997-12-05 22:19:34 +00008459#if defined(PYOS_OS2)
8460/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008461static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008462{
8463 APIRET rc;
8464 ULONG values[QSV_MAX+1];
8465 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008466 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008467
8468 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008469 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008470 Py_END_ALLOW_THREADS
8471
8472 if (rc != NO_ERROR) {
8473 os2_error(rc);
8474 return -1;
8475 }
8476
Fred Drake4d1e64b2002-04-15 19:40:07 +00008477 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8478 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8479 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8480 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8481 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8482 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8483 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008484
8485 switch (values[QSV_VERSION_MINOR]) {
8486 case 0: ver = "2.00"; break;
8487 case 10: ver = "2.10"; break;
8488 case 11: ver = "2.11"; break;
8489 case 30: ver = "3.00"; break;
8490 case 40: ver = "4.00"; break;
8491 case 50: ver = "5.00"; break;
8492 default:
Tim Peters885d4572001-11-28 20:27:42 +00008493 PyOS_snprintf(tmp, sizeof(tmp),
8494 "%d-%d", values[QSV_VERSION_MAJOR],
8495 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008496 ver = &tmp[0];
8497 }
8498
8499 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008500 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008501 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008502
8503 /* Add Indicator of Which Drive was Used to Boot the System */
8504 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8505 tmp[1] = ':';
8506 tmp[2] = '\0';
8507
Fred Drake4d1e64b2002-04-15 19:40:07 +00008508 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008509}
8510#endif
8511
Barry Warsaw4a342091996-12-19 23:50:02 +00008512static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008513all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008514{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008515#ifdef F_OK
8516 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008517#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008518#ifdef R_OK
8519 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008520#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008521#ifdef W_OK
8522 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008523#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008524#ifdef X_OK
8525 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008526#endif
Fred Drakec9680921999-12-13 16:37:25 +00008527#ifdef NGROUPS_MAX
8528 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8529#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008530#ifdef TMP_MAX
8531 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8532#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008533#ifdef WCONTINUED
8534 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8535#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008536#ifdef WNOHANG
8537 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008538#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008539#ifdef WUNTRACED
8540 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8541#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008542#ifdef O_RDONLY
8543 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8544#endif
8545#ifdef O_WRONLY
8546 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8547#endif
8548#ifdef O_RDWR
8549 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8550#endif
8551#ifdef O_NDELAY
8552 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8553#endif
8554#ifdef O_NONBLOCK
8555 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8556#endif
8557#ifdef O_APPEND
8558 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8559#endif
8560#ifdef O_DSYNC
8561 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8562#endif
8563#ifdef O_RSYNC
8564 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8565#endif
8566#ifdef O_SYNC
8567 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8568#endif
8569#ifdef O_NOCTTY
8570 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8571#endif
8572#ifdef O_CREAT
8573 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8574#endif
8575#ifdef O_EXCL
8576 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8577#endif
8578#ifdef O_TRUNC
8579 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8580#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008581#ifdef O_BINARY
8582 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8583#endif
8584#ifdef O_TEXT
8585 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8586#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008587#ifdef O_LARGEFILE
8588 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8589#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008590#ifdef O_SHLOCK
8591 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8592#endif
8593#ifdef O_EXLOCK
8594 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8595#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008596
Tim Peters5aa91602002-01-30 05:46:57 +00008597/* MS Windows */
8598#ifdef O_NOINHERIT
8599 /* Don't inherit in child processes. */
8600 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8601#endif
8602#ifdef _O_SHORT_LIVED
8603 /* Optimize for short life (keep in memory). */
8604 /* MS forgot to define this one with a non-underscore form too. */
8605 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8606#endif
8607#ifdef O_TEMPORARY
8608 /* Automatically delete when last handle is closed. */
8609 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8610#endif
8611#ifdef O_RANDOM
8612 /* Optimize for random access. */
8613 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8614#endif
8615#ifdef O_SEQUENTIAL
8616 /* Optimize for sequential access. */
8617 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8618#endif
8619
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008620/* GNU extensions. */
8621#ifdef O_DIRECT
8622 /* Direct disk access. */
8623 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8624#endif
8625#ifdef O_DIRECTORY
8626 /* Must be a directory. */
8627 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8628#endif
8629#ifdef O_NOFOLLOW
8630 /* Do not follow links. */
8631 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8632#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008633
Barry Warsaw5676bd12003-01-07 20:57:09 +00008634 /* These come from sysexits.h */
8635#ifdef EX_OK
8636 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008637#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008638#ifdef EX_USAGE
8639 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008640#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008641#ifdef EX_DATAERR
8642 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008643#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008644#ifdef EX_NOINPUT
8645 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008646#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008647#ifdef EX_NOUSER
8648 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008649#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008650#ifdef EX_NOHOST
8651 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008652#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008653#ifdef EX_UNAVAILABLE
8654 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008655#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008656#ifdef EX_SOFTWARE
8657 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008658#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008659#ifdef EX_OSERR
8660 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008661#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008662#ifdef EX_OSFILE
8663 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008664#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008665#ifdef EX_CANTCREAT
8666 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008667#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008668#ifdef EX_IOERR
8669 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008670#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008671#ifdef EX_TEMPFAIL
8672 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008673#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008674#ifdef EX_PROTOCOL
8675 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008676#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008677#ifdef EX_NOPERM
8678 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008679#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008680#ifdef EX_CONFIG
8681 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008682#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008683#ifdef EX_NOTFOUND
8684 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008685#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008686
Guido van Rossum246bc171999-02-01 23:54:31 +00008687#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008688#if defined(PYOS_OS2) && defined(PYCC_GCC)
8689 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8690 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8691 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8692 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8693 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8694 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8695 if (ins(d, "P_PM", (long)P_PM)) return -1;
8696 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8697 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8698 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8699 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8700 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8701 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8702 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8703 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8704 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8705 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8706 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8707 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8708 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8709#else
Guido van Rossum7d385291999-02-16 19:38:04 +00008710 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8711 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8712 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8713 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8714 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008715#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008716#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008717
Guido van Rossumd48f2521997-12-05 22:19:34 +00008718#if defined(PYOS_OS2)
8719 if (insertvalues(d)) return -1;
8720#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008721 return 0;
8722}
8723
8724
Tim Peters5aa91602002-01-30 05:46:57 +00008725#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008726#define INITFUNC initnt
8727#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008728
8729#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008730#define INITFUNC initos2
8731#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008732
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008733#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008734#define INITFUNC initposix
8735#define MODNAME "posix"
8736#endif
8737
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008738PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008739INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008740{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008741 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008742
Fred Drake4d1e64b2002-04-15 19:40:07 +00008743 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008744 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008745 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00008746 if (m == NULL)
8747 return;
Tim Peters5aa91602002-01-30 05:46:57 +00008748
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008749 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008750 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00008751 Py_XINCREF(v);
8752 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008753 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00008754 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008755
Fred Drake4d1e64b2002-04-15 19:40:07 +00008756 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00008757 return;
8758
Fred Drake4d1e64b2002-04-15 19:40:07 +00008759 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00008760 return;
8761
Fred Drake4d1e64b2002-04-15 19:40:07 +00008762 Py_INCREF(PyExc_OSError);
8763 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008764
Guido van Rossumb3d39562000-01-31 18:41:26 +00008765#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00008766 if (posix_putenv_garbage == NULL)
8767 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008768#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008769
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008770 if (!initialized) {
8771 stat_result_desc.name = MODNAME ".stat_result";
8772 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8773 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8774 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8775 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8776 structseq_new = StatResultType.tp_new;
8777 StatResultType.tp_new = statresult_new;
8778
8779 statvfs_result_desc.name = MODNAME ".statvfs_result";
8780 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
8781 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008782 Py_INCREF((PyObject*) &StatResultType);
8783 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00008784 Py_INCREF((PyObject*) &StatVFSResultType);
8785 PyModule_AddObject(m, "statvfs_result",
8786 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008787 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008788
8789#ifdef __APPLE__
8790 /*
8791 * Step 2 of weak-linking support on Mac OS X.
8792 *
8793 * The code below removes functions that are not available on the
8794 * currently active platform.
8795 *
8796 * This block allow one to use a python binary that was build on
8797 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8798 * OSX 10.4.
8799 */
8800#ifdef HAVE_FSTATVFS
8801 if (fstatvfs == NULL) {
8802 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8803 return;
8804 }
8805 }
8806#endif /* HAVE_FSTATVFS */
8807
8808#ifdef HAVE_STATVFS
8809 if (statvfs == NULL) {
8810 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8811 return;
8812 }
8813 }
8814#endif /* HAVE_STATVFS */
8815
8816# ifdef HAVE_LCHOWN
8817 if (lchown == NULL) {
8818 if (PyObject_DelAttrString(m, "lchown") == -1) {
8819 return;
8820 }
8821 }
8822#endif /* HAVE_LCHOWN */
8823
8824
8825#endif /* __APPLE__ */
8826
Guido van Rossumb6775db1994-08-01 11:34:53 +00008827}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008828
8829#ifdef __cplusplus
8830}
8831#endif