blob: a375dcb6f534020814834e76d57f360b090ed808 [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
Guido van Rossuma4916fa1996-05-23 22:58:55 +000070/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000071/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000072#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000073#include <process.h>
74#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000075#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000076#define HAVE_GETCWD 1
77#define HAVE_OPENDIR 1
78#define HAVE_SYSTEM 1
79#if defined(__OS2__)
80#define HAVE_EXECV 1
81#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000082#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000083#include <process.h>
84#else
85#ifdef __BORLANDC__ /* Borland compiler */
86#define HAVE_EXECV 1
87#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000088#define HAVE_OPENDIR 1
89#define HAVE_PIPE 1
90#define HAVE_POPEN 1
91#define HAVE_SYSTEM 1
92#define HAVE_WAIT 1
93#else
94#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +000095#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +000096#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000097#define HAVE_EXECV 1
98#define HAVE_PIPE 1
99#define HAVE_POPEN 1
100#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000101#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000102#define HAVE_FSYNC 1
103#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000104#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000105#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
106/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000107#else /* all other compilers */
108/* Unix functions that the configure script doesn't check for */
109#define HAVE_EXECV 1
110#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000111#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
112#define HAVE_FORK1 1
113#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114#define HAVE_GETCWD 1
115#define HAVE_GETEGID 1
116#define HAVE_GETEUID 1
117#define HAVE_GETGID 1
118#define HAVE_GETPPID 1
119#define HAVE_GETUID 1
120#define HAVE_KILL 1
121#define HAVE_OPENDIR 1
122#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000123#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000124#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000125#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_SYSTEM 1
127#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000128#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000129#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000130#endif /* _MSC_VER */
131#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000132#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000133#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000134
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000135#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000136
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000137#if defined(__sgi)&&_COMPILER_VERSION>=700
138/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
139 (default) */
140extern char *ctermid_r(char *);
141#endif
142
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000143#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000144#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000145extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000146#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000147#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000148extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000149#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000150extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000151#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000152#endif
153#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000154extern int chdir(char *);
155extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000156#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000157extern int chdir(const char *);
158extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000159#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000160#ifdef __BORLANDC__
161extern int chmod(const char *, int);
162#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000163extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000164#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000165extern int chown(const char *, uid_t, gid_t);
166extern char *getcwd(char *, int);
167extern char *strerror(int);
168extern int link(const char *, const char *);
169extern int rename(const char *, const char *);
170extern int stat(const char *, struct stat *);
171extern int unlink(const char *);
172extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000173#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000175#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000177extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000178#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000179#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000180
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000181#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183#ifdef HAVE_UTIME_H
184#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000185#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000186
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000187#ifdef HAVE_SYS_UTIME_H
188#include <sys/utime.h>
189#define HAVE_UTIME_H /* pretend we do for the rest of this file */
190#endif /* HAVE_SYS_UTIME_H */
191
Guido van Rossumb6775db1994-08-01 11:34:53 +0000192#ifdef HAVE_SYS_TIMES_H
193#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000194#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195
196#ifdef HAVE_SYS_PARAM_H
197#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000198#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199
200#ifdef HAVE_SYS_UTSNAME_H
201#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000202#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000204#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000206#define NAMLEN(dirent) strlen((dirent)->d_name)
207#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000208#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000209#include <direct.h>
210#define NAMLEN(dirent) strlen((dirent)->d_name)
211#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000213#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000214#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000215#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000217#endif
218#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000220#endif
221#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000223#endif
224#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000226#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#include <direct.h>
228#include <io.h>
229#include <process.h>
Tim Petersbc2e10e2002-03-03 23:17:02 +0000230#include "osdefs.h"
Tim Petersee66d0c2002-07-15 16:10:55 +0000231#define WIN32_LEAN_AND_MEAN
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000233#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000234#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000235#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000236#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237
Guido van Rossumd48f2521997-12-05 22:19:34 +0000238#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000240#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241
Tim Petersbc2e10e2002-03-03 23:17:02 +0000242#ifndef MAXPATHLEN
243#define MAXPATHLEN 1024
244#endif /* MAXPATHLEN */
245
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000246#ifdef UNION_WAIT
247/* Emulate some macros on systems that have a union instead of macros */
248
249#ifndef WIFEXITED
250#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
251#endif
252
253#ifndef WEXITSTATUS
254#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
255#endif
256
257#ifndef WTERMSIG
258#define WTERMSIG(u_wait) ((u_wait).w_termsig)
259#endif
260
261#endif /* UNION_WAIT */
262
Greg Wardb48bc172000-03-01 21:51:56 +0000263/* Don't use the "_r" form if we don't need it (also, won't have a
264 prototype for it, at least on Solaris -- maybe others as well?). */
265#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
266#define USE_CTERMID_R
267#endif
268
269#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
270#define USE_TMPNAM_R
271#endif
272
Fred Drake699f3522000-06-29 21:12:41 +0000273/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000274#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000275#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +0000276# define STAT _stati64
277# define FSTAT _fstati64
278# define STRUCT_STAT struct _stati64
279#else
280# define STAT stat
281# define FSTAT fstat
282# define STRUCT_STAT struct stat
283#endif
284
Tim Peters11b23062003-04-23 02:39:17 +0000285#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000286#include <sys/mkdev.h>
287#else
288#if defined(MAJOR_IN_SYSMACROS)
289#include <sys/sysmacros.h>
290#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000291#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
292#include <sys/mkdev.h>
293#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000294#endif
Fred Drake699f3522000-06-29 21:12:41 +0000295
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000297#ifdef WITH_NEXT_FRAMEWORK
298/* On Darwin/MacOSX a shared library or framework has no access to
299** environ directly, we must obtain it with _NSGetEnviron().
300*/
301#include <crt_externs.h>
302static char **environ;
303#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000305#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000306
Barry Warsaw53699e91996-12-10 23:23:01 +0000307static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000308convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309{
Barry Warsaw53699e91996-12-10 23:23:01 +0000310 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000312 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313 if (d == NULL)
314 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000315#ifdef WITH_NEXT_FRAMEWORK
316 if (environ == NULL)
317 environ = *_NSGetEnviron();
318#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319 if (environ == NULL)
320 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000321 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000323 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000324 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 char *p = strchr(*e, '=');
326 if (p == NULL)
327 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000328 k = PyString_FromStringAndSize(*e, (int)(p-*e));
329 if (k == NULL) {
330 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000332 }
333 v = PyString_FromString(p+1);
334 if (v == NULL) {
335 PyErr_Clear();
336 Py_DECREF(k);
337 continue;
338 }
339 if (PyDict_GetItem(d, k) == NULL) {
340 if (PyDict_SetItem(d, k, v) != 0)
341 PyErr_Clear();
342 }
343 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000344 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000346#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000347 {
348 APIRET rc;
349 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
350
351 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000352 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000353 PyObject *v = PyString_FromString(buffer);
354 PyDict_SetItemString(d, "BEGINLIBPATH", v);
355 Py_DECREF(v);
356 }
357 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
358 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
359 PyObject *v = PyString_FromString(buffer);
360 PyDict_SetItemString(d, "ENDLIBPATH", v);
361 Py_DECREF(v);
362 }
363 }
364#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365 return d;
366}
367
368
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369/* Set a POSIX-specific error from errno, and return NULL */
370
Barry Warsawd58d7641998-07-23 16:14:40 +0000371static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000372posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000373{
Barry Warsawca74da41999-02-09 19:31:45 +0000374 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375}
Barry Warsawd58d7641998-07-23 16:14:40 +0000376static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000377posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000378{
Barry Warsawca74da41999-02-09 19:31:45 +0000379 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000380}
381
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000382#ifdef Py_WIN_WIDE_FILENAMES
383static PyObject *
384posix_error_with_unicode_filename(Py_UNICODE* name)
385{
386 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
387}
388#endif /* Py_WIN_WIDE_FILENAMES */
389
390
Mark Hammondef8b6542001-05-13 08:04:26 +0000391static PyObject *
392posix_error_with_allocated_filename(char* name)
393{
394 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
395 PyMem_Free(name);
396 return rc;
397}
398
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000399#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000400static PyObject *
401win32_error(char* function, char* filename)
402{
Mark Hammond33a6da92000-08-15 00:46:38 +0000403 /* XXX We should pass the function name along in the future.
404 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000405 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000406 Windows error object, which is non-trivial.
407 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000408 errno = GetLastError();
409 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000410 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000411 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000412 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000413}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000414
415#ifdef Py_WIN_WIDE_FILENAMES
416static PyObject *
417win32_error_unicode(char* function, Py_UNICODE* filename)
418{
419 /* XXX - see win32_error for comments on 'function' */
420 errno = GetLastError();
421 if (filename)
422 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
423 else
424 return PyErr_SetFromWindowsErr(errno);
425}
426
427static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
428{
429 /* XXX Perhaps we should make this API an alias of
430 PyObject_Unicode() instead ?! */
431 if (PyUnicode_CheckExact(obj)) {
432 Py_INCREF(obj);
433 return obj;
434 }
435 if (PyUnicode_Check(obj)) {
436 /* For a Unicode subtype that's not a Unicode object,
437 return a true Unicode object with the same data. */
438 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
439 PyUnicode_GET_SIZE(obj));
440 }
Tim Peters11b23062003-04-23 02:39:17 +0000441 return PyUnicode_FromEncodedObject(obj,
442 Py_FileSystemDefaultEncoding,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000443 "strict");
444}
445
446#endif /* Py_WIN_WIDE_FILENAMES */
447
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000448#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449
Guido van Rossumd48f2521997-12-05 22:19:34 +0000450#if defined(PYOS_OS2)
451/**********************************************************************
452 * Helper Function to Trim and Format OS/2 Messages
453 **********************************************************************/
454 static void
455os2_formatmsg(char *msgbuf, int msglen, char *reason)
456{
457 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
458
459 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
460 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
461
462 while (lastc > msgbuf && isspace(*lastc))
463 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
464 }
465
466 /* Add Optional Reason Text */
467 if (reason) {
468 strcat(msgbuf, " : ");
469 strcat(msgbuf, reason);
470 }
471}
472
473/**********************************************************************
474 * Decode an OS/2 Operating System Error Code
475 *
476 * A convenience function to lookup an OS/2 error code and return a
477 * text message we can use to raise a Python exception.
478 *
479 * Notes:
480 * The messages for errors returned from the OS/2 kernel reside in
481 * the file OSO001.MSG in the \OS2 directory hierarchy.
482 *
483 **********************************************************************/
484 static char *
485os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
486{
487 APIRET rc;
488 ULONG msglen;
489
490 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
491 Py_BEGIN_ALLOW_THREADS
492 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
493 errorcode, "oso001.msg", &msglen);
494 Py_END_ALLOW_THREADS
495
496 if (rc == NO_ERROR)
497 os2_formatmsg(msgbuf, msglen, reason);
498 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000499 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000500 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000501
502 return msgbuf;
503}
504
505/* Set an OS/2-specific error and return NULL. OS/2 kernel
506 errors are not in a global variable e.g. 'errno' nor are
507 they congruent with posix error numbers. */
508
509static PyObject * os2_error(int code)
510{
511 char text[1024];
512 PyObject *v;
513
514 os2_strerror(text, sizeof(text), code, "");
515
516 v = Py_BuildValue("(is)", code, text);
517 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000518 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000519 Py_DECREF(v);
520 }
521 return NULL; /* Signal to Python that an Exception is Pending */
522}
523
524#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000525
526/* POSIX generic methods */
527
Barry Warsaw53699e91996-12-10 23:23:01 +0000528static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000529posix_fildes(PyObject *fdobj, int (*func)(int))
530{
531 int fd;
532 int res;
533 fd = PyObject_AsFileDescriptor(fdobj);
534 if (fd < 0)
535 return NULL;
536 Py_BEGIN_ALLOW_THREADS
537 res = (*func)(fd);
538 Py_END_ALLOW_THREADS
539 if (res < 0)
540 return posix_error();
541 Py_INCREF(Py_None);
542 return Py_None;
543}
Guido van Rossum21142a01999-01-08 21:05:37 +0000544
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000545#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000546static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000547unicode_file_names(void)
548{
549 static int canusewide = -1;
550 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000551 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000552 the Windows NT family. */
553 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
554 }
555 return canusewide;
556}
557#endif
Tim Peters11b23062003-04-23 02:39:17 +0000558
Guido van Rossum21142a01999-01-08 21:05:37 +0000559static PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000560posix_1str(PyObject *args, char *format, int (*func)(const char*),
561 char *wformat, int (*wfunc)(const Py_UNICODE*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000562{
Mark Hammondef8b6542001-05-13 08:04:26 +0000563 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000564 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000565#ifdef Py_WIN_WIDE_FILENAMES
566 if (unicode_file_names()) {
567 PyUnicodeObject *po;
568 if (PyArg_ParseTuple(args, wformat, &po)) {
569 Py_BEGIN_ALLOW_THREADS
570 /* PyUnicode_AS_UNICODE OK without thread
571 lock as it is a simple dereference. */
572 res = (*wfunc)(PyUnicode_AS_UNICODE(po));
573 Py_END_ALLOW_THREADS
574 if (res < 0)
Mark Hammondd3890362002-10-03 07:24:48 +0000575 return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000576 Py_INCREF(Py_None);
577 return Py_None;
578 }
579 /* Drop the argument parsing error as narrow
580 strings are also valid. */
581 PyErr_Clear();
582 }
583#else
584 /* Platforms that don't support Unicode filenames
585 shouldn't be passing these extra params */
586 assert(wformat==NULL && wfunc == NULL);
587#endif
588
Tim Peters5aa91602002-01-30 05:46:57 +0000589 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000590 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000592 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000593 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000594 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000595 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000596 return posix_error_with_allocated_filename(path1);
597 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000598 Py_INCREF(Py_None);
599 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000600}
601
Barry Warsaw53699e91996-12-10 23:23:01 +0000602static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000603posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000604 char *format,
605 int (*func)(const char *, const char *),
606 char *wformat,
607 int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000608{
Mark Hammondef8b6542001-05-13 08:04:26 +0000609 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000610 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000611#ifdef Py_WIN_WIDE_FILENAMES
612 if (unicode_file_names()) {
613 PyObject *po1;
614 PyObject *po2;
615 if (PyArg_ParseTuple(args, wformat, &po1, &po2)) {
616 if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) {
617 PyObject *wpath1;
618 PyObject *wpath2;
619 wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1);
620 wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2);
621 if (!wpath1 || !wpath2) {
622 Py_XDECREF(wpath1);
623 Py_XDECREF(wpath2);
624 return NULL;
625 }
626 Py_BEGIN_ALLOW_THREADS
627 /* PyUnicode_AS_UNICODE OK without thread
628 lock as it is a simple dereference. */
629 res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1),
630 PyUnicode_AS_UNICODE(wpath2));
631 Py_END_ALLOW_THREADS
632 Py_XDECREF(wpath1);
633 Py_XDECREF(wpath2);
634 if (res != 0)
635 return posix_error();
636 Py_INCREF(Py_None);
637 return Py_None;
638 }
639 /* Else flow through as neither is Unicode. */
640 }
641 /* Drop the argument parsing error as narrow
642 strings are also valid. */
643 PyErr_Clear();
644 }
645#else
646 /* Platforms that don't support Unicode filenames
647 shouldn't be passing these extra params */
648 assert(wformat==NULL && wfunc == NULL);
649#endif
650
Mark Hammondef8b6542001-05-13 08:04:26 +0000651 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000652 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000653 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000655 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000656 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000657 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000658 PyMem_Free(path1);
659 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000660 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000661 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000662 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000663 Py_INCREF(Py_None);
664 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000665}
666
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000667PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000668"stat_result: Result from stat or lstat.\n\n\
669This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000670 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000671or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
672\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000673Posix/windows: If your platform supports st_blksize, st_blocks, or st_rdev,\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000674they are available as attributes only.\n\
675\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000676See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000677
678static PyStructSequence_Field stat_result_fields[] = {
679 {"st_mode", "protection bits"},
680 {"st_ino", "inode"},
681 {"st_dev", "device"},
682 {"st_nlink", "number of hard links"},
683 {"st_uid", "user ID of owner"},
684 {"st_gid", "group ID of owner"},
685 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000686 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
687 {NULL, "integer time of last access"},
688 {NULL, "integer time of last modification"},
689 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000690 {"st_atime", "time of last access"},
691 {"st_mtime", "time of last modification"},
692 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000693#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000694 {"st_blksize", "blocksize for filesystem I/O"},
695#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000696#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000697 {"st_blocks", "number of blocks allocated"},
698#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000699#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000700 {"st_rdev", "device type (if inode device)"},
701#endif
702 {0}
703};
704
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000705#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000706#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000707#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000708#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000709#endif
710
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000711#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000712#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
713#else
714#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
715#endif
716
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000717#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000718#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
719#else
720#define ST_RDEV_IDX ST_BLOCKS_IDX
721#endif
722
723static PyStructSequence_Desc stat_result_desc = {
724 "stat_result", /* name */
725 stat_result__doc__, /* doc */
726 stat_result_fields,
727 10
728};
729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000730PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000731"statvfs_result: Result from statvfs or fstatvfs.\n\n\
732This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000733 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000734or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000735\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000736See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000737
738static PyStructSequence_Field statvfs_result_fields[] = {
739 {"f_bsize", },
740 {"f_frsize", },
741 {"f_blocks", },
742 {"f_bfree", },
743 {"f_bavail", },
744 {"f_files", },
745 {"f_ffree", },
746 {"f_favail", },
747 {"f_flag", },
748 {"f_namemax",},
749 {0}
750};
751
752static PyStructSequence_Desc statvfs_result_desc = {
753 "statvfs_result", /* name */
754 statvfs_result__doc__, /* doc */
755 statvfs_result_fields,
756 10
757};
758
759static PyTypeObject StatResultType;
760static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000761static newfunc structseq_new;
762
763static PyObject *
764statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
765{
766 PyStructSequence *result;
767 int i;
768
769 result = (PyStructSequence*)structseq_new(type, args, kwds);
770 if (!result)
771 return NULL;
772 /* If we have been initialized from a tuple,
773 st_?time might be set to None. Initialize it
774 from the int slots. */
775 for (i = 7; i <= 9; i++) {
776 if (result->ob_item[i+3] == Py_None) {
777 Py_DECREF(Py_None);
778 Py_INCREF(result->ob_item[i]);
779 result->ob_item[i+3] = result->ob_item[i];
780 }
781 }
782 return (PyObject*)result;
783}
784
785
786
787/* If true, st_?time is float. */
788static int _stat_float_times = 0;
789
790PyDoc_STRVAR(stat_float_times__doc__,
791"stat_float_times([newval]) -> oldval\n\n\
792Determine whether os.[lf]stat represents time stamps as float objects.\n\
793If newval is True, future calls to stat() return floats, if it is False,\n\
794future calls return ints. \n\
795If newval is omitted, return the current setting.\n");
796
797static PyObject*
798stat_float_times(PyObject* self, PyObject *args)
799{
800 int newval = -1;
801 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
802 return NULL;
803 if (newval == -1)
804 /* Return old value */
805 return PyBool_FromLong(_stat_float_times);
806 _stat_float_times = newval;
807 Py_INCREF(Py_None);
808 return Py_None;
809}
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000810
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000811static void
812fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
813{
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000814 PyObject *fval,*ival;
815#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000816 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000817#else
818 ival = PyInt_FromLong((long)sec);
819#endif
820 if (_stat_float_times) {
821 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
822 } else {
823 fval = ival;
824 Py_INCREF(fval);
825 }
826 PyStructSequence_SET_ITEM(v, index, ival);
827 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000828}
829
Tim Peters5aa91602002-01-30 05:46:57 +0000830/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +0000831 (used by posix_stat() and posix_fstat()) */
832static PyObject*
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000833_pystat_fromstructstat(STRUCT_STAT st)
Fred Drake699f3522000-06-29 21:12:41 +0000834{
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000835 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000836 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +0000837 if (v == NULL)
838 return NULL;
839
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000840 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode));
Fred Drake699f3522000-06-29 21:12:41 +0000841#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000842 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000843 PyLong_FromLongLong((PY_LONG_LONG)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000844#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000845 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000846#endif
847#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +0000848 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000849 PyLong_FromLongLong((PY_LONG_LONG)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000850#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000851 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000852#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000853 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink));
854 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid));
855 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid));
Fred Drake699f3522000-06-29 21:12:41 +0000856#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000857 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000858 PyLong_FromLongLong((PY_LONG_LONG)st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000859#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000860 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000861#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000862
863#ifdef HAVE_STAT_TV_NSEC
864 ansec = st.st_atim.tv_nsec;
865 mnsec = st.st_mtim.tv_nsec;
866 cnsec = st.st_ctim.tv_nsec;
Fred Drake699f3522000-06-29 21:12:41 +0000867#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000868 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000869#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000870 fill_time(v, 7, st.st_atime, ansec);
871 fill_time(v, 8, st.st_mtime, mnsec);
872 fill_time(v, 9, st.st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000873
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000874#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +0000875 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000876 PyInt_FromLong((long)st.st_blksize));
877#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000878#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +0000879 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000880 PyInt_FromLong((long)st.st_blocks));
881#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000882#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000883 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
884 PyInt_FromLong((long)st.st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +0000885#endif
886
887 if (PyErr_Occurred()) {
888 Py_DECREF(v);
889 return NULL;
890 }
891
892 return v;
893}
894
Martin v. Löwisd8948722004-06-02 09:57:56 +0000895#ifdef MS_WINDOWS
896
897/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
898 where / can be used in place of \ and the trailing slash is optional.
899 Both SERVER and SHARE must have at least one character.
900*/
901
902#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
903#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
904#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
905
906static BOOL
907IsUNCRootA(char *path, int pathlen)
908{
909 #define ISSLASH ISSLASHA
910
911 int i, share;
912
913 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
914 /* minimum UNCRoot is \\x\y */
915 return FALSE;
916 for (i = 2; i < pathlen ; i++)
917 if (ISSLASH(path[i])) break;
918 if (i == 2 || i == pathlen)
919 /* do not allow \\\SHARE or \\SERVER */
920 return FALSE;
921 share = i+1;
922 for (i = share; i < pathlen; i++)
923 if (ISSLASH(path[i])) break;
924 return (i != share && (i == pathlen || i == pathlen-1));
925
926 #undef ISSLASH
927}
928
929#ifdef Py_WIN_WIDE_FILENAMES
930static BOOL
931IsUNCRootW(Py_UNICODE *path, int pathlen)
932{
933 #define ISSLASH ISSLASHW
934
935 int i, share;
936
937 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
938 /* minimum UNCRoot is \\x\y */
939 return FALSE;
940 for (i = 2; i < pathlen ; i++)
941 if (ISSLASH(path[i])) break;
942 if (i == 2 || i == pathlen)
943 /* do not allow \\\SHARE or \\SERVER */
944 return FALSE;
945 share = i+1;
946 for (i = share; i < pathlen; i++)
947 if (ISSLASH(path[i])) break;
948 return (i != share && (i == pathlen || i == pathlen-1));
949
950 #undef ISSLASH
951}
952#endif /* Py_WIN_WIDE_FILENAMES */
953#endif /* MS_WINDOWS */
954
Barry Warsaw53699e91996-12-10 23:23:01 +0000955static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000956posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000957 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000958#ifdef __VMS
959 int (*statfunc)(const char *, STRUCT_STAT *, ...),
960#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000961 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000962#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000963 char *wformat,
964 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000965{
Fred Drake699f3522000-06-29 21:12:41 +0000966 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +0000967 char *path = NULL; /* pass this to stat; do not free() it */
968 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +0000969 int res;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000970
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000971#ifdef MS_WINDOWS
Tim Peters500bd032001-12-19 19:05:01 +0000972 int pathlen;
973 char pathcopy[MAX_PATH];
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000974#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +0000975
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000976
977#ifdef Py_WIN_WIDE_FILENAMES
978 /* If on wide-character-capable OS see if argument
979 is Unicode and if so use wide API. */
980 if (unicode_file_names()) {
981 PyUnicodeObject *po;
982 if (PyArg_ParseTuple(args, wformat, &po)) {
983 Py_UNICODE wpath[MAX_PATH+1];
984 pathlen = wcslen(PyUnicode_AS_UNICODE(po));
985 /* the library call can blow up if the file name is too long! */
986 if (pathlen > MAX_PATH) {
987 errno = ENAMETOOLONG;
988 return posix_error();
989 }
990 wcscpy(wpath, PyUnicode_AS_UNICODE(po));
991 /* Remove trailing slash or backslash, unless it's the current
992 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
993 */
Martin v. Löwisd8948722004-06-02 09:57:56 +0000994 if (pathlen > 0) {
995 if (ISSLASHW(wpath[pathlen-1])) {
996 /* It does end with a slash -- exempt the root drive cases. */
997 if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':') ||
998 IsUNCRootW(wpath, pathlen))
999 /* leave it alone */;
1000 else {
1001 /* nuke the trailing backslash */
1002 wpath[pathlen-1] = L'\0';
1003 }
1004 }
1005 else if (ISSLASHW(wpath[1]) && pathlen < ARRAYSIZE(wpath)-1 &&
1006 IsUNCRootW(wpath, pathlen)) {
1007 /* UNC root w/o trailing slash: add one when there's room */
1008 wpath[pathlen++] = L'\\';
1009 wpath[pathlen] = L'\0';
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001010 }
1011 }
1012 Py_BEGIN_ALLOW_THREADS
1013 /* PyUnicode_AS_UNICODE result OK without
1014 thread lock as it is a simple dereference. */
1015 res = wstatfunc(wpath, &st);
1016 Py_END_ALLOW_THREADS
1017 if (res != 0)
1018 return posix_error_with_unicode_filename(wpath);
1019 return _pystat_fromstructstat(st);
1020 }
1021 /* Drop the argument parsing error as narrow strings
1022 are also valid. */
1023 PyErr_Clear();
1024 }
1025#endif
1026
Tim Peters5aa91602002-01-30 05:46:57 +00001027 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001028 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001029 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001030 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001031
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001032#ifdef MS_WINDOWS
Guido van Rossumace88ae2000-04-21 18:54:45 +00001033 pathlen = strlen(path);
1034 /* the library call can blow up if the file name is too long! */
1035 if (pathlen > MAX_PATH) {
Tim Peters500bd032001-12-19 19:05:01 +00001036 PyMem_Free(pathfree);
Guido van Rossumace88ae2000-04-21 18:54:45 +00001037 errno = ENAMETOOLONG;
1038 return posix_error();
1039 }
1040
Tim Peters500bd032001-12-19 19:05:01 +00001041 /* Remove trailing slash or backslash, unless it's the current
1042 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
1043 */
Martin v. Löwisd8948722004-06-02 09:57:56 +00001044 if (pathlen > 0) {
1045 if (ISSLASHA(path[pathlen-1])) {
1046 /* It does end with a slash -- exempt the root drive cases. */
1047 if (pathlen == 1 || (pathlen == 3 && path[1] == ':') ||
1048 IsUNCRootA(path, pathlen))
1049 /* leave it alone */;
1050 else {
1051 /* nuke the trailing backslash */
1052 strncpy(pathcopy, path, pathlen);
1053 pathcopy[pathlen-1] = '\0';
1054 path = pathcopy;
1055 }
1056 }
1057 else if (ISSLASHA(path[1]) && pathlen < ARRAYSIZE(pathcopy)-1 &&
1058 IsUNCRootA(path, pathlen)) {
1059 /* UNC root w/o trailing slash: add one when there's room */
Guido van Rossumace88ae2000-04-21 18:54:45 +00001060 strncpy(pathcopy, path, pathlen);
Martin v. Löwisd8948722004-06-02 09:57:56 +00001061 pathcopy[pathlen++] = '\\';
1062 pathcopy[pathlen] = '\0';
Guido van Rossumace88ae2000-04-21 18:54:45 +00001063 path = pathcopy;
1064 }
1065 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001066#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +00001067
Barry Warsaw53699e91996-12-10 23:23:01 +00001068 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001069 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001070 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001071 if (res != 0)
Tim Peters500bd032001-12-19 19:05:01 +00001072 return posix_error_with_allocated_filename(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +00001073
Tim Peters500bd032001-12-19 19:05:01 +00001074 PyMem_Free(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +00001075 return _pystat_fromstructstat(st);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001076}
1077
1078
1079/* POSIX methods */
1080
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001081PyDoc_STRVAR(posix_access__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001082"access(path, mode) -> 1 if granted, 0 otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001083Use the real uid/gid to test for access to a path. Note that most\n\
1084operations will use the effective uid/gid, therefore this routine can\n\
1085be used in a suid/sgid environment to test if the invoking user has the\n\
1086specified access to the path. The mode argument can be F_OK to test\n\
1087existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001088
1089static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001090posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001091{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001092 char *path;
1093 int mode;
1094 int res;
1095
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001096#ifdef Py_WIN_WIDE_FILENAMES
1097 if (unicode_file_names()) {
1098 PyUnicodeObject *po;
1099 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1100 Py_BEGIN_ALLOW_THREADS
1101 /* PyUnicode_AS_UNICODE OK without thread lock as
1102 it is a simple dereference. */
1103 res = _waccess(PyUnicode_AS_UNICODE(po), mode);
1104 Py_END_ALLOW_THREADS
1105 return(PyBool_FromLong(res == 0));
1106 }
1107 /* Drop the argument parsing error as narrow strings
1108 are also valid. */
1109 PyErr_Clear();
1110 }
1111#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001112 if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001113 return NULL;
1114 Py_BEGIN_ALLOW_THREADS
1115 res = access(path, mode);
1116 Py_END_ALLOW_THREADS
Guido van Rossum674deb22002-09-01 15:06:28 +00001117 return(PyBool_FromLong(res == 0));
Guido van Rossum94f6f721999-01-06 18:42:14 +00001118}
1119
Guido van Rossumd371ff11999-01-25 16:12:23 +00001120#ifndef F_OK
1121#define F_OK 0
1122#endif
1123#ifndef R_OK
1124#define R_OK 4
1125#endif
1126#ifndef W_OK
1127#define W_OK 2
1128#endif
1129#ifndef X_OK
1130#define X_OK 1
1131#endif
1132
1133#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001134PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001135"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001136Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001137
1138static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001139posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001140{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001141 int id;
1142 char *ret;
1143
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001144 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001145 return NULL;
1146
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001147#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001148 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001149 if (id == 0) {
1150 ret = ttyname();
1151 }
1152 else {
1153 ret = NULL;
1154 }
1155#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001156 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001157#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001158 if (ret == NULL)
1159 return(posix_error());
1160 return(PyString_FromString(ret));
1161}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001162#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001163
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001164#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001165PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001166"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001167Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001168
1169static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001170posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001171{
1172 char *ret;
1173 char buffer[L_ctermid];
1174
Greg Wardb48bc172000-03-01 21:51:56 +00001175#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001176 ret = ctermid_r(buffer);
1177#else
1178 ret = ctermid(buffer);
1179#endif
1180 if (ret == NULL)
1181 return(posix_error());
1182 return(PyString_FromString(buffer));
1183}
1184#endif
1185
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001186PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001187"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001188Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001189
Barry Warsaw53699e91996-12-10 23:23:01 +00001190static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001191posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001192{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001193#ifdef MS_WINDOWS
1194 return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir);
1195#elif defined(PYOS_OS2) && defined(PYCC_GCC)
1196 return posix_1str(args, "et:chdir", _chdir2, NULL, NULL);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001197#elif defined(__VMS)
Tim Peters11b23062003-04-23 02:39:17 +00001198 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001199 NULL, NULL);
1200#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001201 return posix_1str(args, "et:chdir", chdir, NULL, NULL);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001202#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001203}
1204
Fred Drake4d1e64b2002-04-15 19:40:07 +00001205#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001206PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001207"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001208Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001209opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001210
1211static PyObject *
1212posix_fchdir(PyObject *self, PyObject *fdobj)
1213{
1214 return posix_fildes(fdobj, fchdir);
1215}
1216#endif /* HAVE_FCHDIR */
1217
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001219PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001220"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001221Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001222
Barry Warsaw53699e91996-12-10 23:23:01 +00001223static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001224posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001225{
Mark Hammondef8b6542001-05-13 08:04:26 +00001226 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001227 int i;
1228 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001229#ifdef Py_WIN_WIDE_FILENAMES
1230 if (unicode_file_names()) {
1231 PyUnicodeObject *po;
1232 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1233 Py_BEGIN_ALLOW_THREADS
1234 res = _wchmod(PyUnicode_AS_UNICODE(po), i);
1235 Py_END_ALLOW_THREADS
1236 if (res < 0)
1237 return posix_error_with_unicode_filename(
1238 PyUnicode_AS_UNICODE(po));
1239 Py_INCREF(Py_None);
1240 return Py_None;
1241 }
1242 /* Drop the argument parsing error as narrow strings
1243 are also valid. */
1244 PyErr_Clear();
1245 }
1246#endif /* Py_WIN_WIDE_FILENAMES */
1247 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001248 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001249 return NULL;
1250 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001251 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001252 Py_END_ALLOW_THREADS
1253 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001254 return posix_error_with_allocated_filename(path);
1255 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001256 Py_INCREF(Py_None);
1257 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001258}
1259
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001260
Martin v. Löwis244edc82001-10-04 22:44:26 +00001261#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001262PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001263"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001264Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001265
1266static PyObject *
1267posix_chroot(PyObject *self, PyObject *args)
1268{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001269 return posix_1str(args, "et:chroot", chroot, NULL, NULL);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001270}
1271#endif
1272
Guido van Rossum21142a01999-01-08 21:05:37 +00001273#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001274PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001275"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001276force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001277
1278static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001279posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001280{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001281 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001282}
1283#endif /* HAVE_FSYNC */
1284
1285#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001286
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001287#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001288extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1289#endif
1290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001291PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001292"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001293force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001294 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001295
1296static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001297posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001298{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001299 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001300}
1301#endif /* HAVE_FDATASYNC */
1302
1303
Fredrik Lundh10723342000-07-10 16:38:09 +00001304#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001305PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001306"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001307Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001308
Barry Warsaw53699e91996-12-10 23:23:01 +00001309static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001310posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001311{
Mark Hammondef8b6542001-05-13 08:04:26 +00001312 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001313 int uid, gid;
1314 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001315 if (!PyArg_ParseTuple(args, "etii:chown",
1316 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001317 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001318 return NULL;
1319 Py_BEGIN_ALLOW_THREADS
1320 res = chown(path, (uid_t) uid, (gid_t) gid);
1321 Py_END_ALLOW_THREADS
1322 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001323 return posix_error_with_allocated_filename(path);
1324 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001325 Py_INCREF(Py_None);
1326 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001327}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001328#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001329
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001330#ifdef HAVE_LCHOWN
1331PyDoc_STRVAR(posix_lchown__doc__,
1332"lchown(path, uid, gid)\n\n\
1333Change the owner and group id of path to the numeric uid and gid.\n\
1334This function will not follow symbolic links.");
1335
1336static PyObject *
1337posix_lchown(PyObject *self, PyObject *args)
1338{
1339 char *path = NULL;
1340 int uid, gid;
1341 int res;
1342 if (!PyArg_ParseTuple(args, "etii:lchown",
1343 Py_FileSystemDefaultEncoding, &path,
1344 &uid, &gid))
1345 return NULL;
1346 Py_BEGIN_ALLOW_THREADS
1347 res = lchown(path, (uid_t) uid, (gid_t) gid);
1348 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001349 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001350 return posix_error_with_allocated_filename(path);
1351 PyMem_Free(path);
1352 Py_INCREF(Py_None);
1353 return Py_None;
1354}
1355#endif /* HAVE_LCHOWN */
1356
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001357
Guido van Rossum36bc6801995-06-14 22:54:23 +00001358#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001359PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001360"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001361Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001362
Barry Warsaw53699e91996-12-10 23:23:01 +00001363static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001364posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001365{
1366 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001367 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001368
Barry Warsaw53699e91996-12-10 23:23:01 +00001369 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001370#if defined(PYOS_OS2) && defined(PYCC_GCC)
1371 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001372#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001373 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001374#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001375 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001376 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001377 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001378 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001379}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001380
Walter Dörwald3b918c32002-11-21 20:18:46 +00001381#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001382PyDoc_STRVAR(posix_getcwdu__doc__,
1383"getcwdu() -> path\n\n\
1384Return a unicode string representing the current working directory.");
1385
1386static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001387posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001388{
1389 char buf[1026];
1390 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001391
1392#ifdef Py_WIN_WIDE_FILENAMES
1393 if (unicode_file_names()) {
1394 wchar_t *wres;
1395 wchar_t wbuf[1026];
1396 Py_BEGIN_ALLOW_THREADS
1397 wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]);
1398 Py_END_ALLOW_THREADS
1399 if (wres == NULL)
1400 return posix_error();
1401 return PyUnicode_FromWideChar(wbuf, wcslen(wbuf));
1402 }
1403#endif
1404
1405 Py_BEGIN_ALLOW_THREADS
1406#if defined(PYOS_OS2) && defined(PYCC_GCC)
1407 res = _getcwd2(buf, sizeof buf);
1408#else
1409 res = getcwd(buf, sizeof buf);
1410#endif
1411 Py_END_ALLOW_THREADS
1412 if (res == NULL)
1413 return posix_error();
1414 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1415}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001416#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001417#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001418
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001419
Guido van Rossumb6775db1994-08-01 11:34:53 +00001420#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001421PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001422"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001423Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001424
Barry Warsaw53699e91996-12-10 23:23:01 +00001425static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001426posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001427{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001428 return posix_2str(args, "etet:link", link, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001429}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001430#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001431
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001432
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001433PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001434"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001435Return a list containing the names of the entries in the directory.\n\
1436\n\
1437 path: path of directory to list\n\
1438\n\
1439The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001440entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001441
Barry Warsaw53699e91996-12-10 23:23:01 +00001442static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001443posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001444{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001445 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001446 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001447#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001448
Barry Warsaw53699e91996-12-10 23:23:01 +00001449 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001450 HANDLE hFindFile;
1451 WIN32_FIND_DATA FileData;
Mark Hammondef8b6542001-05-13 08:04:26 +00001452 /* MAX_PATH characters could mean a bigger encoded string */
1453 char namebuf[MAX_PATH*2+5];
1454 char *bufptr = namebuf;
1455 int len = sizeof(namebuf)/sizeof(namebuf[0]);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001456
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001457#ifdef Py_WIN_WIDE_FILENAMES
1458 /* If on wide-character-capable OS see if argument
1459 is Unicode and if so use wide API. */
1460 if (unicode_file_names()) {
1461 PyUnicodeObject *po;
1462 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1463 WIN32_FIND_DATAW wFileData;
1464 Py_UNICODE wnamebuf[MAX_PATH*2+5];
1465 Py_UNICODE wch;
1466 wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH);
1467 wnamebuf[MAX_PATH] = L'\0';
1468 len = wcslen(wnamebuf);
1469 wch = (len > 0) ? wnamebuf[len-1] : L'\0';
1470 if (wch != L'/' && wch != L'\\' && wch != L':')
1471 wnamebuf[len++] = L'/';
1472 wcscpy(wnamebuf + len, L"*.*");
1473 if ((d = PyList_New(0)) == NULL)
1474 return NULL;
1475 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1476 if (hFindFile == INVALID_HANDLE_VALUE) {
1477 errno = GetLastError();
1478 if (errno == ERROR_FILE_NOT_FOUND) {
1479 return d;
1480 }
1481 Py_DECREF(d);
1482 return win32_error_unicode("FindFirstFileW", wnamebuf);
1483 }
1484 do {
1485 if (wFileData.cFileName[0] == L'.' &&
1486 (wFileData.cFileName[1] == L'\0' ||
1487 wFileData.cFileName[1] == L'.' &&
1488 wFileData.cFileName[2] == L'\0'))
1489 continue;
1490 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1491 if (v == NULL) {
1492 Py_DECREF(d);
1493 d = NULL;
1494 break;
1495 }
1496 if (PyList_Append(d, v) != 0) {
1497 Py_DECREF(v);
1498 Py_DECREF(d);
1499 d = NULL;
1500 break;
1501 }
1502 Py_DECREF(v);
1503 } while (FindNextFileW(hFindFile, &wFileData) == TRUE);
1504
1505 if (FindClose(hFindFile) == FALSE) {
1506 Py_DECREF(d);
1507 return win32_error_unicode("FindClose", wnamebuf);
1508 }
1509 return d;
1510 }
1511 /* Drop the argument parsing error as narrow strings
1512 are also valid. */
1513 PyErr_Clear();
1514 }
1515#endif
1516
Tim Peters5aa91602002-01-30 05:46:57 +00001517 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001518 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00001519 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00001520 if (len > 0) {
1521 char ch = namebuf[len-1];
1522 if (ch != SEP && ch != ALTSEP && ch != ':')
1523 namebuf[len++] = '/';
1524 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001525 strcpy(namebuf + len, "*.*");
1526
Barry Warsaw53699e91996-12-10 23:23:01 +00001527 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001528 return NULL;
1529
1530 hFindFile = FindFirstFile(namebuf, &FileData);
1531 if (hFindFile == INVALID_HANDLE_VALUE) {
1532 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +00001533 if (errno == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001534 return d;
1535 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001536 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001537 }
1538 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001539 if (FileData.cFileName[0] == '.' &&
1540 (FileData.cFileName[1] == '\0' ||
1541 FileData.cFileName[1] == '.' &&
1542 FileData.cFileName[2] == '\0'))
1543 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001544 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001545 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001546 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001547 d = NULL;
1548 break;
1549 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001550 if (PyList_Append(d, v) != 0) {
1551 Py_DECREF(v);
1552 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001553 d = NULL;
1554 break;
1555 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001556 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001557 } while (FindNextFile(hFindFile, &FileData) == TRUE);
1558
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001559 if (FindClose(hFindFile) == FALSE) {
1560 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001561 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001562 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001563
1564 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001565
Tim Peters0bb44a42000-09-15 07:44:49 +00001566#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001567
1568#ifndef MAX_PATH
1569#define MAX_PATH CCHMAXPATH
1570#endif
1571 char *name, *pt;
1572 int len;
1573 PyObject *d, *v;
1574 char namebuf[MAX_PATH+5];
1575 HDIR hdir = 1;
1576 ULONG srchcnt = 1;
1577 FILEFINDBUF3 ep;
1578 APIRET rc;
1579
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001580 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001581 return NULL;
1582 if (len >= MAX_PATH) {
1583 PyErr_SetString(PyExc_ValueError, "path too long");
1584 return NULL;
1585 }
1586 strcpy(namebuf, name);
1587 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001588 if (*pt == ALTSEP)
1589 *pt = SEP;
1590 if (namebuf[len-1] != SEP)
1591 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001592 strcpy(namebuf + len, "*.*");
1593
1594 if ((d = PyList_New(0)) == NULL)
1595 return NULL;
1596
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001597 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
1598 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001599 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001600 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
1601 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
1602 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001603
1604 if (rc != NO_ERROR) {
1605 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001606 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001607 }
1608
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001609 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001610 do {
1611 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001612 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001613 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001614
1615 strcpy(namebuf, ep.achName);
1616
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001617 /* Leave Case of Name Alone -- In Native Form */
1618 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001619
1620 v = PyString_FromString(namebuf);
1621 if (v == NULL) {
1622 Py_DECREF(d);
1623 d = NULL;
1624 break;
1625 }
1626 if (PyList_Append(d, v) != 0) {
1627 Py_DECREF(v);
1628 Py_DECREF(d);
1629 d = NULL;
1630 break;
1631 }
1632 Py_DECREF(v);
1633 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
1634 }
1635
1636 return d;
1637#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001638
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001639 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001640 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001641 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001642 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00001643 int arg_is_unicode = 1;
1644
1645 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
1646 arg_is_unicode = 0;
1647 PyErr_Clear();
1648 }
1649 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001650 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001651 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001652 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001653 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001654 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001655 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001656 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001657 return NULL;
1658 }
1659 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001660 if (ep->d_name[0] == '.' &&
1661 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001662 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001663 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001664 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001665 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001666 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001667 d = NULL;
1668 break;
1669 }
Just van Rossum46c97842003-02-25 21:42:15 +00001670#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00001671 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00001672 PyObject *w;
1673
1674 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00001675 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00001676 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00001677 if (w != NULL) {
1678 Py_DECREF(v);
1679 v = w;
1680 }
1681 else {
1682 /* fall back to the original byte string, as
1683 discussed in patch #683592 */
1684 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00001685 }
Just van Rossum46c97842003-02-25 21:42:15 +00001686 }
1687#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001688 if (PyList_Append(d, v) != 0) {
1689 Py_DECREF(v);
1690 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001691 d = NULL;
1692 break;
1693 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001694 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001695 }
1696 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001697 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001698
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001699 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001700
Tim Peters0bb44a42000-09-15 07:44:49 +00001701#endif /* which OS */
1702} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001703
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001704#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00001705/* A helper function for abspath on win32 */
1706static PyObject *
1707posix__getfullpathname(PyObject *self, PyObject *args)
1708{
1709 /* assume encoded strings wont more than double no of chars */
1710 char inbuf[MAX_PATH*2];
1711 char *inbufp = inbuf;
1712 int insize = sizeof(inbuf)/sizeof(inbuf[0]);
1713 char outbuf[MAX_PATH*2];
1714 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001715#ifdef Py_WIN_WIDE_FILENAMES
1716 if (unicode_file_names()) {
1717 PyUnicodeObject *po;
1718 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
1719 Py_UNICODE woutbuf[MAX_PATH*2];
1720 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00001721 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001722 sizeof(woutbuf)/sizeof(woutbuf[0]),
1723 woutbuf, &wtemp))
1724 return win32_error("GetFullPathName", "");
1725 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
1726 }
1727 /* Drop the argument parsing error as narrow strings
1728 are also valid. */
1729 PyErr_Clear();
1730 }
1731#endif
Tim Peters5aa91602002-01-30 05:46:57 +00001732 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
1733 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00001734 &insize))
1735 return NULL;
1736 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
1737 outbuf, &temp))
1738 return win32_error("GetFullPathName", inbuf);
1739 return PyString_FromString(outbuf);
1740} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001741#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00001742
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001743PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001744"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001745Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001746
Barry Warsaw53699e91996-12-10 23:23:01 +00001747static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001748posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001749{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001750 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00001751 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001752 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001753
1754#ifdef Py_WIN_WIDE_FILENAMES
1755 if (unicode_file_names()) {
1756 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00001757 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001758 Py_BEGIN_ALLOW_THREADS
1759 /* PyUnicode_AS_UNICODE OK without thread lock as
1760 it is a simple dereference. */
1761 res = _wmkdir(PyUnicode_AS_UNICODE(po));
1762 Py_END_ALLOW_THREADS
1763 if (res < 0)
1764 return posix_error();
1765 Py_INCREF(Py_None);
1766 return Py_None;
1767 }
1768 /* Drop the argument parsing error as narrow strings
1769 are also valid. */
1770 PyErr_Clear();
1771 }
1772#endif
1773
Tim Peters5aa91602002-01-30 05:46:57 +00001774 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001775 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001776 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001777 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001778#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001779 res = mkdir(path);
1780#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001781 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001782#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001783 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001784 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001785 return posix_error_with_allocated_filename(path);
1786 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001787 Py_INCREF(Py_None);
1788 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001789}
1790
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001791
Guido van Rossumb6775db1994-08-01 11:34:53 +00001792#ifdef HAVE_NICE
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001793#if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H)
1794#if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS)
1795#include <sys/resource.h>
1796#endif
1797#endif
1798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001800"nice(inc) -> new_priority\n\n\
1801Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001802
Barry Warsaw53699e91996-12-10 23:23:01 +00001803static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001804posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00001805{
1806 int increment, value;
1807
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001808 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001809 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001810
1811 /* There are two flavours of 'nice': one that returns the new
1812 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001813 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
1814 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00001815
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001816 If we are of the nice family that returns the new priority, we
1817 need to clear errno before the call, and check if errno is filled
1818 before calling posix_error() on a returnvalue of -1, because the
1819 -1 may be the actual new priority! */
1820
1821 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001822 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001823#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001824 if (value == 0)
1825 value = getpriority(PRIO_PROCESS, 0);
1826#endif
1827 if (value == -1 && errno != 0)
1828 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00001829 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001830 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001831}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001832#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001833
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001834
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001835PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001836"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001837Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001838
Barry Warsaw53699e91996-12-10 23:23:01 +00001839static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001840posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001841{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001842#ifdef MS_WINDOWS
1843 return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename);
1844#else
1845 return posix_2str(args, "etet:rename", rename, NULL, NULL);
1846#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001847}
1848
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001849
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001850PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001851"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001852Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001853
Barry Warsaw53699e91996-12-10 23:23:01 +00001854static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001855posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001856{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001857#ifdef MS_WINDOWS
1858 return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir);
1859#else
1860 return posix_1str(args, "et:rmdir", rmdir, NULL, NULL);
1861#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001862}
1863
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001864
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001865PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001866"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001867Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001868
Barry Warsaw53699e91996-12-10 23:23:01 +00001869static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001870posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001871{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001872#ifdef MS_WINDOWS
1873 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64);
1874#else
1875 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
1876#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001877}
1878
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001879
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001880#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001881PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001882"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001883Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001884
Barry Warsaw53699e91996-12-10 23:23:01 +00001885static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001886posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001887{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001888 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001889 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001890 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001891 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001892 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001893 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001894 Py_END_ALLOW_THREADS
1895 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001896}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001897#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001898
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001899
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001900PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001901"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001902Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001903
Barry Warsaw53699e91996-12-10 23:23:01 +00001904static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001905posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001906{
1907 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001908 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001909 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00001910 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001911 if (i < 0)
1912 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001913 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001914}
1915
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001916
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001917PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001918"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001919Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001920
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001921PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001922"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001923Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001924
Barry Warsaw53699e91996-12-10 23:23:01 +00001925static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001926posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001927{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001928#ifdef MS_WINDOWS
1929 return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink);
1930#else
1931 return posix_1str(args, "et:remove", unlink, NULL, NULL);
1932#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001933}
1934
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001935
Guido van Rossumb6775db1994-08-01 11:34:53 +00001936#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001937PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001938"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001939Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001940
Barry Warsaw53699e91996-12-10 23:23:01 +00001941static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001942posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001943{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001944 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001945 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001946
Barry Warsaw53699e91996-12-10 23:23:01 +00001947 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001948 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001949 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001950 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001951 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001952 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001953 u.sysname,
1954 u.nodename,
1955 u.release,
1956 u.version,
1957 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001958}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001959#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001960
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001961static int
1962extract_time(PyObject *t, long* sec, long* usec)
1963{
1964 long intval;
1965 if (PyFloat_Check(t)) {
1966 double tval = PyFloat_AsDouble(t);
1967 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
1968 if (!intobj)
1969 return -1;
1970 intval = PyInt_AsLong(intobj);
1971 Py_DECREF(intobj);
1972 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00001973 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001974 if (*usec < 0)
1975 /* If rounding gave us a negative number,
1976 truncate. */
1977 *usec = 0;
1978 return 0;
1979 }
1980 intval = PyInt_AsLong(t);
1981 if (intval == -1 && PyErr_Occurred())
1982 return -1;
1983 *sec = intval;
1984 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00001985 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001986}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001987
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001988PyDoc_STRVAR(posix_utime__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001989"utime(path, (atime, utime))\n\
1990utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00001991Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001992second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001993
Barry Warsaw53699e91996-12-10 23:23:01 +00001994static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001995posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001996{
Neal Norwitz2adf2102004-06-09 01:46:02 +00001997 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001998 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001999 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002000 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002001
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002002#if defined(HAVE_UTIMES)
2003 struct timeval buf[2];
2004#define ATIME buf[0].tv_sec
2005#define MTIME buf[1].tv_sec
2006#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002007/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002008 struct utimbuf buf;
2009#define ATIME buf.actime
2010#define MTIME buf.modtime
2011#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002012#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002013 time_t buf[2];
2014#define ATIME buf[0]
2015#define MTIME buf[1]
2016#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002017#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002018
Mark Hammond817c9292003-12-03 01:22:38 +00002019 int have_unicode_filename = 0;
2020#ifdef Py_WIN_WIDE_FILENAMES
2021 PyUnicodeObject *obwpath;
2022 wchar_t *wpath;
2023 if (unicode_file_names()) {
2024 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2025 wpath = PyUnicode_AS_UNICODE(obwpath);
2026 have_unicode_filename = 1;
2027 } else
2028 /* Drop the argument parsing error as narrow strings
2029 are also valid. */
2030 PyErr_Clear();
2031 }
2032#endif /* Py_WIN_WIDE_FILENAMES */
2033
2034 if (!have_unicode_filename && \
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002035 !PyArg_ParseTuple(args, "etO:utime",
2036 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002037 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002038 if (arg == Py_None) {
2039 /* optional time values not given */
2040 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002041#ifdef Py_WIN_WIDE_FILENAMES
2042 if (have_unicode_filename)
2043 res = _wutime(wpath, NULL);
2044 else
2045#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002046 res = utime(path, NULL);
2047 Py_END_ALLOW_THREADS
2048 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002049 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002050 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002051 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002052 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002053 return NULL;
2054 }
2055 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002056 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002057 &atime, &ausec) == -1) {
2058 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002059 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002060 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002061 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002062 &mtime, &musec) == -1) {
2063 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002064 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002065 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002066 ATIME = atime;
2067 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002068#ifdef HAVE_UTIMES
2069 buf[0].tv_usec = ausec;
2070 buf[1].tv_usec = musec;
2071 Py_BEGIN_ALLOW_THREADS
2072 res = utimes(path, buf);
2073 Py_END_ALLOW_THREADS
2074#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002075 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002076#ifdef Py_WIN_WIDE_FILENAMES
2077 if (have_unicode_filename)
2078 /* utime is OK with utimbuf, but _wutime insists
2079 on _utimbuf (the msvc headers assert the
2080 underscore version is ansi) */
2081 res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG);
2082 else
2083#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002084 res = utime(path, UTIME_ARG);
2085 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002086#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002087 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002088 if (res < 0) {
2089#ifdef Py_WIN_WIDE_FILENAMES
Neal Norwitz2adf2102004-06-09 01:46:02 +00002090 if (have_unicode_filename)
Mark Hammond2d5914b2004-05-04 08:10:37 +00002091 return posix_error_with_unicode_filename(wpath);
2092#endif /* Py_WIN_WIDE_FILENAMES */
Neal Norwitz96652712004-06-06 20:40:27 +00002093 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002094 }
Neal Norwitz96652712004-06-06 20:40:27 +00002095 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002096 Py_INCREF(Py_None);
2097 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002098#undef UTIME_ARG
2099#undef ATIME
2100#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002101}
2102
Guido van Rossum85e3b011991-06-03 12:42:10 +00002103
Guido van Rossum3b066191991-06-04 19:40:25 +00002104/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002106PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002107"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002108Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002109
Barry Warsaw53699e91996-12-10 23:23:01 +00002110static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002111posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002112{
2113 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002114 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002115 return NULL;
2116 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002117 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002118}
2119
Martin v. Löwis114619e2002-10-07 06:44:21 +00002120#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2121static void
2122free_string_array(char **array, int count)
2123{
2124 int i;
2125 for (i = 0; i < count; i++)
2126 PyMem_Free(array[i]);
2127 PyMem_DEL(array);
2128}
2129#endif
2130
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002131
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002132#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002133PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002134"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002135Execute an executable path with arguments, replacing current process.\n\
2136\n\
2137 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002138 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002139
Barry Warsaw53699e91996-12-10 23:23:01 +00002140static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002141posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002142{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002143 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002144 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002145 char **argvlist;
2146 int i, argc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002147 PyObject *(*getitem)(PyObject *, int);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002148
Guido van Rossum89b33251993-10-22 14:26:06 +00002149 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002150 argv is a list or tuple of strings. */
2151
Martin v. Löwis114619e2002-10-07 06:44:21 +00002152 if (!PyArg_ParseTuple(args, "etO:execv",
2153 Py_FileSystemDefaultEncoding,
2154 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002155 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002156 if (PyList_Check(argv)) {
2157 argc = PyList_Size(argv);
2158 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002159 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002160 else if (PyTuple_Check(argv)) {
2161 argc = PyTuple_Size(argv);
2162 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002163 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002164 else {
Fred Drake661ea262000-10-24 19:57:45 +00002165 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002166 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002167 return NULL;
2168 }
2169
2170 if (argc == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00002171 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002172 PyMem_Free(path);
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002173 return NULL;
2174 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002175
Barry Warsaw53699e91996-12-10 23:23:01 +00002176 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002177 if (argvlist == NULL) {
2178 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002179 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002180 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002181 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002182 if (!PyArg_Parse((*getitem)(argv, i), "et",
2183 Py_FileSystemDefaultEncoding,
2184 &argvlist[i])) {
2185 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002186 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002187 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002188 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002189 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002190
Guido van Rossum85e3b011991-06-03 12:42:10 +00002191 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002192 }
2193 argvlist[argc] = NULL;
2194
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002195 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002196
Guido van Rossum85e3b011991-06-03 12:42:10 +00002197 /* If we get here it's definitely an error */
2198
Martin v. Löwis114619e2002-10-07 06:44:21 +00002199 free_string_array(argvlist, argc);
2200 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002201 return posix_error();
2202}
2203
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002204
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002205PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002206"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002207Execute a path with arguments and environment, replacing current process.\n\
2208\n\
2209 path: path of executable file\n\
2210 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002211 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002212
Barry Warsaw53699e91996-12-10 23:23:01 +00002213static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002214posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002215{
2216 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002217 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002218 char **argvlist;
2219 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002220 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002221 int i, pos, argc, envc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002222 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002223 int lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002224
2225 /* execve has three arguments: (path, argv, env), where
2226 argv is a list or tuple of strings and env is a dictionary
2227 like posix.environ. */
2228
Martin v. Löwis114619e2002-10-07 06:44:21 +00002229 if (!PyArg_ParseTuple(args, "etOO:execve",
2230 Py_FileSystemDefaultEncoding,
2231 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002232 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002233 if (PyList_Check(argv)) {
2234 argc = PyList_Size(argv);
2235 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002236 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002237 else if (PyTuple_Check(argv)) {
2238 argc = PyTuple_Size(argv);
2239 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002240 }
2241 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002242 PyErr_SetString(PyExc_TypeError,
2243 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002244 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002245 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002246 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002247 PyErr_SetString(PyExc_TypeError,
2248 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002249 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002250 }
2251
Guido van Rossum50422b42000-04-26 20:34:28 +00002252 if (argc == 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00002253 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00002254 "execve() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002255 goto fail_0;
Guido van Rossum50422b42000-04-26 20:34:28 +00002256 }
2257
Barry Warsaw53699e91996-12-10 23:23:01 +00002258 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002259 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002260 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002261 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002262 }
2263 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002264 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002265 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002266 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002267 &argvlist[i]))
2268 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002269 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002270 goto fail_1;
2271 }
2272 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002273 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002274 argvlist[argc] = NULL;
2275
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002276 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002277 if (i < 0)
2278 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002279 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002280 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002281 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002282 goto fail_1;
2283 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002284 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002285 keys = PyMapping_Keys(env);
2286 vals = PyMapping_Values(env);
2287 if (!keys || !vals)
2288 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002289 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2290 PyErr_SetString(PyExc_TypeError,
2291 "execve(): env.keys() or env.values() is not a list");
2292 goto fail_2;
2293 }
Tim Peters5aa91602002-01-30 05:46:57 +00002294
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002295 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002296 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002297 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002298
2299 key = PyList_GetItem(keys, pos);
2300 val = PyList_GetItem(vals, pos);
2301 if (!key || !val)
2302 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002303
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002304 if (!PyArg_Parse(
2305 key,
2306 "s;execve() arg 3 contains a non-string key",
2307 &k) ||
2308 !PyArg_Parse(
2309 val,
2310 "s;execve() arg 3 contains a non-string value",
2311 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002312 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002313 goto fail_2;
2314 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002315
2316#if defined(PYOS_OS2)
2317 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2318 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2319#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002320 len = PyString_Size(key) + PyString_Size(val) + 2;
2321 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002322 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002323 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002324 goto fail_2;
2325 }
Tim Petersc8996f52001-12-03 20:41:00 +00002326 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002327 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002328#if defined(PYOS_OS2)
2329 }
2330#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002331 }
2332 envlist[envc] = 0;
2333
2334 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002335
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002336 /* If we get here it's definitely an error */
2337
2338 (void) posix_error();
2339
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002340 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002341 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002342 PyMem_DEL(envlist[envc]);
2343 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002344 fail_1:
2345 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002346 Py_XDECREF(vals);
2347 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002348 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002349 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002350 return NULL;
2351}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002352#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002353
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002354
Guido van Rossuma1065681999-01-25 23:20:23 +00002355#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002356PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002357"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002358Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002359\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002360 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002361 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002362 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002363
2364static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002365posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002366{
2367 char *path;
2368 PyObject *argv;
2369 char **argvlist;
2370 int mode, i, argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002371 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002372 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00002373
2374 /* spawnv has three arguments: (mode, path, argv), where
2375 argv is a list or tuple of strings. */
2376
Martin v. Löwis114619e2002-10-07 06:44:21 +00002377 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2378 Py_FileSystemDefaultEncoding,
2379 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002380 return NULL;
2381 if (PyList_Check(argv)) {
2382 argc = PyList_Size(argv);
2383 getitem = PyList_GetItem;
2384 }
2385 else if (PyTuple_Check(argv)) {
2386 argc = PyTuple_Size(argv);
2387 getitem = PyTuple_GetItem;
2388 }
2389 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002390 PyErr_SetString(PyExc_TypeError,
2391 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002392 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002393 return NULL;
2394 }
2395
2396 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002397 if (argvlist == NULL) {
2398 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002399 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002400 }
Guido van Rossuma1065681999-01-25 23:20:23 +00002401 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002402 if (!PyArg_Parse((*getitem)(argv, i), "et",
2403 Py_FileSystemDefaultEncoding,
2404 &argvlist[i])) {
2405 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002406 PyErr_SetString(
2407 PyExc_TypeError,
2408 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002409 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00002410 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00002411 }
2412 }
2413 argvlist[argc] = NULL;
2414
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002415#if defined(PYOS_OS2) && defined(PYCC_GCC)
2416 Py_BEGIN_ALLOW_THREADS
2417 spawnval = spawnv(mode, path, argvlist);
2418 Py_END_ALLOW_THREADS
2419#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002420 if (mode == _OLD_P_OVERLAY)
2421 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00002422
Tim Peters25059d32001-12-07 20:35:43 +00002423 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002424 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00002425 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002426#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002427
Martin v. Löwis114619e2002-10-07 06:44:21 +00002428 free_string_array(argvlist, argc);
2429 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002430
Fred Drake699f3522000-06-29 21:12:41 +00002431 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002432 return posix_error();
2433 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002434#if SIZEOF_LONG == SIZEOF_VOID_P
2435 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002436#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002437 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002438#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002439}
2440
2441
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002442PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002443"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002444Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002445\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002446 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002447 path: path of executable file\n\
2448 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002449 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002450
2451static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002452posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002453{
2454 char *path;
2455 PyObject *argv, *env;
2456 char **argvlist;
2457 char **envlist;
2458 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2459 int mode, i, pos, argc, envc;
Tim Peters79248aa2001-08-29 21:37:10 +00002460 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002461 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002462 int lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002463
2464 /* spawnve has four arguments: (mode, path, argv, env), where
2465 argv is a list or tuple of strings and env is a dictionary
2466 like posix.environ. */
2467
Martin v. Löwis114619e2002-10-07 06:44:21 +00002468 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
2469 Py_FileSystemDefaultEncoding,
2470 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00002471 return NULL;
2472 if (PyList_Check(argv)) {
2473 argc = PyList_Size(argv);
2474 getitem = PyList_GetItem;
2475 }
2476 else if (PyTuple_Check(argv)) {
2477 argc = PyTuple_Size(argv);
2478 getitem = PyTuple_GetItem;
2479 }
2480 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002481 PyErr_SetString(PyExc_TypeError,
2482 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002483 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002484 }
2485 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002486 PyErr_SetString(PyExc_TypeError,
2487 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002488 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002489 }
2490
2491 argvlist = PyMem_NEW(char *, argc+1);
2492 if (argvlist == NULL) {
2493 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002494 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002495 }
2496 for (i = 0; i < argc; i++) {
2497 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002498 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00002499 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00002500 &argvlist[i]))
2501 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002502 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00002503 goto fail_1;
2504 }
2505 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002506 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00002507 argvlist[argc] = NULL;
2508
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002509 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002510 if (i < 0)
2511 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00002512 envlist = PyMem_NEW(char *, i + 1);
2513 if (envlist == NULL) {
2514 PyErr_NoMemory();
2515 goto fail_1;
2516 }
2517 envc = 0;
2518 keys = PyMapping_Keys(env);
2519 vals = PyMapping_Values(env);
2520 if (!keys || !vals)
2521 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002522 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2523 PyErr_SetString(PyExc_TypeError,
2524 "spawnve(): env.keys() or env.values() is not a list");
2525 goto fail_2;
2526 }
Tim Peters5aa91602002-01-30 05:46:57 +00002527
Guido van Rossuma1065681999-01-25 23:20:23 +00002528 for (pos = 0; pos < i; pos++) {
2529 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002530 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00002531
2532 key = PyList_GetItem(keys, pos);
2533 val = PyList_GetItem(vals, pos);
2534 if (!key || !val)
2535 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002536
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002537 if (!PyArg_Parse(
2538 key,
2539 "s;spawnve() arg 3 contains a non-string key",
2540 &k) ||
2541 !PyArg_Parse(
2542 val,
2543 "s;spawnve() arg 3 contains a non-string value",
2544 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00002545 {
2546 goto fail_2;
2547 }
Tim Petersc8996f52001-12-03 20:41:00 +00002548 len = PyString_Size(key) + PyString_Size(val) + 2;
2549 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00002550 if (p == NULL) {
2551 PyErr_NoMemory();
2552 goto fail_2;
2553 }
Tim Petersc8996f52001-12-03 20:41:00 +00002554 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00002555 envlist[envc++] = p;
2556 }
2557 envlist[envc] = 0;
2558
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002559#if defined(PYOS_OS2) && defined(PYCC_GCC)
2560 Py_BEGIN_ALLOW_THREADS
2561 spawnval = spawnve(mode, path, argvlist, envlist);
2562 Py_END_ALLOW_THREADS
2563#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002564 if (mode == _OLD_P_OVERLAY)
2565 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00002566
2567 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002568 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00002569 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002570#endif
Tim Peters25059d32001-12-07 20:35:43 +00002571
Fred Drake699f3522000-06-29 21:12:41 +00002572 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002573 (void) posix_error();
2574 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002575#if SIZEOF_LONG == SIZEOF_VOID_P
2576 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002577#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002578 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002579#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002580
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002581 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00002582 while (--envc >= 0)
2583 PyMem_DEL(envlist[envc]);
2584 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002585 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002586 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00002587 Py_XDECREF(vals);
2588 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002589 fail_0:
2590 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002591 return res;
2592}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00002593
2594/* OS/2 supports spawnvp & spawnvpe natively */
2595#if defined(PYOS_OS2)
2596PyDoc_STRVAR(posix_spawnvp__doc__,
2597"spawnvp(mode, file, args)\n\n\
2598Execute the program 'file' in a new process, using the environment\n\
2599search path to find the file.\n\
2600\n\
2601 mode: mode of process creation\n\
2602 file: executable file name\n\
2603 args: tuple or list of strings");
2604
2605static PyObject *
2606posix_spawnvp(PyObject *self, PyObject *args)
2607{
2608 char *path;
2609 PyObject *argv;
2610 char **argvlist;
2611 int mode, i, argc;
2612 Py_intptr_t spawnval;
2613 PyObject *(*getitem)(PyObject *, int);
2614
2615 /* spawnvp has three arguments: (mode, path, argv), where
2616 argv is a list or tuple of strings. */
2617
2618 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
2619 Py_FileSystemDefaultEncoding,
2620 &path, &argv))
2621 return NULL;
2622 if (PyList_Check(argv)) {
2623 argc = PyList_Size(argv);
2624 getitem = PyList_GetItem;
2625 }
2626 else if (PyTuple_Check(argv)) {
2627 argc = PyTuple_Size(argv);
2628 getitem = PyTuple_GetItem;
2629 }
2630 else {
2631 PyErr_SetString(PyExc_TypeError,
2632 "spawnvp() arg 2 must be a tuple or list");
2633 PyMem_Free(path);
2634 return NULL;
2635 }
2636
2637 argvlist = PyMem_NEW(char *, argc+1);
2638 if (argvlist == NULL) {
2639 PyMem_Free(path);
2640 return PyErr_NoMemory();
2641 }
2642 for (i = 0; i < argc; i++) {
2643 if (!PyArg_Parse((*getitem)(argv, i), "et",
2644 Py_FileSystemDefaultEncoding,
2645 &argvlist[i])) {
2646 free_string_array(argvlist, i);
2647 PyErr_SetString(
2648 PyExc_TypeError,
2649 "spawnvp() arg 2 must contain only strings");
2650 PyMem_Free(path);
2651 return NULL;
2652 }
2653 }
2654 argvlist[argc] = NULL;
2655
2656 Py_BEGIN_ALLOW_THREADS
2657#if defined(PYCC_GCC)
2658 spawnval = spawnvp(mode, path, argvlist);
2659#else
2660 spawnval = _spawnvp(mode, path, argvlist);
2661#endif
2662 Py_END_ALLOW_THREADS
2663
2664 free_string_array(argvlist, argc);
2665 PyMem_Free(path);
2666
2667 if (spawnval == -1)
2668 return posix_error();
2669 else
2670 return Py_BuildValue("l", (long) spawnval);
2671}
2672
2673
2674PyDoc_STRVAR(posix_spawnvpe__doc__,
2675"spawnvpe(mode, file, args, env)\n\n\
2676Execute the program 'file' in a new process, using the environment\n\
2677search path to find the file.\n\
2678\n\
2679 mode: mode of process creation\n\
2680 file: executable file name\n\
2681 args: tuple or list of arguments\n\
2682 env: dictionary of strings mapping to strings");
2683
2684static PyObject *
2685posix_spawnvpe(PyObject *self, PyObject *args)
2686{
2687 char *path;
2688 PyObject *argv, *env;
2689 char **argvlist;
2690 char **envlist;
2691 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2692 int mode, i, pos, argc, envc;
2693 Py_intptr_t spawnval;
2694 PyObject *(*getitem)(PyObject *, int);
2695 int lastarg = 0;
2696
2697 /* spawnvpe has four arguments: (mode, path, argv, env), where
2698 argv is a list or tuple of strings and env is a dictionary
2699 like posix.environ. */
2700
2701 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
2702 Py_FileSystemDefaultEncoding,
2703 &path, &argv, &env))
2704 return NULL;
2705 if (PyList_Check(argv)) {
2706 argc = PyList_Size(argv);
2707 getitem = PyList_GetItem;
2708 }
2709 else if (PyTuple_Check(argv)) {
2710 argc = PyTuple_Size(argv);
2711 getitem = PyTuple_GetItem;
2712 }
2713 else {
2714 PyErr_SetString(PyExc_TypeError,
2715 "spawnvpe() arg 2 must be a tuple or list");
2716 goto fail_0;
2717 }
2718 if (!PyMapping_Check(env)) {
2719 PyErr_SetString(PyExc_TypeError,
2720 "spawnvpe() arg 3 must be a mapping object");
2721 goto fail_0;
2722 }
2723
2724 argvlist = PyMem_NEW(char *, argc+1);
2725 if (argvlist == NULL) {
2726 PyErr_NoMemory();
2727 goto fail_0;
2728 }
2729 for (i = 0; i < argc; i++) {
2730 if (!PyArg_Parse((*getitem)(argv, i),
2731 "et;spawnvpe() arg 2 must contain only strings",
2732 Py_FileSystemDefaultEncoding,
2733 &argvlist[i]))
2734 {
2735 lastarg = i;
2736 goto fail_1;
2737 }
2738 }
2739 lastarg = argc;
2740 argvlist[argc] = NULL;
2741
2742 i = PyMapping_Size(env);
2743 if (i < 0)
2744 goto fail_1;
2745 envlist = PyMem_NEW(char *, i + 1);
2746 if (envlist == NULL) {
2747 PyErr_NoMemory();
2748 goto fail_1;
2749 }
2750 envc = 0;
2751 keys = PyMapping_Keys(env);
2752 vals = PyMapping_Values(env);
2753 if (!keys || !vals)
2754 goto fail_2;
2755 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2756 PyErr_SetString(PyExc_TypeError,
2757 "spawnvpe(): env.keys() or env.values() is not a list");
2758 goto fail_2;
2759 }
2760
2761 for (pos = 0; pos < i; pos++) {
2762 char *p, *k, *v;
2763 size_t len;
2764
2765 key = PyList_GetItem(keys, pos);
2766 val = PyList_GetItem(vals, pos);
2767 if (!key || !val)
2768 goto fail_2;
2769
2770 if (!PyArg_Parse(
2771 key,
2772 "s;spawnvpe() arg 3 contains a non-string key",
2773 &k) ||
2774 !PyArg_Parse(
2775 val,
2776 "s;spawnvpe() arg 3 contains a non-string value",
2777 &v))
2778 {
2779 goto fail_2;
2780 }
2781 len = PyString_Size(key) + PyString_Size(val) + 2;
2782 p = PyMem_NEW(char, len);
2783 if (p == NULL) {
2784 PyErr_NoMemory();
2785 goto fail_2;
2786 }
2787 PyOS_snprintf(p, len, "%s=%s", k, v);
2788 envlist[envc++] = p;
2789 }
2790 envlist[envc] = 0;
2791
2792 Py_BEGIN_ALLOW_THREADS
2793#if defined(PYCC_GCC)
2794 spawnval = spawnve(mode, path, argvlist, envlist);
2795#else
2796 spawnval = _spawnve(mode, path, argvlist, envlist);
2797#endif
2798 Py_END_ALLOW_THREADS
2799
2800 if (spawnval == -1)
2801 (void) posix_error();
2802 else
2803 res = Py_BuildValue("l", (long) spawnval);
2804
2805 fail_2:
2806 while (--envc >= 0)
2807 PyMem_DEL(envlist[envc]);
2808 PyMem_DEL(envlist);
2809 fail_1:
2810 free_string_array(argvlist, lastarg);
2811 Py_XDECREF(vals);
2812 Py_XDECREF(keys);
2813 fail_0:
2814 PyMem_Free(path);
2815 return res;
2816}
2817#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00002818#endif /* HAVE_SPAWNV */
2819
2820
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002821#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002822PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002823"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002824Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
2825\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002826Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002827
2828static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002829posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002830{
Neal Norwitze241ce82003-02-17 18:17:05 +00002831 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002832 if (pid == -1)
2833 return posix_error();
2834 PyOS_AfterFork();
2835 return PyInt_FromLong((long)pid);
2836}
2837#endif
2838
2839
Guido van Rossumad0ee831995-03-01 10:34:45 +00002840#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002841PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002842"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002843Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002844Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002845
Barry Warsaw53699e91996-12-10 23:23:01 +00002846static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002847posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002848{
Neal Norwitze241ce82003-02-17 18:17:05 +00002849 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00002850 if (pid == -1)
2851 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002852 if (pid == 0)
2853 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00002854 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002855}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002856#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002857
Neal Norwitzb59798b2003-03-21 01:43:31 +00002858/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00002859/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
2860#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00002861#define DEV_PTY_FILE "/dev/ptc"
2862#define HAVE_DEV_PTMX
2863#else
2864#define DEV_PTY_FILE "/dev/ptmx"
2865#endif
2866
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002867#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002868#ifdef HAVE_PTY_H
2869#include <pty.h>
2870#else
2871#ifdef HAVE_LIBUTIL_H
2872#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00002873#endif /* HAVE_LIBUTIL_H */
2874#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00002875#ifdef HAVE_STROPTS_H
2876#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002877#endif
2878#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002879
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002880#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002881PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002882"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002883Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002884
2885static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002886posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002887{
2888 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002889#ifndef HAVE_OPENPTY
2890 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002891#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002892#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002893 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002894#ifdef sun
2895 extern char *ptsname();
2896#endif
2897#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00002898
Thomas Wouters70c21a12000-07-14 14:28:33 +00002899#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00002900 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
2901 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002902#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00002903 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
2904 if (slave_name == NULL)
2905 return posix_error();
2906
2907 slave_fd = open(slave_name, O_RDWR);
2908 if (slave_fd < 0)
2909 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002910#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00002911 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002912 if (master_fd < 0)
2913 return posix_error();
2914 sig_saved = signal(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002915 /* change permission of slave */
2916 if (grantpt(master_fd) < 0) {
2917 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002918 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002919 }
2920 /* unlock slave */
2921 if (unlockpt(master_fd) < 0) {
2922 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002923 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002924 }
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002925 signal(SIGCHLD, sig_saved);
2926 slave_name = ptsname(master_fd); /* get name of slave */
2927 if (slave_name == NULL)
2928 return posix_error();
2929 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
2930 if (slave_fd < 0)
2931 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002932#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002933 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
2934 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00002935#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002936 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00002937#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002938#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00002939#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00002940
Fred Drake8cef4cf2000-06-28 16:40:38 +00002941 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00002942
Fred Drake8cef4cf2000-06-28 16:40:38 +00002943}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002944#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002945
2946#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002947PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002948"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00002949Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
2950Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002951To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002952
2953static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002954posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002955{
2956 int master_fd, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00002957
Fred Drake8cef4cf2000-06-28 16:40:38 +00002958 pid = forkpty(&master_fd, NULL, NULL, NULL);
2959 if (pid == -1)
2960 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002961 if (pid == 0)
2962 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00002963 return Py_BuildValue("(ii)", pid, master_fd);
2964}
2965#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002966
Guido van Rossumad0ee831995-03-01 10:34:45 +00002967#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002968PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002969"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002970Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002971
Barry Warsaw53699e91996-12-10 23:23:01 +00002972static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002973posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002974{
Barry Warsaw53699e91996-12-10 23:23:01 +00002975 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002976}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002977#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002978
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002979
Guido van Rossumad0ee831995-03-01 10:34:45 +00002980#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002981PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002982"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002983Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002984
Barry Warsaw53699e91996-12-10 23:23:01 +00002985static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002986posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002987{
Barry Warsaw53699e91996-12-10 23:23:01 +00002988 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002989}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002990#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002991
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002992
Guido van Rossumad0ee831995-03-01 10:34:45 +00002993#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002994PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002995"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002996Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002997
Barry Warsaw53699e91996-12-10 23:23:01 +00002998static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002999posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003000{
Barry Warsaw53699e91996-12-10 23:23:01 +00003001 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003002}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003003#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003004
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003005
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003006PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003007"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003008Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003009
Barry Warsaw53699e91996-12-10 23:23:01 +00003010static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003011posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003012{
Barry Warsaw53699e91996-12-10 23:23:01 +00003013 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003014}
3015
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003016
Fred Drakec9680921999-12-13 16:37:25 +00003017#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003018PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003019"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003020Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003021
3022static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003023posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003024{
3025 PyObject *result = NULL;
3026
Fred Drakec9680921999-12-13 16:37:25 +00003027#ifdef NGROUPS_MAX
3028#define MAX_GROUPS NGROUPS_MAX
3029#else
3030 /* defined to be 16 on Solaris7, so this should be a small number */
3031#define MAX_GROUPS 64
3032#endif
3033 gid_t grouplist[MAX_GROUPS];
3034 int n;
3035
3036 n = getgroups(MAX_GROUPS, grouplist);
3037 if (n < 0)
3038 posix_error();
3039 else {
3040 result = PyList_New(n);
3041 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003042 int i;
3043 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003044 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003045 if (o == NULL) {
3046 Py_DECREF(result);
3047 result = NULL;
3048 break;
3049 }
3050 PyList_SET_ITEM(result, i, o);
3051 }
3052 }
3053 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003054
Fred Drakec9680921999-12-13 16:37:25 +00003055 return result;
3056}
3057#endif
3058
Martin v. Löwis606edc12002-06-13 21:09:11 +00003059#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003060PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003061"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003062Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003063
3064static PyObject *
3065posix_getpgid(PyObject *self, PyObject *args)
3066{
3067 int pid, pgid;
3068 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3069 return NULL;
3070 pgid = getpgid(pid);
3071 if (pgid < 0)
3072 return posix_error();
3073 return PyInt_FromLong((long)pgid);
3074}
3075#endif /* HAVE_GETPGID */
3076
3077
Guido van Rossumb6775db1994-08-01 11:34:53 +00003078#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003079PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003080"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003081Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003082
Barry Warsaw53699e91996-12-10 23:23:01 +00003083static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003084posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003085{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003086#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003087 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003088#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003089 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003090#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003091}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003092#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003093
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003094
Guido van Rossumb6775db1994-08-01 11:34:53 +00003095#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003096PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003097"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003098Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003099
Barry Warsaw53699e91996-12-10 23:23:01 +00003100static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003101posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003102{
Guido van Rossum64933891994-10-20 21:56:42 +00003103#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003104 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003105#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003106 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003107#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003108 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003109 Py_INCREF(Py_None);
3110 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003111}
3112
Guido van Rossumb6775db1994-08-01 11:34:53 +00003113#endif /* HAVE_SETPGRP */
3114
Guido van Rossumad0ee831995-03-01 10:34:45 +00003115#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003116PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003117"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003118Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003119
Barry Warsaw53699e91996-12-10 23:23:01 +00003120static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003121posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003122{
Barry Warsaw53699e91996-12-10 23:23:01 +00003123 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003124}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003125#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003126
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003127
Fred Drake12c6e2d1999-12-14 21:25:03 +00003128#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003129PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003130"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003131Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003132
3133static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003134posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003135{
Neal Norwitze241ce82003-02-17 18:17:05 +00003136 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003137 char *name;
3138 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003139
Fred Drakea30680b2000-12-06 21:24:28 +00003140 errno = 0;
3141 name = getlogin();
3142 if (name == NULL) {
3143 if (errno)
3144 posix_error();
3145 else
3146 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003147 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003148 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003149 else
3150 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003151 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003152
Fred Drake12c6e2d1999-12-14 21:25:03 +00003153 return result;
3154}
3155#endif
3156
Guido van Rossumad0ee831995-03-01 10:34:45 +00003157#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003158PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003159"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003160Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003161
Barry Warsaw53699e91996-12-10 23:23:01 +00003162static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003163posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003164{
Barry Warsaw53699e91996-12-10 23:23:01 +00003165 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003166}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003167#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003168
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003169
Guido van Rossumad0ee831995-03-01 10:34:45 +00003170#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003171PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003172"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003173Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003174
Barry Warsaw53699e91996-12-10 23:23:01 +00003175static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003176posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003177{
3178 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003179 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003180 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003181#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003182 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3183 APIRET rc;
3184 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003185 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003186
3187 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3188 APIRET rc;
3189 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003190 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003191
3192 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003193 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003194#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003195 if (kill(pid, sig) == -1)
3196 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003197#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003198 Py_INCREF(Py_None);
3199 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003200}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003201#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003202
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003203#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003204PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003205"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003206Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003207
3208static PyObject *
3209posix_killpg(PyObject *self, PyObject *args)
3210{
3211 int pgid, sig;
3212 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3213 return NULL;
3214 if (killpg(pgid, sig) == -1)
3215 return posix_error();
3216 Py_INCREF(Py_None);
3217 return Py_None;
3218}
3219#endif
3220
Guido van Rossumc0125471996-06-28 18:55:32 +00003221#ifdef HAVE_PLOCK
3222
3223#ifdef HAVE_SYS_LOCK_H
3224#include <sys/lock.h>
3225#endif
3226
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003227PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003228"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003229Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003230
Barry Warsaw53699e91996-12-10 23:23:01 +00003231static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003232posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003233{
3234 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003235 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003236 return NULL;
3237 if (plock(op) == -1)
3238 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003239 Py_INCREF(Py_None);
3240 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003241}
3242#endif
3243
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003244
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003245#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003246PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003247"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003248Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003249
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003250#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003251#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003252static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003253async_system(const char *command)
3254{
3255 char *p, errormsg[256], args[1024];
3256 RESULTCODES rcodes;
3257 APIRET rc;
3258 char *shell = getenv("COMSPEC");
3259 if (!shell)
3260 shell = "cmd";
3261
3262 strcpy(args, shell);
3263 p = &args[ strlen(args)+1 ];
3264 strcpy(p, "/c ");
3265 strcat(p, command);
3266 p += strlen(p) + 1;
3267 *p = '\0';
3268
3269 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003270 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003271 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003272 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003273 &rcodes, shell);
3274 return rc;
3275}
3276
Guido van Rossumd48f2521997-12-05 22:19:34 +00003277static FILE *
3278popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003279{
3280 HFILE rhan, whan;
3281 FILE *retfd = NULL;
3282 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
3283
Guido van Rossumd48f2521997-12-05 22:19:34 +00003284 if (rc != NO_ERROR) {
3285 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003286 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00003287 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003288
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003289 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
3290 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003291
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003292 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3293 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003294
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003295 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
3296 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003297
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003298 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003299 }
3300
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003301 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
3302 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003303
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003304 if (rc == NO_ERROR)
3305 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
3306
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003307 close(oldfd); /* And Close Saved STDOUT Handle */
3308 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003309
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003310 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
3311 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003312
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003313 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3314 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003315
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003316 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
3317 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003318
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003319 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003320 }
3321
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003322 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
3323 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003324
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003325 if (rc == NO_ERROR)
3326 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
3327
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003328 close(oldfd); /* And Close Saved STDIN Handle */
3329 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003330
Guido van Rossumd48f2521997-12-05 22:19:34 +00003331 } else {
3332 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003333 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00003334 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003335}
3336
3337static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003338posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003339{
3340 char *name;
3341 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003342 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003343 FILE *fp;
3344 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003345 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003346 return NULL;
3347 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003348 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003349 Py_END_ALLOW_THREADS
3350 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003351 return os2_error(err);
3352
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003353 f = PyFile_FromFile(fp, name, mode, fclose);
3354 if (f != NULL)
3355 PyFile_SetBufSize(f, bufsize);
3356 return f;
3357}
3358
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003359#elif defined(PYCC_GCC)
3360
3361/* standard posix version of popen() support */
3362static PyObject *
3363posix_popen(PyObject *self, PyObject *args)
3364{
3365 char *name;
3366 char *mode = "r";
3367 int bufsize = -1;
3368 FILE *fp;
3369 PyObject *f;
3370 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3371 return NULL;
3372 Py_BEGIN_ALLOW_THREADS
3373 fp = popen(name, mode);
3374 Py_END_ALLOW_THREADS
3375 if (fp == NULL)
3376 return posix_error();
3377 f = PyFile_FromFile(fp, name, mode, pclose);
3378 if (f != NULL)
3379 PyFile_SetBufSize(f, bufsize);
3380 return f;
3381}
3382
3383/* fork() under OS/2 has lots'o'warts
3384 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
3385 * most of this code is a ripoff of the win32 code, but using the
3386 * capabilities of EMX's C library routines
3387 */
3388
3389/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
3390#define POPEN_1 1
3391#define POPEN_2 2
3392#define POPEN_3 3
3393#define POPEN_4 4
3394
3395static PyObject *_PyPopen(char *, int, int, int);
3396static int _PyPclose(FILE *file);
3397
3398/*
3399 * Internal dictionary mapping popen* file pointers to process handles,
3400 * for use when retrieving the process exit code. See _PyPclose() below
3401 * for more information on this dictionary's use.
3402 */
3403static PyObject *_PyPopenProcs = NULL;
3404
3405/* os2emx version of popen2()
3406 *
3407 * The result of this function is a pipe (file) connected to the
3408 * process's stdin, and a pipe connected to the process's stdout.
3409 */
3410
3411static PyObject *
3412os2emx_popen2(PyObject *self, PyObject *args)
3413{
3414 PyObject *f;
3415 int tm=0;
3416
3417 char *cmdstring;
3418 char *mode = "t";
3419 int bufsize = -1;
3420 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
3421 return NULL;
3422
3423 if (*mode == 't')
3424 tm = O_TEXT;
3425 else if (*mode != 'b') {
3426 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3427 return NULL;
3428 } else
3429 tm = O_BINARY;
3430
3431 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
3432
3433 return f;
3434}
3435
3436/*
3437 * Variation on os2emx.popen2
3438 *
3439 * The result of this function is 3 pipes - the process's stdin,
3440 * stdout and stderr
3441 */
3442
3443static PyObject *
3444os2emx_popen3(PyObject *self, PyObject *args)
3445{
3446 PyObject *f;
3447 int tm = 0;
3448
3449 char *cmdstring;
3450 char *mode = "t";
3451 int bufsize = -1;
3452 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
3453 return NULL;
3454
3455 if (*mode == 't')
3456 tm = O_TEXT;
3457 else if (*mode != 'b') {
3458 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3459 return NULL;
3460 } else
3461 tm = O_BINARY;
3462
3463 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
3464
3465 return f;
3466}
3467
3468/*
3469 * Variation on os2emx.popen2
3470 *
Tim Peters11b23062003-04-23 02:39:17 +00003471 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003472 * and stdout+stderr combined as a single pipe.
3473 */
3474
3475static PyObject *
3476os2emx_popen4(PyObject *self, PyObject *args)
3477{
3478 PyObject *f;
3479 int tm = 0;
3480
3481 char *cmdstring;
3482 char *mode = "t";
3483 int bufsize = -1;
3484 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
3485 return NULL;
3486
3487 if (*mode == 't')
3488 tm = O_TEXT;
3489 else if (*mode != 'b') {
3490 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3491 return NULL;
3492 } else
3493 tm = O_BINARY;
3494
3495 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
3496
3497 return f;
3498}
3499
3500/* a couple of structures for convenient handling of multiple
3501 * file handles and pipes
3502 */
3503struct file_ref
3504{
3505 int handle;
3506 int flags;
3507};
3508
3509struct pipe_ref
3510{
3511 int rd;
3512 int wr;
3513};
3514
3515/* The following code is derived from the win32 code */
3516
3517static PyObject *
3518_PyPopen(char *cmdstring, int mode, int n, int bufsize)
3519{
3520 struct file_ref stdio[3];
3521 struct pipe_ref p_fd[3];
3522 FILE *p_s[3];
3523 int file_count, i, pipe_err, pipe_pid;
3524 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
3525 PyObject *f, *p_f[3];
3526
3527 /* file modes for subsequent fdopen's on pipe handles */
3528 if (mode == O_TEXT)
3529 {
3530 rd_mode = "rt";
3531 wr_mode = "wt";
3532 }
3533 else
3534 {
3535 rd_mode = "rb";
3536 wr_mode = "wb";
3537 }
3538
3539 /* prepare shell references */
3540 if ((shell = getenv("EMXSHELL")) == NULL)
3541 if ((shell = getenv("COMSPEC")) == NULL)
3542 {
3543 errno = ENOENT;
3544 return posix_error();
3545 }
3546
3547 sh_name = _getname(shell);
3548 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
3549 opt = "/c";
3550 else
3551 opt = "-c";
3552
3553 /* save current stdio fds + their flags, and set not inheritable */
3554 i = pipe_err = 0;
3555 while (pipe_err >= 0 && i < 3)
3556 {
3557 pipe_err = stdio[i].handle = dup(i);
3558 stdio[i].flags = fcntl(i, F_GETFD, 0);
3559 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
3560 i++;
3561 }
3562 if (pipe_err < 0)
3563 {
3564 /* didn't get them all saved - clean up and bail out */
3565 int saved_err = errno;
3566 while (i-- > 0)
3567 {
3568 close(stdio[i].handle);
3569 }
3570 errno = saved_err;
3571 return posix_error();
3572 }
3573
3574 /* create pipe ends */
3575 file_count = 2;
3576 if (n == POPEN_3)
3577 file_count = 3;
3578 i = pipe_err = 0;
3579 while ((pipe_err == 0) && (i < file_count))
3580 pipe_err = pipe((int *)&p_fd[i++]);
3581 if (pipe_err < 0)
3582 {
3583 /* didn't get them all made - clean up and bail out */
3584 while (i-- > 0)
3585 {
3586 close(p_fd[i].wr);
3587 close(p_fd[i].rd);
3588 }
3589 errno = EPIPE;
3590 return posix_error();
3591 }
3592
3593 /* change the actual standard IO streams over temporarily,
3594 * making the retained pipe ends non-inheritable
3595 */
3596 pipe_err = 0;
3597
3598 /* - stdin */
3599 if (dup2(p_fd[0].rd, 0) == 0)
3600 {
3601 close(p_fd[0].rd);
3602 i = fcntl(p_fd[0].wr, F_GETFD, 0);
3603 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
3604 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
3605 {
3606 close(p_fd[0].wr);
3607 pipe_err = -1;
3608 }
3609 }
3610 else
3611 {
3612 pipe_err = -1;
3613 }
3614
3615 /* - stdout */
3616 if (pipe_err == 0)
3617 {
3618 if (dup2(p_fd[1].wr, 1) == 1)
3619 {
3620 close(p_fd[1].wr);
3621 i = fcntl(p_fd[1].rd, F_GETFD, 0);
3622 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
3623 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
3624 {
3625 close(p_fd[1].rd);
3626 pipe_err = -1;
3627 }
3628 }
3629 else
3630 {
3631 pipe_err = -1;
3632 }
3633 }
3634
3635 /* - stderr, as required */
3636 if (pipe_err == 0)
3637 switch (n)
3638 {
3639 case POPEN_3:
3640 {
3641 if (dup2(p_fd[2].wr, 2) == 2)
3642 {
3643 close(p_fd[2].wr);
3644 i = fcntl(p_fd[2].rd, F_GETFD, 0);
3645 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
3646 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
3647 {
3648 close(p_fd[2].rd);
3649 pipe_err = -1;
3650 }
3651 }
3652 else
3653 {
3654 pipe_err = -1;
3655 }
3656 break;
3657 }
3658
3659 case POPEN_4:
3660 {
3661 if (dup2(1, 2) != 2)
3662 {
3663 pipe_err = -1;
3664 }
3665 break;
3666 }
3667 }
3668
3669 /* spawn the child process */
3670 if (pipe_err == 0)
3671 {
3672 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
3673 if (pipe_pid == -1)
3674 {
3675 pipe_err = -1;
3676 }
3677 else
3678 {
3679 /* save the PID into the FILE structure
3680 * NOTE: this implementation doesn't actually
3681 * take advantage of this, but do it for
3682 * completeness - AIM Apr01
3683 */
3684 for (i = 0; i < file_count; i++)
3685 p_s[i]->_pid = pipe_pid;
3686 }
3687 }
3688
3689 /* reset standard IO to normal */
3690 for (i = 0; i < 3; i++)
3691 {
3692 dup2(stdio[i].handle, i);
3693 fcntl(i, F_SETFD, stdio[i].flags);
3694 close(stdio[i].handle);
3695 }
3696
3697 /* if any remnant problems, clean up and bail out */
3698 if (pipe_err < 0)
3699 {
3700 for (i = 0; i < 3; i++)
3701 {
3702 close(p_fd[i].rd);
3703 close(p_fd[i].wr);
3704 }
3705 errno = EPIPE;
3706 return posix_error_with_filename(cmdstring);
3707 }
3708
3709 /* build tuple of file objects to return */
3710 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
3711 PyFile_SetBufSize(p_f[0], bufsize);
3712 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
3713 PyFile_SetBufSize(p_f[1], bufsize);
3714 if (n == POPEN_3)
3715 {
3716 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
3717 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003718 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003719 }
3720 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003721 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003722
3723 /*
3724 * Insert the files we've created into the process dictionary
3725 * all referencing the list with the process handle and the
3726 * initial number of files (see description below in _PyPclose).
3727 * Since if _PyPclose later tried to wait on a process when all
3728 * handles weren't closed, it could create a deadlock with the
3729 * child, we spend some energy here to try to ensure that we
3730 * either insert all file handles into the dictionary or none
3731 * at all. It's a little clumsy with the various popen modes
3732 * and variable number of files involved.
3733 */
3734 if (!_PyPopenProcs)
3735 {
3736 _PyPopenProcs = PyDict_New();
3737 }
3738
3739 if (_PyPopenProcs)
3740 {
3741 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
3742 int ins_rc[3];
3743
3744 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
3745 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
3746
3747 procObj = PyList_New(2);
3748 pidObj = PyInt_FromLong((long) pipe_pid);
3749 intObj = PyInt_FromLong((long) file_count);
3750
3751 if (procObj && pidObj && intObj)
3752 {
3753 PyList_SetItem(procObj, 0, pidObj);
3754 PyList_SetItem(procObj, 1, intObj);
3755
3756 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
3757 if (fileObj[0])
3758 {
3759 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
3760 fileObj[0],
3761 procObj);
3762 }
3763 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
3764 if (fileObj[1])
3765 {
3766 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
3767 fileObj[1],
3768 procObj);
3769 }
3770 if (file_count >= 3)
3771 {
3772 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
3773 if (fileObj[2])
3774 {
3775 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
3776 fileObj[2],
3777 procObj);
3778 }
3779 }
3780
3781 if (ins_rc[0] < 0 || !fileObj[0] ||
3782 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
3783 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
3784 {
3785 /* Something failed - remove any dictionary
3786 * entries that did make it.
3787 */
3788 if (!ins_rc[0] && fileObj[0])
3789 {
3790 PyDict_DelItem(_PyPopenProcs,
3791 fileObj[0]);
3792 }
3793 if (!ins_rc[1] && fileObj[1])
3794 {
3795 PyDict_DelItem(_PyPopenProcs,
3796 fileObj[1]);
3797 }
3798 if (!ins_rc[2] && fileObj[2])
3799 {
3800 PyDict_DelItem(_PyPopenProcs,
3801 fileObj[2]);
3802 }
3803 }
3804 }
Tim Peters11b23062003-04-23 02:39:17 +00003805
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003806 /*
3807 * Clean up our localized references for the dictionary keys
3808 * and value since PyDict_SetItem will Py_INCREF any copies
3809 * that got placed in the dictionary.
3810 */
3811 Py_XDECREF(procObj);
3812 Py_XDECREF(fileObj[0]);
3813 Py_XDECREF(fileObj[1]);
3814 Py_XDECREF(fileObj[2]);
3815 }
3816
3817 /* Child is launched. */
3818 return f;
3819}
3820
3821/*
3822 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3823 * exit code for the child process and return as a result of the close.
3824 *
3825 * This function uses the _PyPopenProcs dictionary in order to map the
3826 * input file pointer to information about the process that was
3827 * originally created by the popen* call that created the file pointer.
3828 * The dictionary uses the file pointer as a key (with one entry
3829 * inserted for each file returned by the original popen* call) and a
3830 * single list object as the value for all files from a single call.
3831 * The list object contains the Win32 process handle at [0], and a file
3832 * count at [1], which is initialized to the total number of file
3833 * handles using that list.
3834 *
3835 * This function closes whichever handle it is passed, and decrements
3836 * the file count in the dictionary for the process handle pointed to
3837 * by this file. On the last close (when the file count reaches zero),
3838 * this function will wait for the child process and then return its
3839 * exit code as the result of the close() operation. This permits the
3840 * files to be closed in any order - it is always the close() of the
3841 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003842 *
3843 * NOTE: This function is currently called with the GIL released.
3844 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003845 */
3846
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003847static int _PyPclose(FILE *file)
3848{
3849 int result;
3850 int exit_code;
3851 int pipe_pid;
3852 PyObject *procObj, *pidObj, *intObj, *fileObj;
3853 int file_count;
3854#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003855 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003856#endif
3857
3858 /* Close the file handle first, to ensure it can't block the
3859 * child from exiting if it's the last handle.
3860 */
3861 result = fclose(file);
3862
3863#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003864 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003865#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003866 if (_PyPopenProcs)
3867 {
3868 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
3869 (procObj = PyDict_GetItem(_PyPopenProcs,
3870 fileObj)) != NULL &&
3871 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
3872 (intObj = PyList_GetItem(procObj,1)) != NULL)
3873 {
3874 pipe_pid = (int) PyInt_AsLong(pidObj);
3875 file_count = (int) PyInt_AsLong(intObj);
3876
3877 if (file_count > 1)
3878 {
3879 /* Still other files referencing process */
3880 file_count--;
3881 PyList_SetItem(procObj,1,
3882 PyInt_FromLong((long) file_count));
3883 }
3884 else
3885 {
3886 /* Last file for this process */
3887 if (result != EOF &&
3888 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
3889 {
3890 /* extract exit status */
3891 if (WIFEXITED(exit_code))
3892 {
3893 result = WEXITSTATUS(exit_code);
3894 }
3895 else
3896 {
3897 errno = EPIPE;
3898 result = -1;
3899 }
3900 }
3901 else
3902 {
3903 /* Indicate failure - this will cause the file object
3904 * to raise an I/O error and translate the last
3905 * error code from errno. We do have a problem with
3906 * last errors that overlap the normal errno table,
3907 * but that's a consistent problem with the file object.
3908 */
3909 result = -1;
3910 }
3911 }
3912
3913 /* Remove this file pointer from dictionary */
3914 PyDict_DelItem(_PyPopenProcs, fileObj);
3915
3916 if (PyDict_Size(_PyPopenProcs) == 0)
3917 {
3918 Py_DECREF(_PyPopenProcs);
3919 _PyPopenProcs = NULL;
3920 }
3921
3922 } /* if object retrieval ok */
3923
3924 Py_XDECREF(fileObj);
3925 } /* if _PyPopenProcs */
3926
3927#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003928 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003929#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003930 return result;
3931}
3932
3933#endif /* PYCC_??? */
3934
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003935#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003936
3937/*
3938 * Portable 'popen' replacement for Win32.
3939 *
3940 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
3941 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00003942 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003943 */
3944
3945#include <malloc.h>
3946#include <io.h>
3947#include <fcntl.h>
3948
3949/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
3950#define POPEN_1 1
3951#define POPEN_2 2
3952#define POPEN_3 3
3953#define POPEN_4 4
3954
3955static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00003956static int _PyPclose(FILE *file);
3957
3958/*
3959 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00003960 * for use when retrieving the process exit code. See _PyPclose() below
3961 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00003962 */
3963static PyObject *_PyPopenProcs = NULL;
3964
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003965
3966/* popen that works from a GUI.
3967 *
3968 * The result of this function is a pipe (file) connected to the
3969 * processes stdin or stdout, depending on the requested mode.
3970 */
3971
3972static PyObject *
3973posix_popen(PyObject *self, PyObject *args)
3974{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00003975 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003976 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003977
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003978 char *cmdstring;
3979 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003980 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003981 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003982 return NULL;
3983
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003984 if (*mode == 'r')
3985 tm = _O_RDONLY;
3986 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00003987 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003988 return NULL;
3989 } else
3990 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00003991
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003992 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003993 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003994 return NULL;
3995 }
3996
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003997 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003998 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003999 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004000 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004001 else
4002 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4003
4004 return f;
4005}
4006
4007/* Variation on win32pipe.popen
4008 *
4009 * The result of this function is a pipe (file) connected to the
4010 * process's stdin, and a pipe connected to the process's stdout.
4011 */
4012
4013static PyObject *
4014win32_popen2(PyObject *self, PyObject *args)
4015{
4016 PyObject *f;
4017 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004018
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004019 char *cmdstring;
4020 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004021 int bufsize = -1;
4022 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004023 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004024
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004025 if (*mode == 't')
4026 tm = _O_TEXT;
4027 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004028 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004029 return NULL;
4030 } else
4031 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004032
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004033 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004034 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004035 return NULL;
4036 }
4037
4038 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004039
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004040 return f;
4041}
4042
4043/*
4044 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004045 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004046 * The result of this function is 3 pipes - the process's stdin,
4047 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004048 */
4049
4050static PyObject *
4051win32_popen3(PyObject *self, PyObject *args)
4052{
4053 PyObject *f;
4054 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004055
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004056 char *cmdstring;
4057 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004058 int bufsize = -1;
4059 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004060 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004061
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004062 if (*mode == 't')
4063 tm = _O_TEXT;
4064 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004065 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004066 return NULL;
4067 } else
4068 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004069
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004070 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004071 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004072 return NULL;
4073 }
4074
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004075 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004076
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004077 return f;
4078}
4079
4080/*
4081 * Variation on win32pipe.popen
4082 *
Tim Peters5aa91602002-01-30 05:46:57 +00004083 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004084 * and stdout+stderr combined as a single pipe.
4085 */
4086
4087static PyObject *
4088win32_popen4(PyObject *self, PyObject *args)
4089{
4090 PyObject *f;
4091 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004092
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004093 char *cmdstring;
4094 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004095 int bufsize = -1;
4096 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004097 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004098
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004099 if (*mode == 't')
4100 tm = _O_TEXT;
4101 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004102 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004103 return NULL;
4104 } else
4105 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004106
4107 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004108 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004109 return NULL;
4110 }
4111
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004112 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004113
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004114 return f;
4115}
4116
Mark Hammond08501372001-01-31 07:30:29 +00004117static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004118_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004119 HANDLE hStdin,
4120 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004121 HANDLE hStderr,
4122 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004123{
4124 PROCESS_INFORMATION piProcInfo;
4125 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004126 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004127 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004128 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004129 int i;
4130 int x;
4131
4132 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004133 char *comshell;
4134
Tim Peters92e4dd82002-10-05 01:47:34 +00004135 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004136 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
4137 return x;
Tim Peters402d5982001-08-27 06:37:48 +00004138
4139 /* Explicitly check if we are using COMMAND.COM. If we are
4140 * then use the w9xpopen hack.
4141 */
4142 comshell = s1 + x;
4143 while (comshell >= s1 && *comshell != '\\')
4144 --comshell;
4145 ++comshell;
4146
4147 if (GetVersion() < 0x80000000 &&
4148 _stricmp(comshell, "command.com") != 0) {
4149 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004150 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004151 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004152 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004153 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004154 }
4155 else {
4156 /*
Tim Peters402d5982001-08-27 06:37:48 +00004157 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4158 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004159 */
Mark Hammond08501372001-01-31 07:30:29 +00004160 char modulepath[_MAX_PATH];
4161 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004162 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
4163 for (i = x = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004164 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004165 x = i+1;
4166 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004167 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004168 strncat(modulepath,
4169 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004170 (sizeof(modulepath)/sizeof(modulepath[0]))
4171 -strlen(modulepath));
4172 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004173 /* Eeek - file-not-found - possibly an embedding
4174 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004175 */
Tim Peters5aa91602002-01-30 05:46:57 +00004176 strncpy(modulepath,
4177 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00004178 sizeof(modulepath)/sizeof(modulepath[0]));
4179 if (modulepath[strlen(modulepath)-1] != '\\')
4180 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004181 strncat(modulepath,
4182 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004183 (sizeof(modulepath)/sizeof(modulepath[0]))
4184 -strlen(modulepath));
4185 /* No where else to look - raise an easily identifiable
4186 error, rather than leaving Windows to report
4187 "file not found" - as the user is probably blissfully
4188 unaware this shim EXE is used, and it will confuse them.
4189 (well, it confused me for a while ;-)
4190 */
4191 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004192 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004193 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004194 "for popen to work with your shell "
4195 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004196 szConsoleSpawn);
4197 return FALSE;
4198 }
4199 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004200 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004201 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004202 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004203
Tim Peters92e4dd82002-10-05 01:47:34 +00004204 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004205 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004206 /* To maintain correct argument passing semantics,
4207 we pass the command-line as it stands, and allow
4208 quoting to be applied. w9xpopen.exe will then
4209 use its argv vector, and re-quote the necessary
4210 args for the ultimate child process.
4211 */
Tim Peters75cdad52001-11-28 22:07:30 +00004212 PyOS_snprintf(
4213 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004214 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004215 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004216 s1,
4217 s3,
4218 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004219 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004220 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004221 dialog:
4222 "Your program accessed mem currently in use at xxx"
4223 and a hopeful warning about the stability of your
4224 system.
4225 Cost is Ctrl+C wont kill children, but anyone
4226 who cares can have a go!
4227 */
4228 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004229 }
4230 }
4231
4232 /* Could be an else here to try cmd.exe / command.com in the path
4233 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004234 else {
Tim Peters402d5982001-08-27 06:37:48 +00004235 PyErr_SetString(PyExc_RuntimeError,
4236 "Cannot locate a COMSPEC environment variable to "
4237 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004238 return FALSE;
4239 }
Tim Peters5aa91602002-01-30 05:46:57 +00004240
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004241 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4242 siStartInfo.cb = sizeof(STARTUPINFO);
4243 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4244 siStartInfo.hStdInput = hStdin;
4245 siStartInfo.hStdOutput = hStdout;
4246 siStartInfo.hStdError = hStderr;
4247 siStartInfo.wShowWindow = SW_HIDE;
4248
4249 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004250 s2,
4251 NULL,
4252 NULL,
4253 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004254 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004255 NULL,
4256 NULL,
4257 &siStartInfo,
4258 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004259 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004260 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004261
Mark Hammondb37a3732000-08-14 04:47:33 +00004262 /* Return process handle */
4263 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004264 return TRUE;
4265 }
Tim Peters402d5982001-08-27 06:37:48 +00004266 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004267 return FALSE;
4268}
4269
4270/* The following code is based off of KB: Q190351 */
4271
4272static PyObject *
4273_PyPopen(char *cmdstring, int mode, int n)
4274{
4275 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4276 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004277 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004278
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004279 SECURITY_ATTRIBUTES saAttr;
4280 BOOL fSuccess;
4281 int fd1, fd2, fd3;
4282 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004283 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004284 PyObject *f;
4285
4286 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4287 saAttr.bInheritHandle = TRUE;
4288 saAttr.lpSecurityDescriptor = NULL;
4289
4290 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4291 return win32_error("CreatePipe", NULL);
4292
4293 /* Create new output read handle and the input write handle. Set
4294 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004295 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004296 * being created. */
4297 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004298 GetCurrentProcess(), &hChildStdinWrDup, 0,
4299 FALSE,
4300 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004301 if (!fSuccess)
4302 return win32_error("DuplicateHandle", NULL);
4303
4304 /* Close the inheritable version of ChildStdin
4305 that we're using. */
4306 CloseHandle(hChildStdinWr);
4307
4308 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4309 return win32_error("CreatePipe", NULL);
4310
4311 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004312 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4313 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004314 if (!fSuccess)
4315 return win32_error("DuplicateHandle", NULL);
4316
4317 /* Close the inheritable version of ChildStdout
4318 that we're using. */
4319 CloseHandle(hChildStdoutRd);
4320
4321 if (n != POPEN_4) {
4322 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4323 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004324 fSuccess = DuplicateHandle(GetCurrentProcess(),
4325 hChildStderrRd,
4326 GetCurrentProcess(),
4327 &hChildStderrRdDup, 0,
4328 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004329 if (!fSuccess)
4330 return win32_error("DuplicateHandle", NULL);
4331 /* Close the inheritable version of ChildStdErr that we're using. */
4332 CloseHandle(hChildStderrRd);
4333 }
Tim Peters5aa91602002-01-30 05:46:57 +00004334
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004335 switch (n) {
4336 case POPEN_1:
4337 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4338 case _O_WRONLY | _O_TEXT:
4339 /* Case for writing to child Stdin in text mode. */
4340 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4341 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004342 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004343 PyFile_SetBufSize(f, 0);
4344 /* We don't care about these pipes anymore, so close them. */
4345 CloseHandle(hChildStdoutRdDup);
4346 CloseHandle(hChildStderrRdDup);
4347 break;
4348
4349 case _O_RDONLY | _O_TEXT:
4350 /* Case for reading from child Stdout in text mode. */
4351 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4352 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004353 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004354 PyFile_SetBufSize(f, 0);
4355 /* We don't care about these pipes anymore, so close them. */
4356 CloseHandle(hChildStdinWrDup);
4357 CloseHandle(hChildStderrRdDup);
4358 break;
4359
4360 case _O_RDONLY | _O_BINARY:
4361 /* Case for readinig from child Stdout in binary mode. */
4362 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4363 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004364 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004365 PyFile_SetBufSize(f, 0);
4366 /* We don't care about these pipes anymore, so close them. */
4367 CloseHandle(hChildStdinWrDup);
4368 CloseHandle(hChildStderrRdDup);
4369 break;
4370
4371 case _O_WRONLY | _O_BINARY:
4372 /* Case for writing to child Stdin in binary mode. */
4373 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4374 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004375 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004376 PyFile_SetBufSize(f, 0);
4377 /* We don't care about these pipes anymore, so close them. */
4378 CloseHandle(hChildStdoutRdDup);
4379 CloseHandle(hChildStderrRdDup);
4380 break;
4381 }
Mark Hammondb37a3732000-08-14 04:47:33 +00004382 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004383 break;
Tim Peters5aa91602002-01-30 05:46:57 +00004384
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004385 case POPEN_2:
4386 case POPEN_4:
4387 {
4388 char *m1, *m2;
4389 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00004390
Tim Peters7dca21e2002-08-19 00:42:29 +00004391 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004392 m1 = "r";
4393 m2 = "w";
4394 } else {
4395 m1 = "rb";
4396 m2 = "wb";
4397 }
4398
4399 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4400 f1 = _fdopen(fd1, m2);
4401 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4402 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004403 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004404 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00004405 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004406 PyFile_SetBufSize(p2, 0);
4407
4408 if (n != 4)
4409 CloseHandle(hChildStderrRdDup);
4410
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004411 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00004412 Py_XDECREF(p1);
4413 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00004414 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004415 break;
4416 }
Tim Peters5aa91602002-01-30 05:46:57 +00004417
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004418 case POPEN_3:
4419 {
4420 char *m1, *m2;
4421 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00004422
Tim Peters7dca21e2002-08-19 00:42:29 +00004423 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004424 m1 = "r";
4425 m2 = "w";
4426 } else {
4427 m1 = "rb";
4428 m2 = "wb";
4429 }
4430
4431 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4432 f1 = _fdopen(fd1, m2);
4433 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4434 f2 = _fdopen(fd2, m1);
4435 fd3 = _open_osfhandle((long)hChildStderrRdDup, mode);
4436 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004437 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00004438 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
4439 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004440 PyFile_SetBufSize(p1, 0);
4441 PyFile_SetBufSize(p2, 0);
4442 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004443 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00004444 Py_XDECREF(p1);
4445 Py_XDECREF(p2);
4446 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00004447 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004448 break;
4449 }
4450 }
4451
4452 if (n == POPEN_4) {
4453 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004454 hChildStdinRd,
4455 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004456 hChildStdoutWr,
4457 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004458 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004459 }
4460 else {
4461 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004462 hChildStdinRd,
4463 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004464 hChildStderrWr,
4465 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004466 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004467 }
4468
Mark Hammondb37a3732000-08-14 04:47:33 +00004469 /*
4470 * Insert the files we've created into the process dictionary
4471 * all referencing the list with the process handle and the
4472 * initial number of files (see description below in _PyPclose).
4473 * Since if _PyPclose later tried to wait on a process when all
4474 * handles weren't closed, it could create a deadlock with the
4475 * child, we spend some energy here to try to ensure that we
4476 * either insert all file handles into the dictionary or none
4477 * at all. It's a little clumsy with the various popen modes
4478 * and variable number of files involved.
4479 */
4480 if (!_PyPopenProcs) {
4481 _PyPopenProcs = PyDict_New();
4482 }
4483
4484 if (_PyPopenProcs) {
4485 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
4486 int ins_rc[3];
4487
4488 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4489 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4490
4491 procObj = PyList_New(2);
4492 hProcessObj = PyLong_FromVoidPtr(hProcess);
4493 intObj = PyInt_FromLong(file_count);
4494
4495 if (procObj && hProcessObj && intObj) {
4496 PyList_SetItem(procObj,0,hProcessObj);
4497 PyList_SetItem(procObj,1,intObj);
4498
4499 fileObj[0] = PyLong_FromVoidPtr(f1);
4500 if (fileObj[0]) {
4501 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4502 fileObj[0],
4503 procObj);
4504 }
4505 if (file_count >= 2) {
4506 fileObj[1] = PyLong_FromVoidPtr(f2);
4507 if (fileObj[1]) {
4508 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4509 fileObj[1],
4510 procObj);
4511 }
4512 }
4513 if (file_count >= 3) {
4514 fileObj[2] = PyLong_FromVoidPtr(f3);
4515 if (fileObj[2]) {
4516 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4517 fileObj[2],
4518 procObj);
4519 }
4520 }
4521
4522 if (ins_rc[0] < 0 || !fileObj[0] ||
4523 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4524 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
4525 /* Something failed - remove any dictionary
4526 * entries that did make it.
4527 */
4528 if (!ins_rc[0] && fileObj[0]) {
4529 PyDict_DelItem(_PyPopenProcs,
4530 fileObj[0]);
4531 }
4532 if (!ins_rc[1] && fileObj[1]) {
4533 PyDict_DelItem(_PyPopenProcs,
4534 fileObj[1]);
4535 }
4536 if (!ins_rc[2] && fileObj[2]) {
4537 PyDict_DelItem(_PyPopenProcs,
4538 fileObj[2]);
4539 }
4540 }
4541 }
Tim Peters5aa91602002-01-30 05:46:57 +00004542
Mark Hammondb37a3732000-08-14 04:47:33 +00004543 /*
4544 * Clean up our localized references for the dictionary keys
4545 * and value since PyDict_SetItem will Py_INCREF any copies
4546 * that got placed in the dictionary.
4547 */
4548 Py_XDECREF(procObj);
4549 Py_XDECREF(fileObj[0]);
4550 Py_XDECREF(fileObj[1]);
4551 Py_XDECREF(fileObj[2]);
4552 }
4553
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004554 /* Child is launched. Close the parents copy of those pipe
4555 * handles that only the child should have open. You need to
4556 * make sure that no handles to the write end of the output pipe
4557 * are maintained in this process or else the pipe will not close
4558 * when the child process exits and the ReadFile will hang. */
4559
4560 if (!CloseHandle(hChildStdinRd))
4561 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004562
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004563 if (!CloseHandle(hChildStdoutWr))
4564 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004565
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004566 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
4567 return win32_error("CloseHandle", NULL);
4568
4569 return f;
4570}
Fredrik Lundh56055a42000-07-23 19:47:12 +00004571
4572/*
4573 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4574 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00004575 *
4576 * This function uses the _PyPopenProcs dictionary in order to map the
4577 * input file pointer to information about the process that was
4578 * originally created by the popen* call that created the file pointer.
4579 * The dictionary uses the file pointer as a key (with one entry
4580 * inserted for each file returned by the original popen* call) and a
4581 * single list object as the value for all files from a single call.
4582 * The list object contains the Win32 process handle at [0], and a file
4583 * count at [1], which is initialized to the total number of file
4584 * handles using that list.
4585 *
4586 * This function closes whichever handle it is passed, and decrements
4587 * the file count in the dictionary for the process handle pointed to
4588 * by this file. On the last close (when the file count reaches zero),
4589 * this function will wait for the child process and then return its
4590 * exit code as the result of the close() operation. This permits the
4591 * files to be closed in any order - it is always the close() of the
4592 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004593 *
4594 * NOTE: This function is currently called with the GIL released.
4595 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004596 */
Tim Peters736aa322000-09-01 06:51:24 +00004597
Fredrik Lundh56055a42000-07-23 19:47:12 +00004598static int _PyPclose(FILE *file)
4599{
Fredrik Lundh20318932000-07-26 17:29:12 +00004600 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004601 DWORD exit_code;
4602 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00004603 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
4604 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00004605#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004606 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00004607#endif
4608
Fredrik Lundh20318932000-07-26 17:29:12 +00004609 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00004610 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00004611 */
4612 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00004613#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004614 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00004615#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004616 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00004617 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4618 (procObj = PyDict_GetItem(_PyPopenProcs,
4619 fileObj)) != NULL &&
4620 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
4621 (intObj = PyList_GetItem(procObj,1)) != NULL) {
4622
4623 hProcess = PyLong_AsVoidPtr(hProcessObj);
4624 file_count = PyInt_AsLong(intObj);
4625
4626 if (file_count > 1) {
4627 /* Still other files referencing process */
4628 file_count--;
4629 PyList_SetItem(procObj,1,
4630 PyInt_FromLong(file_count));
4631 } else {
4632 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00004633 if (result != EOF &&
4634 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
4635 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00004636 /* Possible truncation here in 16-bit environments, but
4637 * real exit codes are just the lower byte in any event.
4638 */
4639 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004640 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00004641 /* Indicate failure - this will cause the file object
4642 * to raise an I/O error and translate the last Win32
4643 * error code from errno. We do have a problem with
4644 * last errors that overlap the normal errno table,
4645 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004646 */
Fredrik Lundh20318932000-07-26 17:29:12 +00004647 if (result != EOF) {
4648 /* If the error wasn't from the fclose(), then
4649 * set errno for the file object error handling.
4650 */
4651 errno = GetLastError();
4652 }
4653 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004654 }
4655
4656 /* Free up the native handle at this point */
4657 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00004658 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00004659
Mark Hammondb37a3732000-08-14 04:47:33 +00004660 /* Remove this file pointer from dictionary */
4661 PyDict_DelItem(_PyPopenProcs, fileObj);
4662
4663 if (PyDict_Size(_PyPopenProcs) == 0) {
4664 Py_DECREF(_PyPopenProcs);
4665 _PyPopenProcs = NULL;
4666 }
4667
4668 } /* if object retrieval ok */
4669
4670 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004671 } /* if _PyPopenProcs */
4672
Tim Peters736aa322000-09-01 06:51:24 +00004673#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004674 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00004675#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004676 return result;
4677}
Tim Peters9acdd3a2000-09-01 19:26:36 +00004678
4679#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00004680static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004681posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00004682{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004683 char *name;
4684 char *mode = "r";
4685 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00004686 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00004687 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004688 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00004689 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00004690 /* Strip mode of binary or text modifiers */
4691 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
4692 mode = "r";
4693 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
4694 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00004695 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004696 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004697 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00004698 if (fp == NULL)
4699 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004700 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004701 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00004702 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004703 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00004704}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004705
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004706#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004707#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00004708
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004709
Guido van Rossumb6775db1994-08-01 11:34:53 +00004710#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004711PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004712"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004713Set the current process's user id.");
4714
Barry Warsaw53699e91996-12-10 23:23:01 +00004715static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004716posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004717{
4718 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004719 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004720 return NULL;
4721 if (setuid(uid) < 0)
4722 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004723 Py_INCREF(Py_None);
4724 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004725}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004726#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004727
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004728
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004729#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004730PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004731"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004732Set the current process's effective user id.");
4733
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004734static PyObject *
4735posix_seteuid (PyObject *self, PyObject *args)
4736{
4737 int euid;
4738 if (!PyArg_ParseTuple(args, "i", &euid)) {
4739 return NULL;
4740 } else if (seteuid(euid) < 0) {
4741 return posix_error();
4742 } else {
4743 Py_INCREF(Py_None);
4744 return Py_None;
4745 }
4746}
4747#endif /* HAVE_SETEUID */
4748
4749#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004750PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004751"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004752Set the current process's effective group id.");
4753
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004754static PyObject *
4755posix_setegid (PyObject *self, PyObject *args)
4756{
4757 int egid;
4758 if (!PyArg_ParseTuple(args, "i", &egid)) {
4759 return NULL;
4760 } else if (setegid(egid) < 0) {
4761 return posix_error();
4762 } else {
4763 Py_INCREF(Py_None);
4764 return Py_None;
4765 }
4766}
4767#endif /* HAVE_SETEGID */
4768
4769#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004770PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004771"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004772Set the current process's real and effective user ids.");
4773
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004774static PyObject *
4775posix_setreuid (PyObject *self, PyObject *args)
4776{
4777 int ruid, euid;
4778 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4779 return NULL;
4780 } else if (setreuid(ruid, euid) < 0) {
4781 return posix_error();
4782 } else {
4783 Py_INCREF(Py_None);
4784 return Py_None;
4785 }
4786}
4787#endif /* HAVE_SETREUID */
4788
4789#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004790PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004791"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004792Set the current process's real and effective group ids.");
4793
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004794static PyObject *
4795posix_setregid (PyObject *self, PyObject *args)
4796{
4797 int rgid, egid;
4798 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4799 return NULL;
4800 } else if (setregid(rgid, egid) < 0) {
4801 return posix_error();
4802 } else {
4803 Py_INCREF(Py_None);
4804 return Py_None;
4805 }
4806}
4807#endif /* HAVE_SETREGID */
4808
Guido van Rossumb6775db1994-08-01 11:34:53 +00004809#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004810PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004811"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004812Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004813
Barry Warsaw53699e91996-12-10 23:23:01 +00004814static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004815posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004816{
4817 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004818 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004819 return NULL;
4820 if (setgid(gid) < 0)
4821 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004822 Py_INCREF(Py_None);
4823 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004824}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004825#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004826
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004827#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004828PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004829"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004830Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004831
4832static PyObject *
4833posix_setgroups(PyObject *self, PyObject *args)
4834{
4835 PyObject *groups;
4836 int i, len;
4837 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004838
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004839 if (!PyArg_ParseTuple(args, "O:setgid", &groups))
4840 return NULL;
4841 if (!PySequence_Check(groups)) {
4842 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4843 return NULL;
4844 }
4845 len = PySequence_Size(groups);
4846 if (len > MAX_GROUPS) {
4847 PyErr_SetString(PyExc_ValueError, "too many groups");
4848 return NULL;
4849 }
4850 for(i = 0; i < len; i++) {
4851 PyObject *elem;
4852 elem = PySequence_GetItem(groups, i);
4853 if (!elem)
4854 return NULL;
4855 if (!PyInt_Check(elem)) {
4856 PyErr_SetString(PyExc_TypeError,
4857 "groups must be integers");
4858 Py_DECREF(elem);
4859 return NULL;
4860 }
4861 /* XXX: check that value fits into gid_t. */
4862 grouplist[i] = PyInt_AsLong(elem);
4863 Py_DECREF(elem);
4864 }
4865
4866 if (setgroups(len, grouplist) < 0)
4867 return posix_error();
4868 Py_INCREF(Py_None);
4869 return Py_None;
4870}
4871#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004872
Guido van Rossumb6775db1994-08-01 11:34:53 +00004873#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004874PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004875"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004876Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004877
Barry Warsaw53699e91996-12-10 23:23:01 +00004878static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004879posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004880{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004881 int pid, options;
4882#ifdef UNION_WAIT
4883 union wait status;
4884#define status_i (status.w_status)
4885#else
4886 int status;
4887#define status_i status
4888#endif
4889 status_i = 0;
4890
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004891 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004892 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004893 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004894 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004895 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004896 if (pid == -1)
4897 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00004898 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004899 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00004900}
4901
Tim Petersab034fa2002-02-01 11:27:43 +00004902#elif defined(HAVE_CWAIT)
4903
4904/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004905PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004906"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004907"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004908
4909static PyObject *
4910posix_waitpid(PyObject *self, PyObject *args)
4911{
4912 int pid, options;
4913 int status;
4914
4915 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4916 return NULL;
4917 Py_BEGIN_ALLOW_THREADS
4918 pid = _cwait(&status, pid, options);
4919 Py_END_ALLOW_THREADS
4920 if (pid == -1)
4921 return posix_error();
4922 else
4923 /* shift the status left a byte so this is more like the
4924 POSIX waitpid */
4925 return Py_BuildValue("ii", pid, status << 8);
4926}
4927#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004928
Guido van Rossumad0ee831995-03-01 10:34:45 +00004929#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004930PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004931"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004932Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004933
Barry Warsaw53699e91996-12-10 23:23:01 +00004934static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004935posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004936{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004937 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004938#ifdef UNION_WAIT
4939 union wait status;
4940#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004941#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004942 int status;
4943#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004944#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00004945
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004946 status_i = 0;
4947 Py_BEGIN_ALLOW_THREADS
4948 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004949 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004950 if (pid == -1)
4951 return posix_error();
4952 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004953 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004954#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00004955}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004956#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004957
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004958
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004959PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004960"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004961Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004962
Barry Warsaw53699e91996-12-10 23:23:01 +00004963static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004964posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004965{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004966#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004967 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004968#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004969#ifdef MS_WINDOWS
Mark Hammond7edd0a92003-08-06 02:46:58 +00004970 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004971#else
4972 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4973#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004974#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004975}
4976
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004977
Guido van Rossumb6775db1994-08-01 11:34:53 +00004978#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004979PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004980"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004981Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004982
Barry Warsaw53699e91996-12-10 23:23:01 +00004983static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004984posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004985{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004986 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004987 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004988 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004989 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004990 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004991 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004992 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004993 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004994 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00004995 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00004996 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004997}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004998#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004999
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005000
Guido van Rossumb6775db1994-08-01 11:34:53 +00005001#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005002PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005003"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005004Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005005
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005006static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005007posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005008{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005009 return posix_2str(args, "etet:symlink", symlink, NULL, NULL);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005010}
5011#endif /* HAVE_SYMLINK */
5012
5013
5014#ifdef HAVE_TIMES
5015#ifndef HZ
5016#define HZ 60 /* Universal constant :-) */
5017#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005018
Guido van Rossumd48f2521997-12-05 22:19:34 +00005019#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5020static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005021system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005022{
5023 ULONG value = 0;
5024
5025 Py_BEGIN_ALLOW_THREADS
5026 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5027 Py_END_ALLOW_THREADS
5028
5029 return value;
5030}
5031
5032static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005033posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005034{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005035 /* Currently Only Uptime is Provided -- Others Later */
5036 return Py_BuildValue("ddddd",
5037 (double)0 /* t.tms_utime / HZ */,
5038 (double)0 /* t.tms_stime / HZ */,
5039 (double)0 /* t.tms_cutime / HZ */,
5040 (double)0 /* t.tms_cstime / HZ */,
5041 (double)system_uptime() / 1000);
5042}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005043#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005044static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005045posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005046{
5047 struct tms t;
5048 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005049 errno = 0;
5050 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005051 if (c == (clock_t) -1)
5052 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005053 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005054 (double)t.tms_utime / HZ,
5055 (double)t.tms_stime / HZ,
5056 (double)t.tms_cutime / HZ,
5057 (double)t.tms_cstime / HZ,
5058 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005059}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005060#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005061#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005062
5063
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005064#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005065#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005066static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005067posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005068{
5069 FILETIME create, exit, kernel, user;
5070 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005071 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005072 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5073 /* The fields of a FILETIME structure are the hi and lo part
5074 of a 64-bit value expressed in 100 nanosecond units.
5075 1e7 is one second in such units; 1e-7 the inverse.
5076 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5077 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005078 return Py_BuildValue(
5079 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005080 (double)(kernel.dwHighDateTime*429.4967296 +
5081 kernel.dwLowDateTime*1e-7),
5082 (double)(user.dwHighDateTime*429.4967296 +
5083 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005084 (double)0,
5085 (double)0,
5086 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005087}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005088#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005089
5090#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005091PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005092"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005093Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005094#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005095
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005096
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005097#ifdef HAVE_GETSID
5098PyDoc_STRVAR(posix_getsid__doc__,
5099"getsid(pid) -> sid\n\n\
5100Call the system call getsid().");
5101
5102static PyObject *
5103posix_getsid(PyObject *self, PyObject *args)
5104{
5105 int pid, sid;
5106 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5107 return NULL;
5108 sid = getsid(pid);
5109 if (sid < 0)
5110 return posix_error();
5111 return PyInt_FromLong((long)sid);
5112}
5113#endif /* HAVE_GETSID */
5114
5115
Guido van Rossumb6775db1994-08-01 11:34:53 +00005116#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005117PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005118"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005119Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005120
Barry Warsaw53699e91996-12-10 23:23:01 +00005121static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005122posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005123{
Guido van Rossum687dd131993-05-17 08:34:16 +00005124 if (setsid() < 0)
5125 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005126 Py_INCREF(Py_None);
5127 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005128}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005129#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005130
Guido van Rossumb6775db1994-08-01 11:34:53 +00005131#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005132PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005133"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005134Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005135
Barry Warsaw53699e91996-12-10 23:23:01 +00005136static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005137posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005138{
5139 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005140 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005141 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005142 if (setpgid(pid, pgrp) < 0)
5143 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005144 Py_INCREF(Py_None);
5145 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005146}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005147#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005148
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005149
Guido van Rossumb6775db1994-08-01 11:34:53 +00005150#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005151PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005152"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005153Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005154
Barry Warsaw53699e91996-12-10 23:23:01 +00005155static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005156posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005157{
5158 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005159 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005160 return NULL;
5161 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005162 if (pgid < 0)
5163 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005164 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005165}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005166#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005167
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005168
Guido van Rossumb6775db1994-08-01 11:34:53 +00005169#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005170PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005171"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005172Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005173
Barry Warsaw53699e91996-12-10 23:23:01 +00005174static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005175posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005176{
5177 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005178 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005179 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005180 if (tcsetpgrp(fd, pgid) < 0)
5181 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005182 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005183 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005184}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005185#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005186
Guido van Rossum687dd131993-05-17 08:34:16 +00005187/* Functions acting on file descriptors */
5188
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005189PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005190"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005191Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005192
Barry Warsaw53699e91996-12-10 23:23:01 +00005193static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005194posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005195{
Mark Hammondef8b6542001-05-13 08:04:26 +00005196 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005197 int flag;
5198 int mode = 0777;
5199 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005200
5201#ifdef MS_WINDOWS
5202 if (unicode_file_names()) {
5203 PyUnicodeObject *po;
5204 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5205 Py_BEGIN_ALLOW_THREADS
5206 /* PyUnicode_AS_UNICODE OK without thread
5207 lock as it is a simple dereference. */
5208 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5209 Py_END_ALLOW_THREADS
5210 if (fd < 0)
5211 return posix_error();
5212 return PyInt_FromLong((long)fd);
5213 }
5214 /* Drop the argument parsing error as narrow strings
5215 are also valid. */
5216 PyErr_Clear();
5217 }
5218#endif
5219
Tim Peters5aa91602002-01-30 05:46:57 +00005220 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00005221 Py_FileSystemDefaultEncoding, &file,
5222 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00005223 return NULL;
5224
Barry Warsaw53699e91996-12-10 23:23:01 +00005225 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005226 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005227 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005228 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00005229 return posix_error_with_allocated_filename(file);
5230 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00005231 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005232}
5233
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005234
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005235PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005236"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005237Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005238
Barry Warsaw53699e91996-12-10 23:23:01 +00005239static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005240posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005241{
5242 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005243 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005244 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005245 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005246 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005247 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005248 if (res < 0)
5249 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005250 Py_INCREF(Py_None);
5251 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005252}
5253
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005254
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005255PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005256"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005257Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005258
Barry Warsaw53699e91996-12-10 23:23:01 +00005259static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005260posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005261{
5262 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005263 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005264 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005265 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005266 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005267 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005268 if (fd < 0)
5269 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005270 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005271}
5272
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005273
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005274PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005275"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005276Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005277
Barry Warsaw53699e91996-12-10 23:23:01 +00005278static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005279posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005280{
5281 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005282 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005283 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005284 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005285 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005286 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005287 if (res < 0)
5288 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005289 Py_INCREF(Py_None);
5290 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005291}
5292
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005294PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005295"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005296Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005297
Barry Warsaw53699e91996-12-10 23:23:01 +00005298static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005299posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005300{
5301 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005302#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005303 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005304#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005305 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005306#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005307 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005308 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005309 return NULL;
5310#ifdef SEEK_SET
5311 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5312 switch (how) {
5313 case 0: how = SEEK_SET; break;
5314 case 1: how = SEEK_CUR; break;
5315 case 2: how = SEEK_END; break;
5316 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005317#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005318
5319#if !defined(HAVE_LARGEFILE_SUPPORT)
5320 pos = PyInt_AsLong(posobj);
5321#else
5322 pos = PyLong_Check(posobj) ?
5323 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
5324#endif
5325 if (PyErr_Occurred())
5326 return NULL;
5327
Barry Warsaw53699e91996-12-10 23:23:01 +00005328 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005329#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005330 res = _lseeki64(fd, pos, how);
5331#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005332 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005333#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005334 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005335 if (res < 0)
5336 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005337
5338#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00005339 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005340#else
5341 return PyLong_FromLongLong(res);
5342#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005343}
5344
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005345
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005346PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005347"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005348Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005349
Barry Warsaw53699e91996-12-10 23:23:01 +00005350static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005351posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005352{
Guido van Rossum8bac5461996-06-11 18:38:48 +00005353 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005354 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005355 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005356 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005357 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005358 if (buffer == NULL)
5359 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005360 Py_BEGIN_ALLOW_THREADS
5361 n = read(fd, PyString_AsString(buffer), size);
5362 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005363 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005364 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005365 return posix_error();
5366 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005367 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00005368 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005369 return buffer;
5370}
5371
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005372
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005373PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005374"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005375Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005376
Barry Warsaw53699e91996-12-10 23:23:01 +00005377static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005378posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005379{
5380 int fd, size;
5381 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005382 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005383 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005384 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005385 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005386 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005387 if (size < 0)
5388 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005389 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005390}
5391
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005392
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005393PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005394"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005395Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005396
Barry Warsaw53699e91996-12-10 23:23:01 +00005397static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005398posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005399{
5400 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005401 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005402 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005403 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005404 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005405#ifdef __VMS
5406 /* on OpenVMS we must ensure that all bytes are written to the file */
5407 fsync(fd);
5408#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005409 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005410 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005411 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005412 if (res != 0)
5413 return posix_error();
Tim Peters5aa91602002-01-30 05:46:57 +00005414
Fred Drake699f3522000-06-29 21:12:41 +00005415 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005416}
5417
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005418
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005419PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005420"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005421Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005422
Barry Warsaw53699e91996-12-10 23:23:01 +00005423static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005424posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005425{
Guido van Rossum687dd131993-05-17 08:34:16 +00005426 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005427 char *mode = "r";
5428 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00005429 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005430 PyObject *f;
5431 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00005432 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00005433
Thomas Heller1f043e22002-11-07 16:00:59 +00005434 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
5435 PyErr_Format(PyExc_ValueError,
5436 "invalid file mode '%s'", mode);
5437 return NULL;
5438 }
5439
Barry Warsaw53699e91996-12-10 23:23:01 +00005440 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005441 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005442 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005443 if (fp == NULL)
5444 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00005445 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005446 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005447 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005448 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00005449}
5450
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005451PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005452"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005453Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005454connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005455
5456static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005457posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005458{
5459 int fd;
5460 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5461 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005462 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005463}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005464
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005465#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005466PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005467"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005468Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005469
Barry Warsaw53699e91996-12-10 23:23:01 +00005470static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005471posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005472{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005473#if defined(PYOS_OS2)
5474 HFILE read, write;
5475 APIRET rc;
5476
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005477 Py_BEGIN_ALLOW_THREADS
5478 rc = DosCreatePipe( &read, &write, 4096);
5479 Py_END_ALLOW_THREADS
5480 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005481 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005482
5483 return Py_BuildValue("(ii)", read, write);
5484#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005485#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005486 int fds[2];
5487 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005488 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005489 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005490 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005491 if (res != 0)
5492 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005493 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005494#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005495 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005496 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005497 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005498 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005499 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005500 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005501 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005502 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005503 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5504 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005505 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005506#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005507#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005508}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005509#endif /* HAVE_PIPE */
5510
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005511
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005512#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005513PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005514"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005515Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005516
Barry Warsaw53699e91996-12-10 23:23:01 +00005517static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005518posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005519{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005520 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005521 int mode = 0666;
5522 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005523 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005524 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005525 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005526 res = mkfifo(filename, mode);
5527 Py_END_ALLOW_THREADS
5528 if (res < 0)
5529 return posix_error();
5530 Py_INCREF(Py_None);
5531 return Py_None;
5532}
5533#endif
5534
5535
Neal Norwitz11690112002-07-30 01:08:28 +00005536#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005537PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005538"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005539Create a filesystem node (file, device special file or named pipe)\n\
5540named filename. mode specifies both the permissions to use and the\n\
5541type of node to be created, being combined (bitwise OR) with one of\n\
5542S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005543device defines the newly created device special file (probably using\n\
5544os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005545
5546
5547static PyObject *
5548posix_mknod(PyObject *self, PyObject *args)
5549{
5550 char *filename;
5551 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005552 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005553 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005554 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005555 return NULL;
5556 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005557 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005558 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005559 if (res < 0)
5560 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005561 Py_INCREF(Py_None);
5562 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005563}
5564#endif
5565
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005566#ifdef HAVE_DEVICE_MACROS
5567PyDoc_STRVAR(posix_major__doc__,
5568"major(device) -> major number\n\
5569Extracts a device major number from a raw device number.");
5570
5571static PyObject *
5572posix_major(PyObject *self, PyObject *args)
5573{
5574 int device;
5575 if (!PyArg_ParseTuple(args, "i:major", &device))
5576 return NULL;
5577 return PyInt_FromLong((long)major(device));
5578}
5579
5580PyDoc_STRVAR(posix_minor__doc__,
5581"minor(device) -> minor number\n\
5582Extracts a device minor number from a raw device number.");
5583
5584static PyObject *
5585posix_minor(PyObject *self, PyObject *args)
5586{
5587 int device;
5588 if (!PyArg_ParseTuple(args, "i:minor", &device))
5589 return NULL;
5590 return PyInt_FromLong((long)minor(device));
5591}
5592
5593PyDoc_STRVAR(posix_makedev__doc__,
5594"makedev(major, minor) -> device number\n\
5595Composes a raw device number from the major and minor device numbers.");
5596
5597static PyObject *
5598posix_makedev(PyObject *self, PyObject *args)
5599{
5600 int major, minor;
5601 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5602 return NULL;
5603 return PyInt_FromLong((long)makedev(major, minor));
5604}
5605#endif /* device macros */
5606
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005607
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005608#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005609PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005610"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005611Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005612
Barry Warsaw53699e91996-12-10 23:23:01 +00005613static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005614posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005615{
5616 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005617 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005618 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005619 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005620
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005621 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005622 return NULL;
5623
5624#if !defined(HAVE_LARGEFILE_SUPPORT)
5625 length = PyInt_AsLong(lenobj);
5626#else
5627 length = PyLong_Check(lenobj) ?
5628 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
5629#endif
5630 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005631 return NULL;
5632
Barry Warsaw53699e91996-12-10 23:23:01 +00005633 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005634 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005635 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005636 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005637 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005638 return NULL;
5639 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005640 Py_INCREF(Py_None);
5641 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005642}
5643#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005644
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005645#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005646PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005647"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005648Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005649
Fred Drake762e2061999-08-26 17:23:54 +00005650/* Save putenv() parameters as values here, so we can collect them when they
5651 * get re-set with another call for the same key. */
5652static PyObject *posix_putenv_garbage;
5653
Tim Peters5aa91602002-01-30 05:46:57 +00005654static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005655posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005656{
5657 char *s1, *s2;
5658 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00005659 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005660 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005661
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005662 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005663 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005664
5665#if defined(PYOS_OS2)
5666 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5667 APIRET rc;
5668
Guido van Rossumd48f2521997-12-05 22:19:34 +00005669 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5670 if (rc != NO_ERROR)
5671 return os2_error(rc);
5672
5673 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5674 APIRET rc;
5675
Guido van Rossumd48f2521997-12-05 22:19:34 +00005676 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5677 if (rc != NO_ERROR)
5678 return os2_error(rc);
5679 } else {
5680#endif
5681
Fred Drake762e2061999-08-26 17:23:54 +00005682 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005683 len = strlen(s1) + strlen(s2) + 2;
5684 /* len includes space for a trailing \0; the size arg to
5685 PyString_FromStringAndSize does not count that */
5686 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005687 if (newstr == NULL)
5688 return PyErr_NoMemory();
5689 new = PyString_AS_STRING(newstr);
Tim Petersc8996f52001-12-03 20:41:00 +00005690 PyOS_snprintf(new, len, "%s=%s", s1, s2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005691 if (putenv(new)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005692 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005693 posix_error();
5694 return NULL;
5695 }
Fred Drake762e2061999-08-26 17:23:54 +00005696 /* Install the first arg and newstr in posix_putenv_garbage;
5697 * this will cause previous value to be collected. This has to
5698 * happen after the real putenv() call because the old value
5699 * was still accessible until then. */
5700 if (PyDict_SetItem(posix_putenv_garbage,
5701 PyTuple_GET_ITEM(args, 0), newstr)) {
5702 /* really not much we can do; just leak */
5703 PyErr_Clear();
5704 }
5705 else {
5706 Py_DECREF(newstr);
5707 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005708
5709#if defined(PYOS_OS2)
5710 }
5711#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005712 Py_INCREF(Py_None);
5713 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005714}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005715#endif /* putenv */
5716
Guido van Rossumc524d952001-10-19 01:31:59 +00005717#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005718PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005719"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005720Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005721
5722static PyObject *
5723posix_unsetenv(PyObject *self, PyObject *args)
5724{
5725 char *s1;
5726
5727 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5728 return NULL;
5729
5730 unsetenv(s1);
5731
5732 /* Remove the key from posix_putenv_garbage;
5733 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005734 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005735 * old value was still accessible until then.
5736 */
5737 if (PyDict_DelItem(posix_putenv_garbage,
5738 PyTuple_GET_ITEM(args, 0))) {
5739 /* really not much we can do; just leak */
5740 PyErr_Clear();
5741 }
5742
5743 Py_INCREF(Py_None);
5744 return Py_None;
5745}
5746#endif /* unsetenv */
5747
Guido van Rossumb6a47161997-09-15 22:54:34 +00005748#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005749PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005750"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005751Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005752
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005753static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005754posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005755{
5756 int code;
5757 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005758 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005759 return NULL;
5760 message = strerror(code);
5761 if (message == NULL) {
5762 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005763 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005764 return NULL;
5765 }
5766 return PyString_FromString(message);
5767}
5768#endif /* strerror */
5769
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005770
Guido van Rossumc9641791998-08-04 15:26:23 +00005771#ifdef HAVE_SYS_WAIT_H
5772
Fred Drake106c1a02002-04-23 15:58:02 +00005773#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005774PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005775"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005776Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005777
5778static PyObject *
5779posix_WCOREDUMP(PyObject *self, PyObject *args)
5780{
5781#ifdef UNION_WAIT
5782 union wait status;
5783#define status_i (status.w_status)
5784#else
5785 int status;
5786#define status_i status
5787#endif
5788 status_i = 0;
5789
5790 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i))
5791 {
5792 return NULL;
5793 }
5794
5795 return PyBool_FromLong(WCOREDUMP(status));
5796#undef status_i
5797}
5798#endif /* WCOREDUMP */
5799
5800#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005801PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005802"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005803Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005804job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005805
5806static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005807posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005808{
5809#ifdef UNION_WAIT
5810 union wait status;
5811#define status_i (status.w_status)
5812#else
5813 int status;
5814#define status_i status
5815#endif
5816 status_i = 0;
5817
5818 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i))
5819 {
5820 return NULL;
5821 }
5822
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005823 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005824#undef status_i
5825}
5826#endif /* WIFCONTINUED */
5827
Guido van Rossumc9641791998-08-04 15:26:23 +00005828#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005829PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005830"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005831Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005832
5833static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005834posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005835{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005836#ifdef UNION_WAIT
5837 union wait status;
5838#define status_i (status.w_status)
5839#else
5840 int status;
5841#define status_i status
5842#endif
5843 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005844
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005845 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005846 {
5847 return NULL;
5848 }
Tim Peters5aa91602002-01-30 05:46:57 +00005849
Fred Drake106c1a02002-04-23 15:58:02 +00005850 return PyBool_FromLong(WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005851#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005852}
5853#endif /* WIFSTOPPED */
5854
5855#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005856PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005857"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005858Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005859
5860static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005861posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005862{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005863#ifdef UNION_WAIT
5864 union wait status;
5865#define status_i (status.w_status)
5866#else
5867 int status;
5868#define status_i status
5869#endif
5870 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005871
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005872 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005873 {
5874 return NULL;
5875 }
Tim Peters5aa91602002-01-30 05:46:57 +00005876
Fred Drake106c1a02002-04-23 15:58:02 +00005877 return PyBool_FromLong(WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005878#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005879}
5880#endif /* WIFSIGNALED */
5881
5882#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005883PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005884"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005885Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005886system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005887
5888static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005889posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005890{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005891#ifdef UNION_WAIT
5892 union wait status;
5893#define status_i (status.w_status)
5894#else
5895 int status;
5896#define status_i status
5897#endif
5898 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005899
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005900 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005901 {
5902 return NULL;
5903 }
Tim Peters5aa91602002-01-30 05:46:57 +00005904
Fred Drake106c1a02002-04-23 15:58:02 +00005905 return PyBool_FromLong(WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005906#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005907}
5908#endif /* WIFEXITED */
5909
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005910#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005911PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005912"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005913Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005914
5915static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005916posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005917{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005918#ifdef UNION_WAIT
5919 union wait status;
5920#define status_i (status.w_status)
5921#else
5922 int status;
5923#define status_i status
5924#endif
5925 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005926
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005927 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005928 {
5929 return NULL;
5930 }
Tim Peters5aa91602002-01-30 05:46:57 +00005931
Guido van Rossumc9641791998-08-04 15:26:23 +00005932 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005933#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005934}
5935#endif /* WEXITSTATUS */
5936
5937#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005938PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005939"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005940Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005941value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005942
5943static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005944posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005945{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005946#ifdef UNION_WAIT
5947 union wait status;
5948#define status_i (status.w_status)
5949#else
5950 int status;
5951#define status_i status
5952#endif
5953 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005954
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005955 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005956 {
5957 return NULL;
5958 }
Tim Peters5aa91602002-01-30 05:46:57 +00005959
Guido van Rossumc9641791998-08-04 15:26:23 +00005960 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005961#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005962}
5963#endif /* WTERMSIG */
5964
5965#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005966PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005967"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005968Return the signal that stopped the process that provided\n\
5969the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005970
5971static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005972posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005973{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005974#ifdef UNION_WAIT
5975 union wait status;
5976#define status_i (status.w_status)
5977#else
5978 int status;
5979#define status_i status
5980#endif
5981 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005982
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005983 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005984 {
5985 return NULL;
5986 }
Tim Peters5aa91602002-01-30 05:46:57 +00005987
Guido van Rossumc9641791998-08-04 15:26:23 +00005988 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005989#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005990}
5991#endif /* WSTOPSIG */
5992
5993#endif /* HAVE_SYS_WAIT_H */
5994
5995
Guido van Rossum94f6f721999-01-06 18:42:14 +00005996#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005997#ifdef _SCO_DS
5998/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5999 needed definitions in sys/statvfs.h */
6000#define _SVID3
6001#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006002#include <sys/statvfs.h>
6003
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006004static PyObject*
6005_pystatvfs_fromstructstatvfs(struct statvfs st) {
6006 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6007 if (v == NULL)
6008 return NULL;
6009
6010#if !defined(HAVE_LARGEFILE_SUPPORT)
6011 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6012 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6013 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6014 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6015 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6016 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6017 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6018 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6019 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6020 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6021#else
6022 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6023 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006024 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006025 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006026 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006027 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006028 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006029 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006030 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006031 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006032 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006033 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006034 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006035 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006036 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6037 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6038#endif
6039
6040 return v;
6041}
6042
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006043PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006044"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006045Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006046
6047static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006048posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006049{
6050 int fd, res;
6051 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006052
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006053 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006054 return NULL;
6055 Py_BEGIN_ALLOW_THREADS
6056 res = fstatvfs(fd, &st);
6057 Py_END_ALLOW_THREADS
6058 if (res != 0)
6059 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006060
6061 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006062}
6063#endif /* HAVE_FSTATVFS */
6064
6065
6066#if defined(HAVE_STATVFS)
6067#include <sys/statvfs.h>
6068
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006069PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006070"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006071Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006072
6073static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006074posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006075{
6076 char *path;
6077 int res;
6078 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006079 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006080 return NULL;
6081 Py_BEGIN_ALLOW_THREADS
6082 res = statvfs(path, &st);
6083 Py_END_ALLOW_THREADS
6084 if (res != 0)
6085 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006086
6087 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006088}
6089#endif /* HAVE_STATVFS */
6090
6091
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006092#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006093PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006094"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006095Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006096The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006097or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006098
6099static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006100posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006101{
6102 PyObject *result = NULL;
6103 char *dir = NULL;
6104 char *pfx = NULL;
6105 char *name;
6106
6107 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6108 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006109
6110 if (PyErr_Warn(PyExc_RuntimeWarning,
6111 "tempnam is a potential security risk to your program") < 0)
6112 return NULL;
6113
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006114#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006115 name = _tempnam(dir, pfx);
6116#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006117 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006118#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006119 if (name == NULL)
6120 return PyErr_NoMemory();
6121 result = PyString_FromString(name);
6122 free(name);
6123 return result;
6124}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006125#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006126
6127
6128#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006129PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006130"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006131Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006132
6133static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006134posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006135{
6136 FILE *fp;
6137
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006138 fp = tmpfile();
6139 if (fp == NULL)
6140 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006141 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006142}
6143#endif
6144
6145
6146#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006147PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006148"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006149Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006150
6151static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006152posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006153{
6154 char buffer[L_tmpnam];
6155 char *name;
6156
Skip Montanaro95618b52001-08-18 18:52:10 +00006157 if (PyErr_Warn(PyExc_RuntimeWarning,
6158 "tmpnam is a potential security risk to your program") < 0)
6159 return NULL;
6160
Greg Wardb48bc172000-03-01 21:51:56 +00006161#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006162 name = tmpnam_r(buffer);
6163#else
6164 name = tmpnam(buffer);
6165#endif
6166 if (name == NULL) {
6167 PyErr_SetObject(PyExc_OSError,
6168 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006169#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006170 "unexpected NULL from tmpnam_r"
6171#else
6172 "unexpected NULL from tmpnam"
6173#endif
6174 ));
6175 return NULL;
6176 }
6177 return PyString_FromString(buffer);
6178}
6179#endif
6180
6181
Fred Drakec9680921999-12-13 16:37:25 +00006182/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6183 * It maps strings representing configuration variable names to
6184 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006185 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006186 * rarely-used constants. There are three separate tables that use
6187 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006188 *
6189 * This code is always included, even if none of the interfaces that
6190 * need it are included. The #if hackery needed to avoid it would be
6191 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006192 */
6193struct constdef {
6194 char *name;
6195 long value;
6196};
6197
Fred Drake12c6e2d1999-12-14 21:25:03 +00006198static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006199conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6200 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006201{
6202 if (PyInt_Check(arg)) {
6203 *valuep = PyInt_AS_LONG(arg);
6204 return 1;
6205 }
6206 if (PyString_Check(arg)) {
6207 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006208 size_t lo = 0;
6209 size_t mid;
6210 size_t hi = tablesize;
6211 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006212 char *confname = PyString_AS_STRING(arg);
6213 while (lo < hi) {
6214 mid = (lo + hi) / 2;
6215 cmp = strcmp(confname, table[mid].name);
6216 if (cmp < 0)
6217 hi = mid;
6218 else if (cmp > 0)
6219 lo = mid + 1;
6220 else {
6221 *valuep = table[mid].value;
6222 return 1;
6223 }
6224 }
6225 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6226 }
6227 else
6228 PyErr_SetString(PyExc_TypeError,
6229 "configuration names must be strings or integers");
6230 return 0;
6231}
6232
6233
6234#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6235static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006236#ifdef _PC_ABI_AIO_XFER_MAX
6237 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6238#endif
6239#ifdef _PC_ABI_ASYNC_IO
6240 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6241#endif
Fred Drakec9680921999-12-13 16:37:25 +00006242#ifdef _PC_ASYNC_IO
6243 {"PC_ASYNC_IO", _PC_ASYNC_IO},
6244#endif
6245#ifdef _PC_CHOWN_RESTRICTED
6246 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
6247#endif
6248#ifdef _PC_FILESIZEBITS
6249 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
6250#endif
6251#ifdef _PC_LAST
6252 {"PC_LAST", _PC_LAST},
6253#endif
6254#ifdef _PC_LINK_MAX
6255 {"PC_LINK_MAX", _PC_LINK_MAX},
6256#endif
6257#ifdef _PC_MAX_CANON
6258 {"PC_MAX_CANON", _PC_MAX_CANON},
6259#endif
6260#ifdef _PC_MAX_INPUT
6261 {"PC_MAX_INPUT", _PC_MAX_INPUT},
6262#endif
6263#ifdef _PC_NAME_MAX
6264 {"PC_NAME_MAX", _PC_NAME_MAX},
6265#endif
6266#ifdef _PC_NO_TRUNC
6267 {"PC_NO_TRUNC", _PC_NO_TRUNC},
6268#endif
6269#ifdef _PC_PATH_MAX
6270 {"PC_PATH_MAX", _PC_PATH_MAX},
6271#endif
6272#ifdef _PC_PIPE_BUF
6273 {"PC_PIPE_BUF", _PC_PIPE_BUF},
6274#endif
6275#ifdef _PC_PRIO_IO
6276 {"PC_PRIO_IO", _PC_PRIO_IO},
6277#endif
6278#ifdef _PC_SOCK_MAXBUF
6279 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
6280#endif
6281#ifdef _PC_SYNC_IO
6282 {"PC_SYNC_IO", _PC_SYNC_IO},
6283#endif
6284#ifdef _PC_VDISABLE
6285 {"PC_VDISABLE", _PC_VDISABLE},
6286#endif
6287};
6288
Fred Drakec9680921999-12-13 16:37:25 +00006289static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006290conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006291{
6292 return conv_confname(arg, valuep, posix_constants_pathconf,
6293 sizeof(posix_constants_pathconf)
6294 / sizeof(struct constdef));
6295}
6296#endif
6297
6298#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006299PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006300"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006301Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006302If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006303
6304static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006305posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006306{
6307 PyObject *result = NULL;
6308 int name, fd;
6309
Fred Drake12c6e2d1999-12-14 21:25:03 +00006310 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6311 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00006312 long limit;
6313
6314 errno = 0;
6315 limit = fpathconf(fd, name);
6316 if (limit == -1 && errno != 0)
6317 posix_error();
6318 else
6319 result = PyInt_FromLong(limit);
6320 }
6321 return result;
6322}
6323#endif
6324
6325
6326#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006327PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006328"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006329Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006330If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006331
6332static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006333posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006334{
6335 PyObject *result = NULL;
6336 int name;
6337 char *path;
6338
6339 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6340 conv_path_confname, &name)) {
6341 long limit;
6342
6343 errno = 0;
6344 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006345 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00006346 if (errno == EINVAL)
6347 /* could be a path or name problem */
6348 posix_error();
6349 else
6350 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006351 }
Fred Drakec9680921999-12-13 16:37:25 +00006352 else
6353 result = PyInt_FromLong(limit);
6354 }
6355 return result;
6356}
6357#endif
6358
6359#ifdef HAVE_CONFSTR
6360static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006361#ifdef _CS_ARCHITECTURE
6362 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6363#endif
6364#ifdef _CS_HOSTNAME
6365 {"CS_HOSTNAME", _CS_HOSTNAME},
6366#endif
6367#ifdef _CS_HW_PROVIDER
6368 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6369#endif
6370#ifdef _CS_HW_SERIAL
6371 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6372#endif
6373#ifdef _CS_INITTAB_NAME
6374 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6375#endif
Fred Drakec9680921999-12-13 16:37:25 +00006376#ifdef _CS_LFS64_CFLAGS
6377 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6378#endif
6379#ifdef _CS_LFS64_LDFLAGS
6380 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6381#endif
6382#ifdef _CS_LFS64_LIBS
6383 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6384#endif
6385#ifdef _CS_LFS64_LINTFLAGS
6386 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6387#endif
6388#ifdef _CS_LFS_CFLAGS
6389 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6390#endif
6391#ifdef _CS_LFS_LDFLAGS
6392 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6393#endif
6394#ifdef _CS_LFS_LIBS
6395 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6396#endif
6397#ifdef _CS_LFS_LINTFLAGS
6398 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6399#endif
Fred Draked86ed291999-12-15 15:34:33 +00006400#ifdef _CS_MACHINE
6401 {"CS_MACHINE", _CS_MACHINE},
6402#endif
Fred Drakec9680921999-12-13 16:37:25 +00006403#ifdef _CS_PATH
6404 {"CS_PATH", _CS_PATH},
6405#endif
Fred Draked86ed291999-12-15 15:34:33 +00006406#ifdef _CS_RELEASE
6407 {"CS_RELEASE", _CS_RELEASE},
6408#endif
6409#ifdef _CS_SRPC_DOMAIN
6410 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6411#endif
6412#ifdef _CS_SYSNAME
6413 {"CS_SYSNAME", _CS_SYSNAME},
6414#endif
6415#ifdef _CS_VERSION
6416 {"CS_VERSION", _CS_VERSION},
6417#endif
Fred Drakec9680921999-12-13 16:37:25 +00006418#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6419 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6420#endif
6421#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6422 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6423#endif
6424#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6425 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6426#endif
6427#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6428 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6429#endif
6430#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6431 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6432#endif
6433#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6434 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6435#endif
6436#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6437 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6438#endif
6439#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6440 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6441#endif
6442#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6443 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6444#endif
6445#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6446 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6447#endif
6448#ifdef _CS_XBS5_LP64_OFF64_LIBS
6449 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6450#endif
6451#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6452 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6453#endif
6454#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6455 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6456#endif
6457#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6458 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6459#endif
6460#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6461 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6462#endif
6463#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6464 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6465#endif
Fred Draked86ed291999-12-15 15:34:33 +00006466#ifdef _MIPS_CS_AVAIL_PROCESSORS
6467 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6468#endif
6469#ifdef _MIPS_CS_BASE
6470 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6471#endif
6472#ifdef _MIPS_CS_HOSTID
6473 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6474#endif
6475#ifdef _MIPS_CS_HW_NAME
6476 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6477#endif
6478#ifdef _MIPS_CS_NUM_PROCESSORS
6479 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6480#endif
6481#ifdef _MIPS_CS_OSREL_MAJ
6482 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6483#endif
6484#ifdef _MIPS_CS_OSREL_MIN
6485 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6486#endif
6487#ifdef _MIPS_CS_OSREL_PATCH
6488 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6489#endif
6490#ifdef _MIPS_CS_OS_NAME
6491 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6492#endif
6493#ifdef _MIPS_CS_OS_PROVIDER
6494 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6495#endif
6496#ifdef _MIPS_CS_PROCESSORS
6497 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6498#endif
6499#ifdef _MIPS_CS_SERIAL
6500 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6501#endif
6502#ifdef _MIPS_CS_VENDOR
6503 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6504#endif
Fred Drakec9680921999-12-13 16:37:25 +00006505};
6506
6507static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006508conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006509{
6510 return conv_confname(arg, valuep, posix_constants_confstr,
6511 sizeof(posix_constants_confstr)
6512 / sizeof(struct constdef));
6513}
6514
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006515PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006516"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006517Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006518
6519static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006520posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006521{
6522 PyObject *result = NULL;
6523 int name;
6524 char buffer[64];
6525
6526 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
6527 int len = confstr(name, buffer, sizeof(buffer));
6528
Fred Drakec9680921999-12-13 16:37:25 +00006529 errno = 0;
6530 if (len == 0) {
6531 if (errno != 0)
6532 posix_error();
6533 else
6534 result = PyString_FromString("");
6535 }
6536 else {
6537 if (len >= sizeof(buffer)) {
6538 result = PyString_FromStringAndSize(NULL, len);
6539 if (result != NULL)
6540 confstr(name, PyString_AS_STRING(result), len+1);
6541 }
6542 else
6543 result = PyString_FromString(buffer);
6544 }
6545 }
6546 return result;
6547}
6548#endif
6549
6550
6551#ifdef HAVE_SYSCONF
6552static struct constdef posix_constants_sysconf[] = {
6553#ifdef _SC_2_CHAR_TERM
6554 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6555#endif
6556#ifdef _SC_2_C_BIND
6557 {"SC_2_C_BIND", _SC_2_C_BIND},
6558#endif
6559#ifdef _SC_2_C_DEV
6560 {"SC_2_C_DEV", _SC_2_C_DEV},
6561#endif
6562#ifdef _SC_2_C_VERSION
6563 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6564#endif
6565#ifdef _SC_2_FORT_DEV
6566 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6567#endif
6568#ifdef _SC_2_FORT_RUN
6569 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6570#endif
6571#ifdef _SC_2_LOCALEDEF
6572 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6573#endif
6574#ifdef _SC_2_SW_DEV
6575 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6576#endif
6577#ifdef _SC_2_UPE
6578 {"SC_2_UPE", _SC_2_UPE},
6579#endif
6580#ifdef _SC_2_VERSION
6581 {"SC_2_VERSION", _SC_2_VERSION},
6582#endif
Fred Draked86ed291999-12-15 15:34:33 +00006583#ifdef _SC_ABI_ASYNCHRONOUS_IO
6584 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6585#endif
6586#ifdef _SC_ACL
6587 {"SC_ACL", _SC_ACL},
6588#endif
Fred Drakec9680921999-12-13 16:37:25 +00006589#ifdef _SC_AIO_LISTIO_MAX
6590 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6591#endif
Fred Drakec9680921999-12-13 16:37:25 +00006592#ifdef _SC_AIO_MAX
6593 {"SC_AIO_MAX", _SC_AIO_MAX},
6594#endif
6595#ifdef _SC_AIO_PRIO_DELTA_MAX
6596 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6597#endif
6598#ifdef _SC_ARG_MAX
6599 {"SC_ARG_MAX", _SC_ARG_MAX},
6600#endif
6601#ifdef _SC_ASYNCHRONOUS_IO
6602 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6603#endif
6604#ifdef _SC_ATEXIT_MAX
6605 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6606#endif
Fred Draked86ed291999-12-15 15:34:33 +00006607#ifdef _SC_AUDIT
6608 {"SC_AUDIT", _SC_AUDIT},
6609#endif
Fred Drakec9680921999-12-13 16:37:25 +00006610#ifdef _SC_AVPHYS_PAGES
6611 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6612#endif
6613#ifdef _SC_BC_BASE_MAX
6614 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6615#endif
6616#ifdef _SC_BC_DIM_MAX
6617 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6618#endif
6619#ifdef _SC_BC_SCALE_MAX
6620 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6621#endif
6622#ifdef _SC_BC_STRING_MAX
6623 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6624#endif
Fred Draked86ed291999-12-15 15:34:33 +00006625#ifdef _SC_CAP
6626 {"SC_CAP", _SC_CAP},
6627#endif
Fred Drakec9680921999-12-13 16:37:25 +00006628#ifdef _SC_CHARCLASS_NAME_MAX
6629 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6630#endif
6631#ifdef _SC_CHAR_BIT
6632 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6633#endif
6634#ifdef _SC_CHAR_MAX
6635 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6636#endif
6637#ifdef _SC_CHAR_MIN
6638 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6639#endif
6640#ifdef _SC_CHILD_MAX
6641 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6642#endif
6643#ifdef _SC_CLK_TCK
6644 {"SC_CLK_TCK", _SC_CLK_TCK},
6645#endif
6646#ifdef _SC_COHER_BLKSZ
6647 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6648#endif
6649#ifdef _SC_COLL_WEIGHTS_MAX
6650 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6651#endif
6652#ifdef _SC_DCACHE_ASSOC
6653 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6654#endif
6655#ifdef _SC_DCACHE_BLKSZ
6656 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6657#endif
6658#ifdef _SC_DCACHE_LINESZ
6659 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6660#endif
6661#ifdef _SC_DCACHE_SZ
6662 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6663#endif
6664#ifdef _SC_DCACHE_TBLKSZ
6665 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6666#endif
6667#ifdef _SC_DELAYTIMER_MAX
6668 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6669#endif
6670#ifdef _SC_EQUIV_CLASS_MAX
6671 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6672#endif
6673#ifdef _SC_EXPR_NEST_MAX
6674 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6675#endif
6676#ifdef _SC_FSYNC
6677 {"SC_FSYNC", _SC_FSYNC},
6678#endif
6679#ifdef _SC_GETGR_R_SIZE_MAX
6680 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6681#endif
6682#ifdef _SC_GETPW_R_SIZE_MAX
6683 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6684#endif
6685#ifdef _SC_ICACHE_ASSOC
6686 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6687#endif
6688#ifdef _SC_ICACHE_BLKSZ
6689 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6690#endif
6691#ifdef _SC_ICACHE_LINESZ
6692 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6693#endif
6694#ifdef _SC_ICACHE_SZ
6695 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6696#endif
Fred Draked86ed291999-12-15 15:34:33 +00006697#ifdef _SC_INF
6698 {"SC_INF", _SC_INF},
6699#endif
Fred Drakec9680921999-12-13 16:37:25 +00006700#ifdef _SC_INT_MAX
6701 {"SC_INT_MAX", _SC_INT_MAX},
6702#endif
6703#ifdef _SC_INT_MIN
6704 {"SC_INT_MIN", _SC_INT_MIN},
6705#endif
6706#ifdef _SC_IOV_MAX
6707 {"SC_IOV_MAX", _SC_IOV_MAX},
6708#endif
Fred Draked86ed291999-12-15 15:34:33 +00006709#ifdef _SC_IP_SECOPTS
6710 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6711#endif
Fred Drakec9680921999-12-13 16:37:25 +00006712#ifdef _SC_JOB_CONTROL
6713 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6714#endif
Fred Draked86ed291999-12-15 15:34:33 +00006715#ifdef _SC_KERN_POINTERS
6716 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6717#endif
6718#ifdef _SC_KERN_SIM
6719 {"SC_KERN_SIM", _SC_KERN_SIM},
6720#endif
Fred Drakec9680921999-12-13 16:37:25 +00006721#ifdef _SC_LINE_MAX
6722 {"SC_LINE_MAX", _SC_LINE_MAX},
6723#endif
6724#ifdef _SC_LOGIN_NAME_MAX
6725 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6726#endif
6727#ifdef _SC_LOGNAME_MAX
6728 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6729#endif
6730#ifdef _SC_LONG_BIT
6731 {"SC_LONG_BIT", _SC_LONG_BIT},
6732#endif
Fred Draked86ed291999-12-15 15:34:33 +00006733#ifdef _SC_MAC
6734 {"SC_MAC", _SC_MAC},
6735#endif
Fred Drakec9680921999-12-13 16:37:25 +00006736#ifdef _SC_MAPPED_FILES
6737 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6738#endif
6739#ifdef _SC_MAXPID
6740 {"SC_MAXPID", _SC_MAXPID},
6741#endif
6742#ifdef _SC_MB_LEN_MAX
6743 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6744#endif
6745#ifdef _SC_MEMLOCK
6746 {"SC_MEMLOCK", _SC_MEMLOCK},
6747#endif
6748#ifdef _SC_MEMLOCK_RANGE
6749 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6750#endif
6751#ifdef _SC_MEMORY_PROTECTION
6752 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6753#endif
6754#ifdef _SC_MESSAGE_PASSING
6755 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6756#endif
Fred Draked86ed291999-12-15 15:34:33 +00006757#ifdef _SC_MMAP_FIXED_ALIGNMENT
6758 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6759#endif
Fred Drakec9680921999-12-13 16:37:25 +00006760#ifdef _SC_MQ_OPEN_MAX
6761 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6762#endif
6763#ifdef _SC_MQ_PRIO_MAX
6764 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6765#endif
Fred Draked86ed291999-12-15 15:34:33 +00006766#ifdef _SC_NACLS_MAX
6767 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6768#endif
Fred Drakec9680921999-12-13 16:37:25 +00006769#ifdef _SC_NGROUPS_MAX
6770 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6771#endif
6772#ifdef _SC_NL_ARGMAX
6773 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6774#endif
6775#ifdef _SC_NL_LANGMAX
6776 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6777#endif
6778#ifdef _SC_NL_MSGMAX
6779 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6780#endif
6781#ifdef _SC_NL_NMAX
6782 {"SC_NL_NMAX", _SC_NL_NMAX},
6783#endif
6784#ifdef _SC_NL_SETMAX
6785 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6786#endif
6787#ifdef _SC_NL_TEXTMAX
6788 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6789#endif
6790#ifdef _SC_NPROCESSORS_CONF
6791 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6792#endif
6793#ifdef _SC_NPROCESSORS_ONLN
6794 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6795#endif
Fred Draked86ed291999-12-15 15:34:33 +00006796#ifdef _SC_NPROC_CONF
6797 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6798#endif
6799#ifdef _SC_NPROC_ONLN
6800 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6801#endif
Fred Drakec9680921999-12-13 16:37:25 +00006802#ifdef _SC_NZERO
6803 {"SC_NZERO", _SC_NZERO},
6804#endif
6805#ifdef _SC_OPEN_MAX
6806 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6807#endif
6808#ifdef _SC_PAGESIZE
6809 {"SC_PAGESIZE", _SC_PAGESIZE},
6810#endif
6811#ifdef _SC_PAGE_SIZE
6812 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6813#endif
6814#ifdef _SC_PASS_MAX
6815 {"SC_PASS_MAX", _SC_PASS_MAX},
6816#endif
6817#ifdef _SC_PHYS_PAGES
6818 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6819#endif
6820#ifdef _SC_PII
6821 {"SC_PII", _SC_PII},
6822#endif
6823#ifdef _SC_PII_INTERNET
6824 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6825#endif
6826#ifdef _SC_PII_INTERNET_DGRAM
6827 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6828#endif
6829#ifdef _SC_PII_INTERNET_STREAM
6830 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6831#endif
6832#ifdef _SC_PII_OSI
6833 {"SC_PII_OSI", _SC_PII_OSI},
6834#endif
6835#ifdef _SC_PII_OSI_CLTS
6836 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6837#endif
6838#ifdef _SC_PII_OSI_COTS
6839 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6840#endif
6841#ifdef _SC_PII_OSI_M
6842 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6843#endif
6844#ifdef _SC_PII_SOCKET
6845 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6846#endif
6847#ifdef _SC_PII_XTI
6848 {"SC_PII_XTI", _SC_PII_XTI},
6849#endif
6850#ifdef _SC_POLL
6851 {"SC_POLL", _SC_POLL},
6852#endif
6853#ifdef _SC_PRIORITIZED_IO
6854 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6855#endif
6856#ifdef _SC_PRIORITY_SCHEDULING
6857 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6858#endif
6859#ifdef _SC_REALTIME_SIGNALS
6860 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6861#endif
6862#ifdef _SC_RE_DUP_MAX
6863 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6864#endif
6865#ifdef _SC_RTSIG_MAX
6866 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6867#endif
6868#ifdef _SC_SAVED_IDS
6869 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6870#endif
6871#ifdef _SC_SCHAR_MAX
6872 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6873#endif
6874#ifdef _SC_SCHAR_MIN
6875 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6876#endif
6877#ifdef _SC_SELECT
6878 {"SC_SELECT", _SC_SELECT},
6879#endif
6880#ifdef _SC_SEMAPHORES
6881 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6882#endif
6883#ifdef _SC_SEM_NSEMS_MAX
6884 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6885#endif
6886#ifdef _SC_SEM_VALUE_MAX
6887 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6888#endif
6889#ifdef _SC_SHARED_MEMORY_OBJECTS
6890 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6891#endif
6892#ifdef _SC_SHRT_MAX
6893 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6894#endif
6895#ifdef _SC_SHRT_MIN
6896 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6897#endif
6898#ifdef _SC_SIGQUEUE_MAX
6899 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6900#endif
6901#ifdef _SC_SIGRT_MAX
6902 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6903#endif
6904#ifdef _SC_SIGRT_MIN
6905 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6906#endif
Fred Draked86ed291999-12-15 15:34:33 +00006907#ifdef _SC_SOFTPOWER
6908 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6909#endif
Fred Drakec9680921999-12-13 16:37:25 +00006910#ifdef _SC_SPLIT_CACHE
6911 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6912#endif
6913#ifdef _SC_SSIZE_MAX
6914 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6915#endif
6916#ifdef _SC_STACK_PROT
6917 {"SC_STACK_PROT", _SC_STACK_PROT},
6918#endif
6919#ifdef _SC_STREAM_MAX
6920 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6921#endif
6922#ifdef _SC_SYNCHRONIZED_IO
6923 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6924#endif
6925#ifdef _SC_THREADS
6926 {"SC_THREADS", _SC_THREADS},
6927#endif
6928#ifdef _SC_THREAD_ATTR_STACKADDR
6929 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6930#endif
6931#ifdef _SC_THREAD_ATTR_STACKSIZE
6932 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6933#endif
6934#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6935 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6936#endif
6937#ifdef _SC_THREAD_KEYS_MAX
6938 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6939#endif
6940#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6941 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6942#endif
6943#ifdef _SC_THREAD_PRIO_INHERIT
6944 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6945#endif
6946#ifdef _SC_THREAD_PRIO_PROTECT
6947 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6948#endif
6949#ifdef _SC_THREAD_PROCESS_SHARED
6950 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6951#endif
6952#ifdef _SC_THREAD_SAFE_FUNCTIONS
6953 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6954#endif
6955#ifdef _SC_THREAD_STACK_MIN
6956 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6957#endif
6958#ifdef _SC_THREAD_THREADS_MAX
6959 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6960#endif
6961#ifdef _SC_TIMERS
6962 {"SC_TIMERS", _SC_TIMERS},
6963#endif
6964#ifdef _SC_TIMER_MAX
6965 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6966#endif
6967#ifdef _SC_TTY_NAME_MAX
6968 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6969#endif
6970#ifdef _SC_TZNAME_MAX
6971 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6972#endif
6973#ifdef _SC_T_IOV_MAX
6974 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6975#endif
6976#ifdef _SC_UCHAR_MAX
6977 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6978#endif
6979#ifdef _SC_UINT_MAX
6980 {"SC_UINT_MAX", _SC_UINT_MAX},
6981#endif
6982#ifdef _SC_UIO_MAXIOV
6983 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6984#endif
6985#ifdef _SC_ULONG_MAX
6986 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6987#endif
6988#ifdef _SC_USHRT_MAX
6989 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6990#endif
6991#ifdef _SC_VERSION
6992 {"SC_VERSION", _SC_VERSION},
6993#endif
6994#ifdef _SC_WORD_BIT
6995 {"SC_WORD_BIT", _SC_WORD_BIT},
6996#endif
6997#ifdef _SC_XBS5_ILP32_OFF32
6998 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6999#endif
7000#ifdef _SC_XBS5_ILP32_OFFBIG
7001 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7002#endif
7003#ifdef _SC_XBS5_LP64_OFF64
7004 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7005#endif
7006#ifdef _SC_XBS5_LPBIG_OFFBIG
7007 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7008#endif
7009#ifdef _SC_XOPEN_CRYPT
7010 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7011#endif
7012#ifdef _SC_XOPEN_ENH_I18N
7013 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7014#endif
7015#ifdef _SC_XOPEN_LEGACY
7016 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7017#endif
7018#ifdef _SC_XOPEN_REALTIME
7019 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7020#endif
7021#ifdef _SC_XOPEN_REALTIME_THREADS
7022 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7023#endif
7024#ifdef _SC_XOPEN_SHM
7025 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7026#endif
7027#ifdef _SC_XOPEN_UNIX
7028 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7029#endif
7030#ifdef _SC_XOPEN_VERSION
7031 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7032#endif
7033#ifdef _SC_XOPEN_XCU_VERSION
7034 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7035#endif
7036#ifdef _SC_XOPEN_XPG2
7037 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7038#endif
7039#ifdef _SC_XOPEN_XPG3
7040 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7041#endif
7042#ifdef _SC_XOPEN_XPG4
7043 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7044#endif
7045};
7046
7047static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007048conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007049{
7050 return conv_confname(arg, valuep, posix_constants_sysconf,
7051 sizeof(posix_constants_sysconf)
7052 / sizeof(struct constdef));
7053}
7054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007055PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007056"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007057Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007058
7059static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007060posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007061{
7062 PyObject *result = NULL;
7063 int name;
7064
7065 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7066 int value;
7067
7068 errno = 0;
7069 value = sysconf(name);
7070 if (value == -1 && errno != 0)
7071 posix_error();
7072 else
7073 result = PyInt_FromLong(value);
7074 }
7075 return result;
7076}
7077#endif
7078
7079
Fred Drakebec628d1999-12-15 18:31:10 +00007080/* This code is used to ensure that the tables of configuration value names
7081 * are in sorted order as required by conv_confname(), and also to build the
7082 * the exported dictionaries that are used to publish information about the
7083 * names available on the host platform.
7084 *
7085 * Sorting the table at runtime ensures that the table is properly ordered
7086 * when used, even for platforms we're not able to test on. It also makes
7087 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007088 */
Fred Drakebec628d1999-12-15 18:31:10 +00007089
7090static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007091cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007092{
7093 const struct constdef *c1 =
7094 (const struct constdef *) v1;
7095 const struct constdef *c2 =
7096 (const struct constdef *) v2;
7097
7098 return strcmp(c1->name, c2->name);
7099}
7100
7101static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007102setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007103 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007104{
Fred Drakebec628d1999-12-15 18:31:10 +00007105 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007106 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007107
7108 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7109 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007110 if (d == NULL)
7111 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007112
Barry Warsaw3155db32000-04-13 15:20:40 +00007113 for (i=0; i < tablesize; ++i) {
7114 PyObject *o = PyInt_FromLong(table[i].value);
7115 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7116 Py_XDECREF(o);
7117 Py_DECREF(d);
7118 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007119 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007120 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007121 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007122 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007123}
7124
Fred Drakebec628d1999-12-15 18:31:10 +00007125/* Return -1 on failure, 0 on success. */
7126static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007127setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007128{
7129#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007130 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007131 sizeof(posix_constants_pathconf)
7132 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007133 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007134 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007135#endif
7136#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007137 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007138 sizeof(posix_constants_confstr)
7139 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007140 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007141 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007142#endif
7143#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007144 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007145 sizeof(posix_constants_sysconf)
7146 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007147 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007148 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007149#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007150 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007151}
Fred Draked86ed291999-12-15 15:34:33 +00007152
7153
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007154PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007155"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007156Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007157in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007158
7159static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007160posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007161{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007162 abort();
7163 /*NOTREACHED*/
7164 Py_FatalError("abort() called from Python code didn't abort!");
7165 return NULL;
7166}
Fred Drakebec628d1999-12-15 18:31:10 +00007167
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007168#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007169PyDoc_STRVAR(win32_startfile__doc__,
7170"startfile(filepath) - Start a file with its associated application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007171\n\
7172This acts like double-clicking the file in Explorer, or giving the file\n\
7173name as an argument to the DOS \"start\" command: the file is opened\n\
7174with whatever application (if any) its extension is associated.\n\
7175\n\
7176startfile returns as soon as the associated application is launched.\n\
7177There is no option to wait for the application to close, and no way\n\
7178to retrieve the application's exit status.\n\
7179\n\
7180The filepath is relative to the current directory. If you want to use\n\
7181an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007182the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007183
7184static PyObject *
7185win32_startfile(PyObject *self, PyObject *args)
7186{
7187 char *filepath;
7188 HINSTANCE rc;
7189 if (!PyArg_ParseTuple(args, "s:startfile", &filepath))
7190 return NULL;
7191 Py_BEGIN_ALLOW_THREADS
7192 rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL);
7193 Py_END_ALLOW_THREADS
7194 if (rc <= (HINSTANCE)32)
7195 return win32_error("startfile", filepath);
7196 Py_INCREF(Py_None);
7197 return Py_None;
7198}
7199#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007200
Martin v. Löwis438b5342002-12-27 10:16:42 +00007201#ifdef HAVE_GETLOADAVG
7202PyDoc_STRVAR(posix_getloadavg__doc__,
7203"getloadavg() -> (float, float, float)\n\n\
7204Return the number of processes in the system run queue averaged over\n\
7205the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7206was unobtainable");
7207
7208static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007209posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007210{
7211 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007212 if (getloadavg(loadavg, 3)!=3) {
7213 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7214 return NULL;
7215 } else
7216 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
7217}
7218#endif
7219
7220
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007221static PyMethodDef posix_methods[] = {
7222 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7223#ifdef HAVE_TTYNAME
7224 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7225#endif
7226 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
7227 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007228#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007229 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007230#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007231#ifdef HAVE_LCHOWN
7232 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7233#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007234#ifdef HAVE_CHROOT
7235 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7236#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007237#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007238 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007239#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007240#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00007241 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00007242#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00007243 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007244#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00007245#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007246#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007247 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007248#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007249 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7250 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7251 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007252#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007253 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007254#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007255#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007256 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007257#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007258 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7259 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7260 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007261 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007262#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007263 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007264#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007265#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007266 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007267#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007268 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007269#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007270 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007271#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007272 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7273 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7274 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007275#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007276 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007277#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007278 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007279#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007280 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7281 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007282#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007283#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007284 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7285 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007286#if defined(PYOS_OS2)
7287 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7288 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7289#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007290#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007291#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007292 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007293#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007294#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007295 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007296#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007297#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007298 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007299#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007300#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007301 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007302#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007303#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007304 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007305#endif /* HAVE_GETEGID */
7306#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007307 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007308#endif /* HAVE_GETEUID */
7309#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007310 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007311#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007312#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007313 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007314#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007315 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007316#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007317 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007318#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007319#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007320 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007321#endif /* HAVE_GETPPID */
7322#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007323 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007324#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007325#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007326 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007327#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007328#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007329 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007330#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007331#ifdef HAVE_KILLPG
7332 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7333#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007334#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007335 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007336#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007337#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007338 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007339#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007340 {"popen2", win32_popen2, METH_VARARGS},
7341 {"popen3", win32_popen3, METH_VARARGS},
7342 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00007343 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007344#else
7345#if defined(PYOS_OS2) && defined(PYCC_GCC)
7346 {"popen2", os2emx_popen2, METH_VARARGS},
7347 {"popen3", os2emx_popen3, METH_VARARGS},
7348 {"popen4", os2emx_popen4, METH_VARARGS},
7349#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007350#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007351#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007352#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007353 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007354#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007355#ifdef HAVE_SETEUID
7356 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7357#endif /* HAVE_SETEUID */
7358#ifdef HAVE_SETEGID
7359 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7360#endif /* HAVE_SETEGID */
7361#ifdef HAVE_SETREUID
7362 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7363#endif /* HAVE_SETREUID */
7364#ifdef HAVE_SETREGID
7365 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7366#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007367#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007368 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007369#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007370#ifdef HAVE_SETGROUPS
7371 {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__},
7372#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007373#ifdef HAVE_GETPGID
7374 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7375#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007376#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007377 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007378#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007379#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007380 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007381#endif /* HAVE_WAIT */
Tim Petersab034fa2002-02-01 11:27:43 +00007382#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007383 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007384#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007385#ifdef HAVE_GETSID
7386 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7387#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007388#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007389 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007390#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007391#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007392 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007393#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007394#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007395 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007396#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007397#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007398 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007399#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007400 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7401 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7402 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7403 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7404 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7405 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7406 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7407 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7408 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007409 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007410#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007411 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007412#endif
7413#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007414 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007415#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007416#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007417 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7418#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007419#ifdef HAVE_DEVICE_MACROS
7420 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7421 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7422 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7423#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007424#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007425 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007426#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007427#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007428 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007429#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007430#ifdef HAVE_UNSETENV
7431 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7432#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00007433#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007434 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00007435#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00007436#ifdef HAVE_FCHDIR
7437 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7438#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007439#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007440 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007441#endif
7442#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007443 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007444#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007445#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007446#ifdef WCOREDUMP
7447 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7448#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007449#ifdef WIFCONTINUED
7450 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7451#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007452#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007453 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007454#endif /* WIFSTOPPED */
7455#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007456 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007457#endif /* WIFSIGNALED */
7458#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007459 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007460#endif /* WIFEXITED */
7461#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007462 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007463#endif /* WEXITSTATUS */
7464#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007465 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007466#endif /* WTERMSIG */
7467#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007468 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007469#endif /* WSTOPSIG */
7470#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007471#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007472 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007473#endif
7474#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007475 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007476#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00007477#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00007478 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007479#endif
7480#ifdef HAVE_TEMPNAM
7481 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
7482#endif
7483#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00007484 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007485#endif
Fred Drakec9680921999-12-13 16:37:25 +00007486#ifdef HAVE_CONFSTR
7487 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7488#endif
7489#ifdef HAVE_SYSCONF
7490 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7491#endif
7492#ifdef HAVE_FPATHCONF
7493 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7494#endif
7495#ifdef HAVE_PATHCONF
7496 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7497#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007498 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007499#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007500 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7501#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007502#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007503 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007504#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007505 {NULL, NULL} /* Sentinel */
7506};
7507
7508
Barry Warsaw4a342091996-12-19 23:50:02 +00007509static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007510ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007511{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007512 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007513}
7514
Guido van Rossumd48f2521997-12-05 22:19:34 +00007515#if defined(PYOS_OS2)
7516/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007517static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007518{
7519 APIRET rc;
7520 ULONG values[QSV_MAX+1];
7521 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007522 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007523
7524 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007525 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007526 Py_END_ALLOW_THREADS
7527
7528 if (rc != NO_ERROR) {
7529 os2_error(rc);
7530 return -1;
7531 }
7532
Fred Drake4d1e64b2002-04-15 19:40:07 +00007533 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7534 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7535 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7536 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7537 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7538 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7539 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007540
7541 switch (values[QSV_VERSION_MINOR]) {
7542 case 0: ver = "2.00"; break;
7543 case 10: ver = "2.10"; break;
7544 case 11: ver = "2.11"; break;
7545 case 30: ver = "3.00"; break;
7546 case 40: ver = "4.00"; break;
7547 case 50: ver = "5.00"; break;
7548 default:
Tim Peters885d4572001-11-28 20:27:42 +00007549 PyOS_snprintf(tmp, sizeof(tmp),
7550 "%d-%d", values[QSV_VERSION_MAJOR],
7551 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007552 ver = &tmp[0];
7553 }
7554
7555 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007556 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007557 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007558
7559 /* Add Indicator of Which Drive was Used to Boot the System */
7560 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7561 tmp[1] = ':';
7562 tmp[2] = '\0';
7563
Fred Drake4d1e64b2002-04-15 19:40:07 +00007564 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007565}
7566#endif
7567
Barry Warsaw4a342091996-12-19 23:50:02 +00007568static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007569all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007570{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007571#ifdef F_OK
7572 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007573#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007574#ifdef R_OK
7575 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007576#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007577#ifdef W_OK
7578 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007579#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007580#ifdef X_OK
7581 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007582#endif
Fred Drakec9680921999-12-13 16:37:25 +00007583#ifdef NGROUPS_MAX
7584 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7585#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007586#ifdef TMP_MAX
7587 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7588#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007589#ifdef WCONTINUED
7590 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7591#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007592#ifdef WNOHANG
7593 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007594#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007595#ifdef WUNTRACED
7596 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7597#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007598#ifdef O_RDONLY
7599 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7600#endif
7601#ifdef O_WRONLY
7602 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7603#endif
7604#ifdef O_RDWR
7605 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7606#endif
7607#ifdef O_NDELAY
7608 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7609#endif
7610#ifdef O_NONBLOCK
7611 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7612#endif
7613#ifdef O_APPEND
7614 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7615#endif
7616#ifdef O_DSYNC
7617 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7618#endif
7619#ifdef O_RSYNC
7620 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7621#endif
7622#ifdef O_SYNC
7623 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7624#endif
7625#ifdef O_NOCTTY
7626 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7627#endif
7628#ifdef O_CREAT
7629 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7630#endif
7631#ifdef O_EXCL
7632 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7633#endif
7634#ifdef O_TRUNC
7635 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7636#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007637#ifdef O_BINARY
7638 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7639#endif
7640#ifdef O_TEXT
7641 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7642#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007643#ifdef O_LARGEFILE
7644 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7645#endif
7646
Tim Peters5aa91602002-01-30 05:46:57 +00007647/* MS Windows */
7648#ifdef O_NOINHERIT
7649 /* Don't inherit in child processes. */
7650 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7651#endif
7652#ifdef _O_SHORT_LIVED
7653 /* Optimize for short life (keep in memory). */
7654 /* MS forgot to define this one with a non-underscore form too. */
7655 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7656#endif
7657#ifdef O_TEMPORARY
7658 /* Automatically delete when last handle is closed. */
7659 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7660#endif
7661#ifdef O_RANDOM
7662 /* Optimize for random access. */
7663 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7664#endif
7665#ifdef O_SEQUENTIAL
7666 /* Optimize for sequential access. */
7667 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7668#endif
7669
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007670/* GNU extensions. */
7671#ifdef O_DIRECT
7672 /* Direct disk access. */
7673 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7674#endif
7675#ifdef O_DIRECTORY
7676 /* Must be a directory. */
7677 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7678#endif
7679#ifdef O_NOFOLLOW
7680 /* Do not follow links. */
7681 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7682#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007683
Barry Warsaw5676bd12003-01-07 20:57:09 +00007684 /* These come from sysexits.h */
7685#ifdef EX_OK
7686 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007687#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007688#ifdef EX_USAGE
7689 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007690#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007691#ifdef EX_DATAERR
7692 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007693#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007694#ifdef EX_NOINPUT
7695 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007696#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007697#ifdef EX_NOUSER
7698 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007699#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007700#ifdef EX_NOHOST
7701 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007702#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007703#ifdef EX_UNAVAILABLE
7704 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007705#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007706#ifdef EX_SOFTWARE
7707 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007708#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007709#ifdef EX_OSERR
7710 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007711#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007712#ifdef EX_OSFILE
7713 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007714#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007715#ifdef EX_CANTCREAT
7716 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007717#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007718#ifdef EX_IOERR
7719 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007720#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007721#ifdef EX_TEMPFAIL
7722 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007723#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007724#ifdef EX_PROTOCOL
7725 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007726#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007727#ifdef EX_NOPERM
7728 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007729#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007730#ifdef EX_CONFIG
7731 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007732#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007733#ifdef EX_NOTFOUND
7734 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007735#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007736
Guido van Rossum246bc171999-02-01 23:54:31 +00007737#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007738#if defined(PYOS_OS2) && defined(PYCC_GCC)
7739 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7740 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7741 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7742 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7743 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7744 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7745 if (ins(d, "P_PM", (long)P_PM)) return -1;
7746 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7747 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7748 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7749 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7750 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7751 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7752 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7753 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7754 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7755 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7756 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7757 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7758 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7759#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007760 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7761 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7762 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7763 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7764 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007765#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007766#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007767
Guido van Rossumd48f2521997-12-05 22:19:34 +00007768#if defined(PYOS_OS2)
7769 if (insertvalues(d)) return -1;
7770#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007771 return 0;
7772}
7773
7774
Tim Peters5aa91602002-01-30 05:46:57 +00007775#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007776#define INITFUNC initnt
7777#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007778
7779#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007780#define INITFUNC initos2
7781#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007782
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007783#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007784#define INITFUNC initposix
7785#define MODNAME "posix"
7786#endif
7787
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007788PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007789INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007790{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007791 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007792
Fred Drake4d1e64b2002-04-15 19:40:07 +00007793 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007794 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007795 posix__doc__);
Tim Peters5aa91602002-01-30 05:46:57 +00007796
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007797 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007798 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007799 Py_XINCREF(v);
7800 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007801 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007802 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007803
Fred Drake4d1e64b2002-04-15 19:40:07 +00007804 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007805 return;
7806
Fred Drake4d1e64b2002-04-15 19:40:07 +00007807 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007808 return;
7809
Fred Drake4d1e64b2002-04-15 19:40:07 +00007810 Py_INCREF(PyExc_OSError);
7811 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007812
Guido van Rossumb3d39562000-01-31 18:41:26 +00007813#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007814 if (posix_putenv_garbage == NULL)
7815 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007816#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007817
Guido van Rossum14648392001-12-08 18:02:58 +00007818 stat_result_desc.name = MODNAME ".stat_result";
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007819 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7820 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7821 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007822 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007823 structseq_new = StatResultType.tp_new;
7824 StatResultType.tp_new = statresult_new;
Fred Drake4d1e64b2002-04-15 19:40:07 +00007825 Py_INCREF((PyObject*) &StatResultType);
7826 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007827
Guido van Rossum14648392001-12-08 18:02:58 +00007828 statvfs_result_desc.name = MODNAME ".statvfs_result";
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007829 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007830 Py_INCREF((PyObject*) &StatVFSResultType);
7831 PyModule_AddObject(m, "statvfs_result",
7832 (PyObject*) &StatVFSResultType);
Guido van Rossumb6775db1994-08-01 11:34:53 +00007833}