blob: 31d7c6181723d4b1c1aae2dbf444cb153fbacd4e [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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000016#include "Python.h"
17#include "structseq.h"
18
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000019#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000020# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000021#endif /* defined(__VMS) */
22
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000023PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000024"This module provides access to operating system functionality that is\n\
25standardized by the C Standard and the POSIX standard (a thinly\n\
26disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000027corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000029#ifndef Py_USING_UNICODE
30/* This is used in signatures of functions. */
31#define Py_UNICODE void
32#endif
33
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000034#if defined(PYOS_OS2)
35#define INCL_DOS
36#define INCL_DOSERRORS
37#define INCL_DOSPROCESS
38#define INCL_NOPMAPI
39#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000040#if defined(PYCC_GCC)
41#include <ctype.h>
42#include <io.h>
43#include <stdio.h>
44#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000045#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000046#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000047#endif
48
Guido van Rossumb6775db1994-08-01 11:34:53 +000049#include <sys/types.h>
50#include <sys/stat.h>
Guido van Rossuma6535fd2001-10-18 19:44:10 +000051
Guido van Rossum36bc6801995-06-14 22:54:23 +000052#ifdef HAVE_SYS_WAIT_H
53#include <sys/wait.h> /* For WNOHANG */
54#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000055
Guido van Rossuma376cc51996-12-05 23:43:35 +000056#include <signal.h>
Guido van Rossuma376cc51996-12-05 23:43:35 +000057
Guido van Rossumb6775db1994-08-01 11:34:53 +000058#ifdef HAVE_FCNTL_H
59#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000060#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000061
Guido van Rossuma6535fd2001-10-18 19:44:10 +000062#ifdef HAVE_GRP_H
63#include <grp.h>
64#endif
65
Barry Warsaw5676bd12003-01-07 20:57:09 +000066#ifdef HAVE_SYSEXITS_H
67#include <sysexits.h>
68#endif /* HAVE_SYSEXITS_H */
69
Anthony Baxter8a560de2004-10-13 15:30:56 +000070#ifdef HAVE_SYS_LOADAVG_H
71#include <sys/loadavg.h>
72#endif
73
Guido van Rossuma4916fa1996-05-23 22:58:55 +000074/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000075/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000076#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000077#include <process.h>
78#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000079#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000080#define HAVE_GETCWD 1
81#define HAVE_OPENDIR 1
82#define HAVE_SYSTEM 1
83#if defined(__OS2__)
84#define HAVE_EXECV 1
85#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000086#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000087#include <process.h>
88#else
89#ifdef __BORLANDC__ /* Borland compiler */
90#define HAVE_EXECV 1
91#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000092#define HAVE_OPENDIR 1
93#define HAVE_PIPE 1
94#define HAVE_POPEN 1
95#define HAVE_SYSTEM 1
96#define HAVE_WAIT 1
97#else
98#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +000099#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000100#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000101#define HAVE_EXECV 1
102#define HAVE_PIPE 1
103#define HAVE_POPEN 1
104#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000105#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000106#define HAVE_FSYNC 1
107#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000108#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000109#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
110/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000111#else /* all other compilers */
112/* Unix functions that the configure script doesn't check for */
113#define HAVE_EXECV 1
114#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000115#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
116#define HAVE_FORK1 1
117#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000118#define HAVE_GETCWD 1
119#define HAVE_GETEGID 1
120#define HAVE_GETEUID 1
121#define HAVE_GETGID 1
122#define HAVE_GETPPID 1
123#define HAVE_GETUID 1
124#define HAVE_KILL 1
125#define HAVE_OPENDIR 1
126#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000127#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000128#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000129#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000130#define HAVE_SYSTEM 1
131#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000132#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000133#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#endif /* _MSC_VER */
135#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000136#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000137#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000138
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000139#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000140
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000141#if defined(__sgi)&&_COMPILER_VERSION>=700
142/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
143 (default) */
144extern char *ctermid_r(char *);
145#endif
146
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000147#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000148#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000149extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000150#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000151#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000152extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000154extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000155#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000156#endif
157#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000158extern int chdir(char *);
159extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000160#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000161extern int chdir(const char *);
162extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000163#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000164#ifdef __BORLANDC__
165extern int chmod(const char *, int);
166#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000167extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000168#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int chown(const char *, uid_t, gid_t);
170extern char *getcwd(char *, int);
171extern char *strerror(int);
172extern int link(const char *, const char *);
173extern int rename(const char *, const char *);
174extern int stat(const char *, struct stat *);
175extern int unlink(const char *);
176extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000177#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000178extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000179#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000180#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000181extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000182#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000184
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000185#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186
Guido van Rossumb6775db1994-08-01 11:34:53 +0000187#ifdef HAVE_UTIME_H
188#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000189#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000190
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000191#ifdef HAVE_SYS_UTIME_H
192#include <sys/utime.h>
193#define HAVE_UTIME_H /* pretend we do for the rest of this file */
194#endif /* HAVE_SYS_UTIME_H */
195
Guido van Rossumb6775db1994-08-01 11:34:53 +0000196#ifdef HAVE_SYS_TIMES_H
197#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000198#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199
200#ifdef HAVE_SYS_PARAM_H
201#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000202#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203
204#ifdef HAVE_SYS_UTSNAME_H
205#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000206#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000208#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000210#define NAMLEN(dirent) strlen((dirent)->d_name)
211#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000212#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000213#include <direct.h>
214#define NAMLEN(dirent) strlen((dirent)->d_name)
215#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000217#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000218#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000219#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000221#endif
222#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000224#endif
225#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000227#endif
228#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000230#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231#include <direct.h>
232#include <io.h>
233#include <process.h>
Tim Petersbc2e10e2002-03-03 23:17:02 +0000234#include "osdefs.h"
Martin v. Löwisdc3883f2004-08-29 15:46:35 +0000235#define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000236#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000237#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000239#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000240#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241
Guido van Rossumd48f2521997-12-05 22:19:34 +0000242#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000244#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245
Tim Petersbc2e10e2002-03-03 23:17:02 +0000246#ifndef MAXPATHLEN
247#define MAXPATHLEN 1024
248#endif /* MAXPATHLEN */
249
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000250#ifdef UNION_WAIT
251/* Emulate some macros on systems that have a union instead of macros */
252
253#ifndef WIFEXITED
254#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
255#endif
256
257#ifndef WEXITSTATUS
258#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
259#endif
260
261#ifndef WTERMSIG
262#define WTERMSIG(u_wait) ((u_wait).w_termsig)
263#endif
264
265#endif /* UNION_WAIT */
266
Greg Wardb48bc172000-03-01 21:51:56 +0000267/* Don't use the "_r" form if we don't need it (also, won't have a
268 prototype for it, at least on Solaris -- maybe others as well?). */
269#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
270#define USE_CTERMID_R
271#endif
272
273#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
274#define USE_TMPNAM_R
275#endif
276
Fred Drake699f3522000-06-29 21:12:41 +0000277/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000278#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000279#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +0000280# define STAT _stati64
281# define FSTAT _fstati64
282# define STRUCT_STAT struct _stati64
283#else
284# define STAT stat
285# define FSTAT fstat
286# define STRUCT_STAT struct stat
287#endif
288
Tim Peters11b23062003-04-23 02:39:17 +0000289#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000290#include <sys/mkdev.h>
291#else
292#if defined(MAJOR_IN_SYSMACROS)
293#include <sys/sysmacros.h>
294#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000295#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
296#include <sys/mkdev.h>
297#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000298#endif
Fred Drake699f3522000-06-29 21:12:41 +0000299
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000301#ifdef WITH_NEXT_FRAMEWORK
302/* On Darwin/MacOSX a shared library or framework has no access to
303** environ directly, we must obtain it with _NSGetEnviron().
304*/
305#include <crt_externs.h>
306static char **environ;
307#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000309#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310
Barry Warsaw53699e91996-12-10 23:23:01 +0000311static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000312convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313{
Barry Warsaw53699e91996-12-10 23:23:01 +0000314 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000316 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317 if (d == NULL)
318 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000319#ifdef WITH_NEXT_FRAMEWORK
320 if (environ == NULL)
321 environ = *_NSGetEnviron();
322#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000323 if (environ == NULL)
324 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000325 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000327 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000328 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329 char *p = strchr(*e, '=');
330 if (p == NULL)
331 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000332 k = PyString_FromStringAndSize(*e, (int)(p-*e));
333 if (k == NULL) {
334 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000336 }
337 v = PyString_FromString(p+1);
338 if (v == NULL) {
339 PyErr_Clear();
340 Py_DECREF(k);
341 continue;
342 }
343 if (PyDict_GetItem(d, k) == NULL) {
344 if (PyDict_SetItem(d, k, v) != 0)
345 PyErr_Clear();
346 }
347 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000348 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000350#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000351 {
352 APIRET rc;
353 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
354
355 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000356 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000357 PyObject *v = PyString_FromString(buffer);
358 PyDict_SetItemString(d, "BEGINLIBPATH", v);
359 Py_DECREF(v);
360 }
361 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
362 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
363 PyObject *v = PyString_FromString(buffer);
364 PyDict_SetItemString(d, "ENDLIBPATH", v);
365 Py_DECREF(v);
366 }
367 }
368#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369 return d;
370}
371
372
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373/* Set a POSIX-specific error from errno, and return NULL */
374
Barry Warsawd58d7641998-07-23 16:14:40 +0000375static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000376posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000377{
Barry Warsawca74da41999-02-09 19:31:45 +0000378 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000379}
Barry Warsawd58d7641998-07-23 16:14:40 +0000380static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000381posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000382{
Barry Warsawca74da41999-02-09 19:31:45 +0000383 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000384}
385
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000386#ifdef Py_WIN_WIDE_FILENAMES
387static PyObject *
388posix_error_with_unicode_filename(Py_UNICODE* name)
389{
390 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
391}
392#endif /* Py_WIN_WIDE_FILENAMES */
393
394
Mark Hammondef8b6542001-05-13 08:04:26 +0000395static PyObject *
396posix_error_with_allocated_filename(char* name)
397{
398 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
399 PyMem_Free(name);
400 return rc;
401}
402
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000403#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000404static PyObject *
405win32_error(char* function, char* filename)
406{
Mark Hammond33a6da92000-08-15 00:46:38 +0000407 /* XXX We should pass the function name along in the future.
408 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000409 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000410 Windows error object, which is non-trivial.
411 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000412 errno = GetLastError();
413 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000414 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000415 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000416 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000417}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000418
419#ifdef Py_WIN_WIDE_FILENAMES
420static PyObject *
421win32_error_unicode(char* function, Py_UNICODE* filename)
422{
423 /* XXX - see win32_error for comments on 'function' */
424 errno = GetLastError();
425 if (filename)
426 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
427 else
428 return PyErr_SetFromWindowsErr(errno);
429}
430
431static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
432{
433 /* XXX Perhaps we should make this API an alias of
434 PyObject_Unicode() instead ?! */
435 if (PyUnicode_CheckExact(obj)) {
436 Py_INCREF(obj);
437 return obj;
438 }
439 if (PyUnicode_Check(obj)) {
440 /* For a Unicode subtype that's not a Unicode object,
441 return a true Unicode object with the same data. */
442 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
443 PyUnicode_GET_SIZE(obj));
444 }
Tim Peters11b23062003-04-23 02:39:17 +0000445 return PyUnicode_FromEncodedObject(obj,
446 Py_FileSystemDefaultEncoding,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000447 "strict");
448}
449
450#endif /* Py_WIN_WIDE_FILENAMES */
451
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000452#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453
Guido van Rossumd48f2521997-12-05 22:19:34 +0000454#if defined(PYOS_OS2)
455/**********************************************************************
456 * Helper Function to Trim and Format OS/2 Messages
457 **********************************************************************/
458 static void
459os2_formatmsg(char *msgbuf, int msglen, char *reason)
460{
461 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
462
463 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
464 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
465
466 while (lastc > msgbuf && isspace(*lastc))
467 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
468 }
469
470 /* Add Optional Reason Text */
471 if (reason) {
472 strcat(msgbuf, " : ");
473 strcat(msgbuf, reason);
474 }
475}
476
477/**********************************************************************
478 * Decode an OS/2 Operating System Error Code
479 *
480 * A convenience function to lookup an OS/2 error code and return a
481 * text message we can use to raise a Python exception.
482 *
483 * Notes:
484 * The messages for errors returned from the OS/2 kernel reside in
485 * the file OSO001.MSG in the \OS2 directory hierarchy.
486 *
487 **********************************************************************/
488 static char *
489os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
490{
491 APIRET rc;
492 ULONG msglen;
493
494 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
495 Py_BEGIN_ALLOW_THREADS
496 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
497 errorcode, "oso001.msg", &msglen);
498 Py_END_ALLOW_THREADS
499
500 if (rc == NO_ERROR)
501 os2_formatmsg(msgbuf, msglen, reason);
502 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000503 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000504 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000505
506 return msgbuf;
507}
508
509/* Set an OS/2-specific error and return NULL. OS/2 kernel
510 errors are not in a global variable e.g. 'errno' nor are
511 they congruent with posix error numbers. */
512
513static PyObject * os2_error(int code)
514{
515 char text[1024];
516 PyObject *v;
517
518 os2_strerror(text, sizeof(text), code, "");
519
520 v = Py_BuildValue("(is)", code, text);
521 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000522 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000523 Py_DECREF(v);
524 }
525 return NULL; /* Signal to Python that an Exception is Pending */
526}
527
528#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000529
530/* POSIX generic methods */
531
Barry Warsaw53699e91996-12-10 23:23:01 +0000532static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000533posix_fildes(PyObject *fdobj, int (*func)(int))
534{
535 int fd;
536 int res;
537 fd = PyObject_AsFileDescriptor(fdobj);
538 if (fd < 0)
539 return NULL;
540 Py_BEGIN_ALLOW_THREADS
541 res = (*func)(fd);
542 Py_END_ALLOW_THREADS
543 if (res < 0)
544 return posix_error();
545 Py_INCREF(Py_None);
546 return Py_None;
547}
Guido van Rossum21142a01999-01-08 21:05:37 +0000548
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000549#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000550static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000551unicode_file_names(void)
552{
553 static int canusewide = -1;
554 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000555 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000556 the Windows NT family. */
557 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
558 }
559 return canusewide;
560}
561#endif
Tim Peters11b23062003-04-23 02:39:17 +0000562
Guido van Rossum21142a01999-01-08 21:05:37 +0000563static PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000564posix_1str(PyObject *args, char *format, int (*func)(const char*),
565 char *wformat, int (*wfunc)(const Py_UNICODE*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566{
Mark Hammondef8b6542001-05-13 08:04:26 +0000567 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000568 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000569#ifdef Py_WIN_WIDE_FILENAMES
570 if (unicode_file_names()) {
571 PyUnicodeObject *po;
572 if (PyArg_ParseTuple(args, wformat, &po)) {
573 Py_BEGIN_ALLOW_THREADS
574 /* PyUnicode_AS_UNICODE OK without thread
575 lock as it is a simple dereference. */
576 res = (*wfunc)(PyUnicode_AS_UNICODE(po));
577 Py_END_ALLOW_THREADS
578 if (res < 0)
Mark Hammondd3890362002-10-03 07:24:48 +0000579 return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000580 Py_INCREF(Py_None);
581 return Py_None;
582 }
583 /* Drop the argument parsing error as narrow
584 strings are also valid. */
585 PyErr_Clear();
586 }
587#else
588 /* Platforms that don't support Unicode filenames
589 shouldn't be passing these extra params */
590 assert(wformat==NULL && wfunc == NULL);
591#endif
592
Tim Peters5aa91602002-01-30 05:46:57 +0000593 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000594 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000595 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000596 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000597 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000598 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000599 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000600 return posix_error_with_allocated_filename(path1);
601 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000602 Py_INCREF(Py_None);
603 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604}
605
Barry Warsaw53699e91996-12-10 23:23:01 +0000606static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000607posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000608 char *format,
609 int (*func)(const char *, const char *),
610 char *wformat,
611 int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000612{
Mark Hammondef8b6542001-05-13 08:04:26 +0000613 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000614 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000615#ifdef Py_WIN_WIDE_FILENAMES
616 if (unicode_file_names()) {
617 PyObject *po1;
618 PyObject *po2;
619 if (PyArg_ParseTuple(args, wformat, &po1, &po2)) {
620 if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) {
621 PyObject *wpath1;
622 PyObject *wpath2;
623 wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1);
624 wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2);
625 if (!wpath1 || !wpath2) {
626 Py_XDECREF(wpath1);
627 Py_XDECREF(wpath2);
628 return NULL;
629 }
630 Py_BEGIN_ALLOW_THREADS
631 /* PyUnicode_AS_UNICODE OK without thread
632 lock as it is a simple dereference. */
633 res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1),
634 PyUnicode_AS_UNICODE(wpath2));
635 Py_END_ALLOW_THREADS
636 Py_XDECREF(wpath1);
637 Py_XDECREF(wpath2);
638 if (res != 0)
639 return posix_error();
640 Py_INCREF(Py_None);
641 return Py_None;
642 }
643 /* Else flow through as neither is Unicode. */
644 }
645 /* Drop the argument parsing error as narrow
646 strings are also valid. */
647 PyErr_Clear();
648 }
649#else
650 /* Platforms that don't support Unicode filenames
651 shouldn't be passing these extra params */
652 assert(wformat==NULL && wfunc == NULL);
653#endif
654
Mark Hammondef8b6542001-05-13 08:04:26 +0000655 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000656 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000657 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000658 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000659 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000660 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000661 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000662 PyMem_Free(path1);
663 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000664 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000665 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000666 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000667 Py_INCREF(Py_None);
668 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000669}
670
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000671PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000672"stat_result: Result from stat or lstat.\n\n\
673This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000674 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000675or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
676\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000677Posix/windows: If your platform supports st_blksize, st_blocks, or st_rdev,\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000678they are available as attributes only.\n\
679\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000680See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000681
682static PyStructSequence_Field stat_result_fields[] = {
683 {"st_mode", "protection bits"},
684 {"st_ino", "inode"},
685 {"st_dev", "device"},
686 {"st_nlink", "number of hard links"},
687 {"st_uid", "user ID of owner"},
688 {"st_gid", "group ID of owner"},
689 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000690 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
691 {NULL, "integer time of last access"},
692 {NULL, "integer time of last modification"},
693 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000694 {"st_atime", "time of last access"},
695 {"st_mtime", "time of last modification"},
696 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000697#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000698 {"st_blksize", "blocksize for filesystem I/O"},
699#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000700#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000701 {"st_blocks", "number of blocks allocated"},
702#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000703#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000704 {"st_rdev", "device type (if inode device)"},
705#endif
706 {0}
707};
708
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000709#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000710#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000711#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000712#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000713#endif
714
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000715#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000716#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
717#else
718#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
719#endif
720
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000721#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000722#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
723#else
724#define ST_RDEV_IDX ST_BLOCKS_IDX
725#endif
726
727static PyStructSequence_Desc stat_result_desc = {
728 "stat_result", /* name */
729 stat_result__doc__, /* doc */
730 stat_result_fields,
731 10
732};
733
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000734PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000735"statvfs_result: Result from statvfs or fstatvfs.\n\n\
736This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000737 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000738or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000739\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000740See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000741
742static PyStructSequence_Field statvfs_result_fields[] = {
743 {"f_bsize", },
744 {"f_frsize", },
745 {"f_blocks", },
746 {"f_bfree", },
747 {"f_bavail", },
748 {"f_files", },
749 {"f_ffree", },
750 {"f_favail", },
751 {"f_flag", },
752 {"f_namemax",},
753 {0}
754};
755
756static PyStructSequence_Desc statvfs_result_desc = {
757 "statvfs_result", /* name */
758 statvfs_result__doc__, /* doc */
759 statvfs_result_fields,
760 10
761};
762
763static PyTypeObject StatResultType;
764static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000765static newfunc structseq_new;
766
767static PyObject *
768statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
769{
770 PyStructSequence *result;
771 int i;
772
773 result = (PyStructSequence*)structseq_new(type, args, kwds);
774 if (!result)
775 return NULL;
776 /* If we have been initialized from a tuple,
777 st_?time might be set to None. Initialize it
778 from the int slots. */
779 for (i = 7; i <= 9; i++) {
780 if (result->ob_item[i+3] == Py_None) {
781 Py_DECREF(Py_None);
782 Py_INCREF(result->ob_item[i]);
783 result->ob_item[i+3] = result->ob_item[i];
784 }
785 }
786 return (PyObject*)result;
787}
788
789
790
791/* If true, st_?time is float. */
792static int _stat_float_times = 0;
793
794PyDoc_STRVAR(stat_float_times__doc__,
795"stat_float_times([newval]) -> oldval\n\n\
796Determine whether os.[lf]stat represents time stamps as float objects.\n\
797If newval is True, future calls to stat() return floats, if it is False,\n\
798future calls return ints. \n\
799If newval is omitted, return the current setting.\n");
800
801static PyObject*
802stat_float_times(PyObject* self, PyObject *args)
803{
804 int newval = -1;
805 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
806 return NULL;
807 if (newval == -1)
808 /* Return old value */
809 return PyBool_FromLong(_stat_float_times);
810 _stat_float_times = newval;
811 Py_INCREF(Py_None);
812 return Py_None;
813}
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000814
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000815static void
816fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
817{
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000818 PyObject *fval,*ival;
819#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000820 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000821#else
822 ival = PyInt_FromLong((long)sec);
823#endif
824 if (_stat_float_times) {
825 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
826 } else {
827 fval = ival;
828 Py_INCREF(fval);
829 }
830 PyStructSequence_SET_ITEM(v, index, ival);
831 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000832}
833
Tim Peters5aa91602002-01-30 05:46:57 +0000834/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +0000835 (used by posix_stat() and posix_fstat()) */
836static PyObject*
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000837_pystat_fromstructstat(STRUCT_STAT st)
Fred Drake699f3522000-06-29 21:12:41 +0000838{
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000839 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000840 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +0000841 if (v == NULL)
842 return NULL;
843
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000844 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode));
Fred Drake699f3522000-06-29 21:12:41 +0000845#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000846 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000847 PyLong_FromLongLong((PY_LONG_LONG)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000848#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000849 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000850#endif
851#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +0000852 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000853 PyLong_FromLongLong((PY_LONG_LONG)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000854#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000855 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000856#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000857 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink));
858 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid));
859 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid));
Fred Drake699f3522000-06-29 21:12:41 +0000860#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000861 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000862 PyLong_FromLongLong((PY_LONG_LONG)st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000863#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000864 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000865#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000866
867#ifdef HAVE_STAT_TV_NSEC
868 ansec = st.st_atim.tv_nsec;
869 mnsec = st.st_mtim.tv_nsec;
870 cnsec = st.st_ctim.tv_nsec;
Fred Drake699f3522000-06-29 21:12:41 +0000871#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000872 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000873#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000874 fill_time(v, 7, st.st_atime, ansec);
875 fill_time(v, 8, st.st_mtime, mnsec);
876 fill_time(v, 9, st.st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000877
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000878#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +0000879 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000880 PyInt_FromLong((long)st.st_blksize));
881#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000882#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +0000883 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000884 PyInt_FromLong((long)st.st_blocks));
885#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000886#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000887 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
888 PyInt_FromLong((long)st.st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +0000889#endif
890
891 if (PyErr_Occurred()) {
892 Py_DECREF(v);
893 return NULL;
894 }
895
896 return v;
897}
898
Martin v. Löwisd8948722004-06-02 09:57:56 +0000899#ifdef MS_WINDOWS
900
901/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
902 where / can be used in place of \ and the trailing slash is optional.
903 Both SERVER and SHARE must have at least one character.
904*/
905
906#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
907#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
908#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
909
Tim Peters4ad82172004-08-30 17:02:04 +0000910static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +0000911IsUNCRootA(char *path, int pathlen)
912{
913 #define ISSLASH ISSLASHA
914
915 int i, share;
916
917 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
918 /* minimum UNCRoot is \\x\y */
919 return FALSE;
920 for (i = 2; i < pathlen ; i++)
921 if (ISSLASH(path[i])) break;
922 if (i == 2 || i == pathlen)
923 /* do not allow \\\SHARE or \\SERVER */
924 return FALSE;
925 share = i+1;
926 for (i = share; i < pathlen; i++)
927 if (ISSLASH(path[i])) break;
928 return (i != share && (i == pathlen || i == pathlen-1));
929
930 #undef ISSLASH
931}
932
933#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +0000934static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +0000935IsUNCRootW(Py_UNICODE *path, int pathlen)
936{
937 #define ISSLASH ISSLASHW
938
939 int i, share;
940
941 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
942 /* minimum UNCRoot is \\x\y */
943 return FALSE;
944 for (i = 2; i < pathlen ; i++)
945 if (ISSLASH(path[i])) break;
946 if (i == 2 || i == pathlen)
947 /* do not allow \\\SHARE or \\SERVER */
948 return FALSE;
949 share = i+1;
950 for (i = share; i < pathlen; i++)
951 if (ISSLASH(path[i])) break;
952 return (i != share && (i == pathlen || i == pathlen-1));
953
954 #undef ISSLASH
955}
956#endif /* Py_WIN_WIDE_FILENAMES */
957#endif /* MS_WINDOWS */
958
Barry Warsaw53699e91996-12-10 23:23:01 +0000959static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000960posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000961 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000962#ifdef __VMS
963 int (*statfunc)(const char *, STRUCT_STAT *, ...),
964#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000965 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000966#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000967 char *wformat,
968 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000969{
Fred Drake699f3522000-06-29 21:12:41 +0000970 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +0000971 char *path = NULL; /* pass this to stat; do not free() it */
972 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +0000973 int res;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000974
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000975#ifdef MS_WINDOWS
Tim Peters500bd032001-12-19 19:05:01 +0000976 int pathlen;
977 char pathcopy[MAX_PATH];
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000978#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +0000979
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000980
981#ifdef Py_WIN_WIDE_FILENAMES
982 /* If on wide-character-capable OS see if argument
983 is Unicode and if so use wide API. */
984 if (unicode_file_names()) {
985 PyUnicodeObject *po;
986 if (PyArg_ParseTuple(args, wformat, &po)) {
987 Py_UNICODE wpath[MAX_PATH+1];
988 pathlen = wcslen(PyUnicode_AS_UNICODE(po));
989 /* the library call can blow up if the file name is too long! */
990 if (pathlen > MAX_PATH) {
991 errno = ENAMETOOLONG;
992 return posix_error();
993 }
994 wcscpy(wpath, PyUnicode_AS_UNICODE(po));
995 /* Remove trailing slash or backslash, unless it's the current
996 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
997 */
Martin v. Löwisd8948722004-06-02 09:57:56 +0000998 if (pathlen > 0) {
999 if (ISSLASHW(wpath[pathlen-1])) {
1000 /* It does end with a slash -- exempt the root drive cases. */
Tim Peters4ad82172004-08-30 17:02:04 +00001001 if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':') ||
Martin v. Löwisd8948722004-06-02 09:57:56 +00001002 IsUNCRootW(wpath, pathlen))
1003 /* leave it alone */;
1004 else {
1005 /* nuke the trailing backslash */
1006 wpath[pathlen-1] = L'\0';
1007 }
1008 }
Tim Peters4ad82172004-08-30 17:02:04 +00001009 else if (ISSLASHW(wpath[1]) && pathlen < ARRAYSIZE(wpath)-1 &&
Martin v. Löwisd8948722004-06-02 09:57:56 +00001010 IsUNCRootW(wpath, pathlen)) {
1011 /* UNC root w/o trailing slash: add one when there's room */
1012 wpath[pathlen++] = L'\\';
1013 wpath[pathlen] = L'\0';
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001014 }
1015 }
1016 Py_BEGIN_ALLOW_THREADS
1017 /* PyUnicode_AS_UNICODE result OK without
1018 thread lock as it is a simple dereference. */
1019 res = wstatfunc(wpath, &st);
1020 Py_END_ALLOW_THREADS
1021 if (res != 0)
1022 return posix_error_with_unicode_filename(wpath);
1023 return _pystat_fromstructstat(st);
1024 }
1025 /* Drop the argument parsing error as narrow strings
1026 are also valid. */
1027 PyErr_Clear();
1028 }
1029#endif
1030
Tim Peters5aa91602002-01-30 05:46:57 +00001031 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001032 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001033 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001034 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001035
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001036#ifdef MS_WINDOWS
Guido van Rossumace88ae2000-04-21 18:54:45 +00001037 pathlen = strlen(path);
1038 /* the library call can blow up if the file name is too long! */
1039 if (pathlen > MAX_PATH) {
Tim Peters500bd032001-12-19 19:05:01 +00001040 PyMem_Free(pathfree);
Guido van Rossumace88ae2000-04-21 18:54:45 +00001041 errno = ENAMETOOLONG;
1042 return posix_error();
1043 }
1044
Tim Peters500bd032001-12-19 19:05:01 +00001045 /* Remove trailing slash or backslash, unless it's the current
1046 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
1047 */
Martin v. Löwisd8948722004-06-02 09:57:56 +00001048 if (pathlen > 0) {
1049 if (ISSLASHA(path[pathlen-1])) {
1050 /* It does end with a slash -- exempt the root drive cases. */
Tim Peters4ad82172004-08-30 17:02:04 +00001051 if (pathlen == 1 || (pathlen == 3 && path[1] == ':') ||
Martin v. Löwisd8948722004-06-02 09:57:56 +00001052 IsUNCRootA(path, pathlen))
1053 /* leave it alone */;
1054 else {
1055 /* nuke the trailing backslash */
1056 strncpy(pathcopy, path, pathlen);
1057 pathcopy[pathlen-1] = '\0';
1058 path = pathcopy;
1059 }
1060 }
Tim Peters4ad82172004-08-30 17:02:04 +00001061 else if (ISSLASHA(path[1]) && pathlen < ARRAYSIZE(pathcopy)-1 &&
Martin v. Löwisd8948722004-06-02 09:57:56 +00001062 IsUNCRootA(path, pathlen)) {
1063 /* UNC root w/o trailing slash: add one when there's room */
Guido van Rossumace88ae2000-04-21 18:54:45 +00001064 strncpy(pathcopy, path, pathlen);
Martin v. Löwisd8948722004-06-02 09:57:56 +00001065 pathcopy[pathlen++] = '\\';
1066 pathcopy[pathlen] = '\0';
Guido van Rossumace88ae2000-04-21 18:54:45 +00001067 path = pathcopy;
1068 }
1069 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001070#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +00001071
Barry Warsaw53699e91996-12-10 23:23:01 +00001072 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001073 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001074 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001075 if (res != 0)
Tim Peters500bd032001-12-19 19:05:01 +00001076 return posix_error_with_allocated_filename(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +00001077
Tim Peters500bd032001-12-19 19:05:01 +00001078 PyMem_Free(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +00001079 return _pystat_fromstructstat(st);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001080}
1081
1082
1083/* POSIX methods */
1084
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001085PyDoc_STRVAR(posix_access__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001086"access(path, mode) -> 1 if granted, 0 otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001087Use the real uid/gid to test for access to a path. Note that most\n\
1088operations will use the effective uid/gid, therefore this routine can\n\
1089be used in a suid/sgid environment to test if the invoking user has the\n\
1090specified access to the path. The mode argument can be F_OK to test\n\
1091existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001092
1093static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001094posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001095{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001096 char *path;
1097 int mode;
1098 int res;
1099
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001100#ifdef Py_WIN_WIDE_FILENAMES
1101 if (unicode_file_names()) {
1102 PyUnicodeObject *po;
1103 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1104 Py_BEGIN_ALLOW_THREADS
1105 /* PyUnicode_AS_UNICODE OK without thread lock as
1106 it is a simple dereference. */
1107 res = _waccess(PyUnicode_AS_UNICODE(po), mode);
1108 Py_END_ALLOW_THREADS
1109 return(PyBool_FromLong(res == 0));
1110 }
1111 /* Drop the argument parsing error as narrow strings
1112 are also valid. */
1113 PyErr_Clear();
1114 }
1115#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001116 if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001117 return NULL;
1118 Py_BEGIN_ALLOW_THREADS
1119 res = access(path, mode);
1120 Py_END_ALLOW_THREADS
Guido van Rossum674deb22002-09-01 15:06:28 +00001121 return(PyBool_FromLong(res == 0));
Guido van Rossum94f6f721999-01-06 18:42:14 +00001122}
1123
Guido van Rossumd371ff11999-01-25 16:12:23 +00001124#ifndef F_OK
1125#define F_OK 0
1126#endif
1127#ifndef R_OK
1128#define R_OK 4
1129#endif
1130#ifndef W_OK
1131#define W_OK 2
1132#endif
1133#ifndef X_OK
1134#define X_OK 1
1135#endif
1136
1137#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001138PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001139"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001140Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001141
1142static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001143posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001144{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001145 int id;
1146 char *ret;
1147
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001148 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001149 return NULL;
1150
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001151#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001152 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001153 if (id == 0) {
1154 ret = ttyname();
1155 }
1156 else {
1157 ret = NULL;
1158 }
1159#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001160 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001161#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001162 if (ret == NULL)
1163 return(posix_error());
1164 return(PyString_FromString(ret));
1165}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001166#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001167
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001168#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001169PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001170"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001171Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001172
1173static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001174posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001175{
1176 char *ret;
1177 char buffer[L_ctermid];
1178
Greg Wardb48bc172000-03-01 21:51:56 +00001179#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001180 ret = ctermid_r(buffer);
1181#else
1182 ret = ctermid(buffer);
1183#endif
1184 if (ret == NULL)
1185 return(posix_error());
1186 return(PyString_FromString(buffer));
1187}
1188#endif
1189
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001190PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001191"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001192Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001193
Barry Warsaw53699e91996-12-10 23:23:01 +00001194static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001195posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001196{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001197#ifdef MS_WINDOWS
1198 return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir);
1199#elif defined(PYOS_OS2) && defined(PYCC_GCC)
1200 return posix_1str(args, "et:chdir", _chdir2, NULL, NULL);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001201#elif defined(__VMS)
Tim Peters11b23062003-04-23 02:39:17 +00001202 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001203 NULL, NULL);
1204#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001205 return posix_1str(args, "et:chdir", chdir, NULL, NULL);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001206#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001207}
1208
Fred Drake4d1e64b2002-04-15 19:40:07 +00001209#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001210PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001211"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001212Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001213opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001214
1215static PyObject *
1216posix_fchdir(PyObject *self, PyObject *fdobj)
1217{
1218 return posix_fildes(fdobj, fchdir);
1219}
1220#endif /* HAVE_FCHDIR */
1221
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001223PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001224"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001225Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001226
Barry Warsaw53699e91996-12-10 23:23:01 +00001227static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001228posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001229{
Mark Hammondef8b6542001-05-13 08:04:26 +00001230 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001231 int i;
1232 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001233#ifdef Py_WIN_WIDE_FILENAMES
1234 if (unicode_file_names()) {
1235 PyUnicodeObject *po;
1236 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1237 Py_BEGIN_ALLOW_THREADS
1238 res = _wchmod(PyUnicode_AS_UNICODE(po), i);
1239 Py_END_ALLOW_THREADS
1240 if (res < 0)
1241 return posix_error_with_unicode_filename(
1242 PyUnicode_AS_UNICODE(po));
1243 Py_INCREF(Py_None);
1244 return Py_None;
1245 }
1246 /* Drop the argument parsing error as narrow strings
1247 are also valid. */
1248 PyErr_Clear();
1249 }
1250#endif /* Py_WIN_WIDE_FILENAMES */
1251 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001252 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001253 return NULL;
1254 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001255 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001256 Py_END_ALLOW_THREADS
1257 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001258 return posix_error_with_allocated_filename(path);
1259 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001260 Py_INCREF(Py_None);
1261 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001262}
1263
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001264
Martin v. Löwis244edc82001-10-04 22:44:26 +00001265#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001266PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001267"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001268Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001269
1270static PyObject *
1271posix_chroot(PyObject *self, PyObject *args)
1272{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001273 return posix_1str(args, "et:chroot", chroot, NULL, NULL);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001274}
1275#endif
1276
Guido van Rossum21142a01999-01-08 21:05:37 +00001277#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001278PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001279"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001280force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001281
1282static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001283posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001284{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001285 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001286}
1287#endif /* HAVE_FSYNC */
1288
1289#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001290
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001291#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001292extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1293#endif
1294
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001295PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001296"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001297force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001298 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001299
1300static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001301posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001302{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001303 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001304}
1305#endif /* HAVE_FDATASYNC */
1306
1307
Fredrik Lundh10723342000-07-10 16:38:09 +00001308#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001309PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001310"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001311Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001312
Barry Warsaw53699e91996-12-10 23:23:01 +00001313static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001314posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001315{
Mark Hammondef8b6542001-05-13 08:04:26 +00001316 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001317 int uid, gid;
1318 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001319 if (!PyArg_ParseTuple(args, "etii:chown",
1320 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001321 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001322 return NULL;
1323 Py_BEGIN_ALLOW_THREADS
1324 res = chown(path, (uid_t) uid, (gid_t) gid);
1325 Py_END_ALLOW_THREADS
1326 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001327 return posix_error_with_allocated_filename(path);
1328 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001329 Py_INCREF(Py_None);
1330 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001331}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001332#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001333
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001334#ifdef HAVE_LCHOWN
1335PyDoc_STRVAR(posix_lchown__doc__,
1336"lchown(path, uid, gid)\n\n\
1337Change the owner and group id of path to the numeric uid and gid.\n\
1338This function will not follow symbolic links.");
1339
1340static PyObject *
1341posix_lchown(PyObject *self, PyObject *args)
1342{
1343 char *path = NULL;
1344 int uid, gid;
1345 int res;
1346 if (!PyArg_ParseTuple(args, "etii:lchown",
1347 Py_FileSystemDefaultEncoding, &path,
1348 &uid, &gid))
1349 return NULL;
1350 Py_BEGIN_ALLOW_THREADS
1351 res = lchown(path, (uid_t) uid, (gid_t) gid);
1352 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001353 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001354 return posix_error_with_allocated_filename(path);
1355 PyMem_Free(path);
1356 Py_INCREF(Py_None);
1357 return Py_None;
1358}
1359#endif /* HAVE_LCHOWN */
1360
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001361
Guido van Rossum36bc6801995-06-14 22:54:23 +00001362#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001363PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001364"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001365Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001366
Barry Warsaw53699e91996-12-10 23:23:01 +00001367static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001368posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001369{
1370 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001371 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001372
Barry Warsaw53699e91996-12-10 23:23:01 +00001373 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001374#if defined(PYOS_OS2) && defined(PYCC_GCC)
1375 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001376#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001377 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001378#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001379 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001380 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001381 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001382 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001383}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001384
Walter Dörwald3b918c32002-11-21 20:18:46 +00001385#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001386PyDoc_STRVAR(posix_getcwdu__doc__,
1387"getcwdu() -> path\n\n\
1388Return a unicode string representing the current working directory.");
1389
1390static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001391posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001392{
1393 char buf[1026];
1394 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001395
1396#ifdef Py_WIN_WIDE_FILENAMES
1397 if (unicode_file_names()) {
1398 wchar_t *wres;
1399 wchar_t wbuf[1026];
1400 Py_BEGIN_ALLOW_THREADS
1401 wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]);
1402 Py_END_ALLOW_THREADS
1403 if (wres == NULL)
1404 return posix_error();
1405 return PyUnicode_FromWideChar(wbuf, wcslen(wbuf));
1406 }
1407#endif
1408
1409 Py_BEGIN_ALLOW_THREADS
1410#if defined(PYOS_OS2) && defined(PYCC_GCC)
1411 res = _getcwd2(buf, sizeof buf);
1412#else
1413 res = getcwd(buf, sizeof buf);
1414#endif
1415 Py_END_ALLOW_THREADS
1416 if (res == NULL)
1417 return posix_error();
1418 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1419}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001420#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001421#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001422
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001423
Guido van Rossumb6775db1994-08-01 11:34:53 +00001424#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001425PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001426"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001427Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001428
Barry Warsaw53699e91996-12-10 23:23:01 +00001429static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001430posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001431{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001432 return posix_2str(args, "etet:link", link, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001433}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001434#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001435
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001436
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001437PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001438"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001439Return a list containing the names of the entries in the directory.\n\
1440\n\
1441 path: path of directory to list\n\
1442\n\
1443The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001444entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001445
Barry Warsaw53699e91996-12-10 23:23:01 +00001446static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001447posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001448{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001449 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001450 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001451#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001452
Barry Warsaw53699e91996-12-10 23:23:01 +00001453 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001454 HANDLE hFindFile;
1455 WIN32_FIND_DATA FileData;
Mark Hammondef8b6542001-05-13 08:04:26 +00001456 /* MAX_PATH characters could mean a bigger encoded string */
1457 char namebuf[MAX_PATH*2+5];
1458 char *bufptr = namebuf;
1459 int len = sizeof(namebuf)/sizeof(namebuf[0]);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001460
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001461#ifdef Py_WIN_WIDE_FILENAMES
1462 /* If on wide-character-capable OS see if argument
1463 is Unicode and if so use wide API. */
1464 if (unicode_file_names()) {
1465 PyUnicodeObject *po;
1466 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1467 WIN32_FIND_DATAW wFileData;
1468 Py_UNICODE wnamebuf[MAX_PATH*2+5];
1469 Py_UNICODE wch;
1470 wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH);
1471 wnamebuf[MAX_PATH] = L'\0';
1472 len = wcslen(wnamebuf);
1473 wch = (len > 0) ? wnamebuf[len-1] : L'\0';
1474 if (wch != L'/' && wch != L'\\' && wch != L':')
1475 wnamebuf[len++] = L'/';
1476 wcscpy(wnamebuf + len, L"*.*");
1477 if ((d = PyList_New(0)) == NULL)
1478 return NULL;
1479 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1480 if (hFindFile == INVALID_HANDLE_VALUE) {
1481 errno = GetLastError();
1482 if (errno == ERROR_FILE_NOT_FOUND) {
1483 return d;
1484 }
1485 Py_DECREF(d);
1486 return win32_error_unicode("FindFirstFileW", wnamebuf);
1487 }
1488 do {
1489 if (wFileData.cFileName[0] == L'.' &&
1490 (wFileData.cFileName[1] == L'\0' ||
1491 wFileData.cFileName[1] == L'.' &&
1492 wFileData.cFileName[2] == L'\0'))
1493 continue;
1494 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1495 if (v == NULL) {
1496 Py_DECREF(d);
1497 d = NULL;
1498 break;
1499 }
1500 if (PyList_Append(d, v) != 0) {
1501 Py_DECREF(v);
1502 Py_DECREF(d);
1503 d = NULL;
1504 break;
1505 }
1506 Py_DECREF(v);
1507 } while (FindNextFileW(hFindFile, &wFileData) == TRUE);
1508
1509 if (FindClose(hFindFile) == FALSE) {
1510 Py_DECREF(d);
1511 return win32_error_unicode("FindClose", wnamebuf);
1512 }
1513 return d;
1514 }
1515 /* Drop the argument parsing error as narrow strings
1516 are also valid. */
1517 PyErr_Clear();
1518 }
1519#endif
1520
Tim Peters5aa91602002-01-30 05:46:57 +00001521 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001522 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00001523 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00001524 if (len > 0) {
1525 char ch = namebuf[len-1];
1526 if (ch != SEP && ch != ALTSEP && ch != ':')
1527 namebuf[len++] = '/';
1528 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001529 strcpy(namebuf + len, "*.*");
1530
Barry Warsaw53699e91996-12-10 23:23:01 +00001531 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001532 return NULL;
1533
1534 hFindFile = FindFirstFile(namebuf, &FileData);
1535 if (hFindFile == INVALID_HANDLE_VALUE) {
1536 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +00001537 if (errno == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001538 return d;
1539 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001540 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001541 }
1542 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001543 if (FileData.cFileName[0] == '.' &&
1544 (FileData.cFileName[1] == '\0' ||
1545 FileData.cFileName[1] == '.' &&
1546 FileData.cFileName[2] == '\0'))
1547 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001548 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001549 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001550 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001551 d = NULL;
1552 break;
1553 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001554 if (PyList_Append(d, v) != 0) {
1555 Py_DECREF(v);
1556 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001557 d = NULL;
1558 break;
1559 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001560 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001561 } while (FindNextFile(hFindFile, &FileData) == TRUE);
1562
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001563 if (FindClose(hFindFile) == FALSE) {
1564 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001565 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001566 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001567
1568 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001569
Tim Peters0bb44a42000-09-15 07:44:49 +00001570#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001571
1572#ifndef MAX_PATH
1573#define MAX_PATH CCHMAXPATH
1574#endif
1575 char *name, *pt;
1576 int len;
1577 PyObject *d, *v;
1578 char namebuf[MAX_PATH+5];
1579 HDIR hdir = 1;
1580 ULONG srchcnt = 1;
1581 FILEFINDBUF3 ep;
1582 APIRET rc;
1583
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001584 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001585 return NULL;
1586 if (len >= MAX_PATH) {
1587 PyErr_SetString(PyExc_ValueError, "path too long");
1588 return NULL;
1589 }
1590 strcpy(namebuf, name);
1591 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001592 if (*pt == ALTSEP)
1593 *pt = SEP;
1594 if (namebuf[len-1] != SEP)
1595 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001596 strcpy(namebuf + len, "*.*");
1597
1598 if ((d = PyList_New(0)) == NULL)
1599 return NULL;
1600
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001601 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
1602 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001603 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001604 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
1605 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
1606 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001607
1608 if (rc != NO_ERROR) {
1609 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001610 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001611 }
1612
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001613 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001614 do {
1615 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001616 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001617 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001618
1619 strcpy(namebuf, ep.achName);
1620
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001621 /* Leave Case of Name Alone -- In Native Form */
1622 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001623
1624 v = PyString_FromString(namebuf);
1625 if (v == NULL) {
1626 Py_DECREF(d);
1627 d = NULL;
1628 break;
1629 }
1630 if (PyList_Append(d, v) != 0) {
1631 Py_DECREF(v);
1632 Py_DECREF(d);
1633 d = NULL;
1634 break;
1635 }
1636 Py_DECREF(v);
1637 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
1638 }
1639
1640 return d;
1641#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001642
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001643 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001644 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001645 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001646 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00001647 int arg_is_unicode = 1;
1648
1649 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
1650 arg_is_unicode = 0;
1651 PyErr_Clear();
1652 }
1653 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001654 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001655 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001656 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001657 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001658 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001659 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001660 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001661 return NULL;
1662 }
1663 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001664 if (ep->d_name[0] == '.' &&
1665 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001666 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001667 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001668 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001669 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001670 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001671 d = NULL;
1672 break;
1673 }
Just van Rossum46c97842003-02-25 21:42:15 +00001674#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00001675 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00001676 PyObject *w;
1677
1678 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00001679 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00001680 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00001681 if (w != NULL) {
1682 Py_DECREF(v);
1683 v = w;
1684 }
1685 else {
1686 /* fall back to the original byte string, as
1687 discussed in patch #683592 */
1688 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00001689 }
Just van Rossum46c97842003-02-25 21:42:15 +00001690 }
1691#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001692 if (PyList_Append(d, v) != 0) {
1693 Py_DECREF(v);
1694 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001695 d = NULL;
1696 break;
1697 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001698 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001699 }
1700 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001701 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001702
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001703 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001704
Tim Peters0bb44a42000-09-15 07:44:49 +00001705#endif /* which OS */
1706} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001707
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001708#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00001709/* A helper function for abspath on win32 */
1710static PyObject *
1711posix__getfullpathname(PyObject *self, PyObject *args)
1712{
1713 /* assume encoded strings wont more than double no of chars */
1714 char inbuf[MAX_PATH*2];
1715 char *inbufp = inbuf;
1716 int insize = sizeof(inbuf)/sizeof(inbuf[0]);
1717 char outbuf[MAX_PATH*2];
1718 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001719#ifdef Py_WIN_WIDE_FILENAMES
1720 if (unicode_file_names()) {
1721 PyUnicodeObject *po;
1722 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
1723 Py_UNICODE woutbuf[MAX_PATH*2];
1724 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00001725 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001726 sizeof(woutbuf)/sizeof(woutbuf[0]),
1727 woutbuf, &wtemp))
1728 return win32_error("GetFullPathName", "");
1729 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
1730 }
1731 /* Drop the argument parsing error as narrow strings
1732 are also valid. */
1733 PyErr_Clear();
1734 }
1735#endif
Tim Peters5aa91602002-01-30 05:46:57 +00001736 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
1737 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00001738 &insize))
1739 return NULL;
1740 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
1741 outbuf, &temp))
1742 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00001743 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
1744 return PyUnicode_Decode(outbuf, strlen(outbuf),
1745 Py_FileSystemDefaultEncoding, NULL);
1746 }
Mark Hammondef8b6542001-05-13 08:04:26 +00001747 return PyString_FromString(outbuf);
1748} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001749#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00001750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001751PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001752"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001753Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001754
Barry Warsaw53699e91996-12-10 23:23:01 +00001755static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001756posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001757{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001758 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00001759 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001760 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001761
1762#ifdef Py_WIN_WIDE_FILENAMES
1763 if (unicode_file_names()) {
1764 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00001765 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001766 Py_BEGIN_ALLOW_THREADS
1767 /* PyUnicode_AS_UNICODE OK without thread lock as
1768 it is a simple dereference. */
1769 res = _wmkdir(PyUnicode_AS_UNICODE(po));
1770 Py_END_ALLOW_THREADS
1771 if (res < 0)
1772 return posix_error();
1773 Py_INCREF(Py_None);
1774 return Py_None;
1775 }
1776 /* Drop the argument parsing error as narrow strings
1777 are also valid. */
1778 PyErr_Clear();
1779 }
1780#endif
1781
Tim Peters5aa91602002-01-30 05:46:57 +00001782 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001783 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001784 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001785 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001786#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001787 res = mkdir(path);
1788#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001789 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001790#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001791 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001792 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001793 return posix_error_with_allocated_filename(path);
1794 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001795 Py_INCREF(Py_None);
1796 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001797}
1798
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001799
Guido van Rossumb6775db1994-08-01 11:34:53 +00001800#ifdef HAVE_NICE
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001801#if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H)
1802#if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS)
1803#include <sys/resource.h>
1804#endif
1805#endif
1806
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001807PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001808"nice(inc) -> new_priority\n\n\
1809Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001810
Barry Warsaw53699e91996-12-10 23:23:01 +00001811static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001812posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00001813{
1814 int increment, value;
1815
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001816 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001817 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001818
1819 /* There are two flavours of 'nice': one that returns the new
1820 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001821 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
1822 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00001823
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001824 If we are of the nice family that returns the new priority, we
1825 need to clear errno before the call, and check if errno is filled
1826 before calling posix_error() on a returnvalue of -1, because the
1827 -1 may be the actual new priority! */
1828
1829 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001830 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001831#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001832 if (value == 0)
1833 value = getpriority(PRIO_PROCESS, 0);
1834#endif
1835 if (value == -1 && errno != 0)
1836 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00001837 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001838 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001839}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001840#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001841
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001842
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001843PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001844"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001845Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001846
Barry Warsaw53699e91996-12-10 23:23:01 +00001847static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001848posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001849{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001850#ifdef MS_WINDOWS
1851 return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename);
1852#else
1853 return posix_2str(args, "etet:rename", rename, NULL, NULL);
1854#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001855}
1856
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001857
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001858PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001859"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001860Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001861
Barry Warsaw53699e91996-12-10 23:23:01 +00001862static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001863posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001864{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001865#ifdef MS_WINDOWS
1866 return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir);
1867#else
1868 return posix_1str(args, "et:rmdir", rmdir, NULL, NULL);
1869#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001870}
1871
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001872
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001873PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001874"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001875Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001876
Barry Warsaw53699e91996-12-10 23:23:01 +00001877static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001878posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001879{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001880#ifdef MS_WINDOWS
1881 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64);
1882#else
1883 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
1884#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001885}
1886
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001887
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001888#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001889PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001890"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001891Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001892
Barry Warsaw53699e91996-12-10 23:23:01 +00001893static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001894posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001895{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001896 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001897 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001898 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001899 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001900 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001901 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001902 Py_END_ALLOW_THREADS
1903 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001904}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001905#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001906
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001907
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001908PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001909"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001910Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001911
Barry Warsaw53699e91996-12-10 23:23:01 +00001912static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001913posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001914{
1915 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001916 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001917 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00001918 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001919 if (i < 0)
1920 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001921 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001922}
1923
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001924
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001925PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001926"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001927Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001928
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001929PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001930"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001931Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001932
Barry Warsaw53699e91996-12-10 23:23:01 +00001933static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001934posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001935{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001936#ifdef MS_WINDOWS
1937 return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink);
1938#else
1939 return posix_1str(args, "et:remove", unlink, NULL, NULL);
1940#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001941}
1942
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001943
Guido van Rossumb6775db1994-08-01 11:34:53 +00001944#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001945PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001946"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001947Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001948
Barry Warsaw53699e91996-12-10 23:23:01 +00001949static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001950posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001951{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001952 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001953 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001954
Barry Warsaw53699e91996-12-10 23:23:01 +00001955 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001956 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001957 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001958 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001959 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001960 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001961 u.sysname,
1962 u.nodename,
1963 u.release,
1964 u.version,
1965 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001966}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001967#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001968
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001969static int
1970extract_time(PyObject *t, long* sec, long* usec)
1971{
1972 long intval;
1973 if (PyFloat_Check(t)) {
1974 double tval = PyFloat_AsDouble(t);
1975 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
1976 if (!intobj)
1977 return -1;
1978 intval = PyInt_AsLong(intobj);
1979 Py_DECREF(intobj);
1980 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00001981 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001982 if (*usec < 0)
1983 /* If rounding gave us a negative number,
1984 truncate. */
1985 *usec = 0;
1986 return 0;
1987 }
1988 intval = PyInt_AsLong(t);
1989 if (intval == -1 && PyErr_Occurred())
1990 return -1;
1991 *sec = intval;
1992 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00001993 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001994}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001995
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001996PyDoc_STRVAR(posix_utime__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001997"utime(path, (atime, utime))\n\
1998utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00001999Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002000second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002001
Barry Warsaw53699e91996-12-10 23:23:01 +00002002static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002003posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002004{
Neal Norwitz2adf2102004-06-09 01:46:02 +00002005 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002006 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002007 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002008 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002009
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002010#if defined(HAVE_UTIMES)
2011 struct timeval buf[2];
2012#define ATIME buf[0].tv_sec
2013#define MTIME buf[1].tv_sec
2014#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002015/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002016 struct utimbuf buf;
2017#define ATIME buf.actime
2018#define MTIME buf.modtime
2019#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002020#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002021 time_t buf[2];
2022#define ATIME buf[0]
2023#define MTIME buf[1]
2024#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002025#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002026
Mark Hammond817c9292003-12-03 01:22:38 +00002027 int have_unicode_filename = 0;
2028#ifdef Py_WIN_WIDE_FILENAMES
2029 PyUnicodeObject *obwpath;
2030 wchar_t *wpath;
2031 if (unicode_file_names()) {
2032 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2033 wpath = PyUnicode_AS_UNICODE(obwpath);
2034 have_unicode_filename = 1;
2035 } else
2036 /* Drop the argument parsing error as narrow strings
2037 are also valid. */
2038 PyErr_Clear();
2039 }
2040#endif /* Py_WIN_WIDE_FILENAMES */
2041
2042 if (!have_unicode_filename && \
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002043 !PyArg_ParseTuple(args, "etO:utime",
2044 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002045 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002046 if (arg == Py_None) {
2047 /* optional time values not given */
2048 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002049#ifdef Py_WIN_WIDE_FILENAMES
2050 if (have_unicode_filename)
2051 res = _wutime(wpath, NULL);
2052 else
2053#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002054 res = utime(path, NULL);
2055 Py_END_ALLOW_THREADS
2056 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002057 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002058 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002059 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002060 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002061 return NULL;
2062 }
2063 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002064 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002065 &atime, &ausec) == -1) {
2066 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002067 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002068 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002069 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002070 &mtime, &musec) == -1) {
2071 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002072 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002073 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002074 ATIME = atime;
2075 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002076#ifdef HAVE_UTIMES
2077 buf[0].tv_usec = ausec;
2078 buf[1].tv_usec = musec;
2079 Py_BEGIN_ALLOW_THREADS
2080 res = utimes(path, buf);
2081 Py_END_ALLOW_THREADS
2082#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002083 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002084#ifdef Py_WIN_WIDE_FILENAMES
2085 if (have_unicode_filename)
Tim Peters4ad82172004-08-30 17:02:04 +00002086 /* utime is OK with utimbuf, but _wutime insists
2087 on _utimbuf (the msvc headers assert the
Mark Hammond817c9292003-12-03 01:22:38 +00002088 underscore version is ansi) */
2089 res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG);
2090 else
2091#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002092 res = utime(path, UTIME_ARG);
2093 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002094#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002095 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002096 if (res < 0) {
2097#ifdef Py_WIN_WIDE_FILENAMES
Neal Norwitz2adf2102004-06-09 01:46:02 +00002098 if (have_unicode_filename)
Mark Hammond2d5914b2004-05-04 08:10:37 +00002099 return posix_error_with_unicode_filename(wpath);
2100#endif /* Py_WIN_WIDE_FILENAMES */
Neal Norwitz96652712004-06-06 20:40:27 +00002101 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002102 }
Neal Norwitz96652712004-06-06 20:40:27 +00002103 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002104 Py_INCREF(Py_None);
2105 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002106#undef UTIME_ARG
2107#undef ATIME
2108#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002109}
2110
Guido van Rossum85e3b011991-06-03 12:42:10 +00002111
Guido van Rossum3b066191991-06-04 19:40:25 +00002112/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002113
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002114PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002115"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002116Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002117
Barry Warsaw53699e91996-12-10 23:23:01 +00002118static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002119posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002120{
2121 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002122 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002123 return NULL;
2124 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002125 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002126}
2127
Martin v. Löwis114619e2002-10-07 06:44:21 +00002128#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2129static void
2130free_string_array(char **array, int count)
2131{
2132 int i;
2133 for (i = 0; i < count; i++)
2134 PyMem_Free(array[i]);
2135 PyMem_DEL(array);
2136}
2137#endif
2138
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002139
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002140#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002141PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002142"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002143Execute an executable path with arguments, replacing current process.\n\
2144\n\
2145 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002146 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002147
Barry Warsaw53699e91996-12-10 23:23:01 +00002148static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002149posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002150{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002151 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002152 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002153 char **argvlist;
2154 int i, argc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002155 PyObject *(*getitem)(PyObject *, int);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002156
Guido van Rossum89b33251993-10-22 14:26:06 +00002157 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002158 argv is a list or tuple of strings. */
2159
Martin v. Löwis114619e2002-10-07 06:44:21 +00002160 if (!PyArg_ParseTuple(args, "etO:execv",
2161 Py_FileSystemDefaultEncoding,
2162 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002163 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002164 if (PyList_Check(argv)) {
2165 argc = PyList_Size(argv);
2166 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002167 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002168 else if (PyTuple_Check(argv)) {
2169 argc = PyTuple_Size(argv);
2170 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002171 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002172 else {
Fred Drake661ea262000-10-24 19:57:45 +00002173 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002174 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002175 return NULL;
2176 }
2177
Barry Warsaw53699e91996-12-10 23:23:01 +00002178 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002179 if (argvlist == NULL) {
2180 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002181 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002182 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002183 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002184 if (!PyArg_Parse((*getitem)(argv, i), "et",
2185 Py_FileSystemDefaultEncoding,
2186 &argvlist[i])) {
2187 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002188 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002189 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002190 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002191 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002192
Guido van Rossum85e3b011991-06-03 12:42:10 +00002193 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002194 }
2195 argvlist[argc] = NULL;
2196
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002197 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002198
Guido van Rossum85e3b011991-06-03 12:42:10 +00002199 /* If we get here it's definitely an error */
2200
Martin v. Löwis114619e2002-10-07 06:44:21 +00002201 free_string_array(argvlist, argc);
2202 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002203 return posix_error();
2204}
2205
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002206
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002207PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002208"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002209Execute a path with arguments and environment, replacing current process.\n\
2210\n\
2211 path: path of executable file\n\
2212 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002213 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002214
Barry Warsaw53699e91996-12-10 23:23:01 +00002215static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002216posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002217{
2218 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002219 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002220 char **argvlist;
2221 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002222 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002223 int i, pos, argc, envc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002224 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002225 int lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002226
2227 /* execve has three arguments: (path, argv, env), where
2228 argv is a list or tuple of strings and env is a dictionary
2229 like posix.environ. */
2230
Martin v. Löwis114619e2002-10-07 06:44:21 +00002231 if (!PyArg_ParseTuple(args, "etOO:execve",
2232 Py_FileSystemDefaultEncoding,
2233 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002234 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002235 if (PyList_Check(argv)) {
2236 argc = PyList_Size(argv);
2237 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002238 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002239 else if (PyTuple_Check(argv)) {
2240 argc = PyTuple_Size(argv);
2241 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002242 }
2243 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002244 PyErr_SetString(PyExc_TypeError,
2245 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002246 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002247 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002248 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002249 PyErr_SetString(PyExc_TypeError,
2250 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002251 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002252 }
2253
Barry Warsaw53699e91996-12-10 23:23:01 +00002254 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002255 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002256 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002257 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002258 }
2259 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002260 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002261 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002262 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002263 &argvlist[i]))
2264 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002265 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002266 goto fail_1;
2267 }
2268 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002269 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002270 argvlist[argc] = NULL;
2271
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002272 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002273 if (i < 0)
2274 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002275 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002276 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002277 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002278 goto fail_1;
2279 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002280 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002281 keys = PyMapping_Keys(env);
2282 vals = PyMapping_Values(env);
2283 if (!keys || !vals)
2284 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002285 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2286 PyErr_SetString(PyExc_TypeError,
2287 "execve(): env.keys() or env.values() is not a list");
2288 goto fail_2;
2289 }
Tim Peters5aa91602002-01-30 05:46:57 +00002290
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002291 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002292 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002293 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002294
2295 key = PyList_GetItem(keys, pos);
2296 val = PyList_GetItem(vals, pos);
2297 if (!key || !val)
2298 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002299
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002300 if (!PyArg_Parse(
2301 key,
2302 "s;execve() arg 3 contains a non-string key",
2303 &k) ||
2304 !PyArg_Parse(
2305 val,
2306 "s;execve() arg 3 contains a non-string value",
2307 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002308 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002309 goto fail_2;
2310 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002311
2312#if defined(PYOS_OS2)
2313 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2314 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2315#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002316 len = PyString_Size(key) + PyString_Size(val) + 2;
2317 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002318 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002319 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002320 goto fail_2;
2321 }
Tim Petersc8996f52001-12-03 20:41:00 +00002322 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002323 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002324#if defined(PYOS_OS2)
2325 }
2326#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002327 }
2328 envlist[envc] = 0;
2329
2330 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002331
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002332 /* If we get here it's definitely an error */
2333
2334 (void) posix_error();
2335
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002336 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002337 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002338 PyMem_DEL(envlist[envc]);
2339 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002340 fail_1:
2341 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002342 Py_XDECREF(vals);
2343 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002344 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002345 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002346 return NULL;
2347}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002348#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002349
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002350
Guido van Rossuma1065681999-01-25 23:20:23 +00002351#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002352PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002353"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002354Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002355\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002356 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002357 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002358 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002359
2360static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002361posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002362{
2363 char *path;
2364 PyObject *argv;
2365 char **argvlist;
2366 int mode, i, argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002367 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002368 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00002369
2370 /* spawnv has three arguments: (mode, path, argv), where
2371 argv is a list or tuple of strings. */
2372
Martin v. Löwis114619e2002-10-07 06:44:21 +00002373 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2374 Py_FileSystemDefaultEncoding,
2375 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002376 return NULL;
2377 if (PyList_Check(argv)) {
2378 argc = PyList_Size(argv);
2379 getitem = PyList_GetItem;
2380 }
2381 else if (PyTuple_Check(argv)) {
2382 argc = PyTuple_Size(argv);
2383 getitem = PyTuple_GetItem;
2384 }
2385 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002386 PyErr_SetString(PyExc_TypeError,
2387 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002388 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002389 return NULL;
2390 }
2391
2392 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002393 if (argvlist == NULL) {
2394 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002395 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002396 }
Guido van Rossuma1065681999-01-25 23:20:23 +00002397 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002398 if (!PyArg_Parse((*getitem)(argv, i), "et",
2399 Py_FileSystemDefaultEncoding,
2400 &argvlist[i])) {
2401 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002402 PyErr_SetString(
2403 PyExc_TypeError,
2404 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002405 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00002406 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00002407 }
2408 }
2409 argvlist[argc] = NULL;
2410
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002411#if defined(PYOS_OS2) && defined(PYCC_GCC)
2412 Py_BEGIN_ALLOW_THREADS
2413 spawnval = spawnv(mode, path, argvlist);
2414 Py_END_ALLOW_THREADS
2415#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002416 if (mode == _OLD_P_OVERLAY)
2417 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00002418
Tim Peters25059d32001-12-07 20:35:43 +00002419 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002420 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00002421 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002422#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002423
Martin v. Löwis114619e2002-10-07 06:44:21 +00002424 free_string_array(argvlist, argc);
2425 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002426
Fred Drake699f3522000-06-29 21:12:41 +00002427 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002428 return posix_error();
2429 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002430#if SIZEOF_LONG == SIZEOF_VOID_P
2431 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002432#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002433 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002434#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002435}
2436
2437
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002438PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002439"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002440Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002441\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002442 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002443 path: path of executable file\n\
2444 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002445 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002446
2447static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002448posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002449{
2450 char *path;
2451 PyObject *argv, *env;
2452 char **argvlist;
2453 char **envlist;
2454 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2455 int mode, i, pos, argc, envc;
Tim Peters79248aa2001-08-29 21:37:10 +00002456 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002457 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002458 int lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002459
2460 /* spawnve has four arguments: (mode, path, argv, env), where
2461 argv is a list or tuple of strings and env is a dictionary
2462 like posix.environ. */
2463
Martin v. Löwis114619e2002-10-07 06:44:21 +00002464 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
2465 Py_FileSystemDefaultEncoding,
2466 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00002467 return NULL;
2468 if (PyList_Check(argv)) {
2469 argc = PyList_Size(argv);
2470 getitem = PyList_GetItem;
2471 }
2472 else if (PyTuple_Check(argv)) {
2473 argc = PyTuple_Size(argv);
2474 getitem = PyTuple_GetItem;
2475 }
2476 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002477 PyErr_SetString(PyExc_TypeError,
2478 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002479 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002480 }
2481 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002482 PyErr_SetString(PyExc_TypeError,
2483 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002484 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002485 }
2486
2487 argvlist = PyMem_NEW(char *, argc+1);
2488 if (argvlist == NULL) {
2489 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002490 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002491 }
2492 for (i = 0; i < argc; i++) {
2493 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002494 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00002495 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00002496 &argvlist[i]))
2497 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002498 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00002499 goto fail_1;
2500 }
2501 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002502 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00002503 argvlist[argc] = NULL;
2504
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002505 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002506 if (i < 0)
2507 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00002508 envlist = PyMem_NEW(char *, i + 1);
2509 if (envlist == NULL) {
2510 PyErr_NoMemory();
2511 goto fail_1;
2512 }
2513 envc = 0;
2514 keys = PyMapping_Keys(env);
2515 vals = PyMapping_Values(env);
2516 if (!keys || !vals)
2517 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002518 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2519 PyErr_SetString(PyExc_TypeError,
2520 "spawnve(): env.keys() or env.values() is not a list");
2521 goto fail_2;
2522 }
Tim Peters5aa91602002-01-30 05:46:57 +00002523
Guido van Rossuma1065681999-01-25 23:20:23 +00002524 for (pos = 0; pos < i; pos++) {
2525 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002526 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00002527
2528 key = PyList_GetItem(keys, pos);
2529 val = PyList_GetItem(vals, pos);
2530 if (!key || !val)
2531 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002532
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002533 if (!PyArg_Parse(
2534 key,
2535 "s;spawnve() arg 3 contains a non-string key",
2536 &k) ||
2537 !PyArg_Parse(
2538 val,
2539 "s;spawnve() arg 3 contains a non-string value",
2540 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00002541 {
2542 goto fail_2;
2543 }
Tim Petersc8996f52001-12-03 20:41:00 +00002544 len = PyString_Size(key) + PyString_Size(val) + 2;
2545 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00002546 if (p == NULL) {
2547 PyErr_NoMemory();
2548 goto fail_2;
2549 }
Tim Petersc8996f52001-12-03 20:41:00 +00002550 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00002551 envlist[envc++] = p;
2552 }
2553 envlist[envc] = 0;
2554
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002555#if defined(PYOS_OS2) && defined(PYCC_GCC)
2556 Py_BEGIN_ALLOW_THREADS
2557 spawnval = spawnve(mode, path, argvlist, envlist);
2558 Py_END_ALLOW_THREADS
2559#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002560 if (mode == _OLD_P_OVERLAY)
2561 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00002562
2563 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002564 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00002565 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002566#endif
Tim Peters25059d32001-12-07 20:35:43 +00002567
Fred Drake699f3522000-06-29 21:12:41 +00002568 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002569 (void) posix_error();
2570 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002571#if SIZEOF_LONG == SIZEOF_VOID_P
2572 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002573#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002574 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002575#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002576
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002577 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00002578 while (--envc >= 0)
2579 PyMem_DEL(envlist[envc]);
2580 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002581 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002582 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00002583 Py_XDECREF(vals);
2584 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002585 fail_0:
2586 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002587 return res;
2588}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00002589
2590/* OS/2 supports spawnvp & spawnvpe natively */
2591#if defined(PYOS_OS2)
2592PyDoc_STRVAR(posix_spawnvp__doc__,
2593"spawnvp(mode, file, args)\n\n\
2594Execute the program 'file' in a new process, using the environment\n\
2595search path to find the file.\n\
2596\n\
2597 mode: mode of process creation\n\
2598 file: executable file name\n\
2599 args: tuple or list of strings");
2600
2601static PyObject *
2602posix_spawnvp(PyObject *self, PyObject *args)
2603{
2604 char *path;
2605 PyObject *argv;
2606 char **argvlist;
2607 int mode, i, argc;
2608 Py_intptr_t spawnval;
2609 PyObject *(*getitem)(PyObject *, int);
2610
2611 /* spawnvp has three arguments: (mode, path, argv), where
2612 argv is a list or tuple of strings. */
2613
2614 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
2615 Py_FileSystemDefaultEncoding,
2616 &path, &argv))
2617 return NULL;
2618 if (PyList_Check(argv)) {
2619 argc = PyList_Size(argv);
2620 getitem = PyList_GetItem;
2621 }
2622 else if (PyTuple_Check(argv)) {
2623 argc = PyTuple_Size(argv);
2624 getitem = PyTuple_GetItem;
2625 }
2626 else {
2627 PyErr_SetString(PyExc_TypeError,
2628 "spawnvp() arg 2 must be a tuple or list");
2629 PyMem_Free(path);
2630 return NULL;
2631 }
2632
2633 argvlist = PyMem_NEW(char *, argc+1);
2634 if (argvlist == NULL) {
2635 PyMem_Free(path);
2636 return PyErr_NoMemory();
2637 }
2638 for (i = 0; i < argc; i++) {
2639 if (!PyArg_Parse((*getitem)(argv, i), "et",
2640 Py_FileSystemDefaultEncoding,
2641 &argvlist[i])) {
2642 free_string_array(argvlist, i);
2643 PyErr_SetString(
2644 PyExc_TypeError,
2645 "spawnvp() arg 2 must contain only strings");
2646 PyMem_Free(path);
2647 return NULL;
2648 }
2649 }
2650 argvlist[argc] = NULL;
2651
2652 Py_BEGIN_ALLOW_THREADS
2653#if defined(PYCC_GCC)
2654 spawnval = spawnvp(mode, path, argvlist);
2655#else
2656 spawnval = _spawnvp(mode, path, argvlist);
2657#endif
2658 Py_END_ALLOW_THREADS
2659
2660 free_string_array(argvlist, argc);
2661 PyMem_Free(path);
2662
2663 if (spawnval == -1)
2664 return posix_error();
2665 else
2666 return Py_BuildValue("l", (long) spawnval);
2667}
2668
2669
2670PyDoc_STRVAR(posix_spawnvpe__doc__,
2671"spawnvpe(mode, file, args, env)\n\n\
2672Execute the program 'file' in a new process, using the environment\n\
2673search path to find the file.\n\
2674\n\
2675 mode: mode of process creation\n\
2676 file: executable file name\n\
2677 args: tuple or list of arguments\n\
2678 env: dictionary of strings mapping to strings");
2679
2680static PyObject *
2681posix_spawnvpe(PyObject *self, PyObject *args)
2682{
2683 char *path;
2684 PyObject *argv, *env;
2685 char **argvlist;
2686 char **envlist;
2687 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2688 int mode, i, pos, argc, envc;
2689 Py_intptr_t spawnval;
2690 PyObject *(*getitem)(PyObject *, int);
2691 int lastarg = 0;
2692
2693 /* spawnvpe has four arguments: (mode, path, argv, env), where
2694 argv is a list or tuple of strings and env is a dictionary
2695 like posix.environ. */
2696
2697 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
2698 Py_FileSystemDefaultEncoding,
2699 &path, &argv, &env))
2700 return NULL;
2701 if (PyList_Check(argv)) {
2702 argc = PyList_Size(argv);
2703 getitem = PyList_GetItem;
2704 }
2705 else if (PyTuple_Check(argv)) {
2706 argc = PyTuple_Size(argv);
2707 getitem = PyTuple_GetItem;
2708 }
2709 else {
2710 PyErr_SetString(PyExc_TypeError,
2711 "spawnvpe() arg 2 must be a tuple or list");
2712 goto fail_0;
2713 }
2714 if (!PyMapping_Check(env)) {
2715 PyErr_SetString(PyExc_TypeError,
2716 "spawnvpe() arg 3 must be a mapping object");
2717 goto fail_0;
2718 }
2719
2720 argvlist = PyMem_NEW(char *, argc+1);
2721 if (argvlist == NULL) {
2722 PyErr_NoMemory();
2723 goto fail_0;
2724 }
2725 for (i = 0; i < argc; i++) {
2726 if (!PyArg_Parse((*getitem)(argv, i),
2727 "et;spawnvpe() arg 2 must contain only strings",
2728 Py_FileSystemDefaultEncoding,
2729 &argvlist[i]))
2730 {
2731 lastarg = i;
2732 goto fail_1;
2733 }
2734 }
2735 lastarg = argc;
2736 argvlist[argc] = NULL;
2737
2738 i = PyMapping_Size(env);
2739 if (i < 0)
2740 goto fail_1;
2741 envlist = PyMem_NEW(char *, i + 1);
2742 if (envlist == NULL) {
2743 PyErr_NoMemory();
2744 goto fail_1;
2745 }
2746 envc = 0;
2747 keys = PyMapping_Keys(env);
2748 vals = PyMapping_Values(env);
2749 if (!keys || !vals)
2750 goto fail_2;
2751 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2752 PyErr_SetString(PyExc_TypeError,
2753 "spawnvpe(): env.keys() or env.values() is not a list");
2754 goto fail_2;
2755 }
2756
2757 for (pos = 0; pos < i; pos++) {
2758 char *p, *k, *v;
2759 size_t len;
2760
2761 key = PyList_GetItem(keys, pos);
2762 val = PyList_GetItem(vals, pos);
2763 if (!key || !val)
2764 goto fail_2;
2765
2766 if (!PyArg_Parse(
2767 key,
2768 "s;spawnvpe() arg 3 contains a non-string key",
2769 &k) ||
2770 !PyArg_Parse(
2771 val,
2772 "s;spawnvpe() arg 3 contains a non-string value",
2773 &v))
2774 {
2775 goto fail_2;
2776 }
2777 len = PyString_Size(key) + PyString_Size(val) + 2;
2778 p = PyMem_NEW(char, len);
2779 if (p == NULL) {
2780 PyErr_NoMemory();
2781 goto fail_2;
2782 }
2783 PyOS_snprintf(p, len, "%s=%s", k, v);
2784 envlist[envc++] = p;
2785 }
2786 envlist[envc] = 0;
2787
2788 Py_BEGIN_ALLOW_THREADS
2789#if defined(PYCC_GCC)
2790 spawnval = spawnve(mode, path, argvlist, envlist);
2791#else
2792 spawnval = _spawnve(mode, path, argvlist, envlist);
2793#endif
2794 Py_END_ALLOW_THREADS
2795
2796 if (spawnval == -1)
2797 (void) posix_error();
2798 else
2799 res = Py_BuildValue("l", (long) spawnval);
2800
2801 fail_2:
2802 while (--envc >= 0)
2803 PyMem_DEL(envlist[envc]);
2804 PyMem_DEL(envlist);
2805 fail_1:
2806 free_string_array(argvlist, lastarg);
2807 Py_XDECREF(vals);
2808 Py_XDECREF(keys);
2809 fail_0:
2810 PyMem_Free(path);
2811 return res;
2812}
2813#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00002814#endif /* HAVE_SPAWNV */
2815
2816
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002817#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002818PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002819"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002820Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
2821\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002822Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002823
2824static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002825posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002826{
Neal Norwitze241ce82003-02-17 18:17:05 +00002827 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002828 if (pid == -1)
2829 return posix_error();
2830 PyOS_AfterFork();
2831 return PyInt_FromLong((long)pid);
2832}
2833#endif
2834
2835
Guido van Rossumad0ee831995-03-01 10:34:45 +00002836#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002837PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002838"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002839Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002840Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002841
Barry Warsaw53699e91996-12-10 23:23:01 +00002842static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002843posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002844{
Neal Norwitze241ce82003-02-17 18:17:05 +00002845 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00002846 if (pid == -1)
2847 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002848 if (pid == 0)
2849 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00002850 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002851}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002852#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002853
Neal Norwitzb59798b2003-03-21 01:43:31 +00002854/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00002855/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
2856#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00002857#define DEV_PTY_FILE "/dev/ptc"
2858#define HAVE_DEV_PTMX
2859#else
2860#define DEV_PTY_FILE "/dev/ptmx"
2861#endif
2862
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002863#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002864#ifdef HAVE_PTY_H
2865#include <pty.h>
2866#else
2867#ifdef HAVE_LIBUTIL_H
2868#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00002869#endif /* HAVE_LIBUTIL_H */
2870#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00002871#ifdef HAVE_STROPTS_H
2872#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002873#endif
2874#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002875
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002876#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002877PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002878"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002879Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002880
2881static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002882posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002883{
2884 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002885#ifndef HAVE_OPENPTY
2886 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002887#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002888#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002889 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002890#ifdef sun
2891 extern char *ptsname();
2892#endif
2893#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00002894
Thomas Wouters70c21a12000-07-14 14:28:33 +00002895#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00002896 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
2897 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002898#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00002899 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
2900 if (slave_name == NULL)
2901 return posix_error();
2902
2903 slave_fd = open(slave_name, O_RDWR);
2904 if (slave_fd < 0)
2905 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002906#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00002907 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002908 if (master_fd < 0)
2909 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002910 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002911 /* change permission of slave */
2912 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002913 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002914 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002915 }
2916 /* unlock slave */
2917 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002918 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002919 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002920 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002921 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002922 slave_name = ptsname(master_fd); /* get name of slave */
2923 if (slave_name == NULL)
2924 return posix_error();
2925 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
2926 if (slave_fd < 0)
2927 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002928#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002929 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
2930 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00002931#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002932 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00002933#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002934#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00002935#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00002936
Fred Drake8cef4cf2000-06-28 16:40:38 +00002937 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00002938
Fred Drake8cef4cf2000-06-28 16:40:38 +00002939}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002940#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002941
2942#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002943PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002944"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00002945Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
2946Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002947To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002948
2949static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002950posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002951{
2952 int master_fd, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00002953
Fred Drake8cef4cf2000-06-28 16:40:38 +00002954 pid = forkpty(&master_fd, NULL, NULL, NULL);
2955 if (pid == -1)
2956 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002957 if (pid == 0)
2958 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00002959 return Py_BuildValue("(ii)", pid, master_fd);
2960}
2961#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002962
Guido van Rossumad0ee831995-03-01 10:34:45 +00002963#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002964PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002965"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002966Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002967
Barry Warsaw53699e91996-12-10 23:23:01 +00002968static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002969posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002970{
Barry Warsaw53699e91996-12-10 23:23:01 +00002971 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002972}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002973#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002974
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002975
Guido van Rossumad0ee831995-03-01 10:34:45 +00002976#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002977PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002978"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002979Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002980
Barry Warsaw53699e91996-12-10 23:23:01 +00002981static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002982posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002983{
Barry Warsaw53699e91996-12-10 23:23:01 +00002984 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002985}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002986#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002987
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002988
Guido van Rossumad0ee831995-03-01 10:34:45 +00002989#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002990PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002991"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002992Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002993
Barry Warsaw53699e91996-12-10 23:23:01 +00002994static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002995posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002996{
Barry Warsaw53699e91996-12-10 23:23:01 +00002997 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002998}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002999#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003000
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003001
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003002PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003003"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003004Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003005
Barry Warsaw53699e91996-12-10 23:23:01 +00003006static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003007posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003008{
Barry Warsaw53699e91996-12-10 23:23:01 +00003009 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003010}
3011
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003012
Fred Drakec9680921999-12-13 16:37:25 +00003013#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003014PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003015"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003016Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003017
3018static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003019posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003020{
3021 PyObject *result = NULL;
3022
Fred Drakec9680921999-12-13 16:37:25 +00003023#ifdef NGROUPS_MAX
3024#define MAX_GROUPS NGROUPS_MAX
3025#else
3026 /* defined to be 16 on Solaris7, so this should be a small number */
3027#define MAX_GROUPS 64
3028#endif
3029 gid_t grouplist[MAX_GROUPS];
3030 int n;
3031
3032 n = getgroups(MAX_GROUPS, grouplist);
3033 if (n < 0)
3034 posix_error();
3035 else {
3036 result = PyList_New(n);
3037 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003038 int i;
3039 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003040 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003041 if (o == NULL) {
3042 Py_DECREF(result);
3043 result = NULL;
3044 break;
3045 }
3046 PyList_SET_ITEM(result, i, o);
3047 }
3048 }
3049 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003050
Fred Drakec9680921999-12-13 16:37:25 +00003051 return result;
3052}
3053#endif
3054
Martin v. Löwis606edc12002-06-13 21:09:11 +00003055#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003056PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003057"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003058Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003059
3060static PyObject *
3061posix_getpgid(PyObject *self, PyObject *args)
3062{
3063 int pid, pgid;
3064 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3065 return NULL;
3066 pgid = getpgid(pid);
3067 if (pgid < 0)
3068 return posix_error();
3069 return PyInt_FromLong((long)pgid);
3070}
3071#endif /* HAVE_GETPGID */
3072
3073
Guido van Rossumb6775db1994-08-01 11:34:53 +00003074#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003075PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003076"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003077Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003078
Barry Warsaw53699e91996-12-10 23:23:01 +00003079static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003080posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003081{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003082#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003083 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003084#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003085 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003086#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003087}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003088#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003089
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003090
Guido van Rossumb6775db1994-08-01 11:34:53 +00003091#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003092PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003093"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003094Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003095
Barry Warsaw53699e91996-12-10 23:23:01 +00003096static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003097posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003098{
Guido van Rossum64933891994-10-20 21:56:42 +00003099#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003100 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003101#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003102 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003103#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003104 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003105 Py_INCREF(Py_None);
3106 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003107}
3108
Guido van Rossumb6775db1994-08-01 11:34:53 +00003109#endif /* HAVE_SETPGRP */
3110
Guido van Rossumad0ee831995-03-01 10:34:45 +00003111#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003112PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003113"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003114Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003115
Barry Warsaw53699e91996-12-10 23:23:01 +00003116static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003117posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003118{
Barry Warsaw53699e91996-12-10 23:23:01 +00003119 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003120}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003121#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003122
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003123
Fred Drake12c6e2d1999-12-14 21:25:03 +00003124#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003125PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003126"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003127Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003128
3129static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003130posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003131{
Neal Norwitze241ce82003-02-17 18:17:05 +00003132 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003133 char *name;
3134 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003135
Fred Drakea30680b2000-12-06 21:24:28 +00003136 errno = 0;
3137 name = getlogin();
3138 if (name == NULL) {
3139 if (errno)
3140 posix_error();
3141 else
3142 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003143 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003144 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003145 else
3146 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003147 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003148
Fred Drake12c6e2d1999-12-14 21:25:03 +00003149 return result;
3150}
3151#endif
3152
Guido van Rossumad0ee831995-03-01 10:34:45 +00003153#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003154PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003155"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003156Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003157
Barry Warsaw53699e91996-12-10 23:23:01 +00003158static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003159posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003160{
Barry Warsaw53699e91996-12-10 23:23:01 +00003161 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003162}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003163#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003164
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003165
Guido van Rossumad0ee831995-03-01 10:34:45 +00003166#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003167PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003168"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003169Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003170
Barry Warsaw53699e91996-12-10 23:23:01 +00003171static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003172posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003173{
3174 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003175 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003176 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003177#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003178 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3179 APIRET rc;
3180 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003181 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003182
3183 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3184 APIRET rc;
3185 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003186 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003187
3188 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003189 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003190#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003191 if (kill(pid, sig) == -1)
3192 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003193#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003194 Py_INCREF(Py_None);
3195 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003196}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003197#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003198
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003199#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003200PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003201"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003202Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003203
3204static PyObject *
3205posix_killpg(PyObject *self, PyObject *args)
3206{
3207 int pgid, sig;
3208 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3209 return NULL;
3210 if (killpg(pgid, sig) == -1)
3211 return posix_error();
3212 Py_INCREF(Py_None);
3213 return Py_None;
3214}
3215#endif
3216
Guido van Rossumc0125471996-06-28 18:55:32 +00003217#ifdef HAVE_PLOCK
3218
3219#ifdef HAVE_SYS_LOCK_H
3220#include <sys/lock.h>
3221#endif
3222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003223PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003224"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003225Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003226
Barry Warsaw53699e91996-12-10 23:23:01 +00003227static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003228posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003229{
3230 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003231 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003232 return NULL;
3233 if (plock(op) == -1)
3234 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003235 Py_INCREF(Py_None);
3236 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003237}
3238#endif
3239
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003240
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003241#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003242PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003243"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003244Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003245
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003246#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003247#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003248static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003249async_system(const char *command)
3250{
3251 char *p, errormsg[256], args[1024];
3252 RESULTCODES rcodes;
3253 APIRET rc;
3254 char *shell = getenv("COMSPEC");
3255 if (!shell)
3256 shell = "cmd";
3257
3258 strcpy(args, shell);
3259 p = &args[ strlen(args)+1 ];
3260 strcpy(p, "/c ");
3261 strcat(p, command);
3262 p += strlen(p) + 1;
3263 *p = '\0';
3264
3265 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003266 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003267 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003268 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003269 &rcodes, shell);
3270 return rc;
3271}
3272
Guido van Rossumd48f2521997-12-05 22:19:34 +00003273static FILE *
3274popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003275{
3276 HFILE rhan, whan;
3277 FILE *retfd = NULL;
3278 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
3279
Guido van Rossumd48f2521997-12-05 22:19:34 +00003280 if (rc != NO_ERROR) {
3281 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003282 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00003283 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003284
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003285 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
3286 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003287
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003288 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3289 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003290
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003291 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
3292 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003293
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003294 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003295 }
3296
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003297 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
3298 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003299
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003300 if (rc == NO_ERROR)
3301 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
3302
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003303 close(oldfd); /* And Close Saved STDOUT Handle */
3304 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003305
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003306 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
3307 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003308
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003309 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3310 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003311
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003312 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
3313 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003314
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003315 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003316 }
3317
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003318 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
3319 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003320
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003321 if (rc == NO_ERROR)
3322 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
3323
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003324 close(oldfd); /* And Close Saved STDIN Handle */
3325 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003326
Guido van Rossumd48f2521997-12-05 22:19:34 +00003327 } else {
3328 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003329 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00003330 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003331}
3332
3333static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003334posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003335{
3336 char *name;
3337 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003338 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003339 FILE *fp;
3340 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003341 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003342 return NULL;
3343 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003344 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003345 Py_END_ALLOW_THREADS
3346 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003347 return os2_error(err);
3348
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003349 f = PyFile_FromFile(fp, name, mode, fclose);
3350 if (f != NULL)
3351 PyFile_SetBufSize(f, bufsize);
3352 return f;
3353}
3354
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003355#elif defined(PYCC_GCC)
3356
3357/* standard posix version of popen() support */
3358static PyObject *
3359posix_popen(PyObject *self, PyObject *args)
3360{
3361 char *name;
3362 char *mode = "r";
3363 int bufsize = -1;
3364 FILE *fp;
3365 PyObject *f;
3366 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3367 return NULL;
3368 Py_BEGIN_ALLOW_THREADS
3369 fp = popen(name, mode);
3370 Py_END_ALLOW_THREADS
3371 if (fp == NULL)
3372 return posix_error();
3373 f = PyFile_FromFile(fp, name, mode, pclose);
3374 if (f != NULL)
3375 PyFile_SetBufSize(f, bufsize);
3376 return f;
3377}
3378
3379/* fork() under OS/2 has lots'o'warts
3380 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
3381 * most of this code is a ripoff of the win32 code, but using the
3382 * capabilities of EMX's C library routines
3383 */
3384
3385/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
3386#define POPEN_1 1
3387#define POPEN_2 2
3388#define POPEN_3 3
3389#define POPEN_4 4
3390
3391static PyObject *_PyPopen(char *, int, int, int);
3392static int _PyPclose(FILE *file);
3393
3394/*
3395 * Internal dictionary mapping popen* file pointers to process handles,
3396 * for use when retrieving the process exit code. See _PyPclose() below
3397 * for more information on this dictionary's use.
3398 */
3399static PyObject *_PyPopenProcs = NULL;
3400
3401/* os2emx version of popen2()
3402 *
3403 * The result of this function is a pipe (file) connected to the
3404 * process's stdin, and a pipe connected to the process's stdout.
3405 */
3406
3407static PyObject *
3408os2emx_popen2(PyObject *self, PyObject *args)
3409{
3410 PyObject *f;
3411 int tm=0;
3412
3413 char *cmdstring;
3414 char *mode = "t";
3415 int bufsize = -1;
3416 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
3417 return NULL;
3418
3419 if (*mode == 't')
3420 tm = O_TEXT;
3421 else if (*mode != 'b') {
3422 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3423 return NULL;
3424 } else
3425 tm = O_BINARY;
3426
3427 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
3428
3429 return f;
3430}
3431
3432/*
3433 * Variation on os2emx.popen2
3434 *
3435 * The result of this function is 3 pipes - the process's stdin,
3436 * stdout and stderr
3437 */
3438
3439static PyObject *
3440os2emx_popen3(PyObject *self, PyObject *args)
3441{
3442 PyObject *f;
3443 int tm = 0;
3444
3445 char *cmdstring;
3446 char *mode = "t";
3447 int bufsize = -1;
3448 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
3449 return NULL;
3450
3451 if (*mode == 't')
3452 tm = O_TEXT;
3453 else if (*mode != 'b') {
3454 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3455 return NULL;
3456 } else
3457 tm = O_BINARY;
3458
3459 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
3460
3461 return f;
3462}
3463
3464/*
3465 * Variation on os2emx.popen2
3466 *
Tim Peters11b23062003-04-23 02:39:17 +00003467 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003468 * and stdout+stderr combined as a single pipe.
3469 */
3470
3471static PyObject *
3472os2emx_popen4(PyObject *self, PyObject *args)
3473{
3474 PyObject *f;
3475 int tm = 0;
3476
3477 char *cmdstring;
3478 char *mode = "t";
3479 int bufsize = -1;
3480 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
3481 return NULL;
3482
3483 if (*mode == 't')
3484 tm = O_TEXT;
3485 else if (*mode != 'b') {
3486 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3487 return NULL;
3488 } else
3489 tm = O_BINARY;
3490
3491 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
3492
3493 return f;
3494}
3495
3496/* a couple of structures for convenient handling of multiple
3497 * file handles and pipes
3498 */
3499struct file_ref
3500{
3501 int handle;
3502 int flags;
3503};
3504
3505struct pipe_ref
3506{
3507 int rd;
3508 int wr;
3509};
3510
3511/* The following code is derived from the win32 code */
3512
3513static PyObject *
3514_PyPopen(char *cmdstring, int mode, int n, int bufsize)
3515{
3516 struct file_ref stdio[3];
3517 struct pipe_ref p_fd[3];
3518 FILE *p_s[3];
3519 int file_count, i, pipe_err, pipe_pid;
3520 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
3521 PyObject *f, *p_f[3];
3522
3523 /* file modes for subsequent fdopen's on pipe handles */
3524 if (mode == O_TEXT)
3525 {
3526 rd_mode = "rt";
3527 wr_mode = "wt";
3528 }
3529 else
3530 {
3531 rd_mode = "rb";
3532 wr_mode = "wb";
3533 }
3534
3535 /* prepare shell references */
3536 if ((shell = getenv("EMXSHELL")) == NULL)
3537 if ((shell = getenv("COMSPEC")) == NULL)
3538 {
3539 errno = ENOENT;
3540 return posix_error();
3541 }
3542
3543 sh_name = _getname(shell);
3544 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
3545 opt = "/c";
3546 else
3547 opt = "-c";
3548
3549 /* save current stdio fds + their flags, and set not inheritable */
3550 i = pipe_err = 0;
3551 while (pipe_err >= 0 && i < 3)
3552 {
3553 pipe_err = stdio[i].handle = dup(i);
3554 stdio[i].flags = fcntl(i, F_GETFD, 0);
3555 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
3556 i++;
3557 }
3558 if (pipe_err < 0)
3559 {
3560 /* didn't get them all saved - clean up and bail out */
3561 int saved_err = errno;
3562 while (i-- > 0)
3563 {
3564 close(stdio[i].handle);
3565 }
3566 errno = saved_err;
3567 return posix_error();
3568 }
3569
3570 /* create pipe ends */
3571 file_count = 2;
3572 if (n == POPEN_3)
3573 file_count = 3;
3574 i = pipe_err = 0;
3575 while ((pipe_err == 0) && (i < file_count))
3576 pipe_err = pipe((int *)&p_fd[i++]);
3577 if (pipe_err < 0)
3578 {
3579 /* didn't get them all made - clean up and bail out */
3580 while (i-- > 0)
3581 {
3582 close(p_fd[i].wr);
3583 close(p_fd[i].rd);
3584 }
3585 errno = EPIPE;
3586 return posix_error();
3587 }
3588
3589 /* change the actual standard IO streams over temporarily,
3590 * making the retained pipe ends non-inheritable
3591 */
3592 pipe_err = 0;
3593
3594 /* - stdin */
3595 if (dup2(p_fd[0].rd, 0) == 0)
3596 {
3597 close(p_fd[0].rd);
3598 i = fcntl(p_fd[0].wr, F_GETFD, 0);
3599 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
3600 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
3601 {
3602 close(p_fd[0].wr);
3603 pipe_err = -1;
3604 }
3605 }
3606 else
3607 {
3608 pipe_err = -1;
3609 }
3610
3611 /* - stdout */
3612 if (pipe_err == 0)
3613 {
3614 if (dup2(p_fd[1].wr, 1) == 1)
3615 {
3616 close(p_fd[1].wr);
3617 i = fcntl(p_fd[1].rd, F_GETFD, 0);
3618 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
3619 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
3620 {
3621 close(p_fd[1].rd);
3622 pipe_err = -1;
3623 }
3624 }
3625 else
3626 {
3627 pipe_err = -1;
3628 }
3629 }
3630
3631 /* - stderr, as required */
3632 if (pipe_err == 0)
3633 switch (n)
3634 {
3635 case POPEN_3:
3636 {
3637 if (dup2(p_fd[2].wr, 2) == 2)
3638 {
3639 close(p_fd[2].wr);
3640 i = fcntl(p_fd[2].rd, F_GETFD, 0);
3641 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
3642 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
3643 {
3644 close(p_fd[2].rd);
3645 pipe_err = -1;
3646 }
3647 }
3648 else
3649 {
3650 pipe_err = -1;
3651 }
3652 break;
3653 }
3654
3655 case POPEN_4:
3656 {
3657 if (dup2(1, 2) != 2)
3658 {
3659 pipe_err = -1;
3660 }
3661 break;
3662 }
3663 }
3664
3665 /* spawn the child process */
3666 if (pipe_err == 0)
3667 {
3668 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
3669 if (pipe_pid == -1)
3670 {
3671 pipe_err = -1;
3672 }
3673 else
3674 {
3675 /* save the PID into the FILE structure
3676 * NOTE: this implementation doesn't actually
3677 * take advantage of this, but do it for
3678 * completeness - AIM Apr01
3679 */
3680 for (i = 0; i < file_count; i++)
3681 p_s[i]->_pid = pipe_pid;
3682 }
3683 }
3684
3685 /* reset standard IO to normal */
3686 for (i = 0; i < 3; i++)
3687 {
3688 dup2(stdio[i].handle, i);
3689 fcntl(i, F_SETFD, stdio[i].flags);
3690 close(stdio[i].handle);
3691 }
3692
3693 /* if any remnant problems, clean up and bail out */
3694 if (pipe_err < 0)
3695 {
3696 for (i = 0; i < 3; i++)
3697 {
3698 close(p_fd[i].rd);
3699 close(p_fd[i].wr);
3700 }
3701 errno = EPIPE;
3702 return posix_error_with_filename(cmdstring);
3703 }
3704
3705 /* build tuple of file objects to return */
3706 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
3707 PyFile_SetBufSize(p_f[0], bufsize);
3708 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
3709 PyFile_SetBufSize(p_f[1], bufsize);
3710 if (n == POPEN_3)
3711 {
3712 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
3713 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003714 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003715 }
3716 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003717 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003718
3719 /*
3720 * Insert the files we've created into the process dictionary
3721 * all referencing the list with the process handle and the
3722 * initial number of files (see description below in _PyPclose).
3723 * Since if _PyPclose later tried to wait on a process when all
3724 * handles weren't closed, it could create a deadlock with the
3725 * child, we spend some energy here to try to ensure that we
3726 * either insert all file handles into the dictionary or none
3727 * at all. It's a little clumsy with the various popen modes
3728 * and variable number of files involved.
3729 */
3730 if (!_PyPopenProcs)
3731 {
3732 _PyPopenProcs = PyDict_New();
3733 }
3734
3735 if (_PyPopenProcs)
3736 {
3737 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
3738 int ins_rc[3];
3739
3740 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
3741 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
3742
3743 procObj = PyList_New(2);
3744 pidObj = PyInt_FromLong((long) pipe_pid);
3745 intObj = PyInt_FromLong((long) file_count);
3746
3747 if (procObj && pidObj && intObj)
3748 {
3749 PyList_SetItem(procObj, 0, pidObj);
3750 PyList_SetItem(procObj, 1, intObj);
3751
3752 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
3753 if (fileObj[0])
3754 {
3755 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
3756 fileObj[0],
3757 procObj);
3758 }
3759 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
3760 if (fileObj[1])
3761 {
3762 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
3763 fileObj[1],
3764 procObj);
3765 }
3766 if (file_count >= 3)
3767 {
3768 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
3769 if (fileObj[2])
3770 {
3771 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
3772 fileObj[2],
3773 procObj);
3774 }
3775 }
3776
3777 if (ins_rc[0] < 0 || !fileObj[0] ||
3778 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
3779 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
3780 {
3781 /* Something failed - remove any dictionary
3782 * entries that did make it.
3783 */
3784 if (!ins_rc[0] && fileObj[0])
3785 {
3786 PyDict_DelItem(_PyPopenProcs,
3787 fileObj[0]);
3788 }
3789 if (!ins_rc[1] && fileObj[1])
3790 {
3791 PyDict_DelItem(_PyPopenProcs,
3792 fileObj[1]);
3793 }
3794 if (!ins_rc[2] && fileObj[2])
3795 {
3796 PyDict_DelItem(_PyPopenProcs,
3797 fileObj[2]);
3798 }
3799 }
3800 }
Tim Peters11b23062003-04-23 02:39:17 +00003801
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003802 /*
3803 * Clean up our localized references for the dictionary keys
3804 * and value since PyDict_SetItem will Py_INCREF any copies
3805 * that got placed in the dictionary.
3806 */
3807 Py_XDECREF(procObj);
3808 Py_XDECREF(fileObj[0]);
3809 Py_XDECREF(fileObj[1]);
3810 Py_XDECREF(fileObj[2]);
3811 }
3812
3813 /* Child is launched. */
3814 return f;
3815}
3816
3817/*
3818 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3819 * exit code for the child process and return as a result of the close.
3820 *
3821 * This function uses the _PyPopenProcs dictionary in order to map the
3822 * input file pointer to information about the process that was
3823 * originally created by the popen* call that created the file pointer.
3824 * The dictionary uses the file pointer as a key (with one entry
3825 * inserted for each file returned by the original popen* call) and a
3826 * single list object as the value for all files from a single call.
3827 * The list object contains the Win32 process handle at [0], and a file
3828 * count at [1], which is initialized to the total number of file
3829 * handles using that list.
3830 *
3831 * This function closes whichever handle it is passed, and decrements
3832 * the file count in the dictionary for the process handle pointed to
3833 * by this file. On the last close (when the file count reaches zero),
3834 * this function will wait for the child process and then return its
3835 * exit code as the result of the close() operation. This permits the
3836 * files to be closed in any order - it is always the close() of the
3837 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003838 *
3839 * NOTE: This function is currently called with the GIL released.
3840 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003841 */
3842
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003843static int _PyPclose(FILE *file)
3844{
3845 int result;
3846 int exit_code;
3847 int pipe_pid;
3848 PyObject *procObj, *pidObj, *intObj, *fileObj;
3849 int file_count;
3850#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003851 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003852#endif
3853
3854 /* Close the file handle first, to ensure it can't block the
3855 * child from exiting if it's the last handle.
3856 */
3857 result = fclose(file);
3858
3859#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003860 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003861#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003862 if (_PyPopenProcs)
3863 {
3864 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
3865 (procObj = PyDict_GetItem(_PyPopenProcs,
3866 fileObj)) != NULL &&
3867 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
3868 (intObj = PyList_GetItem(procObj,1)) != NULL)
3869 {
3870 pipe_pid = (int) PyInt_AsLong(pidObj);
3871 file_count = (int) PyInt_AsLong(intObj);
3872
3873 if (file_count > 1)
3874 {
3875 /* Still other files referencing process */
3876 file_count--;
3877 PyList_SetItem(procObj,1,
3878 PyInt_FromLong((long) file_count));
3879 }
3880 else
3881 {
3882 /* Last file for this process */
3883 if (result != EOF &&
3884 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
3885 {
3886 /* extract exit status */
3887 if (WIFEXITED(exit_code))
3888 {
3889 result = WEXITSTATUS(exit_code);
3890 }
3891 else
3892 {
3893 errno = EPIPE;
3894 result = -1;
3895 }
3896 }
3897 else
3898 {
3899 /* Indicate failure - this will cause the file object
3900 * to raise an I/O error and translate the last
3901 * error code from errno. We do have a problem with
3902 * last errors that overlap the normal errno table,
3903 * but that's a consistent problem with the file object.
3904 */
3905 result = -1;
3906 }
3907 }
3908
3909 /* Remove this file pointer from dictionary */
3910 PyDict_DelItem(_PyPopenProcs, fileObj);
3911
3912 if (PyDict_Size(_PyPopenProcs) == 0)
3913 {
3914 Py_DECREF(_PyPopenProcs);
3915 _PyPopenProcs = NULL;
3916 }
3917
3918 } /* if object retrieval ok */
3919
3920 Py_XDECREF(fileObj);
3921 } /* if _PyPopenProcs */
3922
3923#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003924 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003925#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003926 return result;
3927}
3928
3929#endif /* PYCC_??? */
3930
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003931#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003932
3933/*
3934 * Portable 'popen' replacement for Win32.
3935 *
3936 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
3937 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00003938 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003939 */
3940
3941#include <malloc.h>
3942#include <io.h>
3943#include <fcntl.h>
3944
3945/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
3946#define POPEN_1 1
3947#define POPEN_2 2
3948#define POPEN_3 3
3949#define POPEN_4 4
3950
3951static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00003952static int _PyPclose(FILE *file);
3953
3954/*
3955 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00003956 * for use when retrieving the process exit code. See _PyPclose() below
3957 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00003958 */
3959static PyObject *_PyPopenProcs = NULL;
3960
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003961
3962/* popen that works from a GUI.
3963 *
3964 * The result of this function is a pipe (file) connected to the
3965 * processes stdin or stdout, depending on the requested mode.
3966 */
3967
3968static PyObject *
3969posix_popen(PyObject *self, PyObject *args)
3970{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00003971 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003972 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003973
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003974 char *cmdstring;
3975 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003976 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003977 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003978 return NULL;
3979
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003980 if (*mode == 'r')
3981 tm = _O_RDONLY;
3982 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00003983 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003984 return NULL;
3985 } else
3986 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00003987
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003988 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003989 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003990 return NULL;
3991 }
3992
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003993 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003994 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003995 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003996 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003997 else
3998 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
3999
4000 return f;
4001}
4002
4003/* Variation on win32pipe.popen
4004 *
4005 * The result of this function is a pipe (file) connected to the
4006 * process's stdin, and a pipe connected to the process's stdout.
4007 */
4008
4009static PyObject *
4010win32_popen2(PyObject *self, PyObject *args)
4011{
4012 PyObject *f;
4013 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004014
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004015 char *cmdstring;
4016 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004017 int bufsize = -1;
4018 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004019 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004020
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004021 if (*mode == 't')
4022 tm = _O_TEXT;
4023 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004024 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004025 return NULL;
4026 } else
4027 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004028
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004029 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004030 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004031 return NULL;
4032 }
4033
4034 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004035
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004036 return f;
4037}
4038
4039/*
4040 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004041 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004042 * The result of this function is 3 pipes - the process's stdin,
4043 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004044 */
4045
4046static PyObject *
4047win32_popen3(PyObject *self, PyObject *args)
4048{
4049 PyObject *f;
4050 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004051
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004052 char *cmdstring;
4053 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004054 int bufsize = -1;
4055 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004056 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004057
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004058 if (*mode == 't')
4059 tm = _O_TEXT;
4060 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004061 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004062 return NULL;
4063 } else
4064 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004065
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004066 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004067 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004068 return NULL;
4069 }
4070
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004071 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004072
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004073 return f;
4074}
4075
4076/*
4077 * Variation on win32pipe.popen
4078 *
Tim Peters5aa91602002-01-30 05:46:57 +00004079 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004080 * and stdout+stderr combined as a single pipe.
4081 */
4082
4083static PyObject *
4084win32_popen4(PyObject *self, PyObject *args)
4085{
4086 PyObject *f;
4087 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004088
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004089 char *cmdstring;
4090 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004091 int bufsize = -1;
4092 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004093 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004094
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004095 if (*mode == 't')
4096 tm = _O_TEXT;
4097 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004098 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004099 return NULL;
4100 } else
4101 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004102
4103 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004104 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004105 return NULL;
4106 }
4107
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004108 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004109
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004110 return f;
4111}
4112
Mark Hammond08501372001-01-31 07:30:29 +00004113static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004114_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004115 HANDLE hStdin,
4116 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004117 HANDLE hStderr,
4118 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004119{
4120 PROCESS_INFORMATION piProcInfo;
4121 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004122 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004123 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004124 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004125 int i;
4126 int x;
4127
4128 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004129 char *comshell;
4130
Tim Peters92e4dd82002-10-05 01:47:34 +00004131 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004132 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
4133 return x;
Tim Peters402d5982001-08-27 06:37:48 +00004134
4135 /* Explicitly check if we are using COMMAND.COM. If we are
4136 * then use the w9xpopen hack.
4137 */
4138 comshell = s1 + x;
4139 while (comshell >= s1 && *comshell != '\\')
4140 --comshell;
4141 ++comshell;
4142
4143 if (GetVersion() < 0x80000000 &&
4144 _stricmp(comshell, "command.com") != 0) {
4145 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004146 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004147 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004148 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004149 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004150 }
4151 else {
4152 /*
Tim Peters402d5982001-08-27 06:37:48 +00004153 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4154 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004155 */
Mark Hammond08501372001-01-31 07:30:29 +00004156 char modulepath[_MAX_PATH];
4157 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004158 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
4159 for (i = x = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004160 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004161 x = i+1;
4162 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004163 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004164 strncat(modulepath,
4165 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004166 (sizeof(modulepath)/sizeof(modulepath[0]))
4167 -strlen(modulepath));
4168 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004169 /* Eeek - file-not-found - possibly an embedding
4170 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004171 */
Tim Peters5aa91602002-01-30 05:46:57 +00004172 strncpy(modulepath,
4173 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00004174 sizeof(modulepath)/sizeof(modulepath[0]));
4175 if (modulepath[strlen(modulepath)-1] != '\\')
4176 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004177 strncat(modulepath,
4178 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004179 (sizeof(modulepath)/sizeof(modulepath[0]))
4180 -strlen(modulepath));
4181 /* No where else to look - raise an easily identifiable
4182 error, rather than leaving Windows to report
4183 "file not found" - as the user is probably blissfully
4184 unaware this shim EXE is used, and it will confuse them.
4185 (well, it confused me for a while ;-)
4186 */
4187 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004188 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004189 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004190 "for popen to work with your shell "
4191 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004192 szConsoleSpawn);
4193 return FALSE;
4194 }
4195 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004196 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004197 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004198 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004199
Tim Peters92e4dd82002-10-05 01:47:34 +00004200 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004201 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004202 /* To maintain correct argument passing semantics,
4203 we pass the command-line as it stands, and allow
4204 quoting to be applied. w9xpopen.exe will then
4205 use its argv vector, and re-quote the necessary
4206 args for the ultimate child process.
4207 */
Tim Peters75cdad52001-11-28 22:07:30 +00004208 PyOS_snprintf(
4209 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004210 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004211 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004212 s1,
4213 s3,
4214 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004215 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004216 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004217 dialog:
4218 "Your program accessed mem currently in use at xxx"
4219 and a hopeful warning about the stability of your
4220 system.
4221 Cost is Ctrl+C wont kill children, but anyone
4222 who cares can have a go!
4223 */
4224 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004225 }
4226 }
4227
4228 /* Could be an else here to try cmd.exe / command.com in the path
4229 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004230 else {
Tim Peters402d5982001-08-27 06:37:48 +00004231 PyErr_SetString(PyExc_RuntimeError,
4232 "Cannot locate a COMSPEC environment variable to "
4233 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004234 return FALSE;
4235 }
Tim Peters5aa91602002-01-30 05:46:57 +00004236
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004237 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4238 siStartInfo.cb = sizeof(STARTUPINFO);
4239 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4240 siStartInfo.hStdInput = hStdin;
4241 siStartInfo.hStdOutput = hStdout;
4242 siStartInfo.hStdError = hStderr;
4243 siStartInfo.wShowWindow = SW_HIDE;
4244
4245 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004246 s2,
4247 NULL,
4248 NULL,
4249 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004250 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004251 NULL,
4252 NULL,
4253 &siStartInfo,
4254 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004255 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004256 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004257
Mark Hammondb37a3732000-08-14 04:47:33 +00004258 /* Return process handle */
4259 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004260 return TRUE;
4261 }
Tim Peters402d5982001-08-27 06:37:48 +00004262 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004263 return FALSE;
4264}
4265
4266/* The following code is based off of KB: Q190351 */
4267
4268static PyObject *
4269_PyPopen(char *cmdstring, int mode, int n)
4270{
4271 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4272 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004273 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004274
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004275 SECURITY_ATTRIBUTES saAttr;
4276 BOOL fSuccess;
4277 int fd1, fd2, fd3;
4278 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004279 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004280 PyObject *f;
4281
4282 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4283 saAttr.bInheritHandle = TRUE;
4284 saAttr.lpSecurityDescriptor = NULL;
4285
4286 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4287 return win32_error("CreatePipe", NULL);
4288
4289 /* Create new output read handle and the input write handle. Set
4290 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004291 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004292 * being created. */
4293 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004294 GetCurrentProcess(), &hChildStdinWrDup, 0,
4295 FALSE,
4296 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004297 if (!fSuccess)
4298 return win32_error("DuplicateHandle", NULL);
4299
4300 /* Close the inheritable version of ChildStdin
4301 that we're using. */
4302 CloseHandle(hChildStdinWr);
4303
4304 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4305 return win32_error("CreatePipe", NULL);
4306
4307 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004308 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4309 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004310 if (!fSuccess)
4311 return win32_error("DuplicateHandle", NULL);
4312
4313 /* Close the inheritable version of ChildStdout
4314 that we're using. */
4315 CloseHandle(hChildStdoutRd);
4316
4317 if (n != POPEN_4) {
4318 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4319 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004320 fSuccess = DuplicateHandle(GetCurrentProcess(),
4321 hChildStderrRd,
4322 GetCurrentProcess(),
4323 &hChildStderrRdDup, 0,
4324 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004325 if (!fSuccess)
4326 return win32_error("DuplicateHandle", NULL);
4327 /* Close the inheritable version of ChildStdErr that we're using. */
4328 CloseHandle(hChildStderrRd);
4329 }
Tim Peters5aa91602002-01-30 05:46:57 +00004330
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004331 switch (n) {
4332 case POPEN_1:
4333 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4334 case _O_WRONLY | _O_TEXT:
4335 /* Case for writing to child Stdin in text mode. */
4336 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4337 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004338 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004339 PyFile_SetBufSize(f, 0);
4340 /* We don't care about these pipes anymore, so close them. */
4341 CloseHandle(hChildStdoutRdDup);
4342 CloseHandle(hChildStderrRdDup);
4343 break;
4344
4345 case _O_RDONLY | _O_TEXT:
4346 /* Case for reading from child Stdout in text mode. */
4347 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4348 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004349 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004350 PyFile_SetBufSize(f, 0);
4351 /* We don't care about these pipes anymore, so close them. */
4352 CloseHandle(hChildStdinWrDup);
4353 CloseHandle(hChildStderrRdDup);
4354 break;
4355
4356 case _O_RDONLY | _O_BINARY:
4357 /* Case for readinig from child Stdout in binary mode. */
4358 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4359 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004360 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004361 PyFile_SetBufSize(f, 0);
4362 /* We don't care about these pipes anymore, so close them. */
4363 CloseHandle(hChildStdinWrDup);
4364 CloseHandle(hChildStderrRdDup);
4365 break;
4366
4367 case _O_WRONLY | _O_BINARY:
4368 /* Case for writing to child Stdin in binary mode. */
4369 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4370 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004371 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004372 PyFile_SetBufSize(f, 0);
4373 /* We don't care about these pipes anymore, so close them. */
4374 CloseHandle(hChildStdoutRdDup);
4375 CloseHandle(hChildStderrRdDup);
4376 break;
4377 }
Mark Hammondb37a3732000-08-14 04:47:33 +00004378 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004379 break;
Tim Peters5aa91602002-01-30 05:46:57 +00004380
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004381 case POPEN_2:
4382 case POPEN_4:
4383 {
4384 char *m1, *m2;
4385 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00004386
Tim Peters7dca21e2002-08-19 00:42:29 +00004387 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004388 m1 = "r";
4389 m2 = "w";
4390 } else {
4391 m1 = "rb";
4392 m2 = "wb";
4393 }
4394
4395 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4396 f1 = _fdopen(fd1, m2);
4397 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4398 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004399 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004400 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00004401 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004402 PyFile_SetBufSize(p2, 0);
4403
4404 if (n != 4)
4405 CloseHandle(hChildStderrRdDup);
4406
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004407 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00004408 Py_XDECREF(p1);
4409 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00004410 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004411 break;
4412 }
Tim Peters5aa91602002-01-30 05:46:57 +00004413
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004414 case POPEN_3:
4415 {
4416 char *m1, *m2;
4417 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00004418
Tim Peters7dca21e2002-08-19 00:42:29 +00004419 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004420 m1 = "r";
4421 m2 = "w";
4422 } else {
4423 m1 = "rb";
4424 m2 = "wb";
4425 }
4426
4427 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4428 f1 = _fdopen(fd1, m2);
4429 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4430 f2 = _fdopen(fd2, m1);
4431 fd3 = _open_osfhandle((long)hChildStderrRdDup, mode);
4432 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004433 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00004434 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
4435 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004436 PyFile_SetBufSize(p1, 0);
4437 PyFile_SetBufSize(p2, 0);
4438 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004439 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00004440 Py_XDECREF(p1);
4441 Py_XDECREF(p2);
4442 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00004443 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004444 break;
4445 }
4446 }
4447
4448 if (n == POPEN_4) {
4449 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004450 hChildStdinRd,
4451 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004452 hChildStdoutWr,
4453 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004454 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004455 }
4456 else {
4457 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004458 hChildStdinRd,
4459 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004460 hChildStderrWr,
4461 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004462 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004463 }
4464
Mark Hammondb37a3732000-08-14 04:47:33 +00004465 /*
4466 * Insert the files we've created into the process dictionary
4467 * all referencing the list with the process handle and the
4468 * initial number of files (see description below in _PyPclose).
4469 * Since if _PyPclose later tried to wait on a process when all
4470 * handles weren't closed, it could create a deadlock with the
4471 * child, we spend some energy here to try to ensure that we
4472 * either insert all file handles into the dictionary or none
4473 * at all. It's a little clumsy with the various popen modes
4474 * and variable number of files involved.
4475 */
4476 if (!_PyPopenProcs) {
4477 _PyPopenProcs = PyDict_New();
4478 }
4479
4480 if (_PyPopenProcs) {
4481 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
4482 int ins_rc[3];
4483
4484 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4485 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4486
4487 procObj = PyList_New(2);
4488 hProcessObj = PyLong_FromVoidPtr(hProcess);
4489 intObj = PyInt_FromLong(file_count);
4490
4491 if (procObj && hProcessObj && intObj) {
4492 PyList_SetItem(procObj,0,hProcessObj);
4493 PyList_SetItem(procObj,1,intObj);
4494
4495 fileObj[0] = PyLong_FromVoidPtr(f1);
4496 if (fileObj[0]) {
4497 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4498 fileObj[0],
4499 procObj);
4500 }
4501 if (file_count >= 2) {
4502 fileObj[1] = PyLong_FromVoidPtr(f2);
4503 if (fileObj[1]) {
4504 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4505 fileObj[1],
4506 procObj);
4507 }
4508 }
4509 if (file_count >= 3) {
4510 fileObj[2] = PyLong_FromVoidPtr(f3);
4511 if (fileObj[2]) {
4512 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4513 fileObj[2],
4514 procObj);
4515 }
4516 }
4517
4518 if (ins_rc[0] < 0 || !fileObj[0] ||
4519 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4520 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
4521 /* Something failed - remove any dictionary
4522 * entries that did make it.
4523 */
4524 if (!ins_rc[0] && fileObj[0]) {
4525 PyDict_DelItem(_PyPopenProcs,
4526 fileObj[0]);
4527 }
4528 if (!ins_rc[1] && fileObj[1]) {
4529 PyDict_DelItem(_PyPopenProcs,
4530 fileObj[1]);
4531 }
4532 if (!ins_rc[2] && fileObj[2]) {
4533 PyDict_DelItem(_PyPopenProcs,
4534 fileObj[2]);
4535 }
4536 }
4537 }
Tim Peters5aa91602002-01-30 05:46:57 +00004538
Mark Hammondb37a3732000-08-14 04:47:33 +00004539 /*
4540 * Clean up our localized references for the dictionary keys
4541 * and value since PyDict_SetItem will Py_INCREF any copies
4542 * that got placed in the dictionary.
4543 */
4544 Py_XDECREF(procObj);
4545 Py_XDECREF(fileObj[0]);
4546 Py_XDECREF(fileObj[1]);
4547 Py_XDECREF(fileObj[2]);
4548 }
4549
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004550 /* Child is launched. Close the parents copy of those pipe
4551 * handles that only the child should have open. You need to
4552 * make sure that no handles to the write end of the output pipe
4553 * are maintained in this process or else the pipe will not close
4554 * when the child process exits and the ReadFile will hang. */
4555
4556 if (!CloseHandle(hChildStdinRd))
4557 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004558
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004559 if (!CloseHandle(hChildStdoutWr))
4560 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004561
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004562 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
4563 return win32_error("CloseHandle", NULL);
4564
4565 return f;
4566}
Fredrik Lundh56055a42000-07-23 19:47:12 +00004567
4568/*
4569 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4570 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00004571 *
4572 * This function uses the _PyPopenProcs dictionary in order to map the
4573 * input file pointer to information about the process that was
4574 * originally created by the popen* call that created the file pointer.
4575 * The dictionary uses the file pointer as a key (with one entry
4576 * inserted for each file returned by the original popen* call) and a
4577 * single list object as the value for all files from a single call.
4578 * The list object contains the Win32 process handle at [0], and a file
4579 * count at [1], which is initialized to the total number of file
4580 * handles using that list.
4581 *
4582 * This function closes whichever handle it is passed, and decrements
4583 * the file count in the dictionary for the process handle pointed to
4584 * by this file. On the last close (when the file count reaches zero),
4585 * this function will wait for the child process and then return its
4586 * exit code as the result of the close() operation. This permits the
4587 * files to be closed in any order - it is always the close() of the
4588 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004589 *
4590 * NOTE: This function is currently called with the GIL released.
4591 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004592 */
Tim Peters736aa322000-09-01 06:51:24 +00004593
Fredrik Lundh56055a42000-07-23 19:47:12 +00004594static int _PyPclose(FILE *file)
4595{
Fredrik Lundh20318932000-07-26 17:29:12 +00004596 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004597 DWORD exit_code;
4598 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00004599 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
4600 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00004601#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004602 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00004603#endif
4604
Fredrik Lundh20318932000-07-26 17:29:12 +00004605 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00004606 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00004607 */
4608 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00004609#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004610 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00004611#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004612 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00004613 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4614 (procObj = PyDict_GetItem(_PyPopenProcs,
4615 fileObj)) != NULL &&
4616 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
4617 (intObj = PyList_GetItem(procObj,1)) != NULL) {
4618
4619 hProcess = PyLong_AsVoidPtr(hProcessObj);
4620 file_count = PyInt_AsLong(intObj);
4621
4622 if (file_count > 1) {
4623 /* Still other files referencing process */
4624 file_count--;
4625 PyList_SetItem(procObj,1,
4626 PyInt_FromLong(file_count));
4627 } else {
4628 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00004629 if (result != EOF &&
4630 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
4631 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00004632 /* Possible truncation here in 16-bit environments, but
4633 * real exit codes are just the lower byte in any event.
4634 */
4635 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004636 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00004637 /* Indicate failure - this will cause the file object
4638 * to raise an I/O error and translate the last Win32
4639 * error code from errno. We do have a problem with
4640 * last errors that overlap the normal errno table,
4641 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004642 */
Fredrik Lundh20318932000-07-26 17:29:12 +00004643 if (result != EOF) {
4644 /* If the error wasn't from the fclose(), then
4645 * set errno for the file object error handling.
4646 */
4647 errno = GetLastError();
4648 }
4649 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004650 }
4651
4652 /* Free up the native handle at this point */
4653 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00004654 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00004655
Mark Hammondb37a3732000-08-14 04:47:33 +00004656 /* Remove this file pointer from dictionary */
4657 PyDict_DelItem(_PyPopenProcs, fileObj);
4658
4659 if (PyDict_Size(_PyPopenProcs) == 0) {
4660 Py_DECREF(_PyPopenProcs);
4661 _PyPopenProcs = NULL;
4662 }
4663
4664 } /* if object retrieval ok */
4665
4666 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004667 } /* if _PyPopenProcs */
4668
Tim Peters736aa322000-09-01 06:51:24 +00004669#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004670 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00004671#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004672 return result;
4673}
Tim Peters9acdd3a2000-09-01 19:26:36 +00004674
4675#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00004676static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004677posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00004678{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004679 char *name;
4680 char *mode = "r";
4681 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00004682 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00004683 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004684 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00004685 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00004686 /* Strip mode of binary or text modifiers */
4687 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
4688 mode = "r";
4689 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
4690 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00004691 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004692 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004693 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00004694 if (fp == NULL)
4695 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004696 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004697 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00004698 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004699 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00004700}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004701
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004702#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004703#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00004704
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004705
Guido van Rossumb6775db1994-08-01 11:34:53 +00004706#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004707PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004708"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004709Set the current process's user id.");
4710
Barry Warsaw53699e91996-12-10 23:23:01 +00004711static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004712posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004713{
4714 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004715 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004716 return NULL;
4717 if (setuid(uid) < 0)
4718 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004719 Py_INCREF(Py_None);
4720 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004721}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004722#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004723
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004724
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004725#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004726PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004727"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004728Set the current process's effective user id.");
4729
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004730static PyObject *
4731posix_seteuid (PyObject *self, PyObject *args)
4732{
4733 int euid;
4734 if (!PyArg_ParseTuple(args, "i", &euid)) {
4735 return NULL;
4736 } else if (seteuid(euid) < 0) {
4737 return posix_error();
4738 } else {
4739 Py_INCREF(Py_None);
4740 return Py_None;
4741 }
4742}
4743#endif /* HAVE_SETEUID */
4744
4745#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004746PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004747"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004748Set the current process's effective group id.");
4749
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004750static PyObject *
4751posix_setegid (PyObject *self, PyObject *args)
4752{
4753 int egid;
4754 if (!PyArg_ParseTuple(args, "i", &egid)) {
4755 return NULL;
4756 } else if (setegid(egid) < 0) {
4757 return posix_error();
4758 } else {
4759 Py_INCREF(Py_None);
4760 return Py_None;
4761 }
4762}
4763#endif /* HAVE_SETEGID */
4764
4765#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004766PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004767"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004768Set the current process's real and effective user ids.");
4769
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004770static PyObject *
4771posix_setreuid (PyObject *self, PyObject *args)
4772{
4773 int ruid, euid;
4774 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4775 return NULL;
4776 } else if (setreuid(ruid, euid) < 0) {
4777 return posix_error();
4778 } else {
4779 Py_INCREF(Py_None);
4780 return Py_None;
4781 }
4782}
4783#endif /* HAVE_SETREUID */
4784
4785#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004786PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004787"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004788Set the current process's real and effective group ids.");
4789
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004790static PyObject *
4791posix_setregid (PyObject *self, PyObject *args)
4792{
4793 int rgid, egid;
4794 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4795 return NULL;
4796 } else if (setregid(rgid, egid) < 0) {
4797 return posix_error();
4798 } else {
4799 Py_INCREF(Py_None);
4800 return Py_None;
4801 }
4802}
4803#endif /* HAVE_SETREGID */
4804
Guido van Rossumb6775db1994-08-01 11:34:53 +00004805#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004806PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004807"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004808Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004809
Barry Warsaw53699e91996-12-10 23:23:01 +00004810static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004811posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004812{
4813 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004814 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004815 return NULL;
4816 if (setgid(gid) < 0)
4817 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004818 Py_INCREF(Py_None);
4819 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004820}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004821#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004822
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004823#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004824PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004825"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004826Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004827
4828static PyObject *
4829posix_setgroups(PyObject *self, PyObject *args)
4830{
4831 PyObject *groups;
4832 int i, len;
4833 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004834
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004835 if (!PyArg_ParseTuple(args, "O:setgid", &groups))
4836 return NULL;
4837 if (!PySequence_Check(groups)) {
4838 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4839 return NULL;
4840 }
4841 len = PySequence_Size(groups);
4842 if (len > MAX_GROUPS) {
4843 PyErr_SetString(PyExc_ValueError, "too many groups");
4844 return NULL;
4845 }
4846 for(i = 0; i < len; i++) {
4847 PyObject *elem;
4848 elem = PySequence_GetItem(groups, i);
4849 if (!elem)
4850 return NULL;
4851 if (!PyInt_Check(elem)) {
4852 PyErr_SetString(PyExc_TypeError,
4853 "groups must be integers");
4854 Py_DECREF(elem);
4855 return NULL;
4856 }
4857 /* XXX: check that value fits into gid_t. */
4858 grouplist[i] = PyInt_AsLong(elem);
4859 Py_DECREF(elem);
4860 }
4861
4862 if (setgroups(len, grouplist) < 0)
4863 return posix_error();
4864 Py_INCREF(Py_None);
4865 return Py_None;
4866}
4867#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004868
Guido van Rossumb6775db1994-08-01 11:34:53 +00004869#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004870PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004871"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004872Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004873
Barry Warsaw53699e91996-12-10 23:23:01 +00004874static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004875posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004876{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004877 int pid, options;
4878#ifdef UNION_WAIT
4879 union wait status;
4880#define status_i (status.w_status)
4881#else
4882 int status;
4883#define status_i status
4884#endif
4885 status_i = 0;
4886
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004887 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004888 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004889 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004890 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004891 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004892 if (pid == -1)
4893 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00004894 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004895 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00004896}
4897
Tim Petersab034fa2002-02-01 11:27:43 +00004898#elif defined(HAVE_CWAIT)
4899
4900/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004901PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004902"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004903"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004904
4905static PyObject *
4906posix_waitpid(PyObject *self, PyObject *args)
4907{
4908 int pid, options;
4909 int status;
4910
4911 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4912 return NULL;
4913 Py_BEGIN_ALLOW_THREADS
4914 pid = _cwait(&status, pid, options);
4915 Py_END_ALLOW_THREADS
4916 if (pid == -1)
4917 return posix_error();
4918 else
4919 /* shift the status left a byte so this is more like the
4920 POSIX waitpid */
4921 return Py_BuildValue("ii", pid, status << 8);
4922}
4923#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004924
Guido van Rossumad0ee831995-03-01 10:34:45 +00004925#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004926PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004927"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004928Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004929
Barry Warsaw53699e91996-12-10 23:23:01 +00004930static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004931posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004932{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004933 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004934#ifdef UNION_WAIT
4935 union wait status;
4936#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004937#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004938 int status;
4939#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004940#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00004941
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004942 status_i = 0;
4943 Py_BEGIN_ALLOW_THREADS
4944 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004945 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004946 if (pid == -1)
4947 return posix_error();
4948 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004949 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004950#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00004951}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004952#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004953
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004954
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004955PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004956"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004957Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004958
Barry Warsaw53699e91996-12-10 23:23:01 +00004959static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004960posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004961{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004962#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004963 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004964#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004965#ifdef MS_WINDOWS
Mark Hammond7edd0a92003-08-06 02:46:58 +00004966 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004967#else
4968 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4969#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004970#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004971}
4972
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004973
Guido van Rossumb6775db1994-08-01 11:34:53 +00004974#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004975PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004976"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004977Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004978
Barry Warsaw53699e91996-12-10 23:23:01 +00004979static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004980posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004981{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004982 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004983 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004984 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004985 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004986 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004987 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004988 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004989 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004990 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00004991 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00004992 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004993}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004994#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004995
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004996
Guido van Rossumb6775db1994-08-01 11:34:53 +00004997#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004998PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004999"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005000Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005001
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005002static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005003posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005004{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005005 return posix_2str(args, "etet:symlink", symlink, NULL, NULL);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005006}
5007#endif /* HAVE_SYMLINK */
5008
5009
5010#ifdef HAVE_TIMES
5011#ifndef HZ
5012#define HZ 60 /* Universal constant :-) */
5013#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005014
Guido van Rossumd48f2521997-12-05 22:19:34 +00005015#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5016static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005017system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005018{
5019 ULONG value = 0;
5020
5021 Py_BEGIN_ALLOW_THREADS
5022 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5023 Py_END_ALLOW_THREADS
5024
5025 return value;
5026}
5027
5028static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005029posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005030{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005031 /* Currently Only Uptime is Provided -- Others Later */
5032 return Py_BuildValue("ddddd",
5033 (double)0 /* t.tms_utime / HZ */,
5034 (double)0 /* t.tms_stime / HZ */,
5035 (double)0 /* t.tms_cutime / HZ */,
5036 (double)0 /* t.tms_cstime / HZ */,
5037 (double)system_uptime() / 1000);
5038}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005039#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005040static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005041posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005042{
5043 struct tms t;
5044 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005045 errno = 0;
5046 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005047 if (c == (clock_t) -1)
5048 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005049 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005050 (double)t.tms_utime / HZ,
5051 (double)t.tms_stime / HZ,
5052 (double)t.tms_cutime / HZ,
5053 (double)t.tms_cstime / HZ,
5054 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005055}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005056#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005057#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005058
5059
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005060#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005061#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005062static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005063posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005064{
5065 FILETIME create, exit, kernel, user;
5066 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005067 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005068 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5069 /* The fields of a FILETIME structure are the hi and lo part
5070 of a 64-bit value expressed in 100 nanosecond units.
5071 1e7 is one second in such units; 1e-7 the inverse.
5072 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5073 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005074 return Py_BuildValue(
5075 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005076 (double)(kernel.dwHighDateTime*429.4967296 +
5077 kernel.dwLowDateTime*1e-7),
5078 (double)(user.dwHighDateTime*429.4967296 +
5079 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005080 (double)0,
5081 (double)0,
5082 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005083}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005084#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005085
5086#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005087PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005088"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005089Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005090#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005091
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005092
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005093#ifdef HAVE_GETSID
5094PyDoc_STRVAR(posix_getsid__doc__,
5095"getsid(pid) -> sid\n\n\
5096Call the system call getsid().");
5097
5098static PyObject *
5099posix_getsid(PyObject *self, PyObject *args)
5100{
5101 int pid, sid;
5102 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5103 return NULL;
5104 sid = getsid(pid);
5105 if (sid < 0)
5106 return posix_error();
5107 return PyInt_FromLong((long)sid);
5108}
5109#endif /* HAVE_GETSID */
5110
5111
Guido van Rossumb6775db1994-08-01 11:34:53 +00005112#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005113PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005114"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005115Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005116
Barry Warsaw53699e91996-12-10 23:23:01 +00005117static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005118posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005119{
Guido van Rossum687dd131993-05-17 08:34:16 +00005120 if (setsid() < 0)
5121 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005122 Py_INCREF(Py_None);
5123 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005124}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005125#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005126
Guido van Rossumb6775db1994-08-01 11:34:53 +00005127#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005128PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005129"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005130Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005131
Barry Warsaw53699e91996-12-10 23:23:01 +00005132static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005133posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005134{
5135 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005136 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005137 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005138 if (setpgid(pid, pgrp) < 0)
5139 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005140 Py_INCREF(Py_None);
5141 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005142}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005143#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005144
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005145
Guido van Rossumb6775db1994-08-01 11:34:53 +00005146#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005147PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005148"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005149Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005150
Barry Warsaw53699e91996-12-10 23:23:01 +00005151static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005152posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005153{
5154 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005155 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005156 return NULL;
5157 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005158 if (pgid < 0)
5159 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005160 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005161}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005162#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005163
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005164
Guido van Rossumb6775db1994-08-01 11:34:53 +00005165#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005166PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005167"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005168Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005169
Barry Warsaw53699e91996-12-10 23:23:01 +00005170static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005171posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005172{
5173 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005174 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005175 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005176 if (tcsetpgrp(fd, pgid) < 0)
5177 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005178 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005179 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005180}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005181#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005182
Guido van Rossum687dd131993-05-17 08:34:16 +00005183/* Functions acting on file descriptors */
5184
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005185PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005186"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005187Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005188
Barry Warsaw53699e91996-12-10 23:23:01 +00005189static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005190posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005191{
Mark Hammondef8b6542001-05-13 08:04:26 +00005192 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005193 int flag;
5194 int mode = 0777;
5195 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005196
5197#ifdef MS_WINDOWS
5198 if (unicode_file_names()) {
5199 PyUnicodeObject *po;
5200 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5201 Py_BEGIN_ALLOW_THREADS
5202 /* PyUnicode_AS_UNICODE OK without thread
5203 lock as it is a simple dereference. */
5204 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5205 Py_END_ALLOW_THREADS
5206 if (fd < 0)
5207 return posix_error();
5208 return PyInt_FromLong((long)fd);
5209 }
5210 /* Drop the argument parsing error as narrow strings
5211 are also valid. */
5212 PyErr_Clear();
5213 }
5214#endif
5215
Tim Peters5aa91602002-01-30 05:46:57 +00005216 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00005217 Py_FileSystemDefaultEncoding, &file,
5218 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00005219 return NULL;
5220
Barry Warsaw53699e91996-12-10 23:23:01 +00005221 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005222 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005223 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005224 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00005225 return posix_error_with_allocated_filename(file);
5226 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00005227 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005228}
5229
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005230
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005231PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005232"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005233Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005234
Barry Warsaw53699e91996-12-10 23:23:01 +00005235static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005236posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005237{
5238 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005239 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005240 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005241 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005242 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005243 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005244 if (res < 0)
5245 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005246 Py_INCREF(Py_None);
5247 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005248}
5249
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005250
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005251PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005252"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005253Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005254
Barry Warsaw53699e91996-12-10 23:23:01 +00005255static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005256posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005257{
5258 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005259 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005260 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005261 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005262 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005263 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005264 if (fd < 0)
5265 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005266 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005267}
5268
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005269
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005270PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005271"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005272Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005273
Barry Warsaw53699e91996-12-10 23:23:01 +00005274static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005275posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005276{
5277 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005278 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005279 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005280 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005281 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005282 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005283 if (res < 0)
5284 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005285 Py_INCREF(Py_None);
5286 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005287}
5288
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005289
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005290PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005291"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005292Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005293
Barry Warsaw53699e91996-12-10 23:23:01 +00005294static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005295posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005296{
5297 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005298#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005299 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005300#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005301 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005302#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005303 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005304 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005305 return NULL;
5306#ifdef SEEK_SET
5307 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5308 switch (how) {
5309 case 0: how = SEEK_SET; break;
5310 case 1: how = SEEK_CUR; break;
5311 case 2: how = SEEK_END; break;
5312 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005313#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005314
5315#if !defined(HAVE_LARGEFILE_SUPPORT)
5316 pos = PyInt_AsLong(posobj);
5317#else
5318 pos = PyLong_Check(posobj) ?
5319 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
5320#endif
5321 if (PyErr_Occurred())
5322 return NULL;
5323
Barry Warsaw53699e91996-12-10 23:23:01 +00005324 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005325#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005326 res = _lseeki64(fd, pos, how);
5327#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005328 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005329#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005330 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005331 if (res < 0)
5332 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005333
5334#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00005335 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005336#else
5337 return PyLong_FromLongLong(res);
5338#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005339}
5340
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005341
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005342PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005343"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005344Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005345
Barry Warsaw53699e91996-12-10 23:23:01 +00005346static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005347posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005348{
Guido van Rossum8bac5461996-06-11 18:38:48 +00005349 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005350 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005351 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005352 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005353 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005354 if (buffer == NULL)
5355 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005356 Py_BEGIN_ALLOW_THREADS
5357 n = read(fd, PyString_AsString(buffer), size);
5358 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005359 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005360 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005361 return posix_error();
5362 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005363 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00005364 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005365 return buffer;
5366}
5367
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005368
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005369PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005370"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005371Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005372
Barry Warsaw53699e91996-12-10 23:23:01 +00005373static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005374posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005375{
5376 int fd, size;
5377 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005378 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005379 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005380 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005381 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005382 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005383 if (size < 0)
5384 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005385 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005386}
5387
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005388
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005389PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005390"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005391Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005392
Barry Warsaw53699e91996-12-10 23:23:01 +00005393static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005394posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005395{
5396 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005397 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005398 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005399 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005400 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005401#ifdef __VMS
5402 /* on OpenVMS we must ensure that all bytes are written to the file */
5403 fsync(fd);
5404#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005405 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005406 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005407 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005408 if (res != 0)
5409 return posix_error();
Tim Peters5aa91602002-01-30 05:46:57 +00005410
Fred Drake699f3522000-06-29 21:12:41 +00005411 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005412}
5413
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005414
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005415PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005416"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005417Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005418
Barry Warsaw53699e91996-12-10 23:23:01 +00005419static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005420posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005421{
Guido van Rossum687dd131993-05-17 08:34:16 +00005422 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005423 char *mode = "r";
5424 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00005425 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005426 PyObject *f;
5427 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00005428 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00005429
Thomas Heller1f043e22002-11-07 16:00:59 +00005430 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
5431 PyErr_Format(PyExc_ValueError,
5432 "invalid file mode '%s'", mode);
5433 return NULL;
5434 }
5435
Barry Warsaw53699e91996-12-10 23:23:01 +00005436 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005437 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005438 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005439 if (fp == NULL)
5440 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00005441 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005442 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005443 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005444 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00005445}
5446
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005447PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005448"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005449Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005450connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005451
5452static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005453posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005454{
5455 int fd;
5456 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5457 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005458 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005459}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005460
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005461#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005462PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005463"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005464Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005465
Barry Warsaw53699e91996-12-10 23:23:01 +00005466static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005467posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005468{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005469#if defined(PYOS_OS2)
5470 HFILE read, write;
5471 APIRET rc;
5472
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005473 Py_BEGIN_ALLOW_THREADS
5474 rc = DosCreatePipe( &read, &write, 4096);
5475 Py_END_ALLOW_THREADS
5476 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005477 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005478
5479 return Py_BuildValue("(ii)", read, write);
5480#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005481#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005482 int fds[2];
5483 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005484 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005485 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005486 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005487 if (res != 0)
5488 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005489 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005490#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005491 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005492 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005493 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005494 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005495 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005496 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005497 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005498 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005499 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5500 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005501 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005502#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005503#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005504}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005505#endif /* HAVE_PIPE */
5506
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005507
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005508#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005509PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005510"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005511Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005512
Barry Warsaw53699e91996-12-10 23:23:01 +00005513static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005514posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005515{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005516 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005517 int mode = 0666;
5518 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005519 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005520 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005521 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005522 res = mkfifo(filename, mode);
5523 Py_END_ALLOW_THREADS
5524 if (res < 0)
5525 return posix_error();
5526 Py_INCREF(Py_None);
5527 return Py_None;
5528}
5529#endif
5530
5531
Neal Norwitz11690112002-07-30 01:08:28 +00005532#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005533PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005534"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005535Create a filesystem node (file, device special file or named pipe)\n\
5536named filename. mode specifies both the permissions to use and the\n\
5537type of node to be created, being combined (bitwise OR) with one of\n\
5538S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005539device defines the newly created device special file (probably using\n\
5540os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005541
5542
5543static PyObject *
5544posix_mknod(PyObject *self, PyObject *args)
5545{
5546 char *filename;
5547 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005548 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005549 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005550 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005551 return NULL;
5552 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005553 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005554 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005555 if (res < 0)
5556 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005557 Py_INCREF(Py_None);
5558 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005559}
5560#endif
5561
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005562#ifdef HAVE_DEVICE_MACROS
5563PyDoc_STRVAR(posix_major__doc__,
5564"major(device) -> major number\n\
5565Extracts a device major number from a raw device number.");
5566
5567static PyObject *
5568posix_major(PyObject *self, PyObject *args)
5569{
5570 int device;
5571 if (!PyArg_ParseTuple(args, "i:major", &device))
5572 return NULL;
5573 return PyInt_FromLong((long)major(device));
5574}
5575
5576PyDoc_STRVAR(posix_minor__doc__,
5577"minor(device) -> minor number\n\
5578Extracts a device minor number from a raw device number.");
5579
5580static PyObject *
5581posix_minor(PyObject *self, PyObject *args)
5582{
5583 int device;
5584 if (!PyArg_ParseTuple(args, "i:minor", &device))
5585 return NULL;
5586 return PyInt_FromLong((long)minor(device));
5587}
5588
5589PyDoc_STRVAR(posix_makedev__doc__,
5590"makedev(major, minor) -> device number\n\
5591Composes a raw device number from the major and minor device numbers.");
5592
5593static PyObject *
5594posix_makedev(PyObject *self, PyObject *args)
5595{
5596 int major, minor;
5597 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5598 return NULL;
5599 return PyInt_FromLong((long)makedev(major, minor));
5600}
5601#endif /* device macros */
5602
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005603
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005604#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005605PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005606"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005607Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005608
Barry Warsaw53699e91996-12-10 23:23:01 +00005609static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005610posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005611{
5612 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005613 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005614 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005615 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005616
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005617 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005618 return NULL;
5619
5620#if !defined(HAVE_LARGEFILE_SUPPORT)
5621 length = PyInt_AsLong(lenobj);
5622#else
5623 length = PyLong_Check(lenobj) ?
5624 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
5625#endif
5626 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005627 return NULL;
5628
Barry Warsaw53699e91996-12-10 23:23:01 +00005629 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005630 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005631 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005632 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005633 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005634 return NULL;
5635 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005636 Py_INCREF(Py_None);
5637 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005638}
5639#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005640
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005641#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005642PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005643"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005644Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005645
Fred Drake762e2061999-08-26 17:23:54 +00005646/* Save putenv() parameters as values here, so we can collect them when they
5647 * get re-set with another call for the same key. */
5648static PyObject *posix_putenv_garbage;
5649
Tim Peters5aa91602002-01-30 05:46:57 +00005650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005651posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005652{
5653 char *s1, *s2;
5654 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00005655 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005656 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005657
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005658 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005659 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005660
5661#if defined(PYOS_OS2)
5662 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5663 APIRET rc;
5664
Guido van Rossumd48f2521997-12-05 22:19:34 +00005665 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5666 if (rc != NO_ERROR)
5667 return os2_error(rc);
5668
5669 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5670 APIRET rc;
5671
Guido van Rossumd48f2521997-12-05 22:19:34 +00005672 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5673 if (rc != NO_ERROR)
5674 return os2_error(rc);
5675 } else {
5676#endif
5677
Fred Drake762e2061999-08-26 17:23:54 +00005678 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005679 len = strlen(s1) + strlen(s2) + 2;
5680 /* len includes space for a trailing \0; the size arg to
5681 PyString_FromStringAndSize does not count that */
5682 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005683 if (newstr == NULL)
5684 return PyErr_NoMemory();
5685 new = PyString_AS_STRING(newstr);
Tim Petersc8996f52001-12-03 20:41:00 +00005686 PyOS_snprintf(new, len, "%s=%s", s1, s2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005687 if (putenv(new)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005688 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005689 posix_error();
5690 return NULL;
5691 }
Fred Drake762e2061999-08-26 17:23:54 +00005692 /* Install the first arg and newstr in posix_putenv_garbage;
5693 * this will cause previous value to be collected. This has to
5694 * happen after the real putenv() call because the old value
5695 * was still accessible until then. */
5696 if (PyDict_SetItem(posix_putenv_garbage,
5697 PyTuple_GET_ITEM(args, 0), newstr)) {
5698 /* really not much we can do; just leak */
5699 PyErr_Clear();
5700 }
5701 else {
5702 Py_DECREF(newstr);
5703 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005704
5705#if defined(PYOS_OS2)
5706 }
5707#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005708 Py_INCREF(Py_None);
5709 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005710}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005711#endif /* putenv */
5712
Guido van Rossumc524d952001-10-19 01:31:59 +00005713#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005714PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005715"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005716Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005717
5718static PyObject *
5719posix_unsetenv(PyObject *self, PyObject *args)
5720{
5721 char *s1;
5722
5723 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5724 return NULL;
5725
5726 unsetenv(s1);
5727
5728 /* Remove the key from posix_putenv_garbage;
5729 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005730 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005731 * old value was still accessible until then.
5732 */
5733 if (PyDict_DelItem(posix_putenv_garbage,
5734 PyTuple_GET_ITEM(args, 0))) {
5735 /* really not much we can do; just leak */
5736 PyErr_Clear();
5737 }
5738
5739 Py_INCREF(Py_None);
5740 return Py_None;
5741}
5742#endif /* unsetenv */
5743
Guido van Rossumb6a47161997-09-15 22:54:34 +00005744#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005745PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005746"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005747Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005748
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005749static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005750posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005751{
5752 int code;
5753 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005754 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005755 return NULL;
5756 message = strerror(code);
5757 if (message == NULL) {
5758 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005759 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005760 return NULL;
5761 }
5762 return PyString_FromString(message);
5763}
5764#endif /* strerror */
5765
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005766
Guido van Rossumc9641791998-08-04 15:26:23 +00005767#ifdef HAVE_SYS_WAIT_H
5768
Fred Drake106c1a02002-04-23 15:58:02 +00005769#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005770PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005771"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005772Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005773
5774static PyObject *
5775posix_WCOREDUMP(PyObject *self, PyObject *args)
5776{
5777#ifdef UNION_WAIT
5778 union wait status;
5779#define status_i (status.w_status)
5780#else
5781 int status;
5782#define status_i status
5783#endif
5784 status_i = 0;
5785
5786 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i))
5787 {
5788 return NULL;
5789 }
5790
5791 return PyBool_FromLong(WCOREDUMP(status));
5792#undef status_i
5793}
5794#endif /* WCOREDUMP */
5795
5796#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005797PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005798"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005799Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005800job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005801
5802static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005803posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005804{
5805#ifdef UNION_WAIT
5806 union wait status;
5807#define status_i (status.w_status)
5808#else
5809 int status;
5810#define status_i status
5811#endif
5812 status_i = 0;
5813
5814 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i))
5815 {
5816 return NULL;
5817 }
5818
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005819 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005820#undef status_i
5821}
5822#endif /* WIFCONTINUED */
5823
Guido van Rossumc9641791998-08-04 15:26:23 +00005824#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005825PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005826"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005827Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005828
5829static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005830posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005831{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005832#ifdef UNION_WAIT
5833 union wait status;
5834#define status_i (status.w_status)
5835#else
5836 int status;
5837#define status_i status
5838#endif
5839 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005840
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005841 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005842 {
5843 return NULL;
5844 }
Tim Peters5aa91602002-01-30 05:46:57 +00005845
Fred Drake106c1a02002-04-23 15:58:02 +00005846 return PyBool_FromLong(WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005847#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005848}
5849#endif /* WIFSTOPPED */
5850
5851#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005852PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005853"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005854Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005855
5856static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005857posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005858{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005859#ifdef UNION_WAIT
5860 union wait status;
5861#define status_i (status.w_status)
5862#else
5863 int status;
5864#define status_i status
5865#endif
5866 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005867
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005868 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005869 {
5870 return NULL;
5871 }
Tim Peters5aa91602002-01-30 05:46:57 +00005872
Fred Drake106c1a02002-04-23 15:58:02 +00005873 return PyBool_FromLong(WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005874#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005875}
5876#endif /* WIFSIGNALED */
5877
5878#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005879PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005880"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005881Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005882system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005883
5884static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005885posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005886{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005887#ifdef UNION_WAIT
5888 union wait status;
5889#define status_i (status.w_status)
5890#else
5891 int status;
5892#define status_i status
5893#endif
5894 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005895
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005896 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005897 {
5898 return NULL;
5899 }
Tim Peters5aa91602002-01-30 05:46:57 +00005900
Fred Drake106c1a02002-04-23 15:58:02 +00005901 return PyBool_FromLong(WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005902#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005903}
5904#endif /* WIFEXITED */
5905
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005906#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005907PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005908"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005909Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005910
5911static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005912posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005913{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005914#ifdef UNION_WAIT
5915 union wait status;
5916#define status_i (status.w_status)
5917#else
5918 int status;
5919#define status_i status
5920#endif
5921 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005922
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005923 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005924 {
5925 return NULL;
5926 }
Tim Peters5aa91602002-01-30 05:46:57 +00005927
Guido van Rossumc9641791998-08-04 15:26:23 +00005928 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005929#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005930}
5931#endif /* WEXITSTATUS */
5932
5933#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005934PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005935"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005936Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005937value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005938
5939static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005940posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005941{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005942#ifdef UNION_WAIT
5943 union wait status;
5944#define status_i (status.w_status)
5945#else
5946 int status;
5947#define status_i status
5948#endif
5949 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005950
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005951 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005952 {
5953 return NULL;
5954 }
Tim Peters5aa91602002-01-30 05:46:57 +00005955
Guido van Rossumc9641791998-08-04 15:26:23 +00005956 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005957#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005958}
5959#endif /* WTERMSIG */
5960
5961#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005962PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005963"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005964Return the signal that stopped the process that provided\n\
5965the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005966
5967static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005968posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005969{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005970#ifdef UNION_WAIT
5971 union wait status;
5972#define status_i (status.w_status)
5973#else
5974 int status;
5975#define status_i status
5976#endif
5977 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005978
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005979 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005980 {
5981 return NULL;
5982 }
Tim Peters5aa91602002-01-30 05:46:57 +00005983
Guido van Rossumc9641791998-08-04 15:26:23 +00005984 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005985#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005986}
5987#endif /* WSTOPSIG */
5988
5989#endif /* HAVE_SYS_WAIT_H */
5990
5991
Guido van Rossum94f6f721999-01-06 18:42:14 +00005992#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005993#ifdef _SCO_DS
5994/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5995 needed definitions in sys/statvfs.h */
5996#define _SVID3
5997#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005998#include <sys/statvfs.h>
5999
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006000static PyObject*
6001_pystatvfs_fromstructstatvfs(struct statvfs st) {
6002 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6003 if (v == NULL)
6004 return NULL;
6005
6006#if !defined(HAVE_LARGEFILE_SUPPORT)
6007 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6008 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6009 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6010 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6011 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6012 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6013 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6014 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6015 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6016 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6017#else
6018 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6019 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006020 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006021 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006022 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006023 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006024 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006025 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006026 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006027 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006028 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006029 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006030 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006031 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006032 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6033 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6034#endif
6035
6036 return v;
6037}
6038
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006039PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006040"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006041Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006042
6043static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006044posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006045{
6046 int fd, res;
6047 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006048
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006049 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006050 return NULL;
6051 Py_BEGIN_ALLOW_THREADS
6052 res = fstatvfs(fd, &st);
6053 Py_END_ALLOW_THREADS
6054 if (res != 0)
6055 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006056
6057 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006058}
6059#endif /* HAVE_FSTATVFS */
6060
6061
6062#if defined(HAVE_STATVFS)
6063#include <sys/statvfs.h>
6064
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006065PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006066"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006067Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006068
6069static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006070posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006071{
6072 char *path;
6073 int res;
6074 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006075 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006076 return NULL;
6077 Py_BEGIN_ALLOW_THREADS
6078 res = statvfs(path, &st);
6079 Py_END_ALLOW_THREADS
6080 if (res != 0)
6081 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006082
6083 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006084}
6085#endif /* HAVE_STATVFS */
6086
6087
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006088#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006089PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006090"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006091Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006092The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006093or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006094
6095static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006096posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006097{
6098 PyObject *result = NULL;
6099 char *dir = NULL;
6100 char *pfx = NULL;
6101 char *name;
6102
6103 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6104 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006105
6106 if (PyErr_Warn(PyExc_RuntimeWarning,
6107 "tempnam is a potential security risk to your program") < 0)
6108 return NULL;
6109
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006110#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006111 name = _tempnam(dir, pfx);
6112#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006113 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006114#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006115 if (name == NULL)
6116 return PyErr_NoMemory();
6117 result = PyString_FromString(name);
6118 free(name);
6119 return result;
6120}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006121#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006122
6123
6124#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006125PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006126"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006127Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006128
6129static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006130posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006131{
6132 FILE *fp;
6133
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006134 fp = tmpfile();
6135 if (fp == NULL)
6136 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006137 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006138}
6139#endif
6140
6141
6142#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006143PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006144"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006145Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006146
6147static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006148posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006149{
6150 char buffer[L_tmpnam];
6151 char *name;
6152
Skip Montanaro95618b52001-08-18 18:52:10 +00006153 if (PyErr_Warn(PyExc_RuntimeWarning,
6154 "tmpnam is a potential security risk to your program") < 0)
6155 return NULL;
6156
Greg Wardb48bc172000-03-01 21:51:56 +00006157#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006158 name = tmpnam_r(buffer);
6159#else
6160 name = tmpnam(buffer);
6161#endif
6162 if (name == NULL) {
6163 PyErr_SetObject(PyExc_OSError,
6164 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006165#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006166 "unexpected NULL from tmpnam_r"
6167#else
6168 "unexpected NULL from tmpnam"
6169#endif
6170 ));
6171 return NULL;
6172 }
6173 return PyString_FromString(buffer);
6174}
6175#endif
6176
6177
Fred Drakec9680921999-12-13 16:37:25 +00006178/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6179 * It maps strings representing configuration variable names to
6180 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006181 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006182 * rarely-used constants. There are three separate tables that use
6183 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006184 *
6185 * This code is always included, even if none of the interfaces that
6186 * need it are included. The #if hackery needed to avoid it would be
6187 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006188 */
6189struct constdef {
6190 char *name;
6191 long value;
6192};
6193
Fred Drake12c6e2d1999-12-14 21:25:03 +00006194static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006195conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6196 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006197{
6198 if (PyInt_Check(arg)) {
6199 *valuep = PyInt_AS_LONG(arg);
6200 return 1;
6201 }
6202 if (PyString_Check(arg)) {
6203 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006204 size_t lo = 0;
6205 size_t mid;
6206 size_t hi = tablesize;
6207 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006208 char *confname = PyString_AS_STRING(arg);
6209 while (lo < hi) {
6210 mid = (lo + hi) / 2;
6211 cmp = strcmp(confname, table[mid].name);
6212 if (cmp < 0)
6213 hi = mid;
6214 else if (cmp > 0)
6215 lo = mid + 1;
6216 else {
6217 *valuep = table[mid].value;
6218 return 1;
6219 }
6220 }
6221 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6222 }
6223 else
6224 PyErr_SetString(PyExc_TypeError,
6225 "configuration names must be strings or integers");
6226 return 0;
6227}
6228
6229
6230#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6231static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006232#ifdef _PC_ABI_AIO_XFER_MAX
6233 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6234#endif
6235#ifdef _PC_ABI_ASYNC_IO
6236 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6237#endif
Fred Drakec9680921999-12-13 16:37:25 +00006238#ifdef _PC_ASYNC_IO
6239 {"PC_ASYNC_IO", _PC_ASYNC_IO},
6240#endif
6241#ifdef _PC_CHOWN_RESTRICTED
6242 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
6243#endif
6244#ifdef _PC_FILESIZEBITS
6245 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
6246#endif
6247#ifdef _PC_LAST
6248 {"PC_LAST", _PC_LAST},
6249#endif
6250#ifdef _PC_LINK_MAX
6251 {"PC_LINK_MAX", _PC_LINK_MAX},
6252#endif
6253#ifdef _PC_MAX_CANON
6254 {"PC_MAX_CANON", _PC_MAX_CANON},
6255#endif
6256#ifdef _PC_MAX_INPUT
6257 {"PC_MAX_INPUT", _PC_MAX_INPUT},
6258#endif
6259#ifdef _PC_NAME_MAX
6260 {"PC_NAME_MAX", _PC_NAME_MAX},
6261#endif
6262#ifdef _PC_NO_TRUNC
6263 {"PC_NO_TRUNC", _PC_NO_TRUNC},
6264#endif
6265#ifdef _PC_PATH_MAX
6266 {"PC_PATH_MAX", _PC_PATH_MAX},
6267#endif
6268#ifdef _PC_PIPE_BUF
6269 {"PC_PIPE_BUF", _PC_PIPE_BUF},
6270#endif
6271#ifdef _PC_PRIO_IO
6272 {"PC_PRIO_IO", _PC_PRIO_IO},
6273#endif
6274#ifdef _PC_SOCK_MAXBUF
6275 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
6276#endif
6277#ifdef _PC_SYNC_IO
6278 {"PC_SYNC_IO", _PC_SYNC_IO},
6279#endif
6280#ifdef _PC_VDISABLE
6281 {"PC_VDISABLE", _PC_VDISABLE},
6282#endif
6283};
6284
Fred Drakec9680921999-12-13 16:37:25 +00006285static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006286conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006287{
6288 return conv_confname(arg, valuep, posix_constants_pathconf,
6289 sizeof(posix_constants_pathconf)
6290 / sizeof(struct constdef));
6291}
6292#endif
6293
6294#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006295PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006296"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006297Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006298If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006299
6300static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006301posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006302{
6303 PyObject *result = NULL;
6304 int name, fd;
6305
Fred Drake12c6e2d1999-12-14 21:25:03 +00006306 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6307 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00006308 long limit;
6309
6310 errno = 0;
6311 limit = fpathconf(fd, name);
6312 if (limit == -1 && errno != 0)
6313 posix_error();
6314 else
6315 result = PyInt_FromLong(limit);
6316 }
6317 return result;
6318}
6319#endif
6320
6321
6322#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006323PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006324"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006325Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006326If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006327
6328static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006329posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006330{
6331 PyObject *result = NULL;
6332 int name;
6333 char *path;
6334
6335 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6336 conv_path_confname, &name)) {
6337 long limit;
6338
6339 errno = 0;
6340 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006341 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00006342 if (errno == EINVAL)
6343 /* could be a path or name problem */
6344 posix_error();
6345 else
6346 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006347 }
Fred Drakec9680921999-12-13 16:37:25 +00006348 else
6349 result = PyInt_FromLong(limit);
6350 }
6351 return result;
6352}
6353#endif
6354
6355#ifdef HAVE_CONFSTR
6356static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006357#ifdef _CS_ARCHITECTURE
6358 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6359#endif
6360#ifdef _CS_HOSTNAME
6361 {"CS_HOSTNAME", _CS_HOSTNAME},
6362#endif
6363#ifdef _CS_HW_PROVIDER
6364 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6365#endif
6366#ifdef _CS_HW_SERIAL
6367 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6368#endif
6369#ifdef _CS_INITTAB_NAME
6370 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6371#endif
Fred Drakec9680921999-12-13 16:37:25 +00006372#ifdef _CS_LFS64_CFLAGS
6373 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6374#endif
6375#ifdef _CS_LFS64_LDFLAGS
6376 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6377#endif
6378#ifdef _CS_LFS64_LIBS
6379 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6380#endif
6381#ifdef _CS_LFS64_LINTFLAGS
6382 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6383#endif
6384#ifdef _CS_LFS_CFLAGS
6385 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6386#endif
6387#ifdef _CS_LFS_LDFLAGS
6388 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6389#endif
6390#ifdef _CS_LFS_LIBS
6391 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6392#endif
6393#ifdef _CS_LFS_LINTFLAGS
6394 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6395#endif
Fred Draked86ed291999-12-15 15:34:33 +00006396#ifdef _CS_MACHINE
6397 {"CS_MACHINE", _CS_MACHINE},
6398#endif
Fred Drakec9680921999-12-13 16:37:25 +00006399#ifdef _CS_PATH
6400 {"CS_PATH", _CS_PATH},
6401#endif
Fred Draked86ed291999-12-15 15:34:33 +00006402#ifdef _CS_RELEASE
6403 {"CS_RELEASE", _CS_RELEASE},
6404#endif
6405#ifdef _CS_SRPC_DOMAIN
6406 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6407#endif
6408#ifdef _CS_SYSNAME
6409 {"CS_SYSNAME", _CS_SYSNAME},
6410#endif
6411#ifdef _CS_VERSION
6412 {"CS_VERSION", _CS_VERSION},
6413#endif
Fred Drakec9680921999-12-13 16:37:25 +00006414#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6415 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6416#endif
6417#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6418 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6419#endif
6420#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6421 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6422#endif
6423#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6424 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6425#endif
6426#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6427 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6428#endif
6429#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6430 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6431#endif
6432#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6433 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6434#endif
6435#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6436 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6437#endif
6438#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6439 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6440#endif
6441#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6442 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6443#endif
6444#ifdef _CS_XBS5_LP64_OFF64_LIBS
6445 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6446#endif
6447#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6448 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6449#endif
6450#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6451 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6452#endif
6453#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6454 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6455#endif
6456#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6457 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6458#endif
6459#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6460 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6461#endif
Fred Draked86ed291999-12-15 15:34:33 +00006462#ifdef _MIPS_CS_AVAIL_PROCESSORS
6463 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6464#endif
6465#ifdef _MIPS_CS_BASE
6466 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6467#endif
6468#ifdef _MIPS_CS_HOSTID
6469 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6470#endif
6471#ifdef _MIPS_CS_HW_NAME
6472 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6473#endif
6474#ifdef _MIPS_CS_NUM_PROCESSORS
6475 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6476#endif
6477#ifdef _MIPS_CS_OSREL_MAJ
6478 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6479#endif
6480#ifdef _MIPS_CS_OSREL_MIN
6481 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6482#endif
6483#ifdef _MIPS_CS_OSREL_PATCH
6484 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6485#endif
6486#ifdef _MIPS_CS_OS_NAME
6487 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6488#endif
6489#ifdef _MIPS_CS_OS_PROVIDER
6490 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6491#endif
6492#ifdef _MIPS_CS_PROCESSORS
6493 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6494#endif
6495#ifdef _MIPS_CS_SERIAL
6496 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6497#endif
6498#ifdef _MIPS_CS_VENDOR
6499 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6500#endif
Fred Drakec9680921999-12-13 16:37:25 +00006501};
6502
6503static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006504conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006505{
6506 return conv_confname(arg, valuep, posix_constants_confstr,
6507 sizeof(posix_constants_confstr)
6508 / sizeof(struct constdef));
6509}
6510
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006511PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006512"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006513Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006514
6515static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006516posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006517{
6518 PyObject *result = NULL;
6519 int name;
6520 char buffer[64];
6521
6522 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
6523 int len = confstr(name, buffer, sizeof(buffer));
6524
Fred Drakec9680921999-12-13 16:37:25 +00006525 errno = 0;
6526 if (len == 0) {
6527 if (errno != 0)
6528 posix_error();
6529 else
6530 result = PyString_FromString("");
6531 }
6532 else {
6533 if (len >= sizeof(buffer)) {
6534 result = PyString_FromStringAndSize(NULL, len);
6535 if (result != NULL)
6536 confstr(name, PyString_AS_STRING(result), len+1);
6537 }
6538 else
6539 result = PyString_FromString(buffer);
6540 }
6541 }
6542 return result;
6543}
6544#endif
6545
6546
6547#ifdef HAVE_SYSCONF
6548static struct constdef posix_constants_sysconf[] = {
6549#ifdef _SC_2_CHAR_TERM
6550 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6551#endif
6552#ifdef _SC_2_C_BIND
6553 {"SC_2_C_BIND", _SC_2_C_BIND},
6554#endif
6555#ifdef _SC_2_C_DEV
6556 {"SC_2_C_DEV", _SC_2_C_DEV},
6557#endif
6558#ifdef _SC_2_C_VERSION
6559 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6560#endif
6561#ifdef _SC_2_FORT_DEV
6562 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6563#endif
6564#ifdef _SC_2_FORT_RUN
6565 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6566#endif
6567#ifdef _SC_2_LOCALEDEF
6568 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6569#endif
6570#ifdef _SC_2_SW_DEV
6571 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6572#endif
6573#ifdef _SC_2_UPE
6574 {"SC_2_UPE", _SC_2_UPE},
6575#endif
6576#ifdef _SC_2_VERSION
6577 {"SC_2_VERSION", _SC_2_VERSION},
6578#endif
Fred Draked86ed291999-12-15 15:34:33 +00006579#ifdef _SC_ABI_ASYNCHRONOUS_IO
6580 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6581#endif
6582#ifdef _SC_ACL
6583 {"SC_ACL", _SC_ACL},
6584#endif
Fred Drakec9680921999-12-13 16:37:25 +00006585#ifdef _SC_AIO_LISTIO_MAX
6586 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6587#endif
Fred Drakec9680921999-12-13 16:37:25 +00006588#ifdef _SC_AIO_MAX
6589 {"SC_AIO_MAX", _SC_AIO_MAX},
6590#endif
6591#ifdef _SC_AIO_PRIO_DELTA_MAX
6592 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6593#endif
6594#ifdef _SC_ARG_MAX
6595 {"SC_ARG_MAX", _SC_ARG_MAX},
6596#endif
6597#ifdef _SC_ASYNCHRONOUS_IO
6598 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6599#endif
6600#ifdef _SC_ATEXIT_MAX
6601 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6602#endif
Fred Draked86ed291999-12-15 15:34:33 +00006603#ifdef _SC_AUDIT
6604 {"SC_AUDIT", _SC_AUDIT},
6605#endif
Fred Drakec9680921999-12-13 16:37:25 +00006606#ifdef _SC_AVPHYS_PAGES
6607 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6608#endif
6609#ifdef _SC_BC_BASE_MAX
6610 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6611#endif
6612#ifdef _SC_BC_DIM_MAX
6613 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6614#endif
6615#ifdef _SC_BC_SCALE_MAX
6616 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6617#endif
6618#ifdef _SC_BC_STRING_MAX
6619 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6620#endif
Fred Draked86ed291999-12-15 15:34:33 +00006621#ifdef _SC_CAP
6622 {"SC_CAP", _SC_CAP},
6623#endif
Fred Drakec9680921999-12-13 16:37:25 +00006624#ifdef _SC_CHARCLASS_NAME_MAX
6625 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6626#endif
6627#ifdef _SC_CHAR_BIT
6628 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6629#endif
6630#ifdef _SC_CHAR_MAX
6631 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6632#endif
6633#ifdef _SC_CHAR_MIN
6634 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6635#endif
6636#ifdef _SC_CHILD_MAX
6637 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6638#endif
6639#ifdef _SC_CLK_TCK
6640 {"SC_CLK_TCK", _SC_CLK_TCK},
6641#endif
6642#ifdef _SC_COHER_BLKSZ
6643 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6644#endif
6645#ifdef _SC_COLL_WEIGHTS_MAX
6646 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6647#endif
6648#ifdef _SC_DCACHE_ASSOC
6649 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6650#endif
6651#ifdef _SC_DCACHE_BLKSZ
6652 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6653#endif
6654#ifdef _SC_DCACHE_LINESZ
6655 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6656#endif
6657#ifdef _SC_DCACHE_SZ
6658 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6659#endif
6660#ifdef _SC_DCACHE_TBLKSZ
6661 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6662#endif
6663#ifdef _SC_DELAYTIMER_MAX
6664 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6665#endif
6666#ifdef _SC_EQUIV_CLASS_MAX
6667 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6668#endif
6669#ifdef _SC_EXPR_NEST_MAX
6670 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6671#endif
6672#ifdef _SC_FSYNC
6673 {"SC_FSYNC", _SC_FSYNC},
6674#endif
6675#ifdef _SC_GETGR_R_SIZE_MAX
6676 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6677#endif
6678#ifdef _SC_GETPW_R_SIZE_MAX
6679 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6680#endif
6681#ifdef _SC_ICACHE_ASSOC
6682 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6683#endif
6684#ifdef _SC_ICACHE_BLKSZ
6685 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6686#endif
6687#ifdef _SC_ICACHE_LINESZ
6688 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6689#endif
6690#ifdef _SC_ICACHE_SZ
6691 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6692#endif
Fred Draked86ed291999-12-15 15:34:33 +00006693#ifdef _SC_INF
6694 {"SC_INF", _SC_INF},
6695#endif
Fred Drakec9680921999-12-13 16:37:25 +00006696#ifdef _SC_INT_MAX
6697 {"SC_INT_MAX", _SC_INT_MAX},
6698#endif
6699#ifdef _SC_INT_MIN
6700 {"SC_INT_MIN", _SC_INT_MIN},
6701#endif
6702#ifdef _SC_IOV_MAX
6703 {"SC_IOV_MAX", _SC_IOV_MAX},
6704#endif
Fred Draked86ed291999-12-15 15:34:33 +00006705#ifdef _SC_IP_SECOPTS
6706 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6707#endif
Fred Drakec9680921999-12-13 16:37:25 +00006708#ifdef _SC_JOB_CONTROL
6709 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6710#endif
Fred Draked86ed291999-12-15 15:34:33 +00006711#ifdef _SC_KERN_POINTERS
6712 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6713#endif
6714#ifdef _SC_KERN_SIM
6715 {"SC_KERN_SIM", _SC_KERN_SIM},
6716#endif
Fred Drakec9680921999-12-13 16:37:25 +00006717#ifdef _SC_LINE_MAX
6718 {"SC_LINE_MAX", _SC_LINE_MAX},
6719#endif
6720#ifdef _SC_LOGIN_NAME_MAX
6721 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6722#endif
6723#ifdef _SC_LOGNAME_MAX
6724 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6725#endif
6726#ifdef _SC_LONG_BIT
6727 {"SC_LONG_BIT", _SC_LONG_BIT},
6728#endif
Fred Draked86ed291999-12-15 15:34:33 +00006729#ifdef _SC_MAC
6730 {"SC_MAC", _SC_MAC},
6731#endif
Fred Drakec9680921999-12-13 16:37:25 +00006732#ifdef _SC_MAPPED_FILES
6733 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6734#endif
6735#ifdef _SC_MAXPID
6736 {"SC_MAXPID", _SC_MAXPID},
6737#endif
6738#ifdef _SC_MB_LEN_MAX
6739 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6740#endif
6741#ifdef _SC_MEMLOCK
6742 {"SC_MEMLOCK", _SC_MEMLOCK},
6743#endif
6744#ifdef _SC_MEMLOCK_RANGE
6745 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6746#endif
6747#ifdef _SC_MEMORY_PROTECTION
6748 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6749#endif
6750#ifdef _SC_MESSAGE_PASSING
6751 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6752#endif
Fred Draked86ed291999-12-15 15:34:33 +00006753#ifdef _SC_MMAP_FIXED_ALIGNMENT
6754 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6755#endif
Fred Drakec9680921999-12-13 16:37:25 +00006756#ifdef _SC_MQ_OPEN_MAX
6757 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6758#endif
6759#ifdef _SC_MQ_PRIO_MAX
6760 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6761#endif
Fred Draked86ed291999-12-15 15:34:33 +00006762#ifdef _SC_NACLS_MAX
6763 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6764#endif
Fred Drakec9680921999-12-13 16:37:25 +00006765#ifdef _SC_NGROUPS_MAX
6766 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6767#endif
6768#ifdef _SC_NL_ARGMAX
6769 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6770#endif
6771#ifdef _SC_NL_LANGMAX
6772 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6773#endif
6774#ifdef _SC_NL_MSGMAX
6775 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6776#endif
6777#ifdef _SC_NL_NMAX
6778 {"SC_NL_NMAX", _SC_NL_NMAX},
6779#endif
6780#ifdef _SC_NL_SETMAX
6781 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6782#endif
6783#ifdef _SC_NL_TEXTMAX
6784 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6785#endif
6786#ifdef _SC_NPROCESSORS_CONF
6787 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6788#endif
6789#ifdef _SC_NPROCESSORS_ONLN
6790 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6791#endif
Fred Draked86ed291999-12-15 15:34:33 +00006792#ifdef _SC_NPROC_CONF
6793 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6794#endif
6795#ifdef _SC_NPROC_ONLN
6796 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6797#endif
Fred Drakec9680921999-12-13 16:37:25 +00006798#ifdef _SC_NZERO
6799 {"SC_NZERO", _SC_NZERO},
6800#endif
6801#ifdef _SC_OPEN_MAX
6802 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6803#endif
6804#ifdef _SC_PAGESIZE
6805 {"SC_PAGESIZE", _SC_PAGESIZE},
6806#endif
6807#ifdef _SC_PAGE_SIZE
6808 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6809#endif
6810#ifdef _SC_PASS_MAX
6811 {"SC_PASS_MAX", _SC_PASS_MAX},
6812#endif
6813#ifdef _SC_PHYS_PAGES
6814 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6815#endif
6816#ifdef _SC_PII
6817 {"SC_PII", _SC_PII},
6818#endif
6819#ifdef _SC_PII_INTERNET
6820 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6821#endif
6822#ifdef _SC_PII_INTERNET_DGRAM
6823 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6824#endif
6825#ifdef _SC_PII_INTERNET_STREAM
6826 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6827#endif
6828#ifdef _SC_PII_OSI
6829 {"SC_PII_OSI", _SC_PII_OSI},
6830#endif
6831#ifdef _SC_PII_OSI_CLTS
6832 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6833#endif
6834#ifdef _SC_PII_OSI_COTS
6835 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6836#endif
6837#ifdef _SC_PII_OSI_M
6838 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6839#endif
6840#ifdef _SC_PII_SOCKET
6841 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6842#endif
6843#ifdef _SC_PII_XTI
6844 {"SC_PII_XTI", _SC_PII_XTI},
6845#endif
6846#ifdef _SC_POLL
6847 {"SC_POLL", _SC_POLL},
6848#endif
6849#ifdef _SC_PRIORITIZED_IO
6850 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6851#endif
6852#ifdef _SC_PRIORITY_SCHEDULING
6853 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6854#endif
6855#ifdef _SC_REALTIME_SIGNALS
6856 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6857#endif
6858#ifdef _SC_RE_DUP_MAX
6859 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6860#endif
6861#ifdef _SC_RTSIG_MAX
6862 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6863#endif
6864#ifdef _SC_SAVED_IDS
6865 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6866#endif
6867#ifdef _SC_SCHAR_MAX
6868 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6869#endif
6870#ifdef _SC_SCHAR_MIN
6871 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6872#endif
6873#ifdef _SC_SELECT
6874 {"SC_SELECT", _SC_SELECT},
6875#endif
6876#ifdef _SC_SEMAPHORES
6877 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6878#endif
6879#ifdef _SC_SEM_NSEMS_MAX
6880 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6881#endif
6882#ifdef _SC_SEM_VALUE_MAX
6883 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6884#endif
6885#ifdef _SC_SHARED_MEMORY_OBJECTS
6886 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6887#endif
6888#ifdef _SC_SHRT_MAX
6889 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6890#endif
6891#ifdef _SC_SHRT_MIN
6892 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6893#endif
6894#ifdef _SC_SIGQUEUE_MAX
6895 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6896#endif
6897#ifdef _SC_SIGRT_MAX
6898 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6899#endif
6900#ifdef _SC_SIGRT_MIN
6901 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6902#endif
Fred Draked86ed291999-12-15 15:34:33 +00006903#ifdef _SC_SOFTPOWER
6904 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6905#endif
Fred Drakec9680921999-12-13 16:37:25 +00006906#ifdef _SC_SPLIT_CACHE
6907 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6908#endif
6909#ifdef _SC_SSIZE_MAX
6910 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6911#endif
6912#ifdef _SC_STACK_PROT
6913 {"SC_STACK_PROT", _SC_STACK_PROT},
6914#endif
6915#ifdef _SC_STREAM_MAX
6916 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6917#endif
6918#ifdef _SC_SYNCHRONIZED_IO
6919 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6920#endif
6921#ifdef _SC_THREADS
6922 {"SC_THREADS", _SC_THREADS},
6923#endif
6924#ifdef _SC_THREAD_ATTR_STACKADDR
6925 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6926#endif
6927#ifdef _SC_THREAD_ATTR_STACKSIZE
6928 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6929#endif
6930#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6931 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6932#endif
6933#ifdef _SC_THREAD_KEYS_MAX
6934 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6935#endif
6936#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6937 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6938#endif
6939#ifdef _SC_THREAD_PRIO_INHERIT
6940 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6941#endif
6942#ifdef _SC_THREAD_PRIO_PROTECT
6943 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6944#endif
6945#ifdef _SC_THREAD_PROCESS_SHARED
6946 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6947#endif
6948#ifdef _SC_THREAD_SAFE_FUNCTIONS
6949 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6950#endif
6951#ifdef _SC_THREAD_STACK_MIN
6952 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6953#endif
6954#ifdef _SC_THREAD_THREADS_MAX
6955 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6956#endif
6957#ifdef _SC_TIMERS
6958 {"SC_TIMERS", _SC_TIMERS},
6959#endif
6960#ifdef _SC_TIMER_MAX
6961 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6962#endif
6963#ifdef _SC_TTY_NAME_MAX
6964 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6965#endif
6966#ifdef _SC_TZNAME_MAX
6967 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6968#endif
6969#ifdef _SC_T_IOV_MAX
6970 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6971#endif
6972#ifdef _SC_UCHAR_MAX
6973 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6974#endif
6975#ifdef _SC_UINT_MAX
6976 {"SC_UINT_MAX", _SC_UINT_MAX},
6977#endif
6978#ifdef _SC_UIO_MAXIOV
6979 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6980#endif
6981#ifdef _SC_ULONG_MAX
6982 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6983#endif
6984#ifdef _SC_USHRT_MAX
6985 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6986#endif
6987#ifdef _SC_VERSION
6988 {"SC_VERSION", _SC_VERSION},
6989#endif
6990#ifdef _SC_WORD_BIT
6991 {"SC_WORD_BIT", _SC_WORD_BIT},
6992#endif
6993#ifdef _SC_XBS5_ILP32_OFF32
6994 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6995#endif
6996#ifdef _SC_XBS5_ILP32_OFFBIG
6997 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6998#endif
6999#ifdef _SC_XBS5_LP64_OFF64
7000 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7001#endif
7002#ifdef _SC_XBS5_LPBIG_OFFBIG
7003 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7004#endif
7005#ifdef _SC_XOPEN_CRYPT
7006 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7007#endif
7008#ifdef _SC_XOPEN_ENH_I18N
7009 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7010#endif
7011#ifdef _SC_XOPEN_LEGACY
7012 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7013#endif
7014#ifdef _SC_XOPEN_REALTIME
7015 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7016#endif
7017#ifdef _SC_XOPEN_REALTIME_THREADS
7018 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7019#endif
7020#ifdef _SC_XOPEN_SHM
7021 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7022#endif
7023#ifdef _SC_XOPEN_UNIX
7024 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7025#endif
7026#ifdef _SC_XOPEN_VERSION
7027 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7028#endif
7029#ifdef _SC_XOPEN_XCU_VERSION
7030 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7031#endif
7032#ifdef _SC_XOPEN_XPG2
7033 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7034#endif
7035#ifdef _SC_XOPEN_XPG3
7036 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7037#endif
7038#ifdef _SC_XOPEN_XPG4
7039 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7040#endif
7041};
7042
7043static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007044conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007045{
7046 return conv_confname(arg, valuep, posix_constants_sysconf,
7047 sizeof(posix_constants_sysconf)
7048 / sizeof(struct constdef));
7049}
7050
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007051PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007052"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007053Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007054
7055static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007056posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007057{
7058 PyObject *result = NULL;
7059 int name;
7060
7061 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7062 int value;
7063
7064 errno = 0;
7065 value = sysconf(name);
7066 if (value == -1 && errno != 0)
7067 posix_error();
7068 else
7069 result = PyInt_FromLong(value);
7070 }
7071 return result;
7072}
7073#endif
7074
7075
Fred Drakebec628d1999-12-15 18:31:10 +00007076/* This code is used to ensure that the tables of configuration value names
7077 * are in sorted order as required by conv_confname(), and also to build the
7078 * the exported dictionaries that are used to publish information about the
7079 * names available on the host platform.
7080 *
7081 * Sorting the table at runtime ensures that the table is properly ordered
7082 * when used, even for platforms we're not able to test on. It also makes
7083 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007084 */
Fred Drakebec628d1999-12-15 18:31:10 +00007085
7086static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007087cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007088{
7089 const struct constdef *c1 =
7090 (const struct constdef *) v1;
7091 const struct constdef *c2 =
7092 (const struct constdef *) v2;
7093
7094 return strcmp(c1->name, c2->name);
7095}
7096
7097static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007098setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007099 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007100{
Fred Drakebec628d1999-12-15 18:31:10 +00007101 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007102 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007103
7104 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7105 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007106 if (d == NULL)
7107 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007108
Barry Warsaw3155db32000-04-13 15:20:40 +00007109 for (i=0; i < tablesize; ++i) {
7110 PyObject *o = PyInt_FromLong(table[i].value);
7111 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7112 Py_XDECREF(o);
7113 Py_DECREF(d);
7114 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007115 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007116 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007117 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007118 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007119}
7120
Fred Drakebec628d1999-12-15 18:31:10 +00007121/* Return -1 on failure, 0 on success. */
7122static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007123setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007124{
7125#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007126 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007127 sizeof(posix_constants_pathconf)
7128 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007129 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007130 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007131#endif
7132#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007133 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007134 sizeof(posix_constants_confstr)
7135 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007136 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007137 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007138#endif
7139#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007140 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007141 sizeof(posix_constants_sysconf)
7142 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007143 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007144 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007145#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007146 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007147}
Fred Draked86ed291999-12-15 15:34:33 +00007148
7149
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007150PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007151"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007152Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007153in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007154
7155static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007156posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007157{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007158 abort();
7159 /*NOTREACHED*/
7160 Py_FatalError("abort() called from Python code didn't abort!");
7161 return NULL;
7162}
Fred Drakebec628d1999-12-15 18:31:10 +00007163
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007164#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007165PyDoc_STRVAR(win32_startfile__doc__,
7166"startfile(filepath) - Start a file with its associated application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007167\n\
7168This acts like double-clicking the file in Explorer, or giving the file\n\
7169name as an argument to the DOS \"start\" command: the file is opened\n\
7170with whatever application (if any) its extension is associated.\n\
7171\n\
7172startfile returns as soon as the associated application is launched.\n\
7173There is no option to wait for the application to close, and no way\n\
7174to retrieve the application's exit status.\n\
7175\n\
7176The filepath is relative to the current directory. If you want to use\n\
7177an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007178the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007179
7180static PyObject *
7181win32_startfile(PyObject *self, PyObject *args)
7182{
7183 char *filepath;
7184 HINSTANCE rc;
7185 if (!PyArg_ParseTuple(args, "s:startfile", &filepath))
7186 return NULL;
7187 Py_BEGIN_ALLOW_THREADS
7188 rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL);
7189 Py_END_ALLOW_THREADS
7190 if (rc <= (HINSTANCE)32)
7191 return win32_error("startfile", filepath);
7192 Py_INCREF(Py_None);
7193 return Py_None;
7194}
7195#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007196
Martin v. Löwis438b5342002-12-27 10:16:42 +00007197#ifdef HAVE_GETLOADAVG
7198PyDoc_STRVAR(posix_getloadavg__doc__,
7199"getloadavg() -> (float, float, float)\n\n\
7200Return the number of processes in the system run queue averaged over\n\
7201the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7202was unobtainable");
7203
7204static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007205posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007206{
7207 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007208 if (getloadavg(loadavg, 3)!=3) {
7209 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7210 return NULL;
7211 } else
7212 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
7213}
7214#endif
7215
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007216#ifdef MS_WINDOWS
7217
7218PyDoc_STRVAR(win32_urandom__doc__,
7219"urandom(n) -> str\n\n\
7220Return a string of n random bytes suitable for cryptographic use.");
7221
7222typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7223 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7224 DWORD dwFlags );
7225typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7226 BYTE *pbBuffer );
7227
7228static CRYPTGENRANDOM pCryptGenRandom = NULL;
7229static HCRYPTPROV hCryptProv = 0;
7230
Tim Peters4ad82172004-08-30 17:02:04 +00007231static PyObject*
7232win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007233{
Tim Petersd3115382004-08-30 17:36:46 +00007234 int howMany;
7235 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007236
Tim Peters4ad82172004-08-30 17:02:04 +00007237 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00007238 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00007239 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00007240 if (howMany < 0)
7241 return PyErr_Format(PyExc_ValueError,
7242 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007243
Tim Peters4ad82172004-08-30 17:02:04 +00007244 if (hCryptProv == 0) {
7245 HINSTANCE hAdvAPI32 = NULL;
7246 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007247
Tim Peters4ad82172004-08-30 17:02:04 +00007248 /* Obtain handle to the DLL containing CryptoAPI
7249 This should not fail */
7250 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7251 if(hAdvAPI32 == NULL)
7252 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007253
Tim Peters4ad82172004-08-30 17:02:04 +00007254 /* Obtain pointers to the CryptoAPI functions
7255 This will fail on some early versions of Win95 */
7256 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7257 hAdvAPI32,
7258 "CryptAcquireContextA");
7259 if (pCryptAcquireContext == NULL)
7260 return PyErr_Format(PyExc_NotImplementedError,
7261 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007262
Tim Peters4ad82172004-08-30 17:02:04 +00007263 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7264 hAdvAPI32, "CryptGenRandom");
7265 if (pCryptAcquireContext == NULL)
7266 return PyErr_Format(PyExc_NotImplementedError,
7267 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007268
Tim Peters4ad82172004-08-30 17:02:04 +00007269 /* Acquire context */
7270 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7271 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7272 return win32_error("CryptAcquireContext", NULL);
7273 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007274
Tim Peters4ad82172004-08-30 17:02:04 +00007275 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00007276 result = PyString_FromStringAndSize(NULL, howMany);
7277 if (result != NULL) {
7278 /* Get random data */
7279 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7280 PyString_AS_STRING(result))) {
7281 Py_DECREF(result);
7282 return win32_error("CryptGenRandom", NULL);
7283 }
Tim Peters4ad82172004-08-30 17:02:04 +00007284 }
Tim Petersd3115382004-08-30 17:36:46 +00007285 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007286}
7287#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007288
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007289static PyMethodDef posix_methods[] = {
7290 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7291#ifdef HAVE_TTYNAME
7292 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7293#endif
7294 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
7295 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007296#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007297 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007298#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007299#ifdef HAVE_LCHOWN
7300 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7301#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007302#ifdef HAVE_CHROOT
7303 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7304#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007305#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007306 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007307#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007308#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00007309 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00007310#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00007311 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007312#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00007313#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007314#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007315 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007316#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007317 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7318 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7319 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007320#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007321 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007322#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007323#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007324 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007325#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007326 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7327 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7328 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007329 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007330#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007331 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007332#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007333#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007334 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007335#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007336 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007337#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007338 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007339#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007340 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7341 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7342 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007343#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007344 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007345#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007346 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007347#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007348 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7349 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007350#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007351#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007352 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7353 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007354#if defined(PYOS_OS2)
7355 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7356 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7357#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007358#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007359#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007360 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007361#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007362#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007363 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007364#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007365#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007366 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007367#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007368#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007369 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007370#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007371#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007372 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007373#endif /* HAVE_GETEGID */
7374#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007375 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007376#endif /* HAVE_GETEUID */
7377#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007378 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007379#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007380#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007381 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007382#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007383 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007384#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007385 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007386#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007387#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007388 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007389#endif /* HAVE_GETPPID */
7390#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007391 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007392#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007393#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007394 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007395#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007396#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007397 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007398#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007399#ifdef HAVE_KILLPG
7400 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7401#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007402#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007403 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007404#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007405#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007406 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007407#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007408 {"popen2", win32_popen2, METH_VARARGS},
7409 {"popen3", win32_popen3, METH_VARARGS},
7410 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00007411 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007412#else
7413#if defined(PYOS_OS2) && defined(PYCC_GCC)
7414 {"popen2", os2emx_popen2, METH_VARARGS},
7415 {"popen3", os2emx_popen3, METH_VARARGS},
7416 {"popen4", os2emx_popen4, METH_VARARGS},
7417#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007418#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007419#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007420#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007421 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007422#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007423#ifdef HAVE_SETEUID
7424 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7425#endif /* HAVE_SETEUID */
7426#ifdef HAVE_SETEGID
7427 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7428#endif /* HAVE_SETEGID */
7429#ifdef HAVE_SETREUID
7430 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7431#endif /* HAVE_SETREUID */
7432#ifdef HAVE_SETREGID
7433 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7434#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007435#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007436 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007437#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007438#ifdef HAVE_SETGROUPS
7439 {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__},
7440#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007441#ifdef HAVE_GETPGID
7442 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7443#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007444#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007445 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007446#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007447#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007448 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007449#endif /* HAVE_WAIT */
Tim Petersab034fa2002-02-01 11:27:43 +00007450#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007451 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007452#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007453#ifdef HAVE_GETSID
7454 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7455#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007456#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007457 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007458#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007459#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007460 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007461#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007462#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007463 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007464#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007465#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007466 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007467#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007468 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7469 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7470 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7471 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7472 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7473 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7474 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7475 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7476 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007477 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007478#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007479 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007480#endif
7481#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007482 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007483#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007484#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007485 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7486#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007487#ifdef HAVE_DEVICE_MACROS
7488 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7489 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7490 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7491#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007492#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007493 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007494#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007495#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007496 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007497#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007498#ifdef HAVE_UNSETENV
7499 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7500#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00007501#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007502 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00007503#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00007504#ifdef HAVE_FCHDIR
7505 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7506#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007507#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007508 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007509#endif
7510#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007511 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007512#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007513#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007514#ifdef WCOREDUMP
7515 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7516#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007517#ifdef WIFCONTINUED
7518 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7519#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007520#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007521 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007522#endif /* WIFSTOPPED */
7523#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007524 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007525#endif /* WIFSIGNALED */
7526#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007527 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007528#endif /* WIFEXITED */
7529#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007530 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007531#endif /* WEXITSTATUS */
7532#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007533 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007534#endif /* WTERMSIG */
7535#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007536 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007537#endif /* WSTOPSIG */
7538#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007539#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007540 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007541#endif
7542#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007543 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007544#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00007545#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00007546 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007547#endif
7548#ifdef HAVE_TEMPNAM
7549 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
7550#endif
7551#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00007552 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007553#endif
Fred Drakec9680921999-12-13 16:37:25 +00007554#ifdef HAVE_CONFSTR
7555 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7556#endif
7557#ifdef HAVE_SYSCONF
7558 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7559#endif
7560#ifdef HAVE_FPATHCONF
7561 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7562#endif
7563#ifdef HAVE_PATHCONF
7564 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7565#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007566 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007567#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007568 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7569#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007570#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007571 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007572#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007573 #ifdef MS_WINDOWS
7574 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7575 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007576 {NULL, NULL} /* Sentinel */
7577};
7578
7579
Barry Warsaw4a342091996-12-19 23:50:02 +00007580static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007581ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007582{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007583 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007584}
7585
Guido van Rossumd48f2521997-12-05 22:19:34 +00007586#if defined(PYOS_OS2)
7587/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007588static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007589{
7590 APIRET rc;
7591 ULONG values[QSV_MAX+1];
7592 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007593 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007594
7595 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007596 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007597 Py_END_ALLOW_THREADS
7598
7599 if (rc != NO_ERROR) {
7600 os2_error(rc);
7601 return -1;
7602 }
7603
Fred Drake4d1e64b2002-04-15 19:40:07 +00007604 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7605 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7606 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7607 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7608 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7609 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7610 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007611
7612 switch (values[QSV_VERSION_MINOR]) {
7613 case 0: ver = "2.00"; break;
7614 case 10: ver = "2.10"; break;
7615 case 11: ver = "2.11"; break;
7616 case 30: ver = "3.00"; break;
7617 case 40: ver = "4.00"; break;
7618 case 50: ver = "5.00"; break;
7619 default:
Tim Peters885d4572001-11-28 20:27:42 +00007620 PyOS_snprintf(tmp, sizeof(tmp),
7621 "%d-%d", values[QSV_VERSION_MAJOR],
7622 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007623 ver = &tmp[0];
7624 }
7625
7626 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007627 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007628 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007629
7630 /* Add Indicator of Which Drive was Used to Boot the System */
7631 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7632 tmp[1] = ':';
7633 tmp[2] = '\0';
7634
Fred Drake4d1e64b2002-04-15 19:40:07 +00007635 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007636}
7637#endif
7638
Barry Warsaw4a342091996-12-19 23:50:02 +00007639static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007640all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007641{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007642#ifdef F_OK
7643 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007644#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007645#ifdef R_OK
7646 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007647#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007648#ifdef W_OK
7649 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007650#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007651#ifdef X_OK
7652 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007653#endif
Fred Drakec9680921999-12-13 16:37:25 +00007654#ifdef NGROUPS_MAX
7655 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7656#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007657#ifdef TMP_MAX
7658 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7659#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007660#ifdef WCONTINUED
7661 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7662#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007663#ifdef WNOHANG
7664 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007665#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007666#ifdef WUNTRACED
7667 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7668#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007669#ifdef O_RDONLY
7670 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7671#endif
7672#ifdef O_WRONLY
7673 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7674#endif
7675#ifdef O_RDWR
7676 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7677#endif
7678#ifdef O_NDELAY
7679 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7680#endif
7681#ifdef O_NONBLOCK
7682 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7683#endif
7684#ifdef O_APPEND
7685 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7686#endif
7687#ifdef O_DSYNC
7688 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7689#endif
7690#ifdef O_RSYNC
7691 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7692#endif
7693#ifdef O_SYNC
7694 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7695#endif
7696#ifdef O_NOCTTY
7697 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7698#endif
7699#ifdef O_CREAT
7700 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7701#endif
7702#ifdef O_EXCL
7703 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7704#endif
7705#ifdef O_TRUNC
7706 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7707#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007708#ifdef O_BINARY
7709 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7710#endif
7711#ifdef O_TEXT
7712 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7713#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007714#ifdef O_LARGEFILE
7715 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7716#endif
7717
Tim Peters5aa91602002-01-30 05:46:57 +00007718/* MS Windows */
7719#ifdef O_NOINHERIT
7720 /* Don't inherit in child processes. */
7721 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7722#endif
7723#ifdef _O_SHORT_LIVED
7724 /* Optimize for short life (keep in memory). */
7725 /* MS forgot to define this one with a non-underscore form too. */
7726 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7727#endif
7728#ifdef O_TEMPORARY
7729 /* Automatically delete when last handle is closed. */
7730 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7731#endif
7732#ifdef O_RANDOM
7733 /* Optimize for random access. */
7734 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7735#endif
7736#ifdef O_SEQUENTIAL
7737 /* Optimize for sequential access. */
7738 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7739#endif
7740
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007741/* GNU extensions. */
7742#ifdef O_DIRECT
7743 /* Direct disk access. */
7744 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7745#endif
7746#ifdef O_DIRECTORY
7747 /* Must be a directory. */
7748 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7749#endif
7750#ifdef O_NOFOLLOW
7751 /* Do not follow links. */
7752 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7753#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007754
Barry Warsaw5676bd12003-01-07 20:57:09 +00007755 /* These come from sysexits.h */
7756#ifdef EX_OK
7757 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007758#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007759#ifdef EX_USAGE
7760 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007761#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007762#ifdef EX_DATAERR
7763 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007764#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007765#ifdef EX_NOINPUT
7766 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007767#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007768#ifdef EX_NOUSER
7769 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007770#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007771#ifdef EX_NOHOST
7772 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007773#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007774#ifdef EX_UNAVAILABLE
7775 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007776#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007777#ifdef EX_SOFTWARE
7778 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007779#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007780#ifdef EX_OSERR
7781 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007782#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007783#ifdef EX_OSFILE
7784 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007785#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007786#ifdef EX_CANTCREAT
7787 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007788#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007789#ifdef EX_IOERR
7790 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007791#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007792#ifdef EX_TEMPFAIL
7793 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007794#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007795#ifdef EX_PROTOCOL
7796 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007797#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007798#ifdef EX_NOPERM
7799 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007800#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007801#ifdef EX_CONFIG
7802 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007803#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007804#ifdef EX_NOTFOUND
7805 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007806#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007807
Guido van Rossum246bc171999-02-01 23:54:31 +00007808#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007809#if defined(PYOS_OS2) && defined(PYCC_GCC)
7810 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7811 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7812 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7813 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7814 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7815 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7816 if (ins(d, "P_PM", (long)P_PM)) return -1;
7817 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7818 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7819 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7820 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7821 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7822 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7823 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7824 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7825 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7826 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7827 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7828 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7829 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7830#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007831 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7832 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7833 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7834 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7835 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007836#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007837#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007838
Guido van Rossumd48f2521997-12-05 22:19:34 +00007839#if defined(PYOS_OS2)
7840 if (insertvalues(d)) return -1;
7841#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007842 return 0;
7843}
7844
7845
Tim Peters5aa91602002-01-30 05:46:57 +00007846#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007847#define INITFUNC initnt
7848#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007849
7850#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007851#define INITFUNC initos2
7852#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007853
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007854#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007855#define INITFUNC initposix
7856#define MODNAME "posix"
7857#endif
7858
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007859PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007860INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007861{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007862 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007863
Fred Drake4d1e64b2002-04-15 19:40:07 +00007864 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007865 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007866 posix__doc__);
Tim Peters5aa91602002-01-30 05:46:57 +00007867
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007868 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007869 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007870 Py_XINCREF(v);
7871 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007872 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007873 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007874
Fred Drake4d1e64b2002-04-15 19:40:07 +00007875 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007876 return;
7877
Fred Drake4d1e64b2002-04-15 19:40:07 +00007878 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007879 return;
7880
Fred Drake4d1e64b2002-04-15 19:40:07 +00007881 Py_INCREF(PyExc_OSError);
7882 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007883
Guido van Rossumb3d39562000-01-31 18:41:26 +00007884#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007885 if (posix_putenv_garbage == NULL)
7886 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007887#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007888
Guido van Rossum14648392001-12-08 18:02:58 +00007889 stat_result_desc.name = MODNAME ".stat_result";
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007890 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7891 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7892 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007893 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007894 structseq_new = StatResultType.tp_new;
7895 StatResultType.tp_new = statresult_new;
Fred Drake4d1e64b2002-04-15 19:40:07 +00007896 Py_INCREF((PyObject*) &StatResultType);
7897 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007898
Guido van Rossum14648392001-12-08 18:02:58 +00007899 statvfs_result_desc.name = MODNAME ".statvfs_result";
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007900 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007901 Py_INCREF((PyObject*) &StatVFSResultType);
7902 PyModule_AddObject(m, "statvfs_result",
7903 (PyObject*) &StatVFSResultType);
Guido van Rossumb6775db1994-08-01 11:34:53 +00007904}