blob: 764fa4c483741dfefe9afd70ca7824e3c9eaffe4 [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"
Martin v. Löwisdc3883f2004-08-29 15:46:35 +0000231#define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */
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
Tim Peters4ad82172004-08-30 17:02:04 +0000906static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +0000907IsUNCRootA(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
Tim Peters4ad82172004-08-30 17:02:04 +0000930static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +0000931IsUNCRootW(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. */
Tim Peters4ad82172004-08-30 17:02:04 +0000997 if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':') ||
Martin v. Löwisd8948722004-06-02 09:57:56 +0000998 IsUNCRootW(wpath, pathlen))
999 /* leave it alone */;
1000 else {
1001 /* nuke the trailing backslash */
1002 wpath[pathlen-1] = L'\0';
1003 }
1004 }
Tim Peters4ad82172004-08-30 17:02:04 +00001005 else if (ISSLASHW(wpath[1]) && pathlen < ARRAYSIZE(wpath)-1 &&
Martin v. Löwisd8948722004-06-02 09:57:56 +00001006 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. */
Tim Peters4ad82172004-08-30 17:02:04 +00001047 if (pathlen == 1 || (pathlen == 3 && path[1] == ':') ||
Martin v. Löwisd8948722004-06-02 09:57:56 +00001048 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 }
Tim Peters4ad82172004-08-30 17:02:04 +00001057 else if (ISSLASHA(path[1]) && pathlen < ARRAYSIZE(pathcopy)-1 &&
Martin v. Löwisd8948722004-06-02 09:57:56 +00001058 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);
Martin v. Löwis969297f2004-06-15 18:49:58 +00001739 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
1740 return PyUnicode_Decode(outbuf, strlen(outbuf),
1741 Py_FileSystemDefaultEncoding, NULL);
1742 }
Mark Hammondef8b6542001-05-13 08:04:26 +00001743 return PyString_FromString(outbuf);
1744} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001745#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00001746
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001747PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001748"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001749Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001750
Barry Warsaw53699e91996-12-10 23:23:01 +00001751static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001752posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001753{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001754 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00001755 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001756 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001757
1758#ifdef Py_WIN_WIDE_FILENAMES
1759 if (unicode_file_names()) {
1760 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00001761 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001762 Py_BEGIN_ALLOW_THREADS
1763 /* PyUnicode_AS_UNICODE OK without thread lock as
1764 it is a simple dereference. */
1765 res = _wmkdir(PyUnicode_AS_UNICODE(po));
1766 Py_END_ALLOW_THREADS
1767 if (res < 0)
1768 return posix_error();
1769 Py_INCREF(Py_None);
1770 return Py_None;
1771 }
1772 /* Drop the argument parsing error as narrow strings
1773 are also valid. */
1774 PyErr_Clear();
1775 }
1776#endif
1777
Tim Peters5aa91602002-01-30 05:46:57 +00001778 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001779 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001780 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001781 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001782#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001783 res = mkdir(path);
1784#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001785 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001786#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001787 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001788 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001789 return posix_error_with_allocated_filename(path);
1790 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001791 Py_INCREF(Py_None);
1792 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001793}
1794
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001795
Guido van Rossumb6775db1994-08-01 11:34:53 +00001796#ifdef HAVE_NICE
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001797#if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H)
1798#if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS)
1799#include <sys/resource.h>
1800#endif
1801#endif
1802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001803PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001804"nice(inc) -> new_priority\n\n\
1805Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001806
Barry Warsaw53699e91996-12-10 23:23:01 +00001807static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001808posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00001809{
1810 int increment, value;
1811
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001812 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001813 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001814
1815 /* There are two flavours of 'nice': one that returns the new
1816 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001817 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
1818 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00001819
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001820 If we are of the nice family that returns the new priority, we
1821 need to clear errno before the call, and check if errno is filled
1822 before calling posix_error() on a returnvalue of -1, because the
1823 -1 may be the actual new priority! */
1824
1825 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001826 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001827#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001828 if (value == 0)
1829 value = getpriority(PRIO_PROCESS, 0);
1830#endif
1831 if (value == -1 && errno != 0)
1832 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00001833 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001834 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001835}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001836#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001837
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001838
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001839PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001840"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001841Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001842
Barry Warsaw53699e91996-12-10 23:23:01 +00001843static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001844posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001846#ifdef MS_WINDOWS
1847 return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename);
1848#else
1849 return posix_2str(args, "etet:rename", rename, NULL, NULL);
1850#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001851}
1852
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001853
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001854PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001855"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001856Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001857
Barry Warsaw53699e91996-12-10 23:23:01 +00001858static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001859posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001860{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001861#ifdef MS_WINDOWS
1862 return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir);
1863#else
1864 return posix_1str(args, "et:rmdir", rmdir, NULL, NULL);
1865#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001866}
1867
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001868
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001869PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001870"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001871Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001872
Barry Warsaw53699e91996-12-10 23:23:01 +00001873static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001874posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001875{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001876#ifdef MS_WINDOWS
1877 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64);
1878#else
1879 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
1880#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001881}
1882
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001883
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001884#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001885PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001886"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001887Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001888
Barry Warsaw53699e91996-12-10 23:23:01 +00001889static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001890posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001891{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001892 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001893 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001894 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001895 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001896 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001897 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001898 Py_END_ALLOW_THREADS
1899 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001900}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001901#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001902
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001903
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001904PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001905"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001906Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001907
Barry Warsaw53699e91996-12-10 23:23:01 +00001908static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001909posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001910{
1911 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001912 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001913 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00001914 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001915 if (i < 0)
1916 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001917 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001918}
1919
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001920
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001921PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001922"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001923Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001924
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001925PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001926"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001927Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001928
Barry Warsaw53699e91996-12-10 23:23:01 +00001929static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001930posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001931{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001932#ifdef MS_WINDOWS
1933 return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink);
1934#else
1935 return posix_1str(args, "et:remove", unlink, NULL, NULL);
1936#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001937}
1938
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001939
Guido van Rossumb6775db1994-08-01 11:34:53 +00001940#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001941PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001942"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001943Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001944
Barry Warsaw53699e91996-12-10 23:23:01 +00001945static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001946posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001947{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001948 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001949 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001950
Barry Warsaw53699e91996-12-10 23:23:01 +00001951 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001952 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001953 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001954 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001955 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001956 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001957 u.sysname,
1958 u.nodename,
1959 u.release,
1960 u.version,
1961 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001962}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001963#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001964
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001965static int
1966extract_time(PyObject *t, long* sec, long* usec)
1967{
1968 long intval;
1969 if (PyFloat_Check(t)) {
1970 double tval = PyFloat_AsDouble(t);
1971 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
1972 if (!intobj)
1973 return -1;
1974 intval = PyInt_AsLong(intobj);
1975 Py_DECREF(intobj);
1976 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00001977 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001978 if (*usec < 0)
1979 /* If rounding gave us a negative number,
1980 truncate. */
1981 *usec = 0;
1982 return 0;
1983 }
1984 intval = PyInt_AsLong(t);
1985 if (intval == -1 && PyErr_Occurred())
1986 return -1;
1987 *sec = intval;
1988 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00001989 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001990}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001991
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001992PyDoc_STRVAR(posix_utime__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001993"utime(path, (atime, utime))\n\
1994utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00001995Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001996second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001997
Barry Warsaw53699e91996-12-10 23:23:01 +00001998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001999posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002000{
Neal Norwitz2adf2102004-06-09 01:46:02 +00002001 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002002 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002003 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002004 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002005
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002006#if defined(HAVE_UTIMES)
2007 struct timeval buf[2];
2008#define ATIME buf[0].tv_sec
2009#define MTIME buf[1].tv_sec
2010#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002011/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002012 struct utimbuf buf;
2013#define ATIME buf.actime
2014#define MTIME buf.modtime
2015#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002016#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002017 time_t buf[2];
2018#define ATIME buf[0]
2019#define MTIME buf[1]
2020#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002021#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002022
Mark Hammond817c9292003-12-03 01:22:38 +00002023 int have_unicode_filename = 0;
2024#ifdef Py_WIN_WIDE_FILENAMES
2025 PyUnicodeObject *obwpath;
2026 wchar_t *wpath;
2027 if (unicode_file_names()) {
2028 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2029 wpath = PyUnicode_AS_UNICODE(obwpath);
2030 have_unicode_filename = 1;
2031 } else
2032 /* Drop the argument parsing error as narrow strings
2033 are also valid. */
2034 PyErr_Clear();
2035 }
2036#endif /* Py_WIN_WIDE_FILENAMES */
2037
2038 if (!have_unicode_filename && \
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002039 !PyArg_ParseTuple(args, "etO:utime",
2040 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002041 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002042 if (arg == Py_None) {
2043 /* optional time values not given */
2044 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002045#ifdef Py_WIN_WIDE_FILENAMES
2046 if (have_unicode_filename)
2047 res = _wutime(wpath, NULL);
2048 else
2049#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002050 res = utime(path, NULL);
2051 Py_END_ALLOW_THREADS
2052 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002053 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002054 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002055 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002056 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002057 return NULL;
2058 }
2059 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002060 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002061 &atime, &ausec) == -1) {
2062 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002063 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002064 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002065 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002066 &mtime, &musec) == -1) {
2067 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002068 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002069 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002070 ATIME = atime;
2071 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002072#ifdef HAVE_UTIMES
2073 buf[0].tv_usec = ausec;
2074 buf[1].tv_usec = musec;
2075 Py_BEGIN_ALLOW_THREADS
2076 res = utimes(path, buf);
2077 Py_END_ALLOW_THREADS
2078#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002079 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002080#ifdef Py_WIN_WIDE_FILENAMES
2081 if (have_unicode_filename)
Tim Peters4ad82172004-08-30 17:02:04 +00002082 /* utime is OK with utimbuf, but _wutime insists
2083 on _utimbuf (the msvc headers assert the
Mark Hammond817c9292003-12-03 01:22:38 +00002084 underscore version is ansi) */
2085 res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG);
2086 else
2087#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002088 res = utime(path, UTIME_ARG);
2089 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002090#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002091 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002092 if (res < 0) {
2093#ifdef Py_WIN_WIDE_FILENAMES
Neal Norwitz2adf2102004-06-09 01:46:02 +00002094 if (have_unicode_filename)
Mark Hammond2d5914b2004-05-04 08:10:37 +00002095 return posix_error_with_unicode_filename(wpath);
2096#endif /* Py_WIN_WIDE_FILENAMES */
Neal Norwitz96652712004-06-06 20:40:27 +00002097 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002098 }
Neal Norwitz96652712004-06-06 20:40:27 +00002099 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002100 Py_INCREF(Py_None);
2101 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002102#undef UTIME_ARG
2103#undef ATIME
2104#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002105}
2106
Guido van Rossum85e3b011991-06-03 12:42:10 +00002107
Guido van Rossum3b066191991-06-04 19:40:25 +00002108/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002109
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002110PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002111"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002112Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002113
Barry Warsaw53699e91996-12-10 23:23:01 +00002114static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002115posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002116{
2117 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002118 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002119 return NULL;
2120 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002121 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002122}
2123
Martin v. Löwis114619e2002-10-07 06:44:21 +00002124#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2125static void
2126free_string_array(char **array, int count)
2127{
2128 int i;
2129 for (i = 0; i < count; i++)
2130 PyMem_Free(array[i]);
2131 PyMem_DEL(array);
2132}
2133#endif
2134
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002135
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002136#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002137PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002138"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002139Execute an executable path with arguments, replacing current process.\n\
2140\n\
2141 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002142 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002143
Barry Warsaw53699e91996-12-10 23:23:01 +00002144static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002145posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002146{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002147 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002148 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002149 char **argvlist;
2150 int i, argc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002151 PyObject *(*getitem)(PyObject *, int);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002152
Guido van Rossum89b33251993-10-22 14:26:06 +00002153 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002154 argv is a list or tuple of strings. */
2155
Martin v. Löwis114619e2002-10-07 06:44:21 +00002156 if (!PyArg_ParseTuple(args, "etO:execv",
2157 Py_FileSystemDefaultEncoding,
2158 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002159 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002160 if (PyList_Check(argv)) {
2161 argc = PyList_Size(argv);
2162 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002163 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002164 else if (PyTuple_Check(argv)) {
2165 argc = PyTuple_Size(argv);
2166 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002167 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002168 else {
Fred Drake661ea262000-10-24 19:57:45 +00002169 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002170 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002171 return NULL;
2172 }
2173
Barry Warsaw53699e91996-12-10 23:23:01 +00002174 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002175 if (argvlist == NULL) {
2176 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002177 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002178 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002179 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002180 if (!PyArg_Parse((*getitem)(argv, i), "et",
2181 Py_FileSystemDefaultEncoding,
2182 &argvlist[i])) {
2183 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002184 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002185 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002186 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002187 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002188
Guido van Rossum85e3b011991-06-03 12:42:10 +00002189 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002190 }
2191 argvlist[argc] = NULL;
2192
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002193 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002194
Guido van Rossum85e3b011991-06-03 12:42:10 +00002195 /* If we get here it's definitely an error */
2196
Martin v. Löwis114619e2002-10-07 06:44:21 +00002197 free_string_array(argvlist, argc);
2198 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002199 return posix_error();
2200}
2201
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002202
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002203PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002204"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002205Execute a path with arguments and environment, replacing current process.\n\
2206\n\
2207 path: path of executable file\n\
2208 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002209 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002210
Barry Warsaw53699e91996-12-10 23:23:01 +00002211static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002212posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002213{
2214 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002215 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002216 char **argvlist;
2217 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002218 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002219 int i, pos, argc, envc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002220 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002221 int lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002222
2223 /* execve has three arguments: (path, argv, env), where
2224 argv is a list or tuple of strings and env is a dictionary
2225 like posix.environ. */
2226
Martin v. Löwis114619e2002-10-07 06:44:21 +00002227 if (!PyArg_ParseTuple(args, "etOO:execve",
2228 Py_FileSystemDefaultEncoding,
2229 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002230 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002231 if (PyList_Check(argv)) {
2232 argc = PyList_Size(argv);
2233 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002234 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002235 else if (PyTuple_Check(argv)) {
2236 argc = PyTuple_Size(argv);
2237 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002238 }
2239 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002240 PyErr_SetString(PyExc_TypeError,
2241 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002242 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002243 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002244 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002245 PyErr_SetString(PyExc_TypeError,
2246 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002247 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002248 }
2249
Barry Warsaw53699e91996-12-10 23:23:01 +00002250 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002251 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002252 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002253 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002254 }
2255 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002256 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002257 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002258 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002259 &argvlist[i]))
2260 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002261 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002262 goto fail_1;
2263 }
2264 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002265 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002266 argvlist[argc] = NULL;
2267
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002268 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002269 if (i < 0)
2270 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002271 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002272 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002273 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002274 goto fail_1;
2275 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002276 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002277 keys = PyMapping_Keys(env);
2278 vals = PyMapping_Values(env);
2279 if (!keys || !vals)
2280 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002281 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2282 PyErr_SetString(PyExc_TypeError,
2283 "execve(): env.keys() or env.values() is not a list");
2284 goto fail_2;
2285 }
Tim Peters5aa91602002-01-30 05:46:57 +00002286
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002287 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002288 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002289 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002290
2291 key = PyList_GetItem(keys, pos);
2292 val = PyList_GetItem(vals, pos);
2293 if (!key || !val)
2294 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002295
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002296 if (!PyArg_Parse(
2297 key,
2298 "s;execve() arg 3 contains a non-string key",
2299 &k) ||
2300 !PyArg_Parse(
2301 val,
2302 "s;execve() arg 3 contains a non-string value",
2303 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002304 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002305 goto fail_2;
2306 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002307
2308#if defined(PYOS_OS2)
2309 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2310 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2311#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002312 len = PyString_Size(key) + PyString_Size(val) + 2;
2313 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002314 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002315 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002316 goto fail_2;
2317 }
Tim Petersc8996f52001-12-03 20:41:00 +00002318 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002319 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002320#if defined(PYOS_OS2)
2321 }
2322#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002323 }
2324 envlist[envc] = 0;
2325
2326 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002327
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002328 /* If we get here it's definitely an error */
2329
2330 (void) posix_error();
2331
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002332 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002333 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002334 PyMem_DEL(envlist[envc]);
2335 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002336 fail_1:
2337 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002338 Py_XDECREF(vals);
2339 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002340 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002341 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002342 return NULL;
2343}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002344#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002345
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002346
Guido van Rossuma1065681999-01-25 23:20:23 +00002347#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002348PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002349"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002350Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002351\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002352 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002353 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002354 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002355
2356static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002357posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002358{
2359 char *path;
2360 PyObject *argv;
2361 char **argvlist;
2362 int mode, i, argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002363 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002364 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00002365
2366 /* spawnv has three arguments: (mode, path, argv), where
2367 argv is a list or tuple of strings. */
2368
Martin v. Löwis114619e2002-10-07 06:44:21 +00002369 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2370 Py_FileSystemDefaultEncoding,
2371 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002372 return NULL;
2373 if (PyList_Check(argv)) {
2374 argc = PyList_Size(argv);
2375 getitem = PyList_GetItem;
2376 }
2377 else if (PyTuple_Check(argv)) {
2378 argc = PyTuple_Size(argv);
2379 getitem = PyTuple_GetItem;
2380 }
2381 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002382 PyErr_SetString(PyExc_TypeError,
2383 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002384 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002385 return NULL;
2386 }
2387
2388 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002389 if (argvlist == NULL) {
2390 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002391 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002392 }
Guido van Rossuma1065681999-01-25 23:20:23 +00002393 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002394 if (!PyArg_Parse((*getitem)(argv, i), "et",
2395 Py_FileSystemDefaultEncoding,
2396 &argvlist[i])) {
2397 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002398 PyErr_SetString(
2399 PyExc_TypeError,
2400 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002401 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00002402 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00002403 }
2404 }
2405 argvlist[argc] = NULL;
2406
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002407#if defined(PYOS_OS2) && defined(PYCC_GCC)
2408 Py_BEGIN_ALLOW_THREADS
2409 spawnval = spawnv(mode, path, argvlist);
2410 Py_END_ALLOW_THREADS
2411#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002412 if (mode == _OLD_P_OVERLAY)
2413 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00002414
Tim Peters25059d32001-12-07 20:35:43 +00002415 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002416 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00002417 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002418#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002419
Martin v. Löwis114619e2002-10-07 06:44:21 +00002420 free_string_array(argvlist, argc);
2421 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002422
Fred Drake699f3522000-06-29 21:12:41 +00002423 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002424 return posix_error();
2425 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002426#if SIZEOF_LONG == SIZEOF_VOID_P
2427 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002428#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002429 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002430#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002431}
2432
2433
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002434PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002435"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002436Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002437\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002438 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002439 path: path of executable file\n\
2440 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002441 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002442
2443static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002444posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002445{
2446 char *path;
2447 PyObject *argv, *env;
2448 char **argvlist;
2449 char **envlist;
2450 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2451 int mode, i, pos, argc, envc;
Tim Peters79248aa2001-08-29 21:37:10 +00002452 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002453 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002454 int lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002455
2456 /* spawnve has four arguments: (mode, path, argv, env), where
2457 argv is a list or tuple of strings and env is a dictionary
2458 like posix.environ. */
2459
Martin v. Löwis114619e2002-10-07 06:44:21 +00002460 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
2461 Py_FileSystemDefaultEncoding,
2462 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00002463 return NULL;
2464 if (PyList_Check(argv)) {
2465 argc = PyList_Size(argv);
2466 getitem = PyList_GetItem;
2467 }
2468 else if (PyTuple_Check(argv)) {
2469 argc = PyTuple_Size(argv);
2470 getitem = PyTuple_GetItem;
2471 }
2472 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002473 PyErr_SetString(PyExc_TypeError,
2474 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002475 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002476 }
2477 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002478 PyErr_SetString(PyExc_TypeError,
2479 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002480 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002481 }
2482
2483 argvlist = PyMem_NEW(char *, argc+1);
2484 if (argvlist == NULL) {
2485 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002486 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002487 }
2488 for (i = 0; i < argc; i++) {
2489 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002490 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00002491 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00002492 &argvlist[i]))
2493 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002494 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00002495 goto fail_1;
2496 }
2497 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002498 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00002499 argvlist[argc] = NULL;
2500
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002501 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002502 if (i < 0)
2503 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00002504 envlist = PyMem_NEW(char *, i + 1);
2505 if (envlist == NULL) {
2506 PyErr_NoMemory();
2507 goto fail_1;
2508 }
2509 envc = 0;
2510 keys = PyMapping_Keys(env);
2511 vals = PyMapping_Values(env);
2512 if (!keys || !vals)
2513 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002514 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2515 PyErr_SetString(PyExc_TypeError,
2516 "spawnve(): env.keys() or env.values() is not a list");
2517 goto fail_2;
2518 }
Tim Peters5aa91602002-01-30 05:46:57 +00002519
Guido van Rossuma1065681999-01-25 23:20:23 +00002520 for (pos = 0; pos < i; pos++) {
2521 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002522 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00002523
2524 key = PyList_GetItem(keys, pos);
2525 val = PyList_GetItem(vals, pos);
2526 if (!key || !val)
2527 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002528
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002529 if (!PyArg_Parse(
2530 key,
2531 "s;spawnve() arg 3 contains a non-string key",
2532 &k) ||
2533 !PyArg_Parse(
2534 val,
2535 "s;spawnve() arg 3 contains a non-string value",
2536 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00002537 {
2538 goto fail_2;
2539 }
Tim Petersc8996f52001-12-03 20:41:00 +00002540 len = PyString_Size(key) + PyString_Size(val) + 2;
2541 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00002542 if (p == NULL) {
2543 PyErr_NoMemory();
2544 goto fail_2;
2545 }
Tim Petersc8996f52001-12-03 20:41:00 +00002546 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00002547 envlist[envc++] = p;
2548 }
2549 envlist[envc] = 0;
2550
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002551#if defined(PYOS_OS2) && defined(PYCC_GCC)
2552 Py_BEGIN_ALLOW_THREADS
2553 spawnval = spawnve(mode, path, argvlist, envlist);
2554 Py_END_ALLOW_THREADS
2555#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002556 if (mode == _OLD_P_OVERLAY)
2557 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00002558
2559 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002560 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00002561 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002562#endif
Tim Peters25059d32001-12-07 20:35:43 +00002563
Fred Drake699f3522000-06-29 21:12:41 +00002564 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002565 (void) posix_error();
2566 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002567#if SIZEOF_LONG == SIZEOF_VOID_P
2568 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002569#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002570 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002571#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002572
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002573 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00002574 while (--envc >= 0)
2575 PyMem_DEL(envlist[envc]);
2576 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002577 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002578 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00002579 Py_XDECREF(vals);
2580 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002581 fail_0:
2582 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002583 return res;
2584}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00002585
2586/* OS/2 supports spawnvp & spawnvpe natively */
2587#if defined(PYOS_OS2)
2588PyDoc_STRVAR(posix_spawnvp__doc__,
2589"spawnvp(mode, file, args)\n\n\
2590Execute the program 'file' in a new process, using the environment\n\
2591search path to find the file.\n\
2592\n\
2593 mode: mode of process creation\n\
2594 file: executable file name\n\
2595 args: tuple or list of strings");
2596
2597static PyObject *
2598posix_spawnvp(PyObject *self, PyObject *args)
2599{
2600 char *path;
2601 PyObject *argv;
2602 char **argvlist;
2603 int mode, i, argc;
2604 Py_intptr_t spawnval;
2605 PyObject *(*getitem)(PyObject *, int);
2606
2607 /* spawnvp has three arguments: (mode, path, argv), where
2608 argv is a list or tuple of strings. */
2609
2610 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
2611 Py_FileSystemDefaultEncoding,
2612 &path, &argv))
2613 return NULL;
2614 if (PyList_Check(argv)) {
2615 argc = PyList_Size(argv);
2616 getitem = PyList_GetItem;
2617 }
2618 else if (PyTuple_Check(argv)) {
2619 argc = PyTuple_Size(argv);
2620 getitem = PyTuple_GetItem;
2621 }
2622 else {
2623 PyErr_SetString(PyExc_TypeError,
2624 "spawnvp() arg 2 must be a tuple or list");
2625 PyMem_Free(path);
2626 return NULL;
2627 }
2628
2629 argvlist = PyMem_NEW(char *, argc+1);
2630 if (argvlist == NULL) {
2631 PyMem_Free(path);
2632 return PyErr_NoMemory();
2633 }
2634 for (i = 0; i < argc; i++) {
2635 if (!PyArg_Parse((*getitem)(argv, i), "et",
2636 Py_FileSystemDefaultEncoding,
2637 &argvlist[i])) {
2638 free_string_array(argvlist, i);
2639 PyErr_SetString(
2640 PyExc_TypeError,
2641 "spawnvp() arg 2 must contain only strings");
2642 PyMem_Free(path);
2643 return NULL;
2644 }
2645 }
2646 argvlist[argc] = NULL;
2647
2648 Py_BEGIN_ALLOW_THREADS
2649#if defined(PYCC_GCC)
2650 spawnval = spawnvp(mode, path, argvlist);
2651#else
2652 spawnval = _spawnvp(mode, path, argvlist);
2653#endif
2654 Py_END_ALLOW_THREADS
2655
2656 free_string_array(argvlist, argc);
2657 PyMem_Free(path);
2658
2659 if (spawnval == -1)
2660 return posix_error();
2661 else
2662 return Py_BuildValue("l", (long) spawnval);
2663}
2664
2665
2666PyDoc_STRVAR(posix_spawnvpe__doc__,
2667"spawnvpe(mode, file, args, env)\n\n\
2668Execute the program 'file' in a new process, using the environment\n\
2669search path to find the file.\n\
2670\n\
2671 mode: mode of process creation\n\
2672 file: executable file name\n\
2673 args: tuple or list of arguments\n\
2674 env: dictionary of strings mapping to strings");
2675
2676static PyObject *
2677posix_spawnvpe(PyObject *self, PyObject *args)
2678{
2679 char *path;
2680 PyObject *argv, *env;
2681 char **argvlist;
2682 char **envlist;
2683 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2684 int mode, i, pos, argc, envc;
2685 Py_intptr_t spawnval;
2686 PyObject *(*getitem)(PyObject *, int);
2687 int lastarg = 0;
2688
2689 /* spawnvpe has four arguments: (mode, path, argv, env), where
2690 argv is a list or tuple of strings and env is a dictionary
2691 like posix.environ. */
2692
2693 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
2694 Py_FileSystemDefaultEncoding,
2695 &path, &argv, &env))
2696 return NULL;
2697 if (PyList_Check(argv)) {
2698 argc = PyList_Size(argv);
2699 getitem = PyList_GetItem;
2700 }
2701 else if (PyTuple_Check(argv)) {
2702 argc = PyTuple_Size(argv);
2703 getitem = PyTuple_GetItem;
2704 }
2705 else {
2706 PyErr_SetString(PyExc_TypeError,
2707 "spawnvpe() arg 2 must be a tuple or list");
2708 goto fail_0;
2709 }
2710 if (!PyMapping_Check(env)) {
2711 PyErr_SetString(PyExc_TypeError,
2712 "spawnvpe() arg 3 must be a mapping object");
2713 goto fail_0;
2714 }
2715
2716 argvlist = PyMem_NEW(char *, argc+1);
2717 if (argvlist == NULL) {
2718 PyErr_NoMemory();
2719 goto fail_0;
2720 }
2721 for (i = 0; i < argc; i++) {
2722 if (!PyArg_Parse((*getitem)(argv, i),
2723 "et;spawnvpe() arg 2 must contain only strings",
2724 Py_FileSystemDefaultEncoding,
2725 &argvlist[i]))
2726 {
2727 lastarg = i;
2728 goto fail_1;
2729 }
2730 }
2731 lastarg = argc;
2732 argvlist[argc] = NULL;
2733
2734 i = PyMapping_Size(env);
2735 if (i < 0)
2736 goto fail_1;
2737 envlist = PyMem_NEW(char *, i + 1);
2738 if (envlist == NULL) {
2739 PyErr_NoMemory();
2740 goto fail_1;
2741 }
2742 envc = 0;
2743 keys = PyMapping_Keys(env);
2744 vals = PyMapping_Values(env);
2745 if (!keys || !vals)
2746 goto fail_2;
2747 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2748 PyErr_SetString(PyExc_TypeError,
2749 "spawnvpe(): env.keys() or env.values() is not a list");
2750 goto fail_2;
2751 }
2752
2753 for (pos = 0; pos < i; pos++) {
2754 char *p, *k, *v;
2755 size_t len;
2756
2757 key = PyList_GetItem(keys, pos);
2758 val = PyList_GetItem(vals, pos);
2759 if (!key || !val)
2760 goto fail_2;
2761
2762 if (!PyArg_Parse(
2763 key,
2764 "s;spawnvpe() arg 3 contains a non-string key",
2765 &k) ||
2766 !PyArg_Parse(
2767 val,
2768 "s;spawnvpe() arg 3 contains a non-string value",
2769 &v))
2770 {
2771 goto fail_2;
2772 }
2773 len = PyString_Size(key) + PyString_Size(val) + 2;
2774 p = PyMem_NEW(char, len);
2775 if (p == NULL) {
2776 PyErr_NoMemory();
2777 goto fail_2;
2778 }
2779 PyOS_snprintf(p, len, "%s=%s", k, v);
2780 envlist[envc++] = p;
2781 }
2782 envlist[envc] = 0;
2783
2784 Py_BEGIN_ALLOW_THREADS
2785#if defined(PYCC_GCC)
2786 spawnval = spawnve(mode, path, argvlist, envlist);
2787#else
2788 spawnval = _spawnve(mode, path, argvlist, envlist);
2789#endif
2790 Py_END_ALLOW_THREADS
2791
2792 if (spawnval == -1)
2793 (void) posix_error();
2794 else
2795 res = Py_BuildValue("l", (long) spawnval);
2796
2797 fail_2:
2798 while (--envc >= 0)
2799 PyMem_DEL(envlist[envc]);
2800 PyMem_DEL(envlist);
2801 fail_1:
2802 free_string_array(argvlist, lastarg);
2803 Py_XDECREF(vals);
2804 Py_XDECREF(keys);
2805 fail_0:
2806 PyMem_Free(path);
2807 return res;
2808}
2809#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00002810#endif /* HAVE_SPAWNV */
2811
2812
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002813#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002814PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002815"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002816Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
2817\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002818Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002819
2820static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002821posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002822{
Neal Norwitze241ce82003-02-17 18:17:05 +00002823 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002824 if (pid == -1)
2825 return posix_error();
2826 PyOS_AfterFork();
2827 return PyInt_FromLong((long)pid);
2828}
2829#endif
2830
2831
Guido van Rossumad0ee831995-03-01 10:34:45 +00002832#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002833PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002834"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002835Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002836Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002837
Barry Warsaw53699e91996-12-10 23:23:01 +00002838static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002839posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002840{
Neal Norwitze241ce82003-02-17 18:17:05 +00002841 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00002842 if (pid == -1)
2843 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002844 if (pid == 0)
2845 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00002846 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002847}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002848#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002849
Neal Norwitzb59798b2003-03-21 01:43:31 +00002850/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00002851/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
2852#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00002853#define DEV_PTY_FILE "/dev/ptc"
2854#define HAVE_DEV_PTMX
2855#else
2856#define DEV_PTY_FILE "/dev/ptmx"
2857#endif
2858
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002859#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002860#ifdef HAVE_PTY_H
2861#include <pty.h>
2862#else
2863#ifdef HAVE_LIBUTIL_H
2864#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00002865#endif /* HAVE_LIBUTIL_H */
2866#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00002867#ifdef HAVE_STROPTS_H
2868#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002869#endif
2870#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002871
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002872#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002873PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002874"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002875Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002876
2877static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002878posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002879{
2880 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002881#ifndef HAVE_OPENPTY
2882 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002883#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002884#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002885 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002886#ifdef sun
2887 extern char *ptsname();
2888#endif
2889#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00002890
Thomas Wouters70c21a12000-07-14 14:28:33 +00002891#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00002892 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
2893 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002894#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00002895 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
2896 if (slave_name == NULL)
2897 return posix_error();
2898
2899 slave_fd = open(slave_name, O_RDWR);
2900 if (slave_fd < 0)
2901 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002902#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00002903 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002904 if (master_fd < 0)
2905 return posix_error();
2906 sig_saved = signal(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002907 /* change permission of slave */
2908 if (grantpt(master_fd) < 0) {
2909 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002910 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002911 }
2912 /* unlock slave */
2913 if (unlockpt(master_fd) < 0) {
2914 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002915 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002916 }
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002917 signal(SIGCHLD, sig_saved);
2918 slave_name = ptsname(master_fd); /* get name of slave */
2919 if (slave_name == NULL)
2920 return posix_error();
2921 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
2922 if (slave_fd < 0)
2923 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002924#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002925 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
2926 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00002927#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002928 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00002929#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002930#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00002931#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00002932
Fred Drake8cef4cf2000-06-28 16:40:38 +00002933 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00002934
Fred Drake8cef4cf2000-06-28 16:40:38 +00002935}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002936#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002937
2938#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002939PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002940"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00002941Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
2942Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002943To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002944
2945static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002946posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002947{
2948 int master_fd, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00002949
Fred Drake8cef4cf2000-06-28 16:40:38 +00002950 pid = forkpty(&master_fd, NULL, NULL, NULL);
2951 if (pid == -1)
2952 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002953 if (pid == 0)
2954 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00002955 return Py_BuildValue("(ii)", pid, master_fd);
2956}
2957#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002958
Guido van Rossumad0ee831995-03-01 10:34:45 +00002959#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002960PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002961"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002962Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002963
Barry Warsaw53699e91996-12-10 23:23:01 +00002964static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002965posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002966{
Barry Warsaw53699e91996-12-10 23:23:01 +00002967 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002968}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002969#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002970
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002971
Guido van Rossumad0ee831995-03-01 10:34:45 +00002972#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002973PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002974"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002975Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002976
Barry Warsaw53699e91996-12-10 23:23:01 +00002977static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002978posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002979{
Barry Warsaw53699e91996-12-10 23:23:01 +00002980 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002981}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002982#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002983
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002984
Guido van Rossumad0ee831995-03-01 10:34:45 +00002985#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002986PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002987"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002988Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002989
Barry Warsaw53699e91996-12-10 23:23:01 +00002990static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002991posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002992{
Barry Warsaw53699e91996-12-10 23:23:01 +00002993 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002994}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002995#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002996
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002997
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002998PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002999"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003000Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003001
Barry Warsaw53699e91996-12-10 23:23:01 +00003002static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003003posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003004{
Barry Warsaw53699e91996-12-10 23:23:01 +00003005 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003006}
3007
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003008
Fred Drakec9680921999-12-13 16:37:25 +00003009#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003010PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003011"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003012Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003013
3014static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003015posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003016{
3017 PyObject *result = NULL;
3018
Fred Drakec9680921999-12-13 16:37:25 +00003019#ifdef NGROUPS_MAX
3020#define MAX_GROUPS NGROUPS_MAX
3021#else
3022 /* defined to be 16 on Solaris7, so this should be a small number */
3023#define MAX_GROUPS 64
3024#endif
3025 gid_t grouplist[MAX_GROUPS];
3026 int n;
3027
3028 n = getgroups(MAX_GROUPS, grouplist);
3029 if (n < 0)
3030 posix_error();
3031 else {
3032 result = PyList_New(n);
3033 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003034 int i;
3035 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003036 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003037 if (o == NULL) {
3038 Py_DECREF(result);
3039 result = NULL;
3040 break;
3041 }
3042 PyList_SET_ITEM(result, i, o);
3043 }
3044 }
3045 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003046
Fred Drakec9680921999-12-13 16:37:25 +00003047 return result;
3048}
3049#endif
3050
Martin v. Löwis606edc12002-06-13 21:09:11 +00003051#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003052PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003053"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003054Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003055
3056static PyObject *
3057posix_getpgid(PyObject *self, PyObject *args)
3058{
3059 int pid, pgid;
3060 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3061 return NULL;
3062 pgid = getpgid(pid);
3063 if (pgid < 0)
3064 return posix_error();
3065 return PyInt_FromLong((long)pgid);
3066}
3067#endif /* HAVE_GETPGID */
3068
3069
Guido van Rossumb6775db1994-08-01 11:34:53 +00003070#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003071PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003072"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003073Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003074
Barry Warsaw53699e91996-12-10 23:23:01 +00003075static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003076posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003077{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003078#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003079 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003080#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003081 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003082#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003083}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003084#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003085
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003086
Guido van Rossumb6775db1994-08-01 11:34:53 +00003087#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003088PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003089"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003090Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003091
Barry Warsaw53699e91996-12-10 23:23:01 +00003092static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003093posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003094{
Guido van Rossum64933891994-10-20 21:56:42 +00003095#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003096 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003097#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003098 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003099#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003100 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003101 Py_INCREF(Py_None);
3102 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003103}
3104
Guido van Rossumb6775db1994-08-01 11:34:53 +00003105#endif /* HAVE_SETPGRP */
3106
Guido van Rossumad0ee831995-03-01 10:34:45 +00003107#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003108PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003109"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003110Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003111
Barry Warsaw53699e91996-12-10 23:23:01 +00003112static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003113posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003114{
Barry Warsaw53699e91996-12-10 23:23:01 +00003115 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003116}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003117#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003118
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003119
Fred Drake12c6e2d1999-12-14 21:25:03 +00003120#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003121PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003122"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003123Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003124
3125static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003126posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003127{
Neal Norwitze241ce82003-02-17 18:17:05 +00003128 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003129 char *name;
3130 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003131
Fred Drakea30680b2000-12-06 21:24:28 +00003132 errno = 0;
3133 name = getlogin();
3134 if (name == NULL) {
3135 if (errno)
3136 posix_error();
3137 else
3138 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003139 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003140 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003141 else
3142 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003143 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003144
Fred Drake12c6e2d1999-12-14 21:25:03 +00003145 return result;
3146}
3147#endif
3148
Guido van Rossumad0ee831995-03-01 10:34:45 +00003149#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003150PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003151"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003152Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003153
Barry Warsaw53699e91996-12-10 23:23:01 +00003154static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003155posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003156{
Barry Warsaw53699e91996-12-10 23:23:01 +00003157 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003158}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003159#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003160
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003161
Guido van Rossumad0ee831995-03-01 10:34:45 +00003162#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003163PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003164"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003165Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003166
Barry Warsaw53699e91996-12-10 23:23:01 +00003167static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003168posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003169{
3170 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003171 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003172 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003173#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003174 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3175 APIRET rc;
3176 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003177 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003178
3179 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3180 APIRET rc;
3181 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003182 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003183
3184 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003185 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003186#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003187 if (kill(pid, sig) == -1)
3188 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003189#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003190 Py_INCREF(Py_None);
3191 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003192}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003193#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003194
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003195#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003196PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003197"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003198Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003199
3200static PyObject *
3201posix_killpg(PyObject *self, PyObject *args)
3202{
3203 int pgid, sig;
3204 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3205 return NULL;
3206 if (killpg(pgid, sig) == -1)
3207 return posix_error();
3208 Py_INCREF(Py_None);
3209 return Py_None;
3210}
3211#endif
3212
Guido van Rossumc0125471996-06-28 18:55:32 +00003213#ifdef HAVE_PLOCK
3214
3215#ifdef HAVE_SYS_LOCK_H
3216#include <sys/lock.h>
3217#endif
3218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003219PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003220"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003221Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003222
Barry Warsaw53699e91996-12-10 23:23:01 +00003223static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003224posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003225{
3226 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003227 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003228 return NULL;
3229 if (plock(op) == -1)
3230 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003231 Py_INCREF(Py_None);
3232 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003233}
3234#endif
3235
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003236
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003237#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003238PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003239"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003240Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003241
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003242#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003243#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003244static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003245async_system(const char *command)
3246{
3247 char *p, errormsg[256], args[1024];
3248 RESULTCODES rcodes;
3249 APIRET rc;
3250 char *shell = getenv("COMSPEC");
3251 if (!shell)
3252 shell = "cmd";
3253
3254 strcpy(args, shell);
3255 p = &args[ strlen(args)+1 ];
3256 strcpy(p, "/c ");
3257 strcat(p, command);
3258 p += strlen(p) + 1;
3259 *p = '\0';
3260
3261 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003262 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003263 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003264 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003265 &rcodes, shell);
3266 return rc;
3267}
3268
Guido van Rossumd48f2521997-12-05 22:19:34 +00003269static FILE *
3270popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003271{
3272 HFILE rhan, whan;
3273 FILE *retfd = NULL;
3274 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
3275
Guido van Rossumd48f2521997-12-05 22:19:34 +00003276 if (rc != NO_ERROR) {
3277 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003278 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00003279 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003280
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003281 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
3282 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003283
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003284 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3285 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003286
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003287 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
3288 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003289
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003290 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003291 }
3292
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003293 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
3294 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003295
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003296 if (rc == NO_ERROR)
3297 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
3298
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003299 close(oldfd); /* And Close Saved STDOUT Handle */
3300 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003301
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003302 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
3303 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003304
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003305 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3306 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003307
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003308 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
3309 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003310
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003311 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003312 }
3313
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003314 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
3315 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003316
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003317 if (rc == NO_ERROR)
3318 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
3319
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003320 close(oldfd); /* And Close Saved STDIN Handle */
3321 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003322
Guido van Rossumd48f2521997-12-05 22:19:34 +00003323 } else {
3324 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003325 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00003326 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003327}
3328
3329static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003330posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003331{
3332 char *name;
3333 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003334 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003335 FILE *fp;
3336 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003337 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003338 return NULL;
3339 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003340 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003341 Py_END_ALLOW_THREADS
3342 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003343 return os2_error(err);
3344
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003345 f = PyFile_FromFile(fp, name, mode, fclose);
3346 if (f != NULL)
3347 PyFile_SetBufSize(f, bufsize);
3348 return f;
3349}
3350
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003351#elif defined(PYCC_GCC)
3352
3353/* standard posix version of popen() support */
3354static PyObject *
3355posix_popen(PyObject *self, PyObject *args)
3356{
3357 char *name;
3358 char *mode = "r";
3359 int bufsize = -1;
3360 FILE *fp;
3361 PyObject *f;
3362 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3363 return NULL;
3364 Py_BEGIN_ALLOW_THREADS
3365 fp = popen(name, mode);
3366 Py_END_ALLOW_THREADS
3367 if (fp == NULL)
3368 return posix_error();
3369 f = PyFile_FromFile(fp, name, mode, pclose);
3370 if (f != NULL)
3371 PyFile_SetBufSize(f, bufsize);
3372 return f;
3373}
3374
3375/* fork() under OS/2 has lots'o'warts
3376 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
3377 * most of this code is a ripoff of the win32 code, but using the
3378 * capabilities of EMX's C library routines
3379 */
3380
3381/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
3382#define POPEN_1 1
3383#define POPEN_2 2
3384#define POPEN_3 3
3385#define POPEN_4 4
3386
3387static PyObject *_PyPopen(char *, int, int, int);
3388static int _PyPclose(FILE *file);
3389
3390/*
3391 * Internal dictionary mapping popen* file pointers to process handles,
3392 * for use when retrieving the process exit code. See _PyPclose() below
3393 * for more information on this dictionary's use.
3394 */
3395static PyObject *_PyPopenProcs = NULL;
3396
3397/* os2emx version of popen2()
3398 *
3399 * The result of this function is a pipe (file) connected to the
3400 * process's stdin, and a pipe connected to the process's stdout.
3401 */
3402
3403static PyObject *
3404os2emx_popen2(PyObject *self, PyObject *args)
3405{
3406 PyObject *f;
3407 int tm=0;
3408
3409 char *cmdstring;
3410 char *mode = "t";
3411 int bufsize = -1;
3412 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
3413 return NULL;
3414
3415 if (*mode == 't')
3416 tm = O_TEXT;
3417 else if (*mode != 'b') {
3418 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3419 return NULL;
3420 } else
3421 tm = O_BINARY;
3422
3423 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
3424
3425 return f;
3426}
3427
3428/*
3429 * Variation on os2emx.popen2
3430 *
3431 * The result of this function is 3 pipes - the process's stdin,
3432 * stdout and stderr
3433 */
3434
3435static PyObject *
3436os2emx_popen3(PyObject *self, PyObject *args)
3437{
3438 PyObject *f;
3439 int tm = 0;
3440
3441 char *cmdstring;
3442 char *mode = "t";
3443 int bufsize = -1;
3444 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
3445 return NULL;
3446
3447 if (*mode == 't')
3448 tm = O_TEXT;
3449 else if (*mode != 'b') {
3450 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3451 return NULL;
3452 } else
3453 tm = O_BINARY;
3454
3455 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
3456
3457 return f;
3458}
3459
3460/*
3461 * Variation on os2emx.popen2
3462 *
Tim Peters11b23062003-04-23 02:39:17 +00003463 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003464 * and stdout+stderr combined as a single pipe.
3465 */
3466
3467static PyObject *
3468os2emx_popen4(PyObject *self, PyObject *args)
3469{
3470 PyObject *f;
3471 int tm = 0;
3472
3473 char *cmdstring;
3474 char *mode = "t";
3475 int bufsize = -1;
3476 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
3477 return NULL;
3478
3479 if (*mode == 't')
3480 tm = O_TEXT;
3481 else if (*mode != 'b') {
3482 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3483 return NULL;
3484 } else
3485 tm = O_BINARY;
3486
3487 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
3488
3489 return f;
3490}
3491
3492/* a couple of structures for convenient handling of multiple
3493 * file handles and pipes
3494 */
3495struct file_ref
3496{
3497 int handle;
3498 int flags;
3499};
3500
3501struct pipe_ref
3502{
3503 int rd;
3504 int wr;
3505};
3506
3507/* The following code is derived from the win32 code */
3508
3509static PyObject *
3510_PyPopen(char *cmdstring, int mode, int n, int bufsize)
3511{
3512 struct file_ref stdio[3];
3513 struct pipe_ref p_fd[3];
3514 FILE *p_s[3];
3515 int file_count, i, pipe_err, pipe_pid;
3516 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
3517 PyObject *f, *p_f[3];
3518
3519 /* file modes for subsequent fdopen's on pipe handles */
3520 if (mode == O_TEXT)
3521 {
3522 rd_mode = "rt";
3523 wr_mode = "wt";
3524 }
3525 else
3526 {
3527 rd_mode = "rb";
3528 wr_mode = "wb";
3529 }
3530
3531 /* prepare shell references */
3532 if ((shell = getenv("EMXSHELL")) == NULL)
3533 if ((shell = getenv("COMSPEC")) == NULL)
3534 {
3535 errno = ENOENT;
3536 return posix_error();
3537 }
3538
3539 sh_name = _getname(shell);
3540 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
3541 opt = "/c";
3542 else
3543 opt = "-c";
3544
3545 /* save current stdio fds + their flags, and set not inheritable */
3546 i = pipe_err = 0;
3547 while (pipe_err >= 0 && i < 3)
3548 {
3549 pipe_err = stdio[i].handle = dup(i);
3550 stdio[i].flags = fcntl(i, F_GETFD, 0);
3551 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
3552 i++;
3553 }
3554 if (pipe_err < 0)
3555 {
3556 /* didn't get them all saved - clean up and bail out */
3557 int saved_err = errno;
3558 while (i-- > 0)
3559 {
3560 close(stdio[i].handle);
3561 }
3562 errno = saved_err;
3563 return posix_error();
3564 }
3565
3566 /* create pipe ends */
3567 file_count = 2;
3568 if (n == POPEN_3)
3569 file_count = 3;
3570 i = pipe_err = 0;
3571 while ((pipe_err == 0) && (i < file_count))
3572 pipe_err = pipe((int *)&p_fd[i++]);
3573 if (pipe_err < 0)
3574 {
3575 /* didn't get them all made - clean up and bail out */
3576 while (i-- > 0)
3577 {
3578 close(p_fd[i].wr);
3579 close(p_fd[i].rd);
3580 }
3581 errno = EPIPE;
3582 return posix_error();
3583 }
3584
3585 /* change the actual standard IO streams over temporarily,
3586 * making the retained pipe ends non-inheritable
3587 */
3588 pipe_err = 0;
3589
3590 /* - stdin */
3591 if (dup2(p_fd[0].rd, 0) == 0)
3592 {
3593 close(p_fd[0].rd);
3594 i = fcntl(p_fd[0].wr, F_GETFD, 0);
3595 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
3596 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
3597 {
3598 close(p_fd[0].wr);
3599 pipe_err = -1;
3600 }
3601 }
3602 else
3603 {
3604 pipe_err = -1;
3605 }
3606
3607 /* - stdout */
3608 if (pipe_err == 0)
3609 {
3610 if (dup2(p_fd[1].wr, 1) == 1)
3611 {
3612 close(p_fd[1].wr);
3613 i = fcntl(p_fd[1].rd, F_GETFD, 0);
3614 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
3615 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
3616 {
3617 close(p_fd[1].rd);
3618 pipe_err = -1;
3619 }
3620 }
3621 else
3622 {
3623 pipe_err = -1;
3624 }
3625 }
3626
3627 /* - stderr, as required */
3628 if (pipe_err == 0)
3629 switch (n)
3630 {
3631 case POPEN_3:
3632 {
3633 if (dup2(p_fd[2].wr, 2) == 2)
3634 {
3635 close(p_fd[2].wr);
3636 i = fcntl(p_fd[2].rd, F_GETFD, 0);
3637 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
3638 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
3639 {
3640 close(p_fd[2].rd);
3641 pipe_err = -1;
3642 }
3643 }
3644 else
3645 {
3646 pipe_err = -1;
3647 }
3648 break;
3649 }
3650
3651 case POPEN_4:
3652 {
3653 if (dup2(1, 2) != 2)
3654 {
3655 pipe_err = -1;
3656 }
3657 break;
3658 }
3659 }
3660
3661 /* spawn the child process */
3662 if (pipe_err == 0)
3663 {
3664 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
3665 if (pipe_pid == -1)
3666 {
3667 pipe_err = -1;
3668 }
3669 else
3670 {
3671 /* save the PID into the FILE structure
3672 * NOTE: this implementation doesn't actually
3673 * take advantage of this, but do it for
3674 * completeness - AIM Apr01
3675 */
3676 for (i = 0; i < file_count; i++)
3677 p_s[i]->_pid = pipe_pid;
3678 }
3679 }
3680
3681 /* reset standard IO to normal */
3682 for (i = 0; i < 3; i++)
3683 {
3684 dup2(stdio[i].handle, i);
3685 fcntl(i, F_SETFD, stdio[i].flags);
3686 close(stdio[i].handle);
3687 }
3688
3689 /* if any remnant problems, clean up and bail out */
3690 if (pipe_err < 0)
3691 {
3692 for (i = 0; i < 3; i++)
3693 {
3694 close(p_fd[i].rd);
3695 close(p_fd[i].wr);
3696 }
3697 errno = EPIPE;
3698 return posix_error_with_filename(cmdstring);
3699 }
3700
3701 /* build tuple of file objects to return */
3702 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
3703 PyFile_SetBufSize(p_f[0], bufsize);
3704 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
3705 PyFile_SetBufSize(p_f[1], bufsize);
3706 if (n == POPEN_3)
3707 {
3708 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
3709 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003710 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003711 }
3712 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003713 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003714
3715 /*
3716 * Insert the files we've created into the process dictionary
3717 * all referencing the list with the process handle and the
3718 * initial number of files (see description below in _PyPclose).
3719 * Since if _PyPclose later tried to wait on a process when all
3720 * handles weren't closed, it could create a deadlock with the
3721 * child, we spend some energy here to try to ensure that we
3722 * either insert all file handles into the dictionary or none
3723 * at all. It's a little clumsy with the various popen modes
3724 * and variable number of files involved.
3725 */
3726 if (!_PyPopenProcs)
3727 {
3728 _PyPopenProcs = PyDict_New();
3729 }
3730
3731 if (_PyPopenProcs)
3732 {
3733 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
3734 int ins_rc[3];
3735
3736 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
3737 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
3738
3739 procObj = PyList_New(2);
3740 pidObj = PyInt_FromLong((long) pipe_pid);
3741 intObj = PyInt_FromLong((long) file_count);
3742
3743 if (procObj && pidObj && intObj)
3744 {
3745 PyList_SetItem(procObj, 0, pidObj);
3746 PyList_SetItem(procObj, 1, intObj);
3747
3748 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
3749 if (fileObj[0])
3750 {
3751 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
3752 fileObj[0],
3753 procObj);
3754 }
3755 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
3756 if (fileObj[1])
3757 {
3758 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
3759 fileObj[1],
3760 procObj);
3761 }
3762 if (file_count >= 3)
3763 {
3764 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
3765 if (fileObj[2])
3766 {
3767 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
3768 fileObj[2],
3769 procObj);
3770 }
3771 }
3772
3773 if (ins_rc[0] < 0 || !fileObj[0] ||
3774 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
3775 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
3776 {
3777 /* Something failed - remove any dictionary
3778 * entries that did make it.
3779 */
3780 if (!ins_rc[0] && fileObj[0])
3781 {
3782 PyDict_DelItem(_PyPopenProcs,
3783 fileObj[0]);
3784 }
3785 if (!ins_rc[1] && fileObj[1])
3786 {
3787 PyDict_DelItem(_PyPopenProcs,
3788 fileObj[1]);
3789 }
3790 if (!ins_rc[2] && fileObj[2])
3791 {
3792 PyDict_DelItem(_PyPopenProcs,
3793 fileObj[2]);
3794 }
3795 }
3796 }
Tim Peters11b23062003-04-23 02:39:17 +00003797
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003798 /*
3799 * Clean up our localized references for the dictionary keys
3800 * and value since PyDict_SetItem will Py_INCREF any copies
3801 * that got placed in the dictionary.
3802 */
3803 Py_XDECREF(procObj);
3804 Py_XDECREF(fileObj[0]);
3805 Py_XDECREF(fileObj[1]);
3806 Py_XDECREF(fileObj[2]);
3807 }
3808
3809 /* Child is launched. */
3810 return f;
3811}
3812
3813/*
3814 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3815 * exit code for the child process and return as a result of the close.
3816 *
3817 * This function uses the _PyPopenProcs dictionary in order to map the
3818 * input file pointer to information about the process that was
3819 * originally created by the popen* call that created the file pointer.
3820 * The dictionary uses the file pointer as a key (with one entry
3821 * inserted for each file returned by the original popen* call) and a
3822 * single list object as the value for all files from a single call.
3823 * The list object contains the Win32 process handle at [0], and a file
3824 * count at [1], which is initialized to the total number of file
3825 * handles using that list.
3826 *
3827 * This function closes whichever handle it is passed, and decrements
3828 * the file count in the dictionary for the process handle pointed to
3829 * by this file. On the last close (when the file count reaches zero),
3830 * this function will wait for the child process and then return its
3831 * exit code as the result of the close() operation. This permits the
3832 * files to be closed in any order - it is always the close() of the
3833 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003834 *
3835 * NOTE: This function is currently called with the GIL released.
3836 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003837 */
3838
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003839static int _PyPclose(FILE *file)
3840{
3841 int result;
3842 int exit_code;
3843 int pipe_pid;
3844 PyObject *procObj, *pidObj, *intObj, *fileObj;
3845 int file_count;
3846#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003847 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003848#endif
3849
3850 /* Close the file handle first, to ensure it can't block the
3851 * child from exiting if it's the last handle.
3852 */
3853 result = fclose(file);
3854
3855#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003856 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003857#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003858 if (_PyPopenProcs)
3859 {
3860 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
3861 (procObj = PyDict_GetItem(_PyPopenProcs,
3862 fileObj)) != NULL &&
3863 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
3864 (intObj = PyList_GetItem(procObj,1)) != NULL)
3865 {
3866 pipe_pid = (int) PyInt_AsLong(pidObj);
3867 file_count = (int) PyInt_AsLong(intObj);
3868
3869 if (file_count > 1)
3870 {
3871 /* Still other files referencing process */
3872 file_count--;
3873 PyList_SetItem(procObj,1,
3874 PyInt_FromLong((long) file_count));
3875 }
3876 else
3877 {
3878 /* Last file for this process */
3879 if (result != EOF &&
3880 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
3881 {
3882 /* extract exit status */
3883 if (WIFEXITED(exit_code))
3884 {
3885 result = WEXITSTATUS(exit_code);
3886 }
3887 else
3888 {
3889 errno = EPIPE;
3890 result = -1;
3891 }
3892 }
3893 else
3894 {
3895 /* Indicate failure - this will cause the file object
3896 * to raise an I/O error and translate the last
3897 * error code from errno. We do have a problem with
3898 * last errors that overlap the normal errno table,
3899 * but that's a consistent problem with the file object.
3900 */
3901 result = -1;
3902 }
3903 }
3904
3905 /* Remove this file pointer from dictionary */
3906 PyDict_DelItem(_PyPopenProcs, fileObj);
3907
3908 if (PyDict_Size(_PyPopenProcs) == 0)
3909 {
3910 Py_DECREF(_PyPopenProcs);
3911 _PyPopenProcs = NULL;
3912 }
3913
3914 } /* if object retrieval ok */
3915
3916 Py_XDECREF(fileObj);
3917 } /* if _PyPopenProcs */
3918
3919#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003920 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003921#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003922 return result;
3923}
3924
3925#endif /* PYCC_??? */
3926
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003927#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003928
3929/*
3930 * Portable 'popen' replacement for Win32.
3931 *
3932 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
3933 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00003934 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003935 */
3936
3937#include <malloc.h>
3938#include <io.h>
3939#include <fcntl.h>
3940
3941/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
3942#define POPEN_1 1
3943#define POPEN_2 2
3944#define POPEN_3 3
3945#define POPEN_4 4
3946
3947static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00003948static int _PyPclose(FILE *file);
3949
3950/*
3951 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00003952 * for use when retrieving the process exit code. See _PyPclose() below
3953 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00003954 */
3955static PyObject *_PyPopenProcs = NULL;
3956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003957
3958/* popen that works from a GUI.
3959 *
3960 * The result of this function is a pipe (file) connected to the
3961 * processes stdin or stdout, depending on the requested mode.
3962 */
3963
3964static PyObject *
3965posix_popen(PyObject *self, PyObject *args)
3966{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00003967 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003968 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003969
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003970 char *cmdstring;
3971 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003972 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003973 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003974 return NULL;
3975
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003976 if (*mode == 'r')
3977 tm = _O_RDONLY;
3978 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00003979 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003980 return NULL;
3981 } else
3982 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00003983
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003984 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003985 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003986 return NULL;
3987 }
3988
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003989 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003990 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003991 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003992 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003993 else
3994 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
3995
3996 return f;
3997}
3998
3999/* Variation on win32pipe.popen
4000 *
4001 * The result of this function is a pipe (file) connected to the
4002 * process's stdin, and a pipe connected to the process's stdout.
4003 */
4004
4005static PyObject *
4006win32_popen2(PyObject *self, PyObject *args)
4007{
4008 PyObject *f;
4009 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004010
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004011 char *cmdstring;
4012 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004013 int bufsize = -1;
4014 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004015 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004016
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004017 if (*mode == 't')
4018 tm = _O_TEXT;
4019 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004020 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004021 return NULL;
4022 } else
4023 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004024
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004025 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004026 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004027 return NULL;
4028 }
4029
4030 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004031
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004032 return f;
4033}
4034
4035/*
4036 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004037 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004038 * The result of this function is 3 pipes - the process's stdin,
4039 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004040 */
4041
4042static PyObject *
4043win32_popen3(PyObject *self, PyObject *args)
4044{
4045 PyObject *f;
4046 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004047
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004048 char *cmdstring;
4049 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004050 int bufsize = -1;
4051 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004052 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004053
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004054 if (*mode == 't')
4055 tm = _O_TEXT;
4056 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004057 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004058 return NULL;
4059 } else
4060 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004061
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004062 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004063 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004064 return NULL;
4065 }
4066
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004067 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004068
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004069 return f;
4070}
4071
4072/*
4073 * Variation on win32pipe.popen
4074 *
Tim Peters5aa91602002-01-30 05:46:57 +00004075 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004076 * and stdout+stderr combined as a single pipe.
4077 */
4078
4079static PyObject *
4080win32_popen4(PyObject *self, PyObject *args)
4081{
4082 PyObject *f;
4083 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004084
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004085 char *cmdstring;
4086 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004087 int bufsize = -1;
4088 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004089 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004090
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004091 if (*mode == 't')
4092 tm = _O_TEXT;
4093 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004094 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004095 return NULL;
4096 } else
4097 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004098
4099 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004100 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004101 return NULL;
4102 }
4103
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004104 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004105
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004106 return f;
4107}
4108
Mark Hammond08501372001-01-31 07:30:29 +00004109static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004110_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004111 HANDLE hStdin,
4112 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004113 HANDLE hStderr,
4114 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004115{
4116 PROCESS_INFORMATION piProcInfo;
4117 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004118 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004119 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004120 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004121 int i;
4122 int x;
4123
4124 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004125 char *comshell;
4126
Tim Peters92e4dd82002-10-05 01:47:34 +00004127 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004128 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
4129 return x;
Tim Peters402d5982001-08-27 06:37:48 +00004130
4131 /* Explicitly check if we are using COMMAND.COM. If we are
4132 * then use the w9xpopen hack.
4133 */
4134 comshell = s1 + x;
4135 while (comshell >= s1 && *comshell != '\\')
4136 --comshell;
4137 ++comshell;
4138
4139 if (GetVersion() < 0x80000000 &&
4140 _stricmp(comshell, "command.com") != 0) {
4141 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004142 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004143 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004144 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004145 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004146 }
4147 else {
4148 /*
Tim Peters402d5982001-08-27 06:37:48 +00004149 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4150 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004151 */
Mark Hammond08501372001-01-31 07:30:29 +00004152 char modulepath[_MAX_PATH];
4153 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004154 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
4155 for (i = x = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004156 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004157 x = i+1;
4158 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004159 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004160 strncat(modulepath,
4161 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004162 (sizeof(modulepath)/sizeof(modulepath[0]))
4163 -strlen(modulepath));
4164 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004165 /* Eeek - file-not-found - possibly an embedding
4166 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004167 */
Tim Peters5aa91602002-01-30 05:46:57 +00004168 strncpy(modulepath,
4169 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00004170 sizeof(modulepath)/sizeof(modulepath[0]));
4171 if (modulepath[strlen(modulepath)-1] != '\\')
4172 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004173 strncat(modulepath,
4174 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004175 (sizeof(modulepath)/sizeof(modulepath[0]))
4176 -strlen(modulepath));
4177 /* No where else to look - raise an easily identifiable
4178 error, rather than leaving Windows to report
4179 "file not found" - as the user is probably blissfully
4180 unaware this shim EXE is used, and it will confuse them.
4181 (well, it confused me for a while ;-)
4182 */
4183 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004184 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004185 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004186 "for popen to work with your shell "
4187 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004188 szConsoleSpawn);
4189 return FALSE;
4190 }
4191 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004192 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004193 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004194 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004195
Tim Peters92e4dd82002-10-05 01:47:34 +00004196 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004197 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004198 /* To maintain correct argument passing semantics,
4199 we pass the command-line as it stands, and allow
4200 quoting to be applied. w9xpopen.exe will then
4201 use its argv vector, and re-quote the necessary
4202 args for the ultimate child process.
4203 */
Tim Peters75cdad52001-11-28 22:07:30 +00004204 PyOS_snprintf(
4205 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004206 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004207 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004208 s1,
4209 s3,
4210 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004211 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004212 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004213 dialog:
4214 "Your program accessed mem currently in use at xxx"
4215 and a hopeful warning about the stability of your
4216 system.
4217 Cost is Ctrl+C wont kill children, but anyone
4218 who cares can have a go!
4219 */
4220 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004221 }
4222 }
4223
4224 /* Could be an else here to try cmd.exe / command.com in the path
4225 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004226 else {
Tim Peters402d5982001-08-27 06:37:48 +00004227 PyErr_SetString(PyExc_RuntimeError,
4228 "Cannot locate a COMSPEC environment variable to "
4229 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004230 return FALSE;
4231 }
Tim Peters5aa91602002-01-30 05:46:57 +00004232
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004233 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4234 siStartInfo.cb = sizeof(STARTUPINFO);
4235 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4236 siStartInfo.hStdInput = hStdin;
4237 siStartInfo.hStdOutput = hStdout;
4238 siStartInfo.hStdError = hStderr;
4239 siStartInfo.wShowWindow = SW_HIDE;
4240
4241 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004242 s2,
4243 NULL,
4244 NULL,
4245 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004246 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004247 NULL,
4248 NULL,
4249 &siStartInfo,
4250 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004251 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004252 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004253
Mark Hammondb37a3732000-08-14 04:47:33 +00004254 /* Return process handle */
4255 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004256 return TRUE;
4257 }
Tim Peters402d5982001-08-27 06:37:48 +00004258 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004259 return FALSE;
4260}
4261
4262/* The following code is based off of KB: Q190351 */
4263
4264static PyObject *
4265_PyPopen(char *cmdstring, int mode, int n)
4266{
4267 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4268 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004269 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004270
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004271 SECURITY_ATTRIBUTES saAttr;
4272 BOOL fSuccess;
4273 int fd1, fd2, fd3;
4274 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004275 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004276 PyObject *f;
4277
4278 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4279 saAttr.bInheritHandle = TRUE;
4280 saAttr.lpSecurityDescriptor = NULL;
4281
4282 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4283 return win32_error("CreatePipe", NULL);
4284
4285 /* Create new output read handle and the input write handle. Set
4286 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004287 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004288 * being created. */
4289 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004290 GetCurrentProcess(), &hChildStdinWrDup, 0,
4291 FALSE,
4292 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004293 if (!fSuccess)
4294 return win32_error("DuplicateHandle", NULL);
4295
4296 /* Close the inheritable version of ChildStdin
4297 that we're using. */
4298 CloseHandle(hChildStdinWr);
4299
4300 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4301 return win32_error("CreatePipe", NULL);
4302
4303 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004304 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4305 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004306 if (!fSuccess)
4307 return win32_error("DuplicateHandle", NULL);
4308
4309 /* Close the inheritable version of ChildStdout
4310 that we're using. */
4311 CloseHandle(hChildStdoutRd);
4312
4313 if (n != POPEN_4) {
4314 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4315 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004316 fSuccess = DuplicateHandle(GetCurrentProcess(),
4317 hChildStderrRd,
4318 GetCurrentProcess(),
4319 &hChildStderrRdDup, 0,
4320 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004321 if (!fSuccess)
4322 return win32_error("DuplicateHandle", NULL);
4323 /* Close the inheritable version of ChildStdErr that we're using. */
4324 CloseHandle(hChildStderrRd);
4325 }
Tim Peters5aa91602002-01-30 05:46:57 +00004326
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004327 switch (n) {
4328 case POPEN_1:
4329 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4330 case _O_WRONLY | _O_TEXT:
4331 /* Case for writing to child Stdin in text mode. */
4332 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4333 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004334 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004335 PyFile_SetBufSize(f, 0);
4336 /* We don't care about these pipes anymore, so close them. */
4337 CloseHandle(hChildStdoutRdDup);
4338 CloseHandle(hChildStderrRdDup);
4339 break;
4340
4341 case _O_RDONLY | _O_TEXT:
4342 /* Case for reading from child Stdout in text mode. */
4343 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4344 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004345 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004346 PyFile_SetBufSize(f, 0);
4347 /* We don't care about these pipes anymore, so close them. */
4348 CloseHandle(hChildStdinWrDup);
4349 CloseHandle(hChildStderrRdDup);
4350 break;
4351
4352 case _O_RDONLY | _O_BINARY:
4353 /* Case for readinig from child Stdout in binary mode. */
4354 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4355 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004356 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004357 PyFile_SetBufSize(f, 0);
4358 /* We don't care about these pipes anymore, so close them. */
4359 CloseHandle(hChildStdinWrDup);
4360 CloseHandle(hChildStderrRdDup);
4361 break;
4362
4363 case _O_WRONLY | _O_BINARY:
4364 /* Case for writing to child Stdin in binary mode. */
4365 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4366 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004367 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004368 PyFile_SetBufSize(f, 0);
4369 /* We don't care about these pipes anymore, so close them. */
4370 CloseHandle(hChildStdoutRdDup);
4371 CloseHandle(hChildStderrRdDup);
4372 break;
4373 }
Mark Hammondb37a3732000-08-14 04:47:33 +00004374 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004375 break;
Tim Peters5aa91602002-01-30 05:46:57 +00004376
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004377 case POPEN_2:
4378 case POPEN_4:
4379 {
4380 char *m1, *m2;
4381 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00004382
Tim Peters7dca21e2002-08-19 00:42:29 +00004383 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004384 m1 = "r";
4385 m2 = "w";
4386 } else {
4387 m1 = "rb";
4388 m2 = "wb";
4389 }
4390
4391 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4392 f1 = _fdopen(fd1, m2);
4393 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4394 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004395 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004396 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00004397 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004398 PyFile_SetBufSize(p2, 0);
4399
4400 if (n != 4)
4401 CloseHandle(hChildStderrRdDup);
4402
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004403 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00004404 Py_XDECREF(p1);
4405 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00004406 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004407 break;
4408 }
Tim Peters5aa91602002-01-30 05:46:57 +00004409
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004410 case POPEN_3:
4411 {
4412 char *m1, *m2;
4413 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00004414
Tim Peters7dca21e2002-08-19 00:42:29 +00004415 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004416 m1 = "r";
4417 m2 = "w";
4418 } else {
4419 m1 = "rb";
4420 m2 = "wb";
4421 }
4422
4423 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4424 f1 = _fdopen(fd1, m2);
4425 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4426 f2 = _fdopen(fd2, m1);
4427 fd3 = _open_osfhandle((long)hChildStderrRdDup, mode);
4428 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004429 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00004430 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
4431 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004432 PyFile_SetBufSize(p1, 0);
4433 PyFile_SetBufSize(p2, 0);
4434 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004435 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00004436 Py_XDECREF(p1);
4437 Py_XDECREF(p2);
4438 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00004439 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004440 break;
4441 }
4442 }
4443
4444 if (n == POPEN_4) {
4445 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004446 hChildStdinRd,
4447 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004448 hChildStdoutWr,
4449 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004450 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004451 }
4452 else {
4453 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004454 hChildStdinRd,
4455 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004456 hChildStderrWr,
4457 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004458 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004459 }
4460
Mark Hammondb37a3732000-08-14 04:47:33 +00004461 /*
4462 * Insert the files we've created into the process dictionary
4463 * all referencing the list with the process handle and the
4464 * initial number of files (see description below in _PyPclose).
4465 * Since if _PyPclose later tried to wait on a process when all
4466 * handles weren't closed, it could create a deadlock with the
4467 * child, we spend some energy here to try to ensure that we
4468 * either insert all file handles into the dictionary or none
4469 * at all. It's a little clumsy with the various popen modes
4470 * and variable number of files involved.
4471 */
4472 if (!_PyPopenProcs) {
4473 _PyPopenProcs = PyDict_New();
4474 }
4475
4476 if (_PyPopenProcs) {
4477 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
4478 int ins_rc[3];
4479
4480 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4481 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4482
4483 procObj = PyList_New(2);
4484 hProcessObj = PyLong_FromVoidPtr(hProcess);
4485 intObj = PyInt_FromLong(file_count);
4486
4487 if (procObj && hProcessObj && intObj) {
4488 PyList_SetItem(procObj,0,hProcessObj);
4489 PyList_SetItem(procObj,1,intObj);
4490
4491 fileObj[0] = PyLong_FromVoidPtr(f1);
4492 if (fileObj[0]) {
4493 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4494 fileObj[0],
4495 procObj);
4496 }
4497 if (file_count >= 2) {
4498 fileObj[1] = PyLong_FromVoidPtr(f2);
4499 if (fileObj[1]) {
4500 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4501 fileObj[1],
4502 procObj);
4503 }
4504 }
4505 if (file_count >= 3) {
4506 fileObj[2] = PyLong_FromVoidPtr(f3);
4507 if (fileObj[2]) {
4508 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4509 fileObj[2],
4510 procObj);
4511 }
4512 }
4513
4514 if (ins_rc[0] < 0 || !fileObj[0] ||
4515 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4516 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
4517 /* Something failed - remove any dictionary
4518 * entries that did make it.
4519 */
4520 if (!ins_rc[0] && fileObj[0]) {
4521 PyDict_DelItem(_PyPopenProcs,
4522 fileObj[0]);
4523 }
4524 if (!ins_rc[1] && fileObj[1]) {
4525 PyDict_DelItem(_PyPopenProcs,
4526 fileObj[1]);
4527 }
4528 if (!ins_rc[2] && fileObj[2]) {
4529 PyDict_DelItem(_PyPopenProcs,
4530 fileObj[2]);
4531 }
4532 }
4533 }
Tim Peters5aa91602002-01-30 05:46:57 +00004534
Mark Hammondb37a3732000-08-14 04:47:33 +00004535 /*
4536 * Clean up our localized references for the dictionary keys
4537 * and value since PyDict_SetItem will Py_INCREF any copies
4538 * that got placed in the dictionary.
4539 */
4540 Py_XDECREF(procObj);
4541 Py_XDECREF(fileObj[0]);
4542 Py_XDECREF(fileObj[1]);
4543 Py_XDECREF(fileObj[2]);
4544 }
4545
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004546 /* Child is launched. Close the parents copy of those pipe
4547 * handles that only the child should have open. You need to
4548 * make sure that no handles to the write end of the output pipe
4549 * are maintained in this process or else the pipe will not close
4550 * when the child process exits and the ReadFile will hang. */
4551
4552 if (!CloseHandle(hChildStdinRd))
4553 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004554
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004555 if (!CloseHandle(hChildStdoutWr))
4556 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004557
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004558 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
4559 return win32_error("CloseHandle", NULL);
4560
4561 return f;
4562}
Fredrik Lundh56055a42000-07-23 19:47:12 +00004563
4564/*
4565 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4566 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00004567 *
4568 * This function uses the _PyPopenProcs dictionary in order to map the
4569 * input file pointer to information about the process that was
4570 * originally created by the popen* call that created the file pointer.
4571 * The dictionary uses the file pointer as a key (with one entry
4572 * inserted for each file returned by the original popen* call) and a
4573 * single list object as the value for all files from a single call.
4574 * The list object contains the Win32 process handle at [0], and a file
4575 * count at [1], which is initialized to the total number of file
4576 * handles using that list.
4577 *
4578 * This function closes whichever handle it is passed, and decrements
4579 * the file count in the dictionary for the process handle pointed to
4580 * by this file. On the last close (when the file count reaches zero),
4581 * this function will wait for the child process and then return its
4582 * exit code as the result of the close() operation. This permits the
4583 * files to be closed in any order - it is always the close() of the
4584 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004585 *
4586 * NOTE: This function is currently called with the GIL released.
4587 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004588 */
Tim Peters736aa322000-09-01 06:51:24 +00004589
Fredrik Lundh56055a42000-07-23 19:47:12 +00004590static int _PyPclose(FILE *file)
4591{
Fredrik Lundh20318932000-07-26 17:29:12 +00004592 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004593 DWORD exit_code;
4594 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00004595 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
4596 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00004597#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004598 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00004599#endif
4600
Fredrik Lundh20318932000-07-26 17:29:12 +00004601 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00004602 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00004603 */
4604 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00004605#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004606 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00004607#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004608 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00004609 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4610 (procObj = PyDict_GetItem(_PyPopenProcs,
4611 fileObj)) != NULL &&
4612 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
4613 (intObj = PyList_GetItem(procObj,1)) != NULL) {
4614
4615 hProcess = PyLong_AsVoidPtr(hProcessObj);
4616 file_count = PyInt_AsLong(intObj);
4617
4618 if (file_count > 1) {
4619 /* Still other files referencing process */
4620 file_count--;
4621 PyList_SetItem(procObj,1,
4622 PyInt_FromLong(file_count));
4623 } else {
4624 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00004625 if (result != EOF &&
4626 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
4627 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00004628 /* Possible truncation here in 16-bit environments, but
4629 * real exit codes are just the lower byte in any event.
4630 */
4631 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004632 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00004633 /* Indicate failure - this will cause the file object
4634 * to raise an I/O error and translate the last Win32
4635 * error code from errno. We do have a problem with
4636 * last errors that overlap the normal errno table,
4637 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004638 */
Fredrik Lundh20318932000-07-26 17:29:12 +00004639 if (result != EOF) {
4640 /* If the error wasn't from the fclose(), then
4641 * set errno for the file object error handling.
4642 */
4643 errno = GetLastError();
4644 }
4645 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004646 }
4647
4648 /* Free up the native handle at this point */
4649 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00004650 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00004651
Mark Hammondb37a3732000-08-14 04:47:33 +00004652 /* Remove this file pointer from dictionary */
4653 PyDict_DelItem(_PyPopenProcs, fileObj);
4654
4655 if (PyDict_Size(_PyPopenProcs) == 0) {
4656 Py_DECREF(_PyPopenProcs);
4657 _PyPopenProcs = NULL;
4658 }
4659
4660 } /* if object retrieval ok */
4661
4662 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004663 } /* if _PyPopenProcs */
4664
Tim Peters736aa322000-09-01 06:51:24 +00004665#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004666 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00004667#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004668 return result;
4669}
Tim Peters9acdd3a2000-09-01 19:26:36 +00004670
4671#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00004672static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004673posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00004674{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004675 char *name;
4676 char *mode = "r";
4677 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00004678 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00004679 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004680 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00004681 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00004682 /* Strip mode of binary or text modifiers */
4683 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
4684 mode = "r";
4685 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
4686 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00004687 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004688 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004689 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00004690 if (fp == NULL)
4691 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004692 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004693 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00004694 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004695 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00004696}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004697
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004698#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004699#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00004700
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004701
Guido van Rossumb6775db1994-08-01 11:34:53 +00004702#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004703PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004704"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004705Set the current process's user id.");
4706
Barry Warsaw53699e91996-12-10 23:23:01 +00004707static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004708posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004709{
4710 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004711 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004712 return NULL;
4713 if (setuid(uid) < 0)
4714 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004715 Py_INCREF(Py_None);
4716 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004717}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004718#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004719
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004720
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004721#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004722PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004723"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004724Set the current process's effective user id.");
4725
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004726static PyObject *
4727posix_seteuid (PyObject *self, PyObject *args)
4728{
4729 int euid;
4730 if (!PyArg_ParseTuple(args, "i", &euid)) {
4731 return NULL;
4732 } else if (seteuid(euid) < 0) {
4733 return posix_error();
4734 } else {
4735 Py_INCREF(Py_None);
4736 return Py_None;
4737 }
4738}
4739#endif /* HAVE_SETEUID */
4740
4741#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004742PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004743"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004744Set the current process's effective group id.");
4745
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004746static PyObject *
4747posix_setegid (PyObject *self, PyObject *args)
4748{
4749 int egid;
4750 if (!PyArg_ParseTuple(args, "i", &egid)) {
4751 return NULL;
4752 } else if (setegid(egid) < 0) {
4753 return posix_error();
4754 } else {
4755 Py_INCREF(Py_None);
4756 return Py_None;
4757 }
4758}
4759#endif /* HAVE_SETEGID */
4760
4761#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004762PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004763"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004764Set the current process's real and effective user ids.");
4765
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004766static PyObject *
4767posix_setreuid (PyObject *self, PyObject *args)
4768{
4769 int ruid, euid;
4770 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4771 return NULL;
4772 } else if (setreuid(ruid, euid) < 0) {
4773 return posix_error();
4774 } else {
4775 Py_INCREF(Py_None);
4776 return Py_None;
4777 }
4778}
4779#endif /* HAVE_SETREUID */
4780
4781#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004782PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004783"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004784Set the current process's real and effective group ids.");
4785
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004786static PyObject *
4787posix_setregid (PyObject *self, PyObject *args)
4788{
4789 int rgid, egid;
4790 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4791 return NULL;
4792 } else if (setregid(rgid, egid) < 0) {
4793 return posix_error();
4794 } else {
4795 Py_INCREF(Py_None);
4796 return Py_None;
4797 }
4798}
4799#endif /* HAVE_SETREGID */
4800
Guido van Rossumb6775db1994-08-01 11:34:53 +00004801#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004802PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004803"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004804Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004805
Barry Warsaw53699e91996-12-10 23:23:01 +00004806static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004807posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004808{
4809 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004810 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004811 return NULL;
4812 if (setgid(gid) < 0)
4813 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004814 Py_INCREF(Py_None);
4815 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004816}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004817#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004818
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004819#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004820PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004821"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004822Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004823
4824static PyObject *
4825posix_setgroups(PyObject *self, PyObject *args)
4826{
4827 PyObject *groups;
4828 int i, len;
4829 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004830
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004831 if (!PyArg_ParseTuple(args, "O:setgid", &groups))
4832 return NULL;
4833 if (!PySequence_Check(groups)) {
4834 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4835 return NULL;
4836 }
4837 len = PySequence_Size(groups);
4838 if (len > MAX_GROUPS) {
4839 PyErr_SetString(PyExc_ValueError, "too many groups");
4840 return NULL;
4841 }
4842 for(i = 0; i < len; i++) {
4843 PyObject *elem;
4844 elem = PySequence_GetItem(groups, i);
4845 if (!elem)
4846 return NULL;
4847 if (!PyInt_Check(elem)) {
4848 PyErr_SetString(PyExc_TypeError,
4849 "groups must be integers");
4850 Py_DECREF(elem);
4851 return NULL;
4852 }
4853 /* XXX: check that value fits into gid_t. */
4854 grouplist[i] = PyInt_AsLong(elem);
4855 Py_DECREF(elem);
4856 }
4857
4858 if (setgroups(len, grouplist) < 0)
4859 return posix_error();
4860 Py_INCREF(Py_None);
4861 return Py_None;
4862}
4863#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004864
Guido van Rossumb6775db1994-08-01 11:34:53 +00004865#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004866PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004867"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004868Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004869
Barry Warsaw53699e91996-12-10 23:23:01 +00004870static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004871posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004872{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004873 int pid, options;
4874#ifdef UNION_WAIT
4875 union wait status;
4876#define status_i (status.w_status)
4877#else
4878 int status;
4879#define status_i status
4880#endif
4881 status_i = 0;
4882
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004883 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004884 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004885 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004886 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004887 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004888 if (pid == -1)
4889 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00004890 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004891 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00004892}
4893
Tim Petersab034fa2002-02-01 11:27:43 +00004894#elif defined(HAVE_CWAIT)
4895
4896/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004897PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004898"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004899"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004900
4901static PyObject *
4902posix_waitpid(PyObject *self, PyObject *args)
4903{
4904 int pid, options;
4905 int status;
4906
4907 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4908 return NULL;
4909 Py_BEGIN_ALLOW_THREADS
4910 pid = _cwait(&status, pid, options);
4911 Py_END_ALLOW_THREADS
4912 if (pid == -1)
4913 return posix_error();
4914 else
4915 /* shift the status left a byte so this is more like the
4916 POSIX waitpid */
4917 return Py_BuildValue("ii", pid, status << 8);
4918}
4919#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004920
Guido van Rossumad0ee831995-03-01 10:34:45 +00004921#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004922PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004923"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004924Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004925
Barry Warsaw53699e91996-12-10 23:23:01 +00004926static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004927posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004928{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004929 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004930#ifdef UNION_WAIT
4931 union wait status;
4932#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004933#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004934 int status;
4935#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004936#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00004937
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004938 status_i = 0;
4939 Py_BEGIN_ALLOW_THREADS
4940 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004941 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004942 if (pid == -1)
4943 return posix_error();
4944 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004945 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004946#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00004947}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004948#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004949
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004950
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004951PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004952"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004953Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004954
Barry Warsaw53699e91996-12-10 23:23:01 +00004955static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004956posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004957{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004958#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004959 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004960#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004961#ifdef MS_WINDOWS
Mark Hammond7edd0a92003-08-06 02:46:58 +00004962 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004963#else
4964 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4965#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004966#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004967}
4968
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004969
Guido van Rossumb6775db1994-08-01 11:34:53 +00004970#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004971PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004972"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004973Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004974
Barry Warsaw53699e91996-12-10 23:23:01 +00004975static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004976posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004977{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004978 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004979 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004980 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004981 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004982 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004983 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004984 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004985 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004986 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00004987 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00004988 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004989}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004990#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004991
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004992
Guido van Rossumb6775db1994-08-01 11:34:53 +00004993#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004994PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004995"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004996Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004997
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004999posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005000{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005001 return posix_2str(args, "etet:symlink", symlink, NULL, NULL);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005002}
5003#endif /* HAVE_SYMLINK */
5004
5005
5006#ifdef HAVE_TIMES
5007#ifndef HZ
5008#define HZ 60 /* Universal constant :-) */
5009#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005010
Guido van Rossumd48f2521997-12-05 22:19:34 +00005011#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5012static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005013system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005014{
5015 ULONG value = 0;
5016
5017 Py_BEGIN_ALLOW_THREADS
5018 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5019 Py_END_ALLOW_THREADS
5020
5021 return value;
5022}
5023
5024static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005025posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005026{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005027 /* Currently Only Uptime is Provided -- Others Later */
5028 return Py_BuildValue("ddddd",
5029 (double)0 /* t.tms_utime / HZ */,
5030 (double)0 /* t.tms_stime / HZ */,
5031 (double)0 /* t.tms_cutime / HZ */,
5032 (double)0 /* t.tms_cstime / HZ */,
5033 (double)system_uptime() / 1000);
5034}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005035#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005036static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005037posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005038{
5039 struct tms t;
5040 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005041 errno = 0;
5042 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005043 if (c == (clock_t) -1)
5044 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005045 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005046 (double)t.tms_utime / HZ,
5047 (double)t.tms_stime / HZ,
5048 (double)t.tms_cutime / HZ,
5049 (double)t.tms_cstime / HZ,
5050 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005051}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005052#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005053#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005054
5055
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005056#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005057#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005058static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005059posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005060{
5061 FILETIME create, exit, kernel, user;
5062 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005063 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005064 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5065 /* The fields of a FILETIME structure are the hi and lo part
5066 of a 64-bit value expressed in 100 nanosecond units.
5067 1e7 is one second in such units; 1e-7 the inverse.
5068 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5069 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005070 return Py_BuildValue(
5071 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005072 (double)(kernel.dwHighDateTime*429.4967296 +
5073 kernel.dwLowDateTime*1e-7),
5074 (double)(user.dwHighDateTime*429.4967296 +
5075 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005076 (double)0,
5077 (double)0,
5078 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005079}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005080#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005081
5082#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005083PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005084"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005085Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005086#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005087
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005088
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005089#ifdef HAVE_GETSID
5090PyDoc_STRVAR(posix_getsid__doc__,
5091"getsid(pid) -> sid\n\n\
5092Call the system call getsid().");
5093
5094static PyObject *
5095posix_getsid(PyObject *self, PyObject *args)
5096{
5097 int pid, sid;
5098 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5099 return NULL;
5100 sid = getsid(pid);
5101 if (sid < 0)
5102 return posix_error();
5103 return PyInt_FromLong((long)sid);
5104}
5105#endif /* HAVE_GETSID */
5106
5107
Guido van Rossumb6775db1994-08-01 11:34:53 +00005108#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005109PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005110"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005111Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005112
Barry Warsaw53699e91996-12-10 23:23:01 +00005113static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005114posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005115{
Guido van Rossum687dd131993-05-17 08:34:16 +00005116 if (setsid() < 0)
5117 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005118 Py_INCREF(Py_None);
5119 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005120}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005121#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005122
Guido van Rossumb6775db1994-08-01 11:34:53 +00005123#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005124PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005125"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005126Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005127
Barry Warsaw53699e91996-12-10 23:23:01 +00005128static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005129posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005130{
5131 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005132 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005133 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005134 if (setpgid(pid, pgrp) < 0)
5135 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005136 Py_INCREF(Py_None);
5137 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005138}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005139#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005140
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005141
Guido van Rossumb6775db1994-08-01 11:34:53 +00005142#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005143PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005144"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005145Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005146
Barry Warsaw53699e91996-12-10 23:23:01 +00005147static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005148posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005149{
5150 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005151 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005152 return NULL;
5153 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005154 if (pgid < 0)
5155 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005156 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005157}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005158#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005159
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005160
Guido van Rossumb6775db1994-08-01 11:34:53 +00005161#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005162PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005163"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005164Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005165
Barry Warsaw53699e91996-12-10 23:23:01 +00005166static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005167posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005168{
5169 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005170 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005171 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005172 if (tcsetpgrp(fd, pgid) < 0)
5173 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005174 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005175 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005176}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005177#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005178
Guido van Rossum687dd131993-05-17 08:34:16 +00005179/* Functions acting on file descriptors */
5180
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005181PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005182"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005183Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005184
Barry Warsaw53699e91996-12-10 23:23:01 +00005185static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005186posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005187{
Mark Hammondef8b6542001-05-13 08:04:26 +00005188 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005189 int flag;
5190 int mode = 0777;
5191 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005192
5193#ifdef MS_WINDOWS
5194 if (unicode_file_names()) {
5195 PyUnicodeObject *po;
5196 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5197 Py_BEGIN_ALLOW_THREADS
5198 /* PyUnicode_AS_UNICODE OK without thread
5199 lock as it is a simple dereference. */
5200 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5201 Py_END_ALLOW_THREADS
5202 if (fd < 0)
5203 return posix_error();
5204 return PyInt_FromLong((long)fd);
5205 }
5206 /* Drop the argument parsing error as narrow strings
5207 are also valid. */
5208 PyErr_Clear();
5209 }
5210#endif
5211
Tim Peters5aa91602002-01-30 05:46:57 +00005212 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00005213 Py_FileSystemDefaultEncoding, &file,
5214 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00005215 return NULL;
5216
Barry Warsaw53699e91996-12-10 23:23:01 +00005217 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005218 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005219 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005220 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00005221 return posix_error_with_allocated_filename(file);
5222 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00005223 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005224}
5225
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005226
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005227PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005228"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005229Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005230
Barry Warsaw53699e91996-12-10 23:23:01 +00005231static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005232posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005233{
5234 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005235 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005236 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005237 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005238 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005239 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005240 if (res < 0)
5241 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005242 Py_INCREF(Py_None);
5243 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005244}
5245
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005246
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005247PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005248"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005249Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005250
Barry Warsaw53699e91996-12-10 23:23:01 +00005251static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005252posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005253{
5254 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005255 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005256 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005257 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005258 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005259 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005260 if (fd < 0)
5261 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005262 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005263}
5264
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005265
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005266PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005267"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005268Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005269
Barry Warsaw53699e91996-12-10 23:23:01 +00005270static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005271posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005272{
5273 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005274 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005275 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005276 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005277 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005278 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005279 if (res < 0)
5280 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005281 Py_INCREF(Py_None);
5282 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005283}
5284
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005285
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005286PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005287"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005288Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005289
Barry Warsaw53699e91996-12-10 23:23:01 +00005290static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005291posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005292{
5293 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005294#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005295 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005296#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005297 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005298#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005299 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005300 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005301 return NULL;
5302#ifdef SEEK_SET
5303 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5304 switch (how) {
5305 case 0: how = SEEK_SET; break;
5306 case 1: how = SEEK_CUR; break;
5307 case 2: how = SEEK_END; break;
5308 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005309#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005310
5311#if !defined(HAVE_LARGEFILE_SUPPORT)
5312 pos = PyInt_AsLong(posobj);
5313#else
5314 pos = PyLong_Check(posobj) ?
5315 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
5316#endif
5317 if (PyErr_Occurred())
5318 return NULL;
5319
Barry Warsaw53699e91996-12-10 23:23:01 +00005320 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005321#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005322 res = _lseeki64(fd, pos, how);
5323#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005324 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005325#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005326 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005327 if (res < 0)
5328 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005329
5330#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00005331 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005332#else
5333 return PyLong_FromLongLong(res);
5334#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005335}
5336
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005337
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005338PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005339"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005340Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005341
Barry Warsaw53699e91996-12-10 23:23:01 +00005342static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005343posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005344{
Guido van Rossum8bac5461996-06-11 18:38:48 +00005345 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005346 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005347 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005348 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005349 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005350 if (buffer == NULL)
5351 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005352 Py_BEGIN_ALLOW_THREADS
5353 n = read(fd, PyString_AsString(buffer), size);
5354 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005355 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005356 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005357 return posix_error();
5358 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005359 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00005360 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005361 return buffer;
5362}
5363
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005364
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005365PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005366"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005367Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005368
Barry Warsaw53699e91996-12-10 23:23:01 +00005369static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005370posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005371{
5372 int fd, size;
5373 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005374 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005375 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005376 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005377 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005378 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005379 if (size < 0)
5380 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005381 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005382}
5383
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005384
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005385PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005386"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005387Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005388
Barry Warsaw53699e91996-12-10 23:23:01 +00005389static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005390posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005391{
5392 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005393 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005394 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005395 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005396 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005397#ifdef __VMS
5398 /* on OpenVMS we must ensure that all bytes are written to the file */
5399 fsync(fd);
5400#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005401 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005402 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005403 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005404 if (res != 0)
5405 return posix_error();
Tim Peters5aa91602002-01-30 05:46:57 +00005406
Fred Drake699f3522000-06-29 21:12:41 +00005407 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005408}
5409
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005410
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005411PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005412"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005413Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005414
Barry Warsaw53699e91996-12-10 23:23:01 +00005415static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005416posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005417{
Guido van Rossum687dd131993-05-17 08:34:16 +00005418 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005419 char *mode = "r";
5420 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00005421 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005422 PyObject *f;
5423 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00005424 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00005425
Thomas Heller1f043e22002-11-07 16:00:59 +00005426 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
5427 PyErr_Format(PyExc_ValueError,
5428 "invalid file mode '%s'", mode);
5429 return NULL;
5430 }
5431
Barry Warsaw53699e91996-12-10 23:23:01 +00005432 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005433 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005434 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005435 if (fp == NULL)
5436 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00005437 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005438 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005439 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005440 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00005441}
5442
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005443PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005444"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005445Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005446connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005447
5448static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005449posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005450{
5451 int fd;
5452 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5453 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005454 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005455}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005456
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005457#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005458PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005459"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005460Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005461
Barry Warsaw53699e91996-12-10 23:23:01 +00005462static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005463posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005464{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005465#if defined(PYOS_OS2)
5466 HFILE read, write;
5467 APIRET rc;
5468
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005469 Py_BEGIN_ALLOW_THREADS
5470 rc = DosCreatePipe( &read, &write, 4096);
5471 Py_END_ALLOW_THREADS
5472 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005473 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005474
5475 return Py_BuildValue("(ii)", read, write);
5476#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005477#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005478 int fds[2];
5479 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005480 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005481 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005482 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005483 if (res != 0)
5484 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005485 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005486#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005487 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005488 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005489 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005490 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005491 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005492 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005493 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005494 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005495 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5496 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005497 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005498#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005499#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005500}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005501#endif /* HAVE_PIPE */
5502
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005503
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005504#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005505PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005506"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005507Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005508
Barry Warsaw53699e91996-12-10 23:23:01 +00005509static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005510posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005511{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005512 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005513 int mode = 0666;
5514 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005515 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005516 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005517 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005518 res = mkfifo(filename, mode);
5519 Py_END_ALLOW_THREADS
5520 if (res < 0)
5521 return posix_error();
5522 Py_INCREF(Py_None);
5523 return Py_None;
5524}
5525#endif
5526
5527
Neal Norwitz11690112002-07-30 01:08:28 +00005528#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005529PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005530"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005531Create a filesystem node (file, device special file or named pipe)\n\
5532named filename. mode specifies both the permissions to use and the\n\
5533type of node to be created, being combined (bitwise OR) with one of\n\
5534S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005535device defines the newly created device special file (probably using\n\
5536os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005537
5538
5539static PyObject *
5540posix_mknod(PyObject *self, PyObject *args)
5541{
5542 char *filename;
5543 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005544 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005545 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005546 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005547 return NULL;
5548 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005549 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005550 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005551 if (res < 0)
5552 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005553 Py_INCREF(Py_None);
5554 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005555}
5556#endif
5557
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005558#ifdef HAVE_DEVICE_MACROS
5559PyDoc_STRVAR(posix_major__doc__,
5560"major(device) -> major number\n\
5561Extracts a device major number from a raw device number.");
5562
5563static PyObject *
5564posix_major(PyObject *self, PyObject *args)
5565{
5566 int device;
5567 if (!PyArg_ParseTuple(args, "i:major", &device))
5568 return NULL;
5569 return PyInt_FromLong((long)major(device));
5570}
5571
5572PyDoc_STRVAR(posix_minor__doc__,
5573"minor(device) -> minor number\n\
5574Extracts a device minor number from a raw device number.");
5575
5576static PyObject *
5577posix_minor(PyObject *self, PyObject *args)
5578{
5579 int device;
5580 if (!PyArg_ParseTuple(args, "i:minor", &device))
5581 return NULL;
5582 return PyInt_FromLong((long)minor(device));
5583}
5584
5585PyDoc_STRVAR(posix_makedev__doc__,
5586"makedev(major, minor) -> device number\n\
5587Composes a raw device number from the major and minor device numbers.");
5588
5589static PyObject *
5590posix_makedev(PyObject *self, PyObject *args)
5591{
5592 int major, minor;
5593 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5594 return NULL;
5595 return PyInt_FromLong((long)makedev(major, minor));
5596}
5597#endif /* device macros */
5598
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005599
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005600#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005601PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005602"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005603Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005604
Barry Warsaw53699e91996-12-10 23:23:01 +00005605static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005606posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005607{
5608 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005609 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005610 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005611 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005612
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005613 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005614 return NULL;
5615
5616#if !defined(HAVE_LARGEFILE_SUPPORT)
5617 length = PyInt_AsLong(lenobj);
5618#else
5619 length = PyLong_Check(lenobj) ?
5620 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
5621#endif
5622 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005623 return NULL;
5624
Barry Warsaw53699e91996-12-10 23:23:01 +00005625 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005626 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005627 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005628 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005629 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005630 return NULL;
5631 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005632 Py_INCREF(Py_None);
5633 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005634}
5635#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005636
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005637#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005638PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005639"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005640Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005641
Fred Drake762e2061999-08-26 17:23:54 +00005642/* Save putenv() parameters as values here, so we can collect them when they
5643 * get re-set with another call for the same key. */
5644static PyObject *posix_putenv_garbage;
5645
Tim Peters5aa91602002-01-30 05:46:57 +00005646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005647posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005648{
5649 char *s1, *s2;
5650 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00005651 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005652 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005653
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005654 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005655 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005656
5657#if defined(PYOS_OS2)
5658 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5659 APIRET rc;
5660
Guido van Rossumd48f2521997-12-05 22:19:34 +00005661 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5662 if (rc != NO_ERROR)
5663 return os2_error(rc);
5664
5665 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5666 APIRET rc;
5667
Guido van Rossumd48f2521997-12-05 22:19:34 +00005668 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5669 if (rc != NO_ERROR)
5670 return os2_error(rc);
5671 } else {
5672#endif
5673
Fred Drake762e2061999-08-26 17:23:54 +00005674 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005675 len = strlen(s1) + strlen(s2) + 2;
5676 /* len includes space for a trailing \0; the size arg to
5677 PyString_FromStringAndSize does not count that */
5678 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005679 if (newstr == NULL)
5680 return PyErr_NoMemory();
5681 new = PyString_AS_STRING(newstr);
Tim Petersc8996f52001-12-03 20:41:00 +00005682 PyOS_snprintf(new, len, "%s=%s", s1, s2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005683 if (putenv(new)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005684 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005685 posix_error();
5686 return NULL;
5687 }
Fred Drake762e2061999-08-26 17:23:54 +00005688 /* Install the first arg and newstr in posix_putenv_garbage;
5689 * this will cause previous value to be collected. This has to
5690 * happen after the real putenv() call because the old value
5691 * was still accessible until then. */
5692 if (PyDict_SetItem(posix_putenv_garbage,
5693 PyTuple_GET_ITEM(args, 0), newstr)) {
5694 /* really not much we can do; just leak */
5695 PyErr_Clear();
5696 }
5697 else {
5698 Py_DECREF(newstr);
5699 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005700
5701#if defined(PYOS_OS2)
5702 }
5703#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005704 Py_INCREF(Py_None);
5705 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005706}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005707#endif /* putenv */
5708
Guido van Rossumc524d952001-10-19 01:31:59 +00005709#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005710PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005711"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005712Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005713
5714static PyObject *
5715posix_unsetenv(PyObject *self, PyObject *args)
5716{
5717 char *s1;
5718
5719 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5720 return NULL;
5721
5722 unsetenv(s1);
5723
5724 /* Remove the key from posix_putenv_garbage;
5725 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005726 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005727 * old value was still accessible until then.
5728 */
5729 if (PyDict_DelItem(posix_putenv_garbage,
5730 PyTuple_GET_ITEM(args, 0))) {
5731 /* really not much we can do; just leak */
5732 PyErr_Clear();
5733 }
5734
5735 Py_INCREF(Py_None);
5736 return Py_None;
5737}
5738#endif /* unsetenv */
5739
Guido van Rossumb6a47161997-09-15 22:54:34 +00005740#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005741PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005742"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005743Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005744
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005745static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005746posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005747{
5748 int code;
5749 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005750 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005751 return NULL;
5752 message = strerror(code);
5753 if (message == NULL) {
5754 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005755 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005756 return NULL;
5757 }
5758 return PyString_FromString(message);
5759}
5760#endif /* strerror */
5761
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005762
Guido van Rossumc9641791998-08-04 15:26:23 +00005763#ifdef HAVE_SYS_WAIT_H
5764
Fred Drake106c1a02002-04-23 15:58:02 +00005765#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005766PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005767"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005768Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005769
5770static PyObject *
5771posix_WCOREDUMP(PyObject *self, PyObject *args)
5772{
5773#ifdef UNION_WAIT
5774 union wait status;
5775#define status_i (status.w_status)
5776#else
5777 int status;
5778#define status_i status
5779#endif
5780 status_i = 0;
5781
5782 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i))
5783 {
5784 return NULL;
5785 }
5786
5787 return PyBool_FromLong(WCOREDUMP(status));
5788#undef status_i
5789}
5790#endif /* WCOREDUMP */
5791
5792#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005793PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005794"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005795Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005796job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005797
5798static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005799posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005800{
5801#ifdef UNION_WAIT
5802 union wait status;
5803#define status_i (status.w_status)
5804#else
5805 int status;
5806#define status_i status
5807#endif
5808 status_i = 0;
5809
5810 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i))
5811 {
5812 return NULL;
5813 }
5814
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005815 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005816#undef status_i
5817}
5818#endif /* WIFCONTINUED */
5819
Guido van Rossumc9641791998-08-04 15:26:23 +00005820#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005821PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005822"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005823Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005824
5825static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005826posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005827{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005828#ifdef UNION_WAIT
5829 union wait status;
5830#define status_i (status.w_status)
5831#else
5832 int status;
5833#define status_i status
5834#endif
5835 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005836
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005837 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005838 {
5839 return NULL;
5840 }
Tim Peters5aa91602002-01-30 05:46:57 +00005841
Fred Drake106c1a02002-04-23 15:58:02 +00005842 return PyBool_FromLong(WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005843#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005844}
5845#endif /* WIFSTOPPED */
5846
5847#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005848PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005849"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005850Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005851
5852static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005853posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005854{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005855#ifdef UNION_WAIT
5856 union wait status;
5857#define status_i (status.w_status)
5858#else
5859 int status;
5860#define status_i status
5861#endif
5862 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005863
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005864 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005865 {
5866 return NULL;
5867 }
Tim Peters5aa91602002-01-30 05:46:57 +00005868
Fred Drake106c1a02002-04-23 15:58:02 +00005869 return PyBool_FromLong(WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005870#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005871}
5872#endif /* WIFSIGNALED */
5873
5874#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005875PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005876"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005877Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005878system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005879
5880static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005881posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005882{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005883#ifdef UNION_WAIT
5884 union wait status;
5885#define status_i (status.w_status)
5886#else
5887 int status;
5888#define status_i status
5889#endif
5890 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005891
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005892 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005893 {
5894 return NULL;
5895 }
Tim Peters5aa91602002-01-30 05:46:57 +00005896
Fred Drake106c1a02002-04-23 15:58:02 +00005897 return PyBool_FromLong(WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005898#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005899}
5900#endif /* WIFEXITED */
5901
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005902#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005903PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005904"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005905Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005906
5907static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005908posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005909{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005910#ifdef UNION_WAIT
5911 union wait status;
5912#define status_i (status.w_status)
5913#else
5914 int status;
5915#define status_i status
5916#endif
5917 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005918
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005919 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005920 {
5921 return NULL;
5922 }
Tim Peters5aa91602002-01-30 05:46:57 +00005923
Guido van Rossumc9641791998-08-04 15:26:23 +00005924 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005925#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005926}
5927#endif /* WEXITSTATUS */
5928
5929#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005930PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005931"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005932Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005933value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005934
5935static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005936posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005937{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005938#ifdef UNION_WAIT
5939 union wait status;
5940#define status_i (status.w_status)
5941#else
5942 int status;
5943#define status_i status
5944#endif
5945 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005946
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005947 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005948 {
5949 return NULL;
5950 }
Tim Peters5aa91602002-01-30 05:46:57 +00005951
Guido van Rossumc9641791998-08-04 15:26:23 +00005952 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005953#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005954}
5955#endif /* WTERMSIG */
5956
5957#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005958PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005959"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005960Return the signal that stopped the process that provided\n\
5961the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005962
5963static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005964posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005965{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005966#ifdef UNION_WAIT
5967 union wait status;
5968#define status_i (status.w_status)
5969#else
5970 int status;
5971#define status_i status
5972#endif
5973 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005974
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005975 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005976 {
5977 return NULL;
5978 }
Tim Peters5aa91602002-01-30 05:46:57 +00005979
Guido van Rossumc9641791998-08-04 15:26:23 +00005980 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005981#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005982}
5983#endif /* WSTOPSIG */
5984
5985#endif /* HAVE_SYS_WAIT_H */
5986
5987
Guido van Rossum94f6f721999-01-06 18:42:14 +00005988#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005989#ifdef _SCO_DS
5990/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5991 needed definitions in sys/statvfs.h */
5992#define _SVID3
5993#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005994#include <sys/statvfs.h>
5995
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005996static PyObject*
5997_pystatvfs_fromstructstatvfs(struct statvfs st) {
5998 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5999 if (v == NULL)
6000 return NULL;
6001
6002#if !defined(HAVE_LARGEFILE_SUPPORT)
6003 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6004 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6005 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6006 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6007 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6008 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6009 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6010 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6011 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6012 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6013#else
6014 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6015 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006016 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006017 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006018 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006019 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006020 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006021 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006022 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006023 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006024 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006025 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006026 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006027 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006028 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6029 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6030#endif
6031
6032 return v;
6033}
6034
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006035PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006036"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006037Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006038
6039static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006040posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006041{
6042 int fd, res;
6043 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006044
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006045 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006046 return NULL;
6047 Py_BEGIN_ALLOW_THREADS
6048 res = fstatvfs(fd, &st);
6049 Py_END_ALLOW_THREADS
6050 if (res != 0)
6051 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006052
6053 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006054}
6055#endif /* HAVE_FSTATVFS */
6056
6057
6058#if defined(HAVE_STATVFS)
6059#include <sys/statvfs.h>
6060
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006061PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006062"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006063Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006064
6065static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006066posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006067{
6068 char *path;
6069 int res;
6070 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006071 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006072 return NULL;
6073 Py_BEGIN_ALLOW_THREADS
6074 res = statvfs(path, &st);
6075 Py_END_ALLOW_THREADS
6076 if (res != 0)
6077 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006078
6079 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006080}
6081#endif /* HAVE_STATVFS */
6082
6083
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006084#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006085PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006086"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006087Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006088The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006089or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006090
6091static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006092posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006093{
6094 PyObject *result = NULL;
6095 char *dir = NULL;
6096 char *pfx = NULL;
6097 char *name;
6098
6099 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6100 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006101
6102 if (PyErr_Warn(PyExc_RuntimeWarning,
6103 "tempnam is a potential security risk to your program") < 0)
6104 return NULL;
6105
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006106#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006107 name = _tempnam(dir, pfx);
6108#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006109 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006110#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006111 if (name == NULL)
6112 return PyErr_NoMemory();
6113 result = PyString_FromString(name);
6114 free(name);
6115 return result;
6116}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006117#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006118
6119
6120#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006121PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006122"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006123Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006124
6125static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006126posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006127{
6128 FILE *fp;
6129
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006130 fp = tmpfile();
6131 if (fp == NULL)
6132 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006133 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006134}
6135#endif
6136
6137
6138#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006139PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006140"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006141Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006142
6143static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006144posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006145{
6146 char buffer[L_tmpnam];
6147 char *name;
6148
Skip Montanaro95618b52001-08-18 18:52:10 +00006149 if (PyErr_Warn(PyExc_RuntimeWarning,
6150 "tmpnam is a potential security risk to your program") < 0)
6151 return NULL;
6152
Greg Wardb48bc172000-03-01 21:51:56 +00006153#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006154 name = tmpnam_r(buffer);
6155#else
6156 name = tmpnam(buffer);
6157#endif
6158 if (name == NULL) {
6159 PyErr_SetObject(PyExc_OSError,
6160 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006161#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006162 "unexpected NULL from tmpnam_r"
6163#else
6164 "unexpected NULL from tmpnam"
6165#endif
6166 ));
6167 return NULL;
6168 }
6169 return PyString_FromString(buffer);
6170}
6171#endif
6172
6173
Fred Drakec9680921999-12-13 16:37:25 +00006174/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6175 * It maps strings representing configuration variable names to
6176 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006177 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006178 * rarely-used constants. There are three separate tables that use
6179 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006180 *
6181 * This code is always included, even if none of the interfaces that
6182 * need it are included. The #if hackery needed to avoid it would be
6183 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006184 */
6185struct constdef {
6186 char *name;
6187 long value;
6188};
6189
Fred Drake12c6e2d1999-12-14 21:25:03 +00006190static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006191conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6192 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006193{
6194 if (PyInt_Check(arg)) {
6195 *valuep = PyInt_AS_LONG(arg);
6196 return 1;
6197 }
6198 if (PyString_Check(arg)) {
6199 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006200 size_t lo = 0;
6201 size_t mid;
6202 size_t hi = tablesize;
6203 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006204 char *confname = PyString_AS_STRING(arg);
6205 while (lo < hi) {
6206 mid = (lo + hi) / 2;
6207 cmp = strcmp(confname, table[mid].name);
6208 if (cmp < 0)
6209 hi = mid;
6210 else if (cmp > 0)
6211 lo = mid + 1;
6212 else {
6213 *valuep = table[mid].value;
6214 return 1;
6215 }
6216 }
6217 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6218 }
6219 else
6220 PyErr_SetString(PyExc_TypeError,
6221 "configuration names must be strings or integers");
6222 return 0;
6223}
6224
6225
6226#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6227static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006228#ifdef _PC_ABI_AIO_XFER_MAX
6229 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6230#endif
6231#ifdef _PC_ABI_ASYNC_IO
6232 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6233#endif
Fred Drakec9680921999-12-13 16:37:25 +00006234#ifdef _PC_ASYNC_IO
6235 {"PC_ASYNC_IO", _PC_ASYNC_IO},
6236#endif
6237#ifdef _PC_CHOWN_RESTRICTED
6238 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
6239#endif
6240#ifdef _PC_FILESIZEBITS
6241 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
6242#endif
6243#ifdef _PC_LAST
6244 {"PC_LAST", _PC_LAST},
6245#endif
6246#ifdef _PC_LINK_MAX
6247 {"PC_LINK_MAX", _PC_LINK_MAX},
6248#endif
6249#ifdef _PC_MAX_CANON
6250 {"PC_MAX_CANON", _PC_MAX_CANON},
6251#endif
6252#ifdef _PC_MAX_INPUT
6253 {"PC_MAX_INPUT", _PC_MAX_INPUT},
6254#endif
6255#ifdef _PC_NAME_MAX
6256 {"PC_NAME_MAX", _PC_NAME_MAX},
6257#endif
6258#ifdef _PC_NO_TRUNC
6259 {"PC_NO_TRUNC", _PC_NO_TRUNC},
6260#endif
6261#ifdef _PC_PATH_MAX
6262 {"PC_PATH_MAX", _PC_PATH_MAX},
6263#endif
6264#ifdef _PC_PIPE_BUF
6265 {"PC_PIPE_BUF", _PC_PIPE_BUF},
6266#endif
6267#ifdef _PC_PRIO_IO
6268 {"PC_PRIO_IO", _PC_PRIO_IO},
6269#endif
6270#ifdef _PC_SOCK_MAXBUF
6271 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
6272#endif
6273#ifdef _PC_SYNC_IO
6274 {"PC_SYNC_IO", _PC_SYNC_IO},
6275#endif
6276#ifdef _PC_VDISABLE
6277 {"PC_VDISABLE", _PC_VDISABLE},
6278#endif
6279};
6280
Fred Drakec9680921999-12-13 16:37:25 +00006281static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006282conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006283{
6284 return conv_confname(arg, valuep, posix_constants_pathconf,
6285 sizeof(posix_constants_pathconf)
6286 / sizeof(struct constdef));
6287}
6288#endif
6289
6290#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006291PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006292"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006293Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006294If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006295
6296static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006297posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006298{
6299 PyObject *result = NULL;
6300 int name, fd;
6301
Fred Drake12c6e2d1999-12-14 21:25:03 +00006302 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6303 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00006304 long limit;
6305
6306 errno = 0;
6307 limit = fpathconf(fd, name);
6308 if (limit == -1 && errno != 0)
6309 posix_error();
6310 else
6311 result = PyInt_FromLong(limit);
6312 }
6313 return result;
6314}
6315#endif
6316
6317
6318#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006319PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006320"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006321Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006322If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006323
6324static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006325posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006326{
6327 PyObject *result = NULL;
6328 int name;
6329 char *path;
6330
6331 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6332 conv_path_confname, &name)) {
6333 long limit;
6334
6335 errno = 0;
6336 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006337 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00006338 if (errno == EINVAL)
6339 /* could be a path or name problem */
6340 posix_error();
6341 else
6342 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006343 }
Fred Drakec9680921999-12-13 16:37:25 +00006344 else
6345 result = PyInt_FromLong(limit);
6346 }
6347 return result;
6348}
6349#endif
6350
6351#ifdef HAVE_CONFSTR
6352static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006353#ifdef _CS_ARCHITECTURE
6354 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6355#endif
6356#ifdef _CS_HOSTNAME
6357 {"CS_HOSTNAME", _CS_HOSTNAME},
6358#endif
6359#ifdef _CS_HW_PROVIDER
6360 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6361#endif
6362#ifdef _CS_HW_SERIAL
6363 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6364#endif
6365#ifdef _CS_INITTAB_NAME
6366 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6367#endif
Fred Drakec9680921999-12-13 16:37:25 +00006368#ifdef _CS_LFS64_CFLAGS
6369 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6370#endif
6371#ifdef _CS_LFS64_LDFLAGS
6372 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6373#endif
6374#ifdef _CS_LFS64_LIBS
6375 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6376#endif
6377#ifdef _CS_LFS64_LINTFLAGS
6378 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6379#endif
6380#ifdef _CS_LFS_CFLAGS
6381 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6382#endif
6383#ifdef _CS_LFS_LDFLAGS
6384 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6385#endif
6386#ifdef _CS_LFS_LIBS
6387 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6388#endif
6389#ifdef _CS_LFS_LINTFLAGS
6390 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6391#endif
Fred Draked86ed291999-12-15 15:34:33 +00006392#ifdef _CS_MACHINE
6393 {"CS_MACHINE", _CS_MACHINE},
6394#endif
Fred Drakec9680921999-12-13 16:37:25 +00006395#ifdef _CS_PATH
6396 {"CS_PATH", _CS_PATH},
6397#endif
Fred Draked86ed291999-12-15 15:34:33 +00006398#ifdef _CS_RELEASE
6399 {"CS_RELEASE", _CS_RELEASE},
6400#endif
6401#ifdef _CS_SRPC_DOMAIN
6402 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6403#endif
6404#ifdef _CS_SYSNAME
6405 {"CS_SYSNAME", _CS_SYSNAME},
6406#endif
6407#ifdef _CS_VERSION
6408 {"CS_VERSION", _CS_VERSION},
6409#endif
Fred Drakec9680921999-12-13 16:37:25 +00006410#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6411 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6412#endif
6413#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6414 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6415#endif
6416#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6417 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6418#endif
6419#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6420 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6421#endif
6422#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6423 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6424#endif
6425#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6426 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6427#endif
6428#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6429 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6430#endif
6431#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6432 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6433#endif
6434#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6435 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6436#endif
6437#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6438 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6439#endif
6440#ifdef _CS_XBS5_LP64_OFF64_LIBS
6441 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6442#endif
6443#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6444 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6445#endif
6446#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6447 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6448#endif
6449#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6450 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6451#endif
6452#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6453 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6454#endif
6455#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6456 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6457#endif
Fred Draked86ed291999-12-15 15:34:33 +00006458#ifdef _MIPS_CS_AVAIL_PROCESSORS
6459 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6460#endif
6461#ifdef _MIPS_CS_BASE
6462 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6463#endif
6464#ifdef _MIPS_CS_HOSTID
6465 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6466#endif
6467#ifdef _MIPS_CS_HW_NAME
6468 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6469#endif
6470#ifdef _MIPS_CS_NUM_PROCESSORS
6471 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6472#endif
6473#ifdef _MIPS_CS_OSREL_MAJ
6474 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6475#endif
6476#ifdef _MIPS_CS_OSREL_MIN
6477 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6478#endif
6479#ifdef _MIPS_CS_OSREL_PATCH
6480 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6481#endif
6482#ifdef _MIPS_CS_OS_NAME
6483 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6484#endif
6485#ifdef _MIPS_CS_OS_PROVIDER
6486 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6487#endif
6488#ifdef _MIPS_CS_PROCESSORS
6489 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6490#endif
6491#ifdef _MIPS_CS_SERIAL
6492 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6493#endif
6494#ifdef _MIPS_CS_VENDOR
6495 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6496#endif
Fred Drakec9680921999-12-13 16:37:25 +00006497};
6498
6499static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006500conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006501{
6502 return conv_confname(arg, valuep, posix_constants_confstr,
6503 sizeof(posix_constants_confstr)
6504 / sizeof(struct constdef));
6505}
6506
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006507PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006508"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006509Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006510
6511static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006512posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006513{
6514 PyObject *result = NULL;
6515 int name;
6516 char buffer[64];
6517
6518 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
6519 int len = confstr(name, buffer, sizeof(buffer));
6520
Fred Drakec9680921999-12-13 16:37:25 +00006521 errno = 0;
6522 if (len == 0) {
6523 if (errno != 0)
6524 posix_error();
6525 else
6526 result = PyString_FromString("");
6527 }
6528 else {
6529 if (len >= sizeof(buffer)) {
6530 result = PyString_FromStringAndSize(NULL, len);
6531 if (result != NULL)
6532 confstr(name, PyString_AS_STRING(result), len+1);
6533 }
6534 else
6535 result = PyString_FromString(buffer);
6536 }
6537 }
6538 return result;
6539}
6540#endif
6541
6542
6543#ifdef HAVE_SYSCONF
6544static struct constdef posix_constants_sysconf[] = {
6545#ifdef _SC_2_CHAR_TERM
6546 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6547#endif
6548#ifdef _SC_2_C_BIND
6549 {"SC_2_C_BIND", _SC_2_C_BIND},
6550#endif
6551#ifdef _SC_2_C_DEV
6552 {"SC_2_C_DEV", _SC_2_C_DEV},
6553#endif
6554#ifdef _SC_2_C_VERSION
6555 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6556#endif
6557#ifdef _SC_2_FORT_DEV
6558 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6559#endif
6560#ifdef _SC_2_FORT_RUN
6561 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6562#endif
6563#ifdef _SC_2_LOCALEDEF
6564 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6565#endif
6566#ifdef _SC_2_SW_DEV
6567 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6568#endif
6569#ifdef _SC_2_UPE
6570 {"SC_2_UPE", _SC_2_UPE},
6571#endif
6572#ifdef _SC_2_VERSION
6573 {"SC_2_VERSION", _SC_2_VERSION},
6574#endif
Fred Draked86ed291999-12-15 15:34:33 +00006575#ifdef _SC_ABI_ASYNCHRONOUS_IO
6576 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6577#endif
6578#ifdef _SC_ACL
6579 {"SC_ACL", _SC_ACL},
6580#endif
Fred Drakec9680921999-12-13 16:37:25 +00006581#ifdef _SC_AIO_LISTIO_MAX
6582 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6583#endif
Fred Drakec9680921999-12-13 16:37:25 +00006584#ifdef _SC_AIO_MAX
6585 {"SC_AIO_MAX", _SC_AIO_MAX},
6586#endif
6587#ifdef _SC_AIO_PRIO_DELTA_MAX
6588 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6589#endif
6590#ifdef _SC_ARG_MAX
6591 {"SC_ARG_MAX", _SC_ARG_MAX},
6592#endif
6593#ifdef _SC_ASYNCHRONOUS_IO
6594 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6595#endif
6596#ifdef _SC_ATEXIT_MAX
6597 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6598#endif
Fred Draked86ed291999-12-15 15:34:33 +00006599#ifdef _SC_AUDIT
6600 {"SC_AUDIT", _SC_AUDIT},
6601#endif
Fred Drakec9680921999-12-13 16:37:25 +00006602#ifdef _SC_AVPHYS_PAGES
6603 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6604#endif
6605#ifdef _SC_BC_BASE_MAX
6606 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6607#endif
6608#ifdef _SC_BC_DIM_MAX
6609 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6610#endif
6611#ifdef _SC_BC_SCALE_MAX
6612 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6613#endif
6614#ifdef _SC_BC_STRING_MAX
6615 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6616#endif
Fred Draked86ed291999-12-15 15:34:33 +00006617#ifdef _SC_CAP
6618 {"SC_CAP", _SC_CAP},
6619#endif
Fred Drakec9680921999-12-13 16:37:25 +00006620#ifdef _SC_CHARCLASS_NAME_MAX
6621 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6622#endif
6623#ifdef _SC_CHAR_BIT
6624 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6625#endif
6626#ifdef _SC_CHAR_MAX
6627 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6628#endif
6629#ifdef _SC_CHAR_MIN
6630 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6631#endif
6632#ifdef _SC_CHILD_MAX
6633 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6634#endif
6635#ifdef _SC_CLK_TCK
6636 {"SC_CLK_TCK", _SC_CLK_TCK},
6637#endif
6638#ifdef _SC_COHER_BLKSZ
6639 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6640#endif
6641#ifdef _SC_COLL_WEIGHTS_MAX
6642 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6643#endif
6644#ifdef _SC_DCACHE_ASSOC
6645 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6646#endif
6647#ifdef _SC_DCACHE_BLKSZ
6648 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6649#endif
6650#ifdef _SC_DCACHE_LINESZ
6651 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6652#endif
6653#ifdef _SC_DCACHE_SZ
6654 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6655#endif
6656#ifdef _SC_DCACHE_TBLKSZ
6657 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6658#endif
6659#ifdef _SC_DELAYTIMER_MAX
6660 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6661#endif
6662#ifdef _SC_EQUIV_CLASS_MAX
6663 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6664#endif
6665#ifdef _SC_EXPR_NEST_MAX
6666 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6667#endif
6668#ifdef _SC_FSYNC
6669 {"SC_FSYNC", _SC_FSYNC},
6670#endif
6671#ifdef _SC_GETGR_R_SIZE_MAX
6672 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6673#endif
6674#ifdef _SC_GETPW_R_SIZE_MAX
6675 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6676#endif
6677#ifdef _SC_ICACHE_ASSOC
6678 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6679#endif
6680#ifdef _SC_ICACHE_BLKSZ
6681 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6682#endif
6683#ifdef _SC_ICACHE_LINESZ
6684 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6685#endif
6686#ifdef _SC_ICACHE_SZ
6687 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6688#endif
Fred Draked86ed291999-12-15 15:34:33 +00006689#ifdef _SC_INF
6690 {"SC_INF", _SC_INF},
6691#endif
Fred Drakec9680921999-12-13 16:37:25 +00006692#ifdef _SC_INT_MAX
6693 {"SC_INT_MAX", _SC_INT_MAX},
6694#endif
6695#ifdef _SC_INT_MIN
6696 {"SC_INT_MIN", _SC_INT_MIN},
6697#endif
6698#ifdef _SC_IOV_MAX
6699 {"SC_IOV_MAX", _SC_IOV_MAX},
6700#endif
Fred Draked86ed291999-12-15 15:34:33 +00006701#ifdef _SC_IP_SECOPTS
6702 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6703#endif
Fred Drakec9680921999-12-13 16:37:25 +00006704#ifdef _SC_JOB_CONTROL
6705 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6706#endif
Fred Draked86ed291999-12-15 15:34:33 +00006707#ifdef _SC_KERN_POINTERS
6708 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6709#endif
6710#ifdef _SC_KERN_SIM
6711 {"SC_KERN_SIM", _SC_KERN_SIM},
6712#endif
Fred Drakec9680921999-12-13 16:37:25 +00006713#ifdef _SC_LINE_MAX
6714 {"SC_LINE_MAX", _SC_LINE_MAX},
6715#endif
6716#ifdef _SC_LOGIN_NAME_MAX
6717 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6718#endif
6719#ifdef _SC_LOGNAME_MAX
6720 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6721#endif
6722#ifdef _SC_LONG_BIT
6723 {"SC_LONG_BIT", _SC_LONG_BIT},
6724#endif
Fred Draked86ed291999-12-15 15:34:33 +00006725#ifdef _SC_MAC
6726 {"SC_MAC", _SC_MAC},
6727#endif
Fred Drakec9680921999-12-13 16:37:25 +00006728#ifdef _SC_MAPPED_FILES
6729 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6730#endif
6731#ifdef _SC_MAXPID
6732 {"SC_MAXPID", _SC_MAXPID},
6733#endif
6734#ifdef _SC_MB_LEN_MAX
6735 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6736#endif
6737#ifdef _SC_MEMLOCK
6738 {"SC_MEMLOCK", _SC_MEMLOCK},
6739#endif
6740#ifdef _SC_MEMLOCK_RANGE
6741 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6742#endif
6743#ifdef _SC_MEMORY_PROTECTION
6744 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6745#endif
6746#ifdef _SC_MESSAGE_PASSING
6747 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6748#endif
Fred Draked86ed291999-12-15 15:34:33 +00006749#ifdef _SC_MMAP_FIXED_ALIGNMENT
6750 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6751#endif
Fred Drakec9680921999-12-13 16:37:25 +00006752#ifdef _SC_MQ_OPEN_MAX
6753 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6754#endif
6755#ifdef _SC_MQ_PRIO_MAX
6756 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6757#endif
Fred Draked86ed291999-12-15 15:34:33 +00006758#ifdef _SC_NACLS_MAX
6759 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6760#endif
Fred Drakec9680921999-12-13 16:37:25 +00006761#ifdef _SC_NGROUPS_MAX
6762 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6763#endif
6764#ifdef _SC_NL_ARGMAX
6765 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6766#endif
6767#ifdef _SC_NL_LANGMAX
6768 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6769#endif
6770#ifdef _SC_NL_MSGMAX
6771 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6772#endif
6773#ifdef _SC_NL_NMAX
6774 {"SC_NL_NMAX", _SC_NL_NMAX},
6775#endif
6776#ifdef _SC_NL_SETMAX
6777 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6778#endif
6779#ifdef _SC_NL_TEXTMAX
6780 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6781#endif
6782#ifdef _SC_NPROCESSORS_CONF
6783 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6784#endif
6785#ifdef _SC_NPROCESSORS_ONLN
6786 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6787#endif
Fred Draked86ed291999-12-15 15:34:33 +00006788#ifdef _SC_NPROC_CONF
6789 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6790#endif
6791#ifdef _SC_NPROC_ONLN
6792 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6793#endif
Fred Drakec9680921999-12-13 16:37:25 +00006794#ifdef _SC_NZERO
6795 {"SC_NZERO", _SC_NZERO},
6796#endif
6797#ifdef _SC_OPEN_MAX
6798 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6799#endif
6800#ifdef _SC_PAGESIZE
6801 {"SC_PAGESIZE", _SC_PAGESIZE},
6802#endif
6803#ifdef _SC_PAGE_SIZE
6804 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6805#endif
6806#ifdef _SC_PASS_MAX
6807 {"SC_PASS_MAX", _SC_PASS_MAX},
6808#endif
6809#ifdef _SC_PHYS_PAGES
6810 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6811#endif
6812#ifdef _SC_PII
6813 {"SC_PII", _SC_PII},
6814#endif
6815#ifdef _SC_PII_INTERNET
6816 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6817#endif
6818#ifdef _SC_PII_INTERNET_DGRAM
6819 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6820#endif
6821#ifdef _SC_PII_INTERNET_STREAM
6822 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6823#endif
6824#ifdef _SC_PII_OSI
6825 {"SC_PII_OSI", _SC_PII_OSI},
6826#endif
6827#ifdef _SC_PII_OSI_CLTS
6828 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6829#endif
6830#ifdef _SC_PII_OSI_COTS
6831 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6832#endif
6833#ifdef _SC_PII_OSI_M
6834 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6835#endif
6836#ifdef _SC_PII_SOCKET
6837 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6838#endif
6839#ifdef _SC_PII_XTI
6840 {"SC_PII_XTI", _SC_PII_XTI},
6841#endif
6842#ifdef _SC_POLL
6843 {"SC_POLL", _SC_POLL},
6844#endif
6845#ifdef _SC_PRIORITIZED_IO
6846 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6847#endif
6848#ifdef _SC_PRIORITY_SCHEDULING
6849 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6850#endif
6851#ifdef _SC_REALTIME_SIGNALS
6852 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6853#endif
6854#ifdef _SC_RE_DUP_MAX
6855 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6856#endif
6857#ifdef _SC_RTSIG_MAX
6858 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6859#endif
6860#ifdef _SC_SAVED_IDS
6861 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6862#endif
6863#ifdef _SC_SCHAR_MAX
6864 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6865#endif
6866#ifdef _SC_SCHAR_MIN
6867 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6868#endif
6869#ifdef _SC_SELECT
6870 {"SC_SELECT", _SC_SELECT},
6871#endif
6872#ifdef _SC_SEMAPHORES
6873 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6874#endif
6875#ifdef _SC_SEM_NSEMS_MAX
6876 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6877#endif
6878#ifdef _SC_SEM_VALUE_MAX
6879 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6880#endif
6881#ifdef _SC_SHARED_MEMORY_OBJECTS
6882 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6883#endif
6884#ifdef _SC_SHRT_MAX
6885 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6886#endif
6887#ifdef _SC_SHRT_MIN
6888 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6889#endif
6890#ifdef _SC_SIGQUEUE_MAX
6891 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6892#endif
6893#ifdef _SC_SIGRT_MAX
6894 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6895#endif
6896#ifdef _SC_SIGRT_MIN
6897 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6898#endif
Fred Draked86ed291999-12-15 15:34:33 +00006899#ifdef _SC_SOFTPOWER
6900 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6901#endif
Fred Drakec9680921999-12-13 16:37:25 +00006902#ifdef _SC_SPLIT_CACHE
6903 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6904#endif
6905#ifdef _SC_SSIZE_MAX
6906 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6907#endif
6908#ifdef _SC_STACK_PROT
6909 {"SC_STACK_PROT", _SC_STACK_PROT},
6910#endif
6911#ifdef _SC_STREAM_MAX
6912 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6913#endif
6914#ifdef _SC_SYNCHRONIZED_IO
6915 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6916#endif
6917#ifdef _SC_THREADS
6918 {"SC_THREADS", _SC_THREADS},
6919#endif
6920#ifdef _SC_THREAD_ATTR_STACKADDR
6921 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6922#endif
6923#ifdef _SC_THREAD_ATTR_STACKSIZE
6924 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6925#endif
6926#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6927 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6928#endif
6929#ifdef _SC_THREAD_KEYS_MAX
6930 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6931#endif
6932#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6933 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6934#endif
6935#ifdef _SC_THREAD_PRIO_INHERIT
6936 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6937#endif
6938#ifdef _SC_THREAD_PRIO_PROTECT
6939 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6940#endif
6941#ifdef _SC_THREAD_PROCESS_SHARED
6942 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6943#endif
6944#ifdef _SC_THREAD_SAFE_FUNCTIONS
6945 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6946#endif
6947#ifdef _SC_THREAD_STACK_MIN
6948 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6949#endif
6950#ifdef _SC_THREAD_THREADS_MAX
6951 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6952#endif
6953#ifdef _SC_TIMERS
6954 {"SC_TIMERS", _SC_TIMERS},
6955#endif
6956#ifdef _SC_TIMER_MAX
6957 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6958#endif
6959#ifdef _SC_TTY_NAME_MAX
6960 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6961#endif
6962#ifdef _SC_TZNAME_MAX
6963 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6964#endif
6965#ifdef _SC_T_IOV_MAX
6966 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6967#endif
6968#ifdef _SC_UCHAR_MAX
6969 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6970#endif
6971#ifdef _SC_UINT_MAX
6972 {"SC_UINT_MAX", _SC_UINT_MAX},
6973#endif
6974#ifdef _SC_UIO_MAXIOV
6975 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6976#endif
6977#ifdef _SC_ULONG_MAX
6978 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6979#endif
6980#ifdef _SC_USHRT_MAX
6981 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6982#endif
6983#ifdef _SC_VERSION
6984 {"SC_VERSION", _SC_VERSION},
6985#endif
6986#ifdef _SC_WORD_BIT
6987 {"SC_WORD_BIT", _SC_WORD_BIT},
6988#endif
6989#ifdef _SC_XBS5_ILP32_OFF32
6990 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6991#endif
6992#ifdef _SC_XBS5_ILP32_OFFBIG
6993 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6994#endif
6995#ifdef _SC_XBS5_LP64_OFF64
6996 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6997#endif
6998#ifdef _SC_XBS5_LPBIG_OFFBIG
6999 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7000#endif
7001#ifdef _SC_XOPEN_CRYPT
7002 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7003#endif
7004#ifdef _SC_XOPEN_ENH_I18N
7005 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7006#endif
7007#ifdef _SC_XOPEN_LEGACY
7008 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7009#endif
7010#ifdef _SC_XOPEN_REALTIME
7011 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7012#endif
7013#ifdef _SC_XOPEN_REALTIME_THREADS
7014 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7015#endif
7016#ifdef _SC_XOPEN_SHM
7017 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7018#endif
7019#ifdef _SC_XOPEN_UNIX
7020 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7021#endif
7022#ifdef _SC_XOPEN_VERSION
7023 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7024#endif
7025#ifdef _SC_XOPEN_XCU_VERSION
7026 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7027#endif
7028#ifdef _SC_XOPEN_XPG2
7029 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7030#endif
7031#ifdef _SC_XOPEN_XPG3
7032 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7033#endif
7034#ifdef _SC_XOPEN_XPG4
7035 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7036#endif
7037};
7038
7039static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007040conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007041{
7042 return conv_confname(arg, valuep, posix_constants_sysconf,
7043 sizeof(posix_constants_sysconf)
7044 / sizeof(struct constdef));
7045}
7046
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007047PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007048"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007049Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007050
7051static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007052posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007053{
7054 PyObject *result = NULL;
7055 int name;
7056
7057 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7058 int value;
7059
7060 errno = 0;
7061 value = sysconf(name);
7062 if (value == -1 && errno != 0)
7063 posix_error();
7064 else
7065 result = PyInt_FromLong(value);
7066 }
7067 return result;
7068}
7069#endif
7070
7071
Fred Drakebec628d1999-12-15 18:31:10 +00007072/* This code is used to ensure that the tables of configuration value names
7073 * are in sorted order as required by conv_confname(), and also to build the
7074 * the exported dictionaries that are used to publish information about the
7075 * names available on the host platform.
7076 *
7077 * Sorting the table at runtime ensures that the table is properly ordered
7078 * when used, even for platforms we're not able to test on. It also makes
7079 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007080 */
Fred Drakebec628d1999-12-15 18:31:10 +00007081
7082static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007083cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007084{
7085 const struct constdef *c1 =
7086 (const struct constdef *) v1;
7087 const struct constdef *c2 =
7088 (const struct constdef *) v2;
7089
7090 return strcmp(c1->name, c2->name);
7091}
7092
7093static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007094setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007095 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007096{
Fred Drakebec628d1999-12-15 18:31:10 +00007097 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007098 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007099
7100 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7101 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007102 if (d == NULL)
7103 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007104
Barry Warsaw3155db32000-04-13 15:20:40 +00007105 for (i=0; i < tablesize; ++i) {
7106 PyObject *o = PyInt_FromLong(table[i].value);
7107 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7108 Py_XDECREF(o);
7109 Py_DECREF(d);
7110 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007111 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007112 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007113 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007114 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007115}
7116
Fred Drakebec628d1999-12-15 18:31:10 +00007117/* Return -1 on failure, 0 on success. */
7118static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007119setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007120{
7121#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007122 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007123 sizeof(posix_constants_pathconf)
7124 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007125 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007126 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007127#endif
7128#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007129 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007130 sizeof(posix_constants_confstr)
7131 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007132 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007133 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007134#endif
7135#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007136 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007137 sizeof(posix_constants_sysconf)
7138 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007139 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007140 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007141#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007142 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007143}
Fred Draked86ed291999-12-15 15:34:33 +00007144
7145
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007146PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007147"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007148Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007149in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007150
7151static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007152posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007153{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007154 abort();
7155 /*NOTREACHED*/
7156 Py_FatalError("abort() called from Python code didn't abort!");
7157 return NULL;
7158}
Fred Drakebec628d1999-12-15 18:31:10 +00007159
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007160#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007161PyDoc_STRVAR(win32_startfile__doc__,
7162"startfile(filepath) - Start a file with its associated application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007163\n\
7164This acts like double-clicking the file in Explorer, or giving the file\n\
7165name as an argument to the DOS \"start\" command: the file is opened\n\
7166with whatever application (if any) its extension is associated.\n\
7167\n\
7168startfile returns as soon as the associated application is launched.\n\
7169There is no option to wait for the application to close, and no way\n\
7170to retrieve the application's exit status.\n\
7171\n\
7172The filepath is relative to the current directory. If you want to use\n\
7173an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007174the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007175
7176static PyObject *
7177win32_startfile(PyObject *self, PyObject *args)
7178{
7179 char *filepath;
7180 HINSTANCE rc;
7181 if (!PyArg_ParseTuple(args, "s:startfile", &filepath))
7182 return NULL;
7183 Py_BEGIN_ALLOW_THREADS
7184 rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL);
7185 Py_END_ALLOW_THREADS
7186 if (rc <= (HINSTANCE)32)
7187 return win32_error("startfile", filepath);
7188 Py_INCREF(Py_None);
7189 return Py_None;
7190}
7191#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007192
Martin v. Löwis438b5342002-12-27 10:16:42 +00007193#ifdef HAVE_GETLOADAVG
7194PyDoc_STRVAR(posix_getloadavg__doc__,
7195"getloadavg() -> (float, float, float)\n\n\
7196Return the number of processes in the system run queue averaged over\n\
7197the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7198was unobtainable");
7199
7200static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007201posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007202{
7203 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007204 if (getloadavg(loadavg, 3)!=3) {
7205 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7206 return NULL;
7207 } else
7208 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
7209}
7210#endif
7211
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007212#ifdef MS_WINDOWS
7213
7214PyDoc_STRVAR(win32_urandom__doc__,
7215"urandom(n) -> str\n\n\
7216Return a string of n random bytes suitable for cryptographic use.");
7217
7218typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7219 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7220 DWORD dwFlags );
7221typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7222 BYTE *pbBuffer );
7223
7224static CRYPTGENRANDOM pCryptGenRandom = NULL;
7225static HCRYPTPROV hCryptProv = 0;
7226
Tim Peters4ad82172004-08-30 17:02:04 +00007227static PyObject*
7228win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007229{
Tim Petersd3115382004-08-30 17:36:46 +00007230 int howMany;
7231 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007232
Tim Peters4ad82172004-08-30 17:02:04 +00007233 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00007234 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00007235 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00007236 if (howMany < 0)
7237 return PyErr_Format(PyExc_ValueError,
7238 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007239
Tim Peters4ad82172004-08-30 17:02:04 +00007240 if (hCryptProv == 0) {
7241 HINSTANCE hAdvAPI32 = NULL;
7242 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007243
Tim Peters4ad82172004-08-30 17:02:04 +00007244 /* Obtain handle to the DLL containing CryptoAPI
7245 This should not fail */
7246 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7247 if(hAdvAPI32 == NULL)
7248 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007249
Tim Peters4ad82172004-08-30 17:02:04 +00007250 /* Obtain pointers to the CryptoAPI functions
7251 This will fail on some early versions of Win95 */
7252 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7253 hAdvAPI32,
7254 "CryptAcquireContextA");
7255 if (pCryptAcquireContext == NULL)
7256 return PyErr_Format(PyExc_NotImplementedError,
7257 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007258
Tim Peters4ad82172004-08-30 17:02:04 +00007259 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7260 hAdvAPI32, "CryptGenRandom");
7261 if (pCryptAcquireContext == NULL)
7262 return PyErr_Format(PyExc_NotImplementedError,
7263 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007264
Tim Peters4ad82172004-08-30 17:02:04 +00007265 /* Acquire context */
7266 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7267 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7268 return win32_error("CryptAcquireContext", NULL);
7269 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007270
Tim Peters4ad82172004-08-30 17:02:04 +00007271 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00007272 result = PyString_FromStringAndSize(NULL, howMany);
7273 if (result != NULL) {
7274 /* Get random data */
7275 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7276 PyString_AS_STRING(result))) {
7277 Py_DECREF(result);
7278 return win32_error("CryptGenRandom", NULL);
7279 }
Tim Peters4ad82172004-08-30 17:02:04 +00007280 }
Tim Petersd3115382004-08-30 17:36:46 +00007281 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007282}
7283#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007284
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007285static PyMethodDef posix_methods[] = {
7286 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7287#ifdef HAVE_TTYNAME
7288 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7289#endif
7290 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
7291 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007292#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007293 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007294#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007295#ifdef HAVE_LCHOWN
7296 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7297#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007298#ifdef HAVE_CHROOT
7299 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7300#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007301#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007302 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007303#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007304#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00007305 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00007306#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00007307 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007308#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00007309#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007310#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007311 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007312#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007313 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7314 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7315 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007316#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007317 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007318#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007319#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007320 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007321#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007322 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7323 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7324 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007325 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007326#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007327 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007328#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007329#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007330 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007331#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007332 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007333#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007334 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007335#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007336 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7337 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7338 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007339#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007340 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007341#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007342 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007343#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007344 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7345 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007346#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007347#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007348 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7349 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007350#if defined(PYOS_OS2)
7351 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7352 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7353#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007354#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007355#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007356 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007357#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007358#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007359 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007360#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007361#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007362 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007363#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007364#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007365 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007366#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007367#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007368 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007369#endif /* HAVE_GETEGID */
7370#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007371 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007372#endif /* HAVE_GETEUID */
7373#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007374 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007375#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007376#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007377 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007378#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007379 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007380#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007381 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007382#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007383#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007384 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007385#endif /* HAVE_GETPPID */
7386#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007387 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007388#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007389#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007390 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007391#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007392#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007393 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007394#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007395#ifdef HAVE_KILLPG
7396 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7397#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007398#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007399 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007400#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007401#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007402 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007403#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007404 {"popen2", win32_popen2, METH_VARARGS},
7405 {"popen3", win32_popen3, METH_VARARGS},
7406 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00007407 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007408#else
7409#if defined(PYOS_OS2) && defined(PYCC_GCC)
7410 {"popen2", os2emx_popen2, METH_VARARGS},
7411 {"popen3", os2emx_popen3, METH_VARARGS},
7412 {"popen4", os2emx_popen4, METH_VARARGS},
7413#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007414#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007415#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007416#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007417 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007418#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007419#ifdef HAVE_SETEUID
7420 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7421#endif /* HAVE_SETEUID */
7422#ifdef HAVE_SETEGID
7423 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7424#endif /* HAVE_SETEGID */
7425#ifdef HAVE_SETREUID
7426 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7427#endif /* HAVE_SETREUID */
7428#ifdef HAVE_SETREGID
7429 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7430#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007431#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007432 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007433#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007434#ifdef HAVE_SETGROUPS
7435 {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__},
7436#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007437#ifdef HAVE_GETPGID
7438 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7439#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007440#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007441 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007442#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007443#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007444 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007445#endif /* HAVE_WAIT */
Tim Petersab034fa2002-02-01 11:27:43 +00007446#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007447 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007448#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007449#ifdef HAVE_GETSID
7450 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7451#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007452#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007453 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007454#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007455#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007456 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007457#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007458#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007459 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007460#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007461#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007462 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007463#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007464 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7465 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7466 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7467 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7468 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7469 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7470 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7471 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7472 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007473 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007474#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007475 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007476#endif
7477#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007478 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007479#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007480#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007481 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7482#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007483#ifdef HAVE_DEVICE_MACROS
7484 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7485 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7486 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7487#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007488#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007489 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007490#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007491#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007492 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007493#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007494#ifdef HAVE_UNSETENV
7495 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7496#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00007497#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007498 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00007499#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00007500#ifdef HAVE_FCHDIR
7501 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7502#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007503#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007504 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007505#endif
7506#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007507 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007508#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007509#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007510#ifdef WCOREDUMP
7511 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7512#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007513#ifdef WIFCONTINUED
7514 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7515#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007516#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007517 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007518#endif /* WIFSTOPPED */
7519#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007520 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007521#endif /* WIFSIGNALED */
7522#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007523 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007524#endif /* WIFEXITED */
7525#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007526 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007527#endif /* WEXITSTATUS */
7528#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007529 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007530#endif /* WTERMSIG */
7531#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007532 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007533#endif /* WSTOPSIG */
7534#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007535#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007536 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007537#endif
7538#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007539 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007540#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00007541#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00007542 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007543#endif
7544#ifdef HAVE_TEMPNAM
7545 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
7546#endif
7547#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00007548 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007549#endif
Fred Drakec9680921999-12-13 16:37:25 +00007550#ifdef HAVE_CONFSTR
7551 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7552#endif
7553#ifdef HAVE_SYSCONF
7554 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7555#endif
7556#ifdef HAVE_FPATHCONF
7557 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7558#endif
7559#ifdef HAVE_PATHCONF
7560 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7561#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007562 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007563#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007564 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7565#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007566#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007567 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007568#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007569 #ifdef MS_WINDOWS
7570 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7571 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007572 {NULL, NULL} /* Sentinel */
7573};
7574
7575
Barry Warsaw4a342091996-12-19 23:50:02 +00007576static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007577ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007578{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007579 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007580}
7581
Guido van Rossumd48f2521997-12-05 22:19:34 +00007582#if defined(PYOS_OS2)
7583/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007584static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007585{
7586 APIRET rc;
7587 ULONG values[QSV_MAX+1];
7588 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007589 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007590
7591 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007592 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007593 Py_END_ALLOW_THREADS
7594
7595 if (rc != NO_ERROR) {
7596 os2_error(rc);
7597 return -1;
7598 }
7599
Fred Drake4d1e64b2002-04-15 19:40:07 +00007600 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7601 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7602 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7603 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7604 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7605 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7606 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007607
7608 switch (values[QSV_VERSION_MINOR]) {
7609 case 0: ver = "2.00"; break;
7610 case 10: ver = "2.10"; break;
7611 case 11: ver = "2.11"; break;
7612 case 30: ver = "3.00"; break;
7613 case 40: ver = "4.00"; break;
7614 case 50: ver = "5.00"; break;
7615 default:
Tim Peters885d4572001-11-28 20:27:42 +00007616 PyOS_snprintf(tmp, sizeof(tmp),
7617 "%d-%d", values[QSV_VERSION_MAJOR],
7618 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007619 ver = &tmp[0];
7620 }
7621
7622 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007623 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007624 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007625
7626 /* Add Indicator of Which Drive was Used to Boot the System */
7627 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7628 tmp[1] = ':';
7629 tmp[2] = '\0';
7630
Fred Drake4d1e64b2002-04-15 19:40:07 +00007631 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007632}
7633#endif
7634
Barry Warsaw4a342091996-12-19 23:50:02 +00007635static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007636all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007637{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007638#ifdef F_OK
7639 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007640#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007641#ifdef R_OK
7642 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007643#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007644#ifdef W_OK
7645 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007646#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007647#ifdef X_OK
7648 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007649#endif
Fred Drakec9680921999-12-13 16:37:25 +00007650#ifdef NGROUPS_MAX
7651 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7652#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007653#ifdef TMP_MAX
7654 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7655#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007656#ifdef WCONTINUED
7657 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7658#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007659#ifdef WNOHANG
7660 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007661#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007662#ifdef WUNTRACED
7663 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7664#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007665#ifdef O_RDONLY
7666 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7667#endif
7668#ifdef O_WRONLY
7669 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7670#endif
7671#ifdef O_RDWR
7672 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7673#endif
7674#ifdef O_NDELAY
7675 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7676#endif
7677#ifdef O_NONBLOCK
7678 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7679#endif
7680#ifdef O_APPEND
7681 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7682#endif
7683#ifdef O_DSYNC
7684 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7685#endif
7686#ifdef O_RSYNC
7687 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7688#endif
7689#ifdef O_SYNC
7690 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7691#endif
7692#ifdef O_NOCTTY
7693 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7694#endif
7695#ifdef O_CREAT
7696 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7697#endif
7698#ifdef O_EXCL
7699 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7700#endif
7701#ifdef O_TRUNC
7702 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7703#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007704#ifdef O_BINARY
7705 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7706#endif
7707#ifdef O_TEXT
7708 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7709#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007710#ifdef O_LARGEFILE
7711 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7712#endif
7713
Tim Peters5aa91602002-01-30 05:46:57 +00007714/* MS Windows */
7715#ifdef O_NOINHERIT
7716 /* Don't inherit in child processes. */
7717 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7718#endif
7719#ifdef _O_SHORT_LIVED
7720 /* Optimize for short life (keep in memory). */
7721 /* MS forgot to define this one with a non-underscore form too. */
7722 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7723#endif
7724#ifdef O_TEMPORARY
7725 /* Automatically delete when last handle is closed. */
7726 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7727#endif
7728#ifdef O_RANDOM
7729 /* Optimize for random access. */
7730 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7731#endif
7732#ifdef O_SEQUENTIAL
7733 /* Optimize for sequential access. */
7734 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7735#endif
7736
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007737/* GNU extensions. */
7738#ifdef O_DIRECT
7739 /* Direct disk access. */
7740 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7741#endif
7742#ifdef O_DIRECTORY
7743 /* Must be a directory. */
7744 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7745#endif
7746#ifdef O_NOFOLLOW
7747 /* Do not follow links. */
7748 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7749#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007750
Barry Warsaw5676bd12003-01-07 20:57:09 +00007751 /* These come from sysexits.h */
7752#ifdef EX_OK
7753 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007754#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007755#ifdef EX_USAGE
7756 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007757#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007758#ifdef EX_DATAERR
7759 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007760#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007761#ifdef EX_NOINPUT
7762 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007763#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007764#ifdef EX_NOUSER
7765 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007766#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007767#ifdef EX_NOHOST
7768 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007769#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007770#ifdef EX_UNAVAILABLE
7771 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007772#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007773#ifdef EX_SOFTWARE
7774 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007775#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007776#ifdef EX_OSERR
7777 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007778#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007779#ifdef EX_OSFILE
7780 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007781#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007782#ifdef EX_CANTCREAT
7783 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007784#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007785#ifdef EX_IOERR
7786 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007787#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007788#ifdef EX_TEMPFAIL
7789 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007790#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007791#ifdef EX_PROTOCOL
7792 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007793#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007794#ifdef EX_NOPERM
7795 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007796#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007797#ifdef EX_CONFIG
7798 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007799#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007800#ifdef EX_NOTFOUND
7801 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007802#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007803
Guido van Rossum246bc171999-02-01 23:54:31 +00007804#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007805#if defined(PYOS_OS2) && defined(PYCC_GCC)
7806 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7807 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7808 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7809 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7810 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7811 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7812 if (ins(d, "P_PM", (long)P_PM)) return -1;
7813 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7814 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7815 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7816 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7817 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7818 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7819 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7820 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7821 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7822 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7823 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7824 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7825 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7826#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007827 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7828 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7829 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7830 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7831 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007832#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007833#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007834
Guido van Rossumd48f2521997-12-05 22:19:34 +00007835#if defined(PYOS_OS2)
7836 if (insertvalues(d)) return -1;
7837#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007838 return 0;
7839}
7840
7841
Tim Peters5aa91602002-01-30 05:46:57 +00007842#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007843#define INITFUNC initnt
7844#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007845
7846#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007847#define INITFUNC initos2
7848#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007849
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007850#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007851#define INITFUNC initposix
7852#define MODNAME "posix"
7853#endif
7854
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007855PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007856INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007857{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007858 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007859
Fred Drake4d1e64b2002-04-15 19:40:07 +00007860 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007861 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007862 posix__doc__);
Tim Peters5aa91602002-01-30 05:46:57 +00007863
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007864 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007865 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007866 Py_XINCREF(v);
7867 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007868 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007869 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007870
Fred Drake4d1e64b2002-04-15 19:40:07 +00007871 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007872 return;
7873
Fred Drake4d1e64b2002-04-15 19:40:07 +00007874 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007875 return;
7876
Fred Drake4d1e64b2002-04-15 19:40:07 +00007877 Py_INCREF(PyExc_OSError);
7878 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007879
Guido van Rossumb3d39562000-01-31 18:41:26 +00007880#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007881 if (posix_putenv_garbage == NULL)
7882 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007883#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007884
Guido van Rossum14648392001-12-08 18:02:58 +00007885 stat_result_desc.name = MODNAME ".stat_result";
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007886 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7887 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7888 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007889 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007890 structseq_new = StatResultType.tp_new;
7891 StatResultType.tp_new = statresult_new;
Fred Drake4d1e64b2002-04-15 19:40:07 +00007892 Py_INCREF((PyObject*) &StatResultType);
7893 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007894
Guido van Rossum14648392001-12-08 18:02:58 +00007895 statvfs_result_desc.name = MODNAME ".statvfs_result";
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007896 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007897 Py_INCREF((PyObject*) &StatVFSResultType);
7898 PyModule_AddObject(m, "statvfs_result",
7899 (PyObject*) &StatVFSResultType);
Guido van Rossumb6775db1994-08-01 11:34:53 +00007900}